zload.c
changeset 11 68ea18fe402c
equal deleted inserted replaced
10:188a0e3b3fb4 11:68ea18fe402c
       
     1 #include <stdio.h>
       
     2 #include <sys/stat.h>
       
     3 #include "dictre.h"
       
     4 
       
     5 static FILE *index, *dict;
       
     6 static remove_tmp_file = 0;
       
     7 
       
     8 static new_word(const char *w, const char *defstr)
       
     9 {
       
    10     printf("'%s': '%s'\n", w, defstr);
       
    11 }
       
    12 
       
    13 void zload_words(FILE *index, FILE *fdefs)
       
    14 {
       
    15     int last_offset = 0;
       
    16     int def_avoided = 0;
       
    17     int numword = 0;;
       
    18     static int dispnwords = 0;
       
    19     static int nwords = 0;
       
    20 
       
    21     do {
       
    22         int offset, length;
       
    23         char *defstr;
       
    24         char *word;
       
    25         word = get_word(index);
       
    26         /*numword++;
       
    27         printf("words: %i\n", numword);*/
       
    28         if (word == 0)
       
    29             break;
       
    30         /*printf("Word: %s\n", w.w);*/
       
    31         offset = get_int(index);
       
    32         length = get_int(index);
       
    33         defstr = get_def(fdefs, offset, length);
       
    34 
       
    35         /* sizeof -1  instead of strlen() */
       
    36         /* If the word is not 00database* ... */
       
    37         if (strncmp(word, "00database", sizeof("00database") - 1) != 0)
       
    38             zprocess_def(word, defstr);
       
    39 
       
    40         /* stdout Display */
       
    41         dispnwords++;
       
    42         nwords++;
       
    43         if (dispnwords >= 1000)
       
    44         {
       
    45             dispnwords = 0;
       
    46             fprintf(stderr,
       
    47                     "Loaded: %i Repeated definitions avoided: %i\n", nwords,
       
    48                     def_avoided);
       
    49         }
       
    50 
       
    51     } while(1);
       
    52 }
       
    53 
       
    54 static void close_files()
       
    55 {
       
    56     fclose(index);
       
    57     fclose(dict);
       
    58 
       
    59     if (remove_tmp_file)
       
    60         unlink("/tmp/tmp.dict");
       
    61 }
       
    62 
       
    63 static void open_files(int argn, char **argv)
       
    64 {
       
    65     char tmpname[500];
       
    66     if (argn < 2)
       
    67     {
       
    68         fprintf(stderr, "usage: %s <dict_basename>\n", argv[0]);
       
    69         exit(1);
       
    70     }
       
    71     strcpy(tmpname, argv[1]);
       
    72     strcat(tmpname, ".index");
       
    73     index = fopen(tmpname, "r");
       
    74     if(index == NULL)
       
    75     {
       
    76         fprintf(stderr, "File: %s ", tmpname);
       
    77         perror("- cannot open file.");
       
    78         exit(-1);
       
    79     }
       
    80 
       
    81     strcpy(tmpname, argv[1]);
       
    82     strcat(tmpname, ".dict");
       
    83     dict = fopen(tmpname, "r");
       
    84     if(dict == NULL)
       
    85     {
       
    86         struct stat st;
       
    87         int res;
       
    88         char tmp[500];
       
    89         strcat(tmpname, ".dz");
       
    90         res = stat(tmpname, &st);
       
    91         if (res == -1)
       
    92         {
       
    93             fprintf(stderr, "File: %s ", tmpname);
       
    94             perror("- cannot open file.");
       
    95             exit(-1);
       
    96         }
       
    97         sprintf(tmp, "gzip -cd %s > /tmp/tmp.dict",
       
    98                 tmpname);
       
    99         printf("Gunzipping...\n");
       
   100         res = system(tmp);
       
   101         dict = fopen("/tmp/tmp.dict", "r");
       
   102         if(dict == NULL || res != 0)
       
   103         {
       
   104             fprintf(stderr, "Error gunzipping file: %s ", tmpname);
       
   105             perror("- something happened to /tmp/tmp.dict.");
       
   106             exit(-1);
       
   107         }
       
   108         remove_tmp_file = 1;
       
   109     }
       
   110 }
       
   111 
       
   112 int main(int argn, char **argv)
       
   113 {
       
   114     open_files(argn, argv);
       
   115     init_wordlist();
       
   116     zload_words(index, dict);
       
   117     dump_wordlist();
       
   118     close_files();
       
   119 }