Clean up: use separate function for fatal error
[ftsbench.git] / finnegan.c
index efb0a5a..18630bc 100644 (file)
@@ -60,10 +60,9 @@ no_newline(char *filename)
        FILE           *fp;
        int             c         , cnt = 0;
 
-       if ((fp = fopen(filename, "r")) == NULL) {
-               fprintf(stderr,"Cannot open %s\n", filename);
-               exit(1);
-       }
+       if ((fp = fopen(filename, "r")) == NULL) 
+               fatal("Cannot open %s\n", filename);
+
        while ((c = getc(fp)) != EOF)
                if (c == '\n')
                        cnt += 1;
@@ -79,22 +78,18 @@ build_word_array(char *filename)
        FILE           *fp;
        int             i         , temp_cfreq = 0;
 
-       if ((fp = fopen(filename, "r")) == NULL) {
-               fprintf(stderr,"Cannot open %s\n", filename);
-               exit(1);
-       }
+       if ((fp = fopen(filename, "r")) == NULL) 
+               fatal("Cannot open %s\n", filename);
+       
        a = (struct word_info *)malloc(no_of_words * sizeof(struct word_info));
-       if (!a) {
-               fprintf(stderr,"Can't allocate %d bytes\n", no_of_words * sizeof(struct word_info));
-               exit(1);
-       }
+       if (!a) 
+               fatal("Can't allocate %d bytes\n", no_of_words * sizeof(struct word_info));
+       
        for (i = 0; i < no_of_words; i++) {
                fscanf(fp, "%s", buf);  /* store word in temporary buffer */
                a[i].word = strdup(buf);
-               if (!a[i].word) {
-                       fprintf(stderr,"strdup failed\n");
-                       exit(1);
-               }
+               if (!a[i].word) 
+                       fatal("strdup failed\n");
                fscanf(fp, "%d", &a[i].freq);
                a[i].word_len = strlen(a[i].word);
                a[i].cum_freq = temp_cfreq;
@@ -112,11 +107,12 @@ build_doc_array(char *filename)
        FILE           *fp;
        int             i         , temp_cfreq = 0;
 
-       if ((fp = fopen(filename, "r")) == NULL) {
-               fprintf(stderr,"Cannot open %s\n", filename);
-               exit(1);
-       }
+       if ((fp = fopen(filename, "r")) == NULL) 
+               fatal("Cannot open %s\n", filename);
+
        d = (struct doc_info *)malloc(no_of_docs * sizeof(struct doc_info));
+       if ( !d ) 
+               fatal("Can't allocate %d bytes\n", no_of_docs * sizeof(struct doc_info));
        for (i = 0; i < no_of_docs; i++) {
                fscanf(fp, "%d", &d[i].doc_len);
                fscanf(fp, "%d", &d[i].doc_freq);
@@ -230,10 +226,8 @@ generate_querywords() {
 
 void
 finnegan_init(char *lex_file, char *doc_file, int quiet) {
-       if ( isInited ) {
-               fprintf(stderr,"finnegan is already inited\n");
-               exit(1);
-       }
+       if ( isInited ) 
+               fatal("finnegan is already inited\n");
 
        if (!quiet) {
                printf("Initialize text generator with:\n");