static gboolean gda_data_model_dir_set_values (GdaDataModel *model, gint row, GList *values, GError **error) { GdaDataModelDir *imodel; GList *list; gint col; FileRow *frow; gboolean has_changed = FALSE; g_return_val_if_fail (GDA_IS_DATA_MODEL_DIR (model), FALSE); g_return_val_if_fail (row >= 0, FALSE); imodel = (GdaDataModelDir *) model; g_return_val_if_fail (imodel->priv, FALSE); if (!values) return TRUE; if ((guint)row >= imodel->priv->rows->len) { gchar *str; if (imodel->priv->rows->len > 0) str = g_strdup_printf (_("Row %d out of range (0-%d)"), row, imodel->priv->rows->len - 1); else str = g_strdup_printf (_("Row %d not found (empty data model)"), row); add_error (imodel, str); g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ROW_OUT_OF_RANGE_ERROR, "%s", str); g_free (str); return FALSE; } frow = g_ptr_array_index (imodel->priv->rows, row); for (col = 0, list = values; list; list = list->next, col++) { GValue *value = (GValue *) list->data; const GValue *cvalue = gda_data_model_get_value_at (model, col, row, error); if (!cvalue) return FALSE; if (!value || !gda_value_compare (value, cvalue)) continue; switch (col) { case COL_SIZE: case COL_MIME: case COL_MD5SUM: default: add_error (imodel, _("Column cannot be modified")); g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR, "%s", _("Column cannot be modified")); return FALSE; case COL_DIRNAME: { /* check that the new dir still starts with the basedir */ const gchar *new_path; gchar *old_path; gint len, base_len; new_path = value ? g_value_get_string (value) : ""; len = strlen (new_path); base_len = strlen (imodel->priv->basedir); if ((len < base_len) || (strncmp (new_path, imodel->priv->basedir, base_len))) { add_error (imodel, _("New path must be a subpath of the base directory")); g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR, "%s", _("New path must be a subpath of the base directory")); return FALSE; } old_path = compute_dirname (imodel, frow); if (dir_equal (new_path, old_path)) { g_free (old_path); g_print ("Paths are equal...\n"); break; } if (!g_mkdir_with_parents (new_path, 0755)) { gchar *new_filename; GMappedFile *old_file; gboolean allok = FALSE; gchar *filename; new_filename = g_build_filename (new_path, frow->raw_filename_value ? frow->raw_filename_value : g_value_get_string (frow->filename_value), NULL); filename = compute_filename (imodel, frow); old_file = g_mapped_file_new (filename, FALSE, NULL); if (old_file) { if (g_file_set_contents (new_filename, g_mapped_file_get_contents (old_file), g_mapped_file_get_length (old_file), NULL)) { g_unlink (filename); allok = TRUE; if (frow->data_value) { GdaBlob *blob; blob = (GdaBlob *) gda_value_get_blob (frow->data_value); if (blob && blob->op) _gda_dir_blob_set_filename (GDA_DIR_BLOB_OP (blob->op), new_filename); } } g_mapped_file_unref (old_file); } if (!allok) { gchar *str; str = g_strdup_printf (_("Could not rename file '%s' to '%s'"), filename, new_filename); add_error (imodel, str); g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR, "%s", str); g_free (str); g_free (new_filename); g_free (filename); g_free (old_path); return FALSE; } else { /* renaming succeeded => update FileRow */ #ifndef G_OS_WIN32 g_rmdir (old_path); #endif g_free (frow->reldir); frow->reldir = g_strdup (new_path + base_len); } g_free (filename); g_free (new_filename); has_changed = TRUE; } else { gchar *str; str = g_strdup_printf (_("Could not create directory '%s'"), new_path); add_error (imodel, str); g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR, "%s", str); g_free (str); g_free (old_path); return FALSE; } g_free (old_path); break; } case COL_FILENAME: { gchar *new_filename; gchar *filename; new_filename = g_build_filename (imodel->priv->basedir, frow->reldir, g_value_get_string (value), NULL); filename = compute_filename (imodel, frow); if (g_rename (filename, new_filename)) { gchar *str; str = g_strdup_printf (_("Could not rename file '%s' to '%s'"), filename, new_filename); add_error (imodel, str); g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR, "%s", str); g_free (str); g_free (new_filename); g_free (filename); return FALSE; } else { /* renaming succeeded => update FileRow */ gda_value_free (frow->filename_value); frow->filename_value = gda_value_copy (value); if (frow->raw_filename_value) { g_free (frow->raw_filename_value); frow->raw_filename_value = g_strdup (g_value_get_string (value)); } if (frow->data_value) { GdaBlob *blob; blob = (GdaBlob *) gda_value_get_blob (frow->data_value); if (blob && blob->op) _gda_dir_blob_set_filename (GDA_DIR_BLOB_OP (blob->op), new_filename); } } g_free (new_filename); g_free (filename); has_changed = TRUE; break; } case COL_DATA: { GdaBlob *blob = NULL; if (gda_value_isa (value, GDA_TYPE_BLOB)) { blob = (GdaBlob *) gda_value_get_blob (value); } else if (gda_value_isa (value, GDA_TYPE_BINARY)) { blob = (GdaBlob *) gda_value_get_binary (value); } else if (gda_value_is_null (value)) { /* create a new empty blob */ blob = g_new0 (GdaBlob, 1); } if (blob) { GdaBlobOp *op; gchar *filename; filename = compute_filename (imodel, frow); op = _gda_dir_blob_op_new (filename); if (gda_blob_op_write_all (op, blob) < 0) { gchar *str; str = g_strdup_printf (_("Could not overwrite contents of file '%s'"), filename); add_error (imodel, str); g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR, "%s", str); g_free (str); g_object_unref (op); g_free (filename); return FALSE; } g_object_unref (op); if (gda_value_is_null (value)) g_free (blob); has_changed = FALSE; has_changed = update_file_size (frow, filename); has_changed = update_file_md5sum (frow, filename) || has_changed; has_changed = update_file_mime (frow, filename) || has_changed; g_free (filename); } else { add_error (imodel, _("Wrong type of data")); g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR, "%s", _("Wrong type of data")); return FALSE; } break; } } } if (has_changed) /* signal changes to data model */ gda_data_model_row_updated ((GdaDataModel *) model, row); return TRUE; }
static pos createFood(int width, int height, posList snake) { // The high level view of how this code works: (1) Creates a list of // open positions (i.e positions not occuipied by the snake) and (2) // grabs a random element from that list. But it does this without // creating a list of open positions. int maxRand = width*height - snake.len; if (maxRand <= 0) { return (pos){ .x = -1, .y = -1 }; } int randIndex = rand() % maxRand; int curRandIndex = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pos p = { .x = x, .y = y }; if (posList_contains(snake, p)) { continue; } if (curRandIndex == randIndex) { return p; } curRandIndex++; } } // This should never be reached but I added it to get rid of the // compiler warning. return (pos){ .x = -1, .y = -1 }; } static const dir NO_DIRECTION = { .x = 0, .y = 0 }; static dir actionToDirection(playeraction a) { if (a == UP) { return (dir){ .x = 0, .y = -1 }; } if (a == DOWN) { return (dir){ .x = 0, .y = 1 }; } if (a == LEFT) { return (dir){ .x = -1, .y = 0 }; } if (a == RIGHT) { return (dir){ .x = 1, .y = 0 }; } return NO_DIRECTION; } static void initState(int width, int height, posList *snake, pos *food, playeractionQueue *queue, dir *curDirection, bool *paused, int *score) { srand(time(NULL)); snake->len = 4; snake->list[0] = (pos){ .x = 0, .y = 1 }; snake->list[1] = (pos){ .x = 0, .y = 0 }; snake->list[2] = (pos){ .x = 1, .y = 0 }; snake->list[3] = (pos){ .x = 1, .y = 1 }; *food = createFood(width, height, *snake); while (playeractionQueue_dequeue(queue) != NO_ACTION); *curDirection = actionToDirection(DOWN); *paused = false; *score = 0; } static bool gameIsWon(int width, int height, posList snake) { return width*height == snake.len; } static bool gameIsLost(int width, int height, posList snake) { { posList headless = snake; headless.len--; if (posList_contains(headless, snake.list[snake.len-1])) { return true; } } { pos head = snake.list[snake.len-1]; if (head.x < 0 || width <= head.x) { return true; } if (head.y < 0 || height <= head.y) { return true; } } return false; } static void updateGameState(playeraction action, int width, int height, bool *paused, posList *snake, pos *food, dir *d, int *score) { if (action == PAUSE) { *paused = !(*paused); return; } if (*paused) { return; } dir newDir = actionToDirection(action); if (!dir_equal(newDir, NO_DIRECTION)) { *d = newDir; } pos newHead = { .x = snake->list[snake->len-1].x + d->x, .y = snake->list[snake->len-1].y + d->y }; if (pos_equal(newHead, *food)) { *snake = posList_append(*snake, newHead); *food = createFood(width, height, *snake); (*score)++; } else { for (int i = 0; i < snake->len-1; i++) { snake->list[i] = snake->list[i+1]; } snake->list[snake->len-1].x += d->x; snake->list[snake->len-1].y += d->y; } } char *playeraction_toString(playeraction p) { switch (p) { case NO_ACTION: return "no action"; case UP: return "move the snake up"; case DOWN: return "move the snake down"; case LEFT: return "move the snake left"; case RIGHT: return "move the snake right"; case PAUSE: return "pause the game"; case QUIT: return "quit the game"; case NEW_GAME: return "start a new game"; default: return "BUG!!! Unknown player action, please add another case to the switch statement"; } } void playeractionQueue_enqueue(playeractionQueue *q, playeraction c) { if (q->isFull) { return; } pthread_mutex_lock(q->mu); q->arr[q->tail] = c; q->tail = (q->tail+1) % PLAYERACTIONQUEUE_SIZE; if (q->tail == q->head) { q->isFull = 1; } pthread_mutex_unlock(q->mu); } size_t snakeSpaceRequired(int width, int height) { posList snake; return width * height * sizeof(*snake.list); } void snake(int width, int height, struct timespec frameRate, void *snakeMem, playeractionQueue *actionsQueue, void (*render)(int width, int height, posList snake, pos food, bool paused, int score, bool gameIsWon, bool gameIsLost)) { posList snake; pos food; dir curDirection; bool paused; int score; snake.list = snakeMem; initState(width, height, &snake, &food, actionsQueue, &curDirection, &paused, &score); while (true) { bool won = gameIsWon(width, height, snake); bool lost = gameIsLost(width, height, snake); render(width, height, snake, food, paused, score, won, lost); nanosleep(&frameRate, NULL); playeraction action = playeractionQueue_dequeue(actionsQueue); while (!dir_orthogonal(curDirection, actionToDirection(action))) { action = playeractionQueue_dequeue(actionsQueue); } if (action == QUIT) { break; } if (action == NEW_GAME) { initState(width, height, &snake, &food, actionsQueue, &curDirection, &paused, &score); continue; } if (won || lost) { continue; } updateGameState(action, width, height, &paused, &snake, &food, &curDirection, &score); } }