4 #include "buildFacade.h"
6 /* only for getTime() */
8 #include <sys/resource.h>
10 /* macro to detect and notify errors */
11 #define IFERROR(error) {{if (error) { fprintf(stderr, "%s\n", error_index(error)); exit(1); }}}
13 int read_file(char *filename, uchar **textt, ulong *length);
14 void print_usage(char *);
17 int main(int argc, char *argv[]) {
19 char *infile, *outfile;
27 if (argc < 3) print_usage(argv[0]);
32 nchars += strlen(argv[i]);
33 params = (char *) malloc((nchars+1)*sizeof(char));
34 params[nchars] = '\0';
37 len = strlen(argv[i]);
38 strncpy(params+nchars,argv[i],len);
39 params[nchars+len] = ' ';
42 params[nchars-1] = '\0';
49 error = read_file(infile, &text, &text_len);
52 //error = build_index(text, text_len, params, &index);
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);
60 error = save_index(index, outfile);
66 index_size(index, &index_len);
68 error = free_index(index);
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);
82 Opens and reads a text file
84 int read_file(char *filename, uchar **textt, ulong *length) {
90 infile = fopen(filename, "rb"); // b is for binary: required by DOS
91 if(infile == NULL) return 1;
93 /* store input file length */
94 if(fseek(infile,0,SEEK_END) !=0 ) return 1;
95 *length = ftell(infile);
97 /* alloc memory for text (the overshoot is for suffix sorting) */
98 text = (uchar *) malloc((*length)*sizeof(*text));
99 if(text == NULL) return 1;
101 /* read text in one sweep */
103 t = fread(text, sizeof(*text), (size_t) *length, infile);
104 if(t!=*length) return 1;
115 double usertime, systime;
118 getrusage (RUSAGE_SELF, &usage);
120 usertime = (double) usage.ru_utime.tv_sec +
121 (double) usage.ru_utime.tv_usec / 1000000.0;
123 systime = (double) usage.ru_stime.tv_sec +
124 (double) usage.ru_stime.tv_usec / 1000000.0;
126 return (usertime + systime);
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");