Implemented GZIP functionality for the data file
[wordblah.git] / wordblox.h
index bde57c3..22963e4 100644 (file)
@@ -1,6 +1,11 @@
 #ifndef __WORDBLOX_H
 #define __WORDBLOX_H
-
+#define _XOPEN_SOURCE
+#include <unistd.h>
+#include <gd.h>
+#include <gdfontmb.h>
+#include <gdfontg.h>
+#include <zlib.h>
 #include "constantstrings.h"
 
 #define MAX_PUZZLE_SIZE 20
@@ -18,6 +23,16 @@ enum COLOR {
        WHITE=7
 };
 
+enum ATTR {
+       NORMAL = 23,
+       BOLD=1
+};
+
+enum ORIENTATION {
+       ACROSS=1,
+       DOWN=2
+};
+
 typedef char String[MAX_CLUE_LENGTH];
 
 /* The main puzzle struct type */
@@ -29,8 +44,11 @@ typedef struct {
        String clue_down[MAX_PUZZLE_SIZE][MAX_PUZZLE_SIZE];
        int grid_size;
        bool grid_frozen;
+       char hashed_password[256];
+       char salt[256];
 } Puzzle;
 
+/* get a number from the user */
 int get_num ()
 {
        char s[5];
@@ -39,9 +57,146 @@ int get_num ()
        return n;
 }
 
+/* verify password */
+bool verify_password (Puzzle *p, const char* password)
+{
+       /* no password set */
+       if (strcmp (p->hashed_password, "\0") == 0)
+               return true;
+               
+       /* hash the user input password and compare it with the stored password */
+       char* hashed_password = crypt (password, (const char *)p->salt);
+       
+       if (strcmp (p->hashed_password, hashed_password) == 0)
+               return true;
+               
+       return false;
+}
+
+/* Set or reset password for puzzle */
+void set_puzzle_password (Puzzle *p, const char *password)
+{
+       /* if it is a null string, reset the password */
+       if (strcmp (password, "\0") == 0)
+       {
+               strcpy (p->hashed_password, "\0");
+               strcpy (p->salt, "\0");
+       }
+       else 
+       {
+               srand (time(NULL));
+               char salt[256];
+               sprintf (salt, "puzzle%d", rand()%1000);
+               char* hashedpwd = crypt (password, (const char*)salt);
+               strcpy (p->hashed_password, hashedpwd);
+               strcpy (p->salt, salt);
+       }
+}
+
+/* Output the clues to text file */
+void export_clues (Puzzle *p, const char *filename)
+{
+       FILE *outfile = fopen (filename, "w");
+       if (outfile == NULL)
+       {
+               fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
+               exit (1);
+       }
+       /* first the across clues */
+       fprintf (outfile, "ACROSS CLUES\n");
+       for (int i = 0; i < p->grid_size; i ++)
+       {
+               for (int j = 0; j < p->grid_size; j ++)
+               {
+                       if (p->start_across_word[i][j] != -1)
+                               fprintf (outfile, "%d - %s\n", p->start_across_word[i][j],
+                                                       p->clue_across[i][j]);
+               }
+       }
+       /* now the down clues */
+       fprintf (outfile, "DOWN CLUES\n");
+       for (int i = 0; i < p->grid_size; i ++)
+       {
+               for (int j = 0; j < p->grid_size; j ++)
+               {
+                       if (p->start_down_word[i][j] != -1)
+                               fprintf (outfile, "%d - %s\n", p->start_down_word[i][j],
+                                                       p->clue_down[i][j]);
+               }
+       }
+       fclose (outfile);
+}
+
+/* Output the grid to image - if answerkey is true export filled grid */
+void export_grid_image  (Puzzle *p, const char *filename, bool answerkey) 
+{
+       int img_size = p->grid_size * 40;
+       FILE * outfile = fopen (filename, "wb");
+       if (outfile == NULL)
+       {
+               fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
+               exit (1);
+       }
+       
+       gdImagePtr img = gdImageCreate (img_size, img_size);
+       int white = gdImageColorAllocate (img, 255,255,255);
+       int black = gdImageColorAllocate (img, 0, 0, 0);
+       int blue = gdImageColorAllocate (img, 0, 0, 216);
+       gdFontPtr sm_fnt = gdFontGetMediumBold ();
+       gdFontPtr lg_fnt = gdFontGetGiant ();
+       
+       for (int i = 0; i < p->grid_size; i ++)
+       {
+               for (int j = 0; j < p->grid_size; j++)
+               {
+                       /* if it is a block, draw the black square */
+                       if (p->chars[i][j] == '#')
+                               gdImageFilledRectangle (img, j*40, i*40, j*40+40, 
+                                                                                       i*40+40,black);
+                       else
+                       {
+                               /* draw a regular square */
+                               gdImageRectangle (img, j*40, i*40, j*40+40, 
+                                                                       i*40+40, black);
+                               
+                               /* print the numers, if it is either start across word or 
+                               a down word */
+                               if (p->start_across_word[i][j] != -1 || 
+                                               p->start_down_word[i][j] != -1)
+                               {
+                                       if (p->start_across_word[i][j] != -1)
+                                       {
+                                               char str[5];
+                                               sprintf (str, "%d", p->start_across_word[i][j]);
+                                               gdImageString (img,  sm_fnt, j*40+2, i*40+2,
+                                                                       (unsigned char *)str, blue);   
+                                       }
+                                       else 
+                                       {
+                                               char str[5];
+                                               sprintf (str, "%d", p->start_down_word[i][j]);
+                                               gdImageString (img,  sm_fnt, j*40+2, i*40+2,
+                                                                       (unsigned char *)str, blue);                                    
+                                       }
+                               }
+                               /* if answerkey is true, draw the character in the cell */
+                               if (answerkey)
+                               {
+                                       gdImageChar (img, lg_fnt, j*40+15, i*40+15, 
+                                               p->chars[i][j], black);
+                               }
+                       }
+               }
+       }
+       
+       gdImagePng (img, outfile);
+       gdImageDestroy (img);
+       fclose (outfile);
+}
+
 /* Set the terminal colour */
