X-Git-Url: https://harishankar.org/repos/?p=wordblah.git;a=blobdiff_plain;f=wordblox.h;h=7519ff4d067d10b96e84acac13c8c45107edda97;hp=a355a09acf16e178ae3e46c02dce9c08f28fff98;hb=7defe51e8a1da9feec6319defa7c906fcb735726;hpb=8f3a3d44db5d297336dc1138193514c6d79d080e diff --git a/wordblox.h b/wordblox.h index a355a09..7519ff4 100644 --- a/wordblox.h +++ b/wordblox.h @@ -506,7 +506,8 @@ void init_puzzle (Puzzle *p, int grid_size) } /* save the puzzle to a file */ -void save_puzzle (Puzzle *puzzle, const char* file) { +void save_puzzle (Puzzle *puzzle, const char* file) +{ FILE *outfile; /* First output the uncompressed contents to a temp file */ outfile = tmpfile (); @@ -605,7 +606,8 @@ void save_puzzle (Puzzle *puzzle, const char* file) { } /* read the puzzle from a file */ -Puzzle load_puzzle (const char* file) { +Puzzle load_puzzle (const char* file) +{ /* First open the GZip file */ gzFile insourcefile = gzopen (file, "rb"); if (insourcefile == NULL) @@ -914,6 +916,53 @@ void reset_player_data (MainPlayerData *app_data, const char *filename) } +/* save the user grid to a file */ +void save_user_data (MainPlayerData *app_data, const char *filename) +{ + FILE *outfile; + outfile = fopen (filename, "wb"); + if (outfile == NULL) + { + fprintf (stderr, ERROR_WRITING_FILE); + return; + } + fprintf (outfile, "%s\n", app_data->filename); + for (int i = 0; i < app_data->puzzle.grid_size; i ++) + { + for (int j = 0; j < app_data->puzzle.grid_size; j ++) + fprintf (outfile, "%c", app_data->char_ans[i][j]); + fprintf (outfile, "\n"); + } + + fclose (outfile); +} + +/* load the user grid from a file */ +void load_user_data (MainPlayerData *app_data, const char *filename) +{ + FILE *infile; + infile = fopen (filename, "rb"); + if (infile == NULL) + { + fprintf (stderr, "%s\n", ERROR_READING_FILE); + return; + } + + char puzzle_file_name[65535]; + fgets (puzzle_file_name, 65535, infile); + reset_player_data (app_data, strtok (puzzle_file_name, "\n")); + + char line[MAX_PUZZLE_SIZE+10]; + for (int i = 0; i < app_data->puzzle.grid_size; i ++) + { + fgets (line, MAX_PUZZLE_SIZE+10, infile); + for (int j = 0; j < app_data->puzzle.grid_size; j ++) + app_data->char_ans[i][j] = line[j]; + + } + fclose (infile); +} + /* in the player app, move the current selection index left or right */ void move_current_col (MainPlayerData *app_data, enum DIRECTION dir) {