From: Harishankar Date: Mon, 11 May 2020 05:49:37 +0000 (+0530) Subject: Fixed error handling for loading puzzle or grid state X-Git-Tag: 0.1a~4 X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=89e08816eea395375fa364aab6adb126378c2c56;p=wordblah.git Fixed error handling for loading puzzle or grid state Fixed the error handling for loading puzzle file or grid state from a file --- diff --git a/wordblah.h b/wordblah.h index 48e1f28..c641abc 100644 --- a/wordblah.h +++ b/wordblah.h @@ -600,19 +600,23 @@ void save_puzzle (Puzzle *puzzle, const char* file) /* read the puzzle from a file */ Puzzle load_puzzle (const char* file) { + Puzzle p; /* First open the GZip file */ gzFile insourcefile = gzopen (file, "rb"); if (insourcefile == NULL) { fprintf (stderr, "%s %s\n", ERROR_READING_FILE, COMPRESSED); - exit (1); + /* return an invalid puzzle */ + init_puzzle (&p, 0); + return p; } /* Open a temporary file to uncompress the contents */ FILE *infile = tmpfile (); if (infile == NULL) { fprintf (stderr, "%s\n", ERROR_READING_FILE); - exit (1); + init_puzzle (&p, 0); + return p; } /* Put the uncompressed content to the temp file */ char buf[128]; @@ -626,7 +630,9 @@ Puzzle load_puzzle (const char* file) fprintf (stderr, "%s\n", ERROR_READING_FILE); fclose (infile); gzclose (insourcefile); - exit (1); + /* return an invalid puzzle */ + init_puzzle (&p, 0); + return p; } num = gzread (insourcefile, buf, 128); } @@ -637,7 +643,6 @@ Puzzle load_puzzle (const char* file) fseek (infile, 0, 0); /* Read the temporary file contents to the structure Puzzle */ - Puzzle p; char line[MAX_CLUE_LENGTH+10]; fgets (line, MAX_CLUE_LENGTH + 10, infile); p.grid_size = atoi (line); @@ -645,6 +650,7 @@ Puzzle load_puzzle (const char* file) puzzle object */ if (p.grid_size == 0) { + fprintf (stderr, "%s\n", INVALID_PUZZLE); init_puzzle (&p, 0); return p; } diff --git a/wordblah_player.c b/wordblah_player.c index 08db02e..2107a11 100644 --- a/wordblah_player.c +++ b/wordblah_player.c @@ -554,7 +554,30 @@ void on_menu_load_grid_state_activate (GtkMenuItem *item, GtkDrawingArea *area) { char *filename; filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); - load_user_data (&app_data, filename); + MainPlayerData temp; + load_user_data (&temp, filename); + /* if the puzzle file is invalid or the grid is frozen */ + if (temp.puzzle.grid_size == 0 || + temp.puzzle.grid_frozen == false) + { + GtkWidget *errordlg; + char message[256]; + if (temp.puzzle.grid_size == 0) + strcpy (message, INVALID_PUZZLE); + else + strcpy (message, UNFROZEN_GRID_PLAYER); + + errordlg = gtk_message_dialog_new (GTK_WINDOW(main_window), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_CLOSE, + "%s",message); + gtk_dialog_run (GTK_DIALOG(errordlg)); + gtk_widget_destroy (errordlg); + + } + else + app_data = temp; g_free (filename); }