X-Git-Url: https://harishankar.org/repos/?p=wordblah.git;a=blobdiff_plain;f=wordblah.h;h=c641abcd604c40bcd4ee46690b4479641d99cd28;hp=372ca60c90533cd8e804db149314f0de3338de75;hb=9448369630128d388ee64e3bed682f49d7a7de07;hpb=90048188dd2b930733606983377560e5a07c4884 diff --git a/wordblah.h b/wordblah.h index 372ca60..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,10 +643,17 @@ 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); + /* if puzzle is invalid or otherwise not proper grid, return an invalid + puzzle object */ + if (p.grid_size == 0) + { + fprintf (stderr, "%s\n", INVALID_PUZZLE); + init_puzzle (&p, 0); + return p; + } fgets (line, MAX_CLUE_LENGTH + 10, infile); p.grid_frozen = atoi (line) == 0 ? false : true ; fgets (line, MAX_CLUE_LENGTH + 10, infile);