Implemented GZIP functionality for the data file
authorHarishankar <v.harishankar@gmail.com>
Sat, 2 May 2020 08:03:46 +0000 (13:33 +0530)
committerHarishankar <v.harishankar@gmail.com>
Sat, 2 May 2020 08:03:46 +0000 (13:33 +0530)
Implemented GZIP functionality for the saved data file. Also
started work on the GUI front end for the puzzle player

Makefile
constantstrings.h
wordblox.c
wordblox.h
wordblox_player.glade [new file with mode: 0644]

index 0842251..90cbb91 100644 (file)
--- 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
 
index 89755ba..6b8a0bb 100644 (file)
@@ -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"
index 61de6b6..fae8b83 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdbool.h>
 #include <string.h>
 #include <ctype.h>
+#include <unistd.h>
 
 #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
                {
index cabda97..22963e4 100644 (file)
@@ -5,6 +5,7 @@
 #include <gd.h>
 #include <gdfontmb.h>
 #include <gdfontg.h>
+#include <zlib.h>
 #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 (file)
index 0000000..e27bcc5
--- /dev/null
@@ -0,0 +1,222 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.22.2 -->
+<interface>
+  <requires lib="gtk+" version="3.20"/>
+  <object class="GtkListStore" id="store_across_clues">
+    <columns>
+      <!-- column-name ID -->
+      <column type="gint"/>
+      <!-- column-name Clue -->
+      <column type="gchararray"/>
+    </columns>
+  </object>
+  <object class="GtkListStore" id="store_down_clues">
+    <columns>
+      <!-- column-name ID -->
+      <column type="gint"/>
+      <!-- column-name Clue -->
+      <column type="gchararray"/>
+    </columns>
+  </object>
+  <object class="GtkApplicationWindow" id="main_window">
+    <property name="can_focus">False</property>
+    <property name="title" translatable="yes">Wordblox Player</property>
+    <property name="default_width">540</property>
+    <property name="default_height">400</property>
+    <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
+    <child type="titlebar">
+      <placeholder/>
+    </child>
+    <child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <child>
+          <object class="GtkMenuBar" id="main_menu">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <child>
+              <object class="GtkMenuItem" id="menu_puzzle">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="label" translatable="yes">_Puzzle</property>
+                <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <child>
+                      <object class="GtkMenuItem" id="menu_open">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">_Open...</property>
+                        <property name="use_underline">True</property>
+                        <signal name="activate" handler="on_menu_open_activate" swapped="no"/>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkMenuItem" id="menu_save">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">_Save Grid State...</property>
+                        <property name="use_underline">True</property>
+                        <signal name="activate" handler="on_menu_save_activate" swapped="no"/>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkSeparatorMenuItem">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="GtkMenuItem" id="menu_exit">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">E_xit</property>
+                        <property name="use_underline">True</property>
+                        <signal name="activate" handler="on_menu_exit_activate" swapped="no"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkMenuItem" id="menu_view">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="label" translatable="yes">_View</property>
+                <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <child>
+                      <object class="GtkMenuItem" id="menu_view_answers">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">_Reveal Solution...</property>
+                        <property name="use_underline">True</property>
+                        <signal name="activate" handler="on_menu_view_answers_activate" swapped="no"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkMenuItem" id="menu_help">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="label" translatable="yes">_Help</property>
+                <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <child>
+                      <object class="GtkMenuItem" id="menu_about">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="label" translatable="yes">_About...</property>
+                        <property name="use_underline">True</property>
+                        <signal name="activate" handler="on_menu_about_activate" swapped="no"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkPaned">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <child>
+              <object class="GtkScrolledWindow">
+                <property name="width_request">70</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="shadow_type">in</property>
+              </object>
+              <packing>
+                <property name="resize">True</property>
+                <property name="shrink">True</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkPaned">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="orientation">vertical</property>
+                <child>
+                  <object class="GtkScrolledWindow">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="shadow_type">in</property>
+                    <child>
+                      <object class="GtkTreeView" id="list_across_clues">
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="model">store_across_clues</property>
+                        <property name="enable_search">False</property>
+                        <property name="search_column">0</property>
+                        <property name="show_expanders">False</property>
+                        <child internal-child="selection">
+                          <object class="GtkTreeSelection"/>
+                        </child>
+                      </object>
+                    </child>
+                  </object>
+                  <packing>
+                    <property name="resize">True</property>
+                    <property name="shrink">True</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkScrolledWindow">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="shadow_type">in</property>
+                    <child>
+                      <object class="GtkTreeView" id="list_down_clues">
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="model">store_down_clues</property>
+                        <property name="search_column">0</property>
+                        <child internal-child="selection">
+                          <object class="GtkTreeSelection"/>
+                        </child>
+                      </object>
+                    </child>
+                  </object>
+                  <packing>
+                    <property name="resize">True</property>
+                    <property name="shrink">True</property>
+                  </packing>
+                </child>
+              </object>
+              <packing>
+                <property name="resize">True</property>
+                <property name="shrink">True</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>