Commit 176b63b6 authored by Thomas White's avatar Thomas White
Browse files

Fix up stream error handling

parent 2a6ce4a2
......@@ -393,7 +393,9 @@ static void *get_image(void *qp)
/* Get the next filename */
if ( read_chunk(qargs->fh, &image) == 1 ) {
ERROR("Failed to read chunk from the input stream.\n");
if ( ferror(qargs->fh) ) {
ERROR("Stream read error.\n");
}
return NULL;
}
......
......@@ -262,6 +262,10 @@ int main(int argc, char *argv[])
}
n_total_patterns = count_patterns(fh);
if ( n_total_patterns == 0 ) {
ERROR("No patterns to process.\n");
return 1;
}
STATUS("There are %i patterns to process\n", n_total_patterns);
images = malloc(n_total_patterns * sizeof(struct image));
......@@ -280,7 +284,9 @@ int main(int argc, char *argv[])
Reflection *refl;
RefListIterator *iter;
if ( read_chunk(fh, &images[i]) == 1 ) {
if ( read_chunk(fh, &images[i]) != 0 ) {
/* Should not happen, because we counted the patterns
* earlier. */
ERROR("Failed to read chunk from the input stream.\n");
return 1;
}
......
......@@ -307,7 +307,7 @@ static void merge_all(FILE *fh, RefList *model,
/* Get data from next chunk */
rval = read_chunk(fh, &image);
if ( rval ) continue;
if ( rval ) break;
n_patterns++;
......@@ -509,6 +509,10 @@ int main(int argc, char *argv[])
/* Count the number of patterns in the file */
n_total_patterns = count_patterns(fh);
if ( n_total_patterns == 0 ) {
ERROR("No patterns to process.\n");
return 1;
}
STATUS("There are %i patterns to process\n", n_total_patterns);
rewind(fh);
......@@ -536,6 +540,10 @@ int main(int argc, char *argv[])
config_startafter, config_stopafter,
sym, n_total_patterns,
hist_vals, hist_h, hist_k, hist_l, &hist_i, 1);
if ( ferror(fh) ) {
ERROR("Stream read error.\n");
return 1;
}
rewind(fh);
if ( space_for_hist && (hist_i >= space_for_hist) ) {
ERROR("Histogram array was too small!\n");
......@@ -552,6 +560,10 @@ int main(int argc, char *argv[])
merge_all(fh, model, config_maxonly, config_scale, 0,
config_startafter, config_stopafter, sym, n_total_patterns,
NULL, 0, 0, 0, NULL, 2);
if ( ferror(fh) ) {
ERROR("Stream read error.\n");
return 1;
}
write_reflist(output, model, cell);
......
......@@ -129,6 +129,11 @@ int count_patterns(FILE *fh)
} while ( rval != NULL );
if ( ferror(fh) ) {
ERROR("Read error while counting patterns.\n");
return 0;
}
return n_total_patterns;
}
......@@ -319,7 +324,7 @@ int read_chunk(FILE *fh, struct image *image)
rval = fgets(line, 1023, fh);
/* Trouble? */
if ( rval == NULL ) return 1;
if ( rval == NULL ) break;
chomp(line);
......@@ -378,12 +383,16 @@ int read_chunk(FILE *fh, struct image *image)
}
}
} while ( strcmp(line, CHUNK_END_MARKER) != 0 );
if ( strcmp(line, CHUNK_END_MARKER) == 0 ) {
if ( have_filename && have_ev ) return 0;
ERROR("Incomplete chunk found in input file.\n");
return 1;
}
if ( have_filename && have_ev ) return 0;
} while ( 1 );
ERROR("Incomplete chunk found in input file.\n");
return 1;
return 1; /* Either error or EOF, don't care because we will complain
* on the terminal if it was an error. */
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment