From 3d73a89d86191634ac382e0772708389686b8dde Mon Sep 17 00:00:00 2001 From: Harishankar Date: Sat, 2 May 2020 13:33:46 +0530 Subject: [PATCH] Implemented GZIP functionality for the data file Implemented GZIP functionality for the saved data file. Also started work on the GUI front end for the puzzle player --- Makefile | 2 +- constantstrings.h | 1 + wordblox.c | 28 +++--- wordblox.h | 87 +++++++++++++++-- wordblox_player.glade | 222 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 317 insertions(+), 23 deletions(-) create mode 100644 wordblox_player.glade diff --git a/Makefile b/Makefile index 0842251..90cbb91 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,3 @@ wordblox: wordblox.c wordblox.h constantstrings.h - clang wordblox.c -lgd -lcrypt -o wordblox + clang wordblox.c -lgd -lz -lcrypt -o wordblox diff --git a/constantstrings.h b/constantstrings.h index 89755ba..6b8a0bb 100644 --- a/constantstrings.h +++ b/constantstrings.h @@ -20,6 +20,7 @@ #define EXCEED_MAX_GRID_SIZE "Exceeds max puzzle size" #define ERROR_WRITING_FILE "Error writing file" #define ERROR_READING_FILE "Error reading file" +#define COMPRESSED " (compressed)" #define INVALID_WORD "Word contains illegal characters. Only alphabets allowed!" #define FILE_SAVED "File saved successfully" #define PASSWORD_SET "Password set successfully" diff --git a/wordblox.c b/wordblox.c index 61de6b6..fae8b83 100644 --- a/wordblox.c +++ b/wordblox.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "wordblox.h" #include "constantstrings.h" @@ -73,11 +74,10 @@ void do_reset_puzzle (Puzzle *p) /* set the password for the puzzle */ void do_set_password (Puzzle *p) { - printf (INPUT_PASSWORD); - char password[256]; - fgets (password, 256, stdin); + char* password; + password = getpass (INPUT_PASSWORD); /* if empty reset the password to nothing */ - if (strlen (password) == 1) + if (strlen(password) == 0) { set_puzzle_password (p, "\0"); printf (PASSWORD_RESET); @@ -86,9 +86,7 @@ void do_set_password (Puzzle *p) /* set the password */ else { - char *passwd = strtok (password, "\n"); - - set_puzzle_password (p, (const char* )passwd); + set_puzzle_password (p, (const char* )password); printf (PASSWORD_SET); char ch = getchar (); } @@ -257,7 +255,9 @@ void puzzle_editor_loop (Puzzle *p, const char *filename) bool loop = true; while (loop) { - print_menu (WHITE, BLUE, PUZZLE_MENU_TITLE, PUZZLE_EDIT_MENU, 14, 50); + char puzzle_title[60]; + sprintf (puzzle_title, "%s - %s", PUZZLE_MENU_TITLE, filename); + print_menu (WHITE, BLUE, puzzle_title, PUZZLE_EDIT_MENU, 14, 50); printf (INPUT_CHOICE); int ch = get_num (); switch (ch) @@ -320,14 +320,12 @@ void do_open_puzzle (const char *filename) if (strcmp (p.hashed_password, "\0") != 0) { - char passwd[256]; - printf (INPUT_PASSWORD); - fgets (passwd, 256, stdin); - if (strlen (passwd) == 1) + char *passwd; + passwd = getpass (INPUT_PASSWORD); + if (strlen (passwd) == 0) return; - char *pwd = strtok (passwd, "\n"); - - if (verify_password (&p, (const char*) pwd)) + + if (verify_password (&p, (const char*) passwd)) puzzle_editor_loop (&p, filename); else { diff --git a/wordblox.h b/wordblox.h index cabda97..22963e4 100644 --- a/wordblox.h +++ b/wordblox.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "constantstrings.h" #define MAX_PUZZLE_SIZE 20 @@ -316,22 +317,31 @@ void init_puzzle (Puzzle *p, int grid_size) /* save the puzzle to a file */ void save_puzzle (Puzzle *puzzle, const char* file) { FILE *outfile; - outfile = fopen (file, "w"); + /* First output the uncompressed contents to temp file */ + outfile = tmpfile (); if (outfile == NULL) { fprintf (stderr, "%s\n", ERROR_WRITING_FILE); exit (1); } + /* grid size is the first field */ fprintf (outfile, "%d\n", puzzle->grid_size); + /* whether grid is frozen or not */ fprintf (outfile, "%d\n", puzzle->grid_frozen); + /* the hashed password */ fprintf (outfile, "%s\n", puzzle->hashed_password); + /* the salt */ fprintf (outfile, "%s\n", puzzle->salt); + + /* First output the grid characters columns/rows */ for (int i = 0; i < puzzle->grid_size; i ++) { for (int j = 0; j < puzzle->grid_size; j ++) fprintf (outfile, "%c", puzzle->chars[i][j]); fprintf (outfile, "\n"); } + + /* Next output the start across/down numbers */ for (int i = 0; i < puzzle->grid_size; i ++) { for (int j = 0; j < puzzle->grid_size; j++) @@ -341,40 +351,103 @@ void save_puzzle (Puzzle *puzzle, const char* file) { } fprintf (outfile, "\n"); } + + /* Output the across clues */ fprintf (outfile, "ACROSS\n"); + /* Search the grid for across words */ for (int i = 0; i < puzzle->grid_size; i ++) { for (int j = 0; j < puzzle->grid_size; j++) { + /* if it is an across word, then put the word index followed by + tab character (as separator) and the clue */ if (puzzle->start_across_word[i][j] != -1) fprintf (outfile, "%d\t%s\n", puzzle->start_across_word[i][j], puzzle->clue_across[i][j]); } } + + /* Output the down clues */ fprintf (outfile, "DOWN\n"); + /* Search the grid for down words */ for (int i = 0; i < puzzle->grid_size; i ++) { for (int j = 0; j < puzzle->grid_size; j++) { + /* same as across word, put the word index followed by the tab + character and then the clue */ if (puzzle->start_down_word[i][j] != -1) fprintf (outfile, "%d\t%s\n", puzzle->start_down_word[i][j], puzzle->clue_down[i][j]); } } - + + /* Flush the buffer and rewind to beginning - to read and save into + gzip compressed file */ + fflush (outfile); + fseek (outfile, 0, 0); + + /* now compress the file and save it to destination file */ + gzFile outdestfile = gzopen (file, "wb"); + if (outdestfile == NULL) + { + fprintf (stderr, ERROR_WRITING_FILE); + fclose (outfile); + exit (1); + } + char buf[4096]; + while (fread (buf, sizeof(char), 4096, outfile)) + { + int res = gzwrite (outdestfile, buf, strlen (buf) ); + if (res == 0) + { + fprintf (stderr, "%s %s", ERROR_WRITING_FILE, COMPRESSED); + fclose (outfile); + exit (1); + } + } + gzclose (outdestfile); fclose (outfile); + } /* read the puzzle from a file */ Puzzle load_puzzle (const char* file) { - FILE *infile; - Puzzle p; - infile = fopen (file, "rb"); - if (infile == NULL) + /* First open the GZip file */ + gzFile insourcefile = gzopen (file, "rb"); + if (insourcefile == NULL) { - fprintf (stderr, "%s\n", ERROR_READING_FILE); + fprintf (stderr, "%s %s", ERROR_READING_FILE, COMPRESSED); exit (1); } + /* Open a temporary file to uncompress the contents */ + FILE *infile = tmpfile (); + if (infile == NULL) + { + fprintf (stderr, ERROR_READING_FILE); + exit (1); + } + /* Put the uncompressed content to the temp file */ + char buf[4096]; + while (gzread (insourcefile, buf, 4096)) + { + int res = fwrite (buf, sizeof(char), strlen (buf), infile); + if (res == 0) + { + fprintf (stderr, ERROR_READING_FILE); + fclose (infile); + gzclose (insourcefile); + exit (1); + } + } + /* Close the gzip file */ + gzclose (insourcefile); + /* Flush the temp file buffer and rewind to beginning */ + fflush (infile); + 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); diff --git a/wordblox_player.glade b/wordblox_player.glade new file mode 100644 index 0000000..e27bcc5 --- /dev/null +++ b/wordblox_player.glade @@ -0,0 +1,222 @@ + + + + + + + + + + + + + + + + + + + + + + False + Wordblox Player + 540 + 400 + + + + + + + True + False + vertical + + + True + False + + + True + False + _Puzzle + True + + + True + False + + + True + False + _Open... + True + + + + + + True + False + _Save Grid State... + True + + + + + + True + False + + + + + True + False + E_xit + True + + + + + + + + + + True + False + _View + True + + + True + False + + + True + False + _Reveal Solution... + True + + + + + + + + + + True + False + _Help + True + + + True + False + + + True + False + _About... + True + + + + + + + + + + False + True + 0 + + + + + True + True + + + 70 + True + True + in + + + True + True + + + + + True + True + vertical + + + True + True + in + + + True + True + store_across_clues + False + 0 + False + + + + + + + + True + True + + + + + True + True + in + + + True + True + store_down_clues + 0 + + + + + + + + True + True + + + + + True + True + + + + + True + True + 1 + + + + + + -- 2.20.1