-void set_color (enum COLOR fg, enum COLOR bg) {
-       printf ("\x1B[%d;%dm", fg+30, bg+40);
+void set_color (enum COLOR fg, enum COLOR bg, enum ATTR at) {
+       printf ("\x1B[%d;%d;%dm", fg+30, bg+40, at);
 }
 
 /* Reset the terminal colour */
@@ -154,32 +309,238 @@ void init_puzzle (Puzzle *p, int grid_size)
                        strcpy (p->clue_down[i][j], "");                        
                }
        }
+       strcpy (p->hashed_password, "\0");
+       strcpy (p->salt, "\0");
+       
 }
 
-/* save the puzzle */
+/* save the puzzle to a file */
 void save_puzzle (Puzzle *puzzle, const char* file) {
        FILE *outfile;
-       outfile = fopen (file, "wb");
+       /* First output the uncompressed contents to temp file */
+       outfile = tmpfile ();
        if (outfile == NULL)
        {
                fprintf (stderr, "%s\n", ERROR_WRITING_FILE);
                exit (1);
        }
-       fwrite (puzzle, sizeof (*puzzle), 1, outfile);
+       /* 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++)
+               {
+                       fprintf (outfile, "%d ", puzzle->start_across_word[i][j]);
+                       fprintf (outfile, "%d ", puzzle->start_down_word[i][j]);
+               }
+               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 */
+/* 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);
        }
-       fread (&p, sizeof(p), 1, infile);
+       /* 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);
+       fgets (line, MAX_CLUE_LENGTH + 10, infile);
+       p.grid_frozen = atoi (line) == 0 ? false : true ;
+       fgets (line, MAX_CLUE_LENGTH + 10, infile);
+       if (strlen (line) != 1)
+               strcpy (p.hashed_password, strtok (line, "\n"));
+       else
+               strcpy (p.hashed_password, "\0");
+       fgets (line, MAX_CLUE_LENGTH + 10, infile);
+       if (strlen (line) != 1)
+               strcpy (p.salt, strtok (line, "\n"));
+       else
+               strcpy (p.salt, "\0");
+       
+       /* read each character of the grid */
+       for (int i = 0; i < p.grid_size; i ++ )
+       {
+               fgets (line, MAX_CLUE_LENGTH + 10, infile);
+               for (int j = 0; j < p.grid_size; j ++)
+                       p.chars[i][j] = line[j];
+       }
+       /* read the word numbers */
+       for (int i = 0; i < p.grid_size; i ++)
+       {
+               fgets (line, MAX_CLUE_LENGTH + 10, infile);
+               char *token = strtok (line, " ");
+               for (int j = 0; j < p.grid_size; j ++)
+               {
+                       if (token != NULL) 
+                               p.start_across_word[i][j] = atoi (token);
+                       token = strtok (NULL, " ");
+                       if (token != NULL)
+                               p.start_down_word[i][j] = atoi (token);
+                       token = strtok (NULL, " ");
+               }
+       }
+       /* read the clues */
+       fgets (line, MAX_CLUE_LENGTH + 10, infile);
+       
+       /* across clues */
+       char clues[100][MAX_CLUE_LENGTH];
+       int word_num[100];
+       int c = 0;
+       /* first read the across clues from file */
+       while (1)
+       {
+               fgets (line, MAX_CLUE_LENGTH + 10, infile);
+               /* if reached the end of across clues */
+               if (strcmp (line, "DOWN\n") == 0)
+                       break;
+               word_num[c] = atoi (strtok (line, "\t"));
+               char *cl = strtok (NULL, "\n");
+               if (cl != NULL)         
+                       strcpy (clues[c], cl);
+               else
+                       strcpy (clues[c], "\0");
+               c++;
+       }
+       /* set the clue to the correct cell in grid */
+       for (int i = 0; i < p.grid_size; i ++)
+       {
+               for (int j = 0; j < p.grid_size; j ++)
+               {
+                       for (int r = 0; r < c; r ++)
+                               if (p.start_across_word[i][j] == word_num[r])
+                                       strcpy (p.clue_across[i][j], clues[r]);
+               }
+       }
+
+       /* down clues */
+       c = 0;
+       while (fgets (line, MAX_CLUE_LENGTH + 10, infile))
+       {
+               word_num[c] = atoi (strtok (line, "\t"));
+               char* cl = strtok (NULL, "\n");
+               if (cl != NULL)
+                       strcpy (clues[c], cl);
+               else
+                       strcpy (clues[c], "\0");
+               c++;
+       }
+       for (int i = 0; i < p.grid_size; i ++)
+       {
+               for (int j = 0; j < p.grid_size; j ++)
+               {
+                       for (int r = 0; r < c; r ++)
+                               if (p.start_down_word[i][j] == word_num[r])
+                                       strcpy (p.clue_down[i][j], clues[r]);
+               }
+       }
+
        fclose (infile);
        return p;
 }
@@ -188,7 +549,7 @@ Puzzle load_puzzle (const char* file) {
 void print_puzzle (Puzzle *p) 
 {
        printf ("\n");
-       set_color (WHITE, CYAN);
+       set_color (WHITE, CYAN, NORMAL);
        printf ("    ");
        for (int i = 0; i < p->grid_size; i ++)
                printf ("%3d", i);
@@ -196,12 +557,12 @@ void print_puzzle (Puzzle *p)
        printf("\n");
        for (int i = 0; i < p->grid_size; i ++)
        {
-               set_color (WHITE, CYAN);
+               set_color (WHITE, CYAN, NORMAL);
                printf ("%3d ", i);
                for (int j = 0; j < p->grid_size; j ++) 
                {
                        if (p->chars[i][j] == '#') {
-                               set_color (WHITE, BLACK);
+                               set_color (WHITE, BLACK, NORMAL);
                                printf ("   ");
                        }
                        else 
@@ -209,7 +570,7 @@ void print_puzzle (Puzzle *p)
                                if (p->start_across_word[i][j] != -1 || 
                                        p->start_down_word[i][j] != -1) 
                                {
-                                       set_color (BLUE, WHITE);
+                                       set_color (BLUE, WHITE, NORMAL);
                                        if (p->start_across_word[i][j] != -1)
                                                printf ("%-2d", p->start_across_word[i][j]);
                                        else 
@@ -217,11 +578,11 @@ void print_puzzle (Puzzle *p)
                                }
                                else
                                {
-                                       set_color (BLACK, WHITE);
+                                       set_color (BLACK, WHITE,NORMAL);
                                        printf ("  ");
                                }
                                        
-                               set_color (BLACK, WHITE);
+                               set_color (BLACK, WHITE, BOLD);
                                printf ("%c", p->chars[i][j]);
                        }
                        reset_color ();
@@ -231,7 +592,7 @@ void print_puzzle (Puzzle *p)
        /* print the clues if set */
        if (p->grid_frozen == true) 
        {
-               printf ("ACROSS - CLUES\n");
+               printf ("\x1B[1mACROSS - CLUES\x1B[0m\n");
                for (int i = 0; i < p->grid_size; i ++)
                {
                        for (int j = 0; j < p->grid_size; j ++)
@@ -243,7 +604,7 @@ void print_puzzle (Puzzle *p)
                                }
                        }
                }
-               printf ("\nDOWN - CLUES\n");
+               printf ("\n\x1B[1mDOWN - CLUES\x1B[0m\n");
                for (int i = 0; i < p->grid_size; i ++)
                {
                        for (int j = 0; j < p->grid_size; j ++)
@@ -260,27 +621,41 @@ void print_puzzle (Puzzle *p)
 }
 
 /* function to check if a word is valid or not */
-bool is_valid_word (const char *word) 
+char* is_valid_word (char *word) 
 {
-       for (int i = 0; i < strlen (word); i ++)
+       if (word == NULL || strlen(word) == 0)
+               return NULL;
+       for (int i = 0; i < strlen (word) - 1; i ++)
                if (! isalpha (word[i])) 
-                       return false;
+                       return NULL;
                        
-       return true;
+       return strtok (word, "\n");
 }
 
-/* function set a clue for an across word */
-bool set_across_clue (Puzzle *p, String clue, int index) 
+
+/* function to set a clue for an across word */
+bool set_clue (Puzzle *p, String clue, int index, enum ORIENTATION order) 
 {
        for (int i = 0; i < p->grid_size; i ++)
        {
                for (int j = 0; j < p->grid_size; j ++)
                {
-                       if (p->start_across_word[i][j] == index)
+                       if (order == ACROSS)
                        {
-                               strcpy (p->clue_across[i][j], clue);
-                               return true;
-                       }       
+                               if (p->start_across_word[i][j] == index)
+                               {
+                                       strcpy (p->clue_across[i][j], clue);
+                                       return true;
+                               }
+                       }
+                       else if (order == DOWN)
+                       {
+                               if (p->start_down_word[i][j] == index)
+                               {
+                                       strcpy (p->clue_down[i][j], clue);
+                                       return true;
+                               }
+                       }               
                }
        }
        return false;
@@ -292,16 +667,20 @@ void print_menu (enum COLOR fg, enum COLOR bg, const char* title,
 {
                /* clear screen */
                printf ("\e[1;1H\e[2J");
-               set_color (fg, bg);
+               set_color (fg, bg, NORMAL);
                printf ("\u2554");
                for (int i = 0; i < padding; i ++)
                        printf ("\u2550");
                printf ("\u2557"); 
                reset_color (); printf ("\n");
-               set_color (fg, bg);
-               printf ("\u2551%-*s\u2551", padding, title);
+               printf ("\u2551");
+               set_color (fg, bg, BOLD);
+               printf ("%-*s", padding, title);
+               reset_color ();
+               set_color (fg, bg, NORMAL);
+               printf ("\u2551");
                reset_color (); printf ("\n");
-               set_color (fg, bg);
+               set_color (fg, bg, NORMAL);
                printf ("\u2560");
                for (int i = 0; i < padding; i ++)
                        printf ("\u2550");
@@ -309,11 +688,11 @@ void print_menu (enum COLOR fg, enum COLOR bg, const char* title,
                reset_color (); printf ("\n");
                for (int i = 0; i < num_items; i ++) 
                {
-                       set_color (fg, bg);
+                       set_color (fg, bg, NORMAL);
                        printf ("\u2551%-*s\u2551", padding, items[i]);
                        reset_color (); printf ("\n");
                }
-               set_color (fg, bg);
+               set_color (fg, bg, NORMAL);
                printf ("\u255A");
                for (int i = 0; i < padding; i ++)
                        printf ("\u2550");