Debug swcsa
[SXSI/TextCollection.git] / swcsa / buildParser.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "buildFacade.h"
5
6 /* only for getTime() */
7 #include <sys/time.h>
8 #include <sys/resource.h>
9  
10 /* macro to detect and notify errors  */
11 #define IFERROR(error) {{if (error) { fprintf(stderr, "%s\n", error_index(error)); exit(1); }}}
12
13 int read_file(char *filename, uchar **textt, ulong *length);
14 void print_usage(char *);
15 double getTime(void);
16
17 int main(int argc, char *argv[]) {
18
19         char *infile, *outfile;
20     uchar *text;
21         char *params = NULL;
22         ulong text_len;
23         void *index;
24         int error, i;
25         double start, end;
26
27     if (argc < 3) print_usage(argv[0]);
28         if (argc > 3) { 
29                 int nchars, len;
30                 nchars = argc-3;
31                 for(i=2;i<argc;i++)
32                         nchars += strlen(argv[i]);
33                 params = (char *) malloc((nchars+1)*sizeof(char));
34                 params[nchars] = '\0';
35                 nchars = 0;
36                 for(i=3;i<argc;i++) {
37                         len = strlen(argv[i]);
38                         strncpy(params+nchars,argv[i],len);
39                         params[nchars+len] = ' ';
40                         nchars += len+1;
41                 }
42                 params[nchars-1] = '\0';
43         }
44
45         infile = argv[1];
46         outfile = argv[2];
47
48         start = getTime();
49         error = read_file(infile, &text, &text_len);
50         IFERROR(error);
51
52         //error = build_index(text, text_len, params, &index);
53
54     //fprintf(stderr,"\n parameters (stopwordsFilename): \"%s\"\n",params); fflush(stderr);
55     error = build_WCSA (text, text_len, params, &index);
56     //returnvalue = build_iCSA (params,*index);
57                         
58         IFERROR(error);
59
60         error = save_index(index, outfile);
61         IFERROR(error);
62         end = getTime();        
63
64         
65         ulong index_len;
66         index_size(index, &index_len);
67
68         error = free_index(index);
69         IFERROR(error);
70
71         free(params);
72         
73         fprintf(stdout,"\n\n\t ## Building time (**parsing into integers + present_layer: %.3f secs", end-start );
74         fprintf(stdout,"\n\t ## Input: %lu bytes --> Output (pres_layer) %lu bytes.", text_len, index_len);
75         fprintf(stdout,"\n\t ## Overall compression --> %.2f%% (%.2f bits per char).\n\n",
76                         (100.0*index_len)/text_len, (index_len*8.0)/text_len);
77
78         exit(0);
79 }
80
81 /* 
82   Opens and reads a text file 
83 */
84 int read_file(char *filename, uchar **textt, ulong *length) {
85
86   uchar *text;
87   unsigned long t; 
88   FILE *infile;
89   
90   infile = fopen(filename, "rb"); // b is for binary: required by DOS
91   if(infile == NULL) return 1;
92   
93   /* store input file length */
94   if(fseek(infile,0,SEEK_END) !=0 ) return 1;
95   *length = ftell(infile);
96   
97   /* alloc memory for text (the overshoot is for suffix sorting) */
98   text = (uchar *) malloc((*length)*sizeof(*text)); 
99   if(text == NULL) return 1;  
100   
101   /* read text in one sweep */
102   rewind(infile);
103   t = fread(text, sizeof(*text), (size_t) *length, infile);
104   if(t!=*length) return 1;
105   *textt = text;
106   fclose(infile);
107   
108   return 0;
109 }
110
111 double
112 getTime (void)
113 {
114
115         double usertime, systime;
116         struct rusage usage;
117
118         getrusage (RUSAGE_SELF, &usage);
119
120         usertime = (double) usage.ru_utime.tv_sec +
121                 (double) usage.ru_utime.tv_usec / 1000000.0;
122
123         systime = (double) usage.ru_stime.tv_sec +
124                 (double) usage.ru_stime.tv_usec / 1000000.0;
125
126         return (usertime + systime);
127
128 }
129
130 void print_usage(char * progname) {
131         fprintf(stderr, "Usage: %s <source file> <index file> [<parameters>]\n", progname);
132         fprintf(stderr, "\nIt builds the index for the text in file <source file>,\n");
133         fprintf(stderr, "storing it in <index file>. Any additional <parameters> \n");
134         fprintf(stderr, "will be passed to the construction function.\n");
135         fprintf(stderr, "At the end, the program sends to the standard error \n");
136         fprintf(stderr, "performance measures on time to build the index.\n\n");
137         exit(1);
138 }