Save and load grid state functionality implemented
[wordblah.git] / wordblox.h
index a355a09..7519ff4 100644 (file)
@@ -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)
 {