From ab916aa452f9e56d4246e1fbfebf2b3313f7caf1 Mon Sep 17 00:00:00 2001 From: Valerio Mariani Date: Tue, 27 Jan 2015 14:05:26 +0100 Subject: Split nested code into separate functions --- libcrystfel/src/detector.c | 124 ++++++++++++++++++++++++--------------------- libcrystfel/src/stream.c | 60 +++++++++++++--------- 2 files changed, 104 insertions(+), 80 deletions(-) (limited to 'libcrystfel') diff --git a/libcrystfel/src/detector.c b/libcrystfel/src/detector.c index 1a799637..a0de3be9 100644 --- a/libcrystfel/src/detector.c +++ b/libcrystfel/src/detector.c @@ -1882,6 +1882,71 @@ static void check_extents(struct panel p, double *min_x, double *min_y, } +static void process_panel_fields(const struct panel *p, char *line, + FILE *fh, char **bits, + int write_panel_coffset) +{ + char new_line[1024]; + char string_to_write[512]; + + strcpy(new_line,"\0"); + strcpy(string_to_write,"\0"); + + if(strstr(bits[1], "fs") != NULL && + strstr(bits[1], "min_fs") == NULL && + strstr(bits[1], "max_fs") == NULL) { + + sprintf(string_to_write, "%+fx %+fy", + p->fsx, p->fsy); + build_output_line(line, new_line, + string_to_write); + fputs(new_line, fh); + return; + + } else if ( strstr(bits[1], "ss") != NULL && + strstr(bits[1], "min_ss") == NULL && + strstr(bits[1], "max_ss") == NULL) { + + sprintf(string_to_write, "%+fx %+fy", + p->ssx, p->ssy); + build_output_line(line, new_line, + string_to_write); + fputs(new_line, fh); + return; + + } else if ( strstr(bits[1], "corner_x") != NULL) { + + sprintf(string_to_write, "%g", + p->cnx); + build_output_line(line, new_line, + string_to_write); + fputs(new_line, fh); + return; + + } else if ( strstr(bits[1], "corner_y") != NULL) { + + sprintf(string_to_write, "%g", + p->cny); + build_output_line(line, new_line, + string_to_write); + fputs(new_line, fh); + return; + + } else if ( strstr(bits[1], "coffset") != NULL) { + + if ( write_panel_coffset ) { + return; + } else { + fputs(line, fh); + return; + } + + } else { + fputs(line, fh); + } +} + + double largest_q(struct image *image) { struct rvec q; @@ -1994,8 +2059,6 @@ int write_detector_geometry_2(const char *geometry_filename, char *rval; char line[1024]; - char new_line[1024]; - char string_to_write[512]; int n_bits; char **bits; @@ -2003,9 +2066,6 @@ int write_detector_geometry_2(const char *geometry_filename, int i; struct panel *p; - strcpy(new_line,"\0"); - strcpy(string_to_write,"\0"); - rval = fgets(line, 1023, ifh); if ( rval == NULL ) break; @@ -2023,59 +2083,9 @@ int write_detector_geometry_2(const char *geometry_filename, p = find_panel_by_name(det, bits[0]); if ( p != NULL ) { + process_panel_fields(p, line, fh, bits, + write_panel_coffset); - if(strstr(bits[1], "fs") != NULL && - strstr(bits[1], "min_fs") == NULL && - strstr(bits[1], "max_fs") == NULL) { - - sprintf(string_to_write, "%+fx %+fy", - p->fsx, p->fsy); - build_output_line(line, new_line, - string_to_write); - fputs(new_line, fh); - continue; - - } else if ( strstr(bits[1], "ss") != NULL && - strstr(bits[1], "min_ss") == NULL && - strstr(bits[1], "max_ss") == NULL) { - - sprintf(string_to_write, "%+fx %+fy", - p->ssx, p->ssy); - build_output_line(line, new_line, - string_to_write); - fputs(new_line, fh); - continue; - - } else if ( strstr(bits[1], "corner_x") != NULL) { - - sprintf(string_to_write, "%g", - p->cnx); - build_output_line(line, new_line, - string_to_write); - fputs(new_line, fh); - continue; - - } else if ( strstr(bits[1], "corner_y") != NULL) { - - sprintf(string_to_write, "%g", - p->cny); - build_output_line(line, new_line, - string_to_write); - fputs(new_line, fh); - continue; - - } else if ( strstr(bits[1], "coffset") != NULL) { - - if ( write_panel_coffset ) { - continue; - } else { - fputs(line, fh); - continue; - } - - } else { - fputs(line, fh); - } } else { fputs(line, fh); } diff --git a/libcrystfel/src/stream.c b/libcrystfel/src/stream.c index 55ff73fe..118cd782 100644 --- a/libcrystfel/src/stream.c +++ b/libcrystfel/src/stream.c @@ -1051,6 +1051,39 @@ static void read_crystal(Stream *st, struct image *image, StreamReadFlags srf) } +static int read_and_store_hdf5_field(struct image *image, const char *line) +{ + + char **new_fields; + + if ( image->stuff_from_stream == NULL ) { + image->stuff_from_stream = + malloc(sizeof(struct stuff_from_stream)); + if ( image->stuff_from_stream == NULL) { + ERROR("Failed reading hdf5 entries from " + "stream\n"); + return 1; + } + image->stuff_from_stream->fields = NULL; + image->stuff_from_stream->n_fields = 0; + } + + new_fields = realloc(image->stuff_from_stream->fields, + (1+image->stuff_from_stream->n_fields)* + sizeof(char *)); + if ( new_fields == NULL ) { + ERROR("Failed reading hdf5 entries from stream\n"); + return 1; + } + image->stuff_from_stream->fields = new_fields; + image->stuff_from_stream->fields[image->stuff_from_stream->n_fields] + = strdup(line); + image->stuff_from_stream->n_fields++; + + return 0; +} + + /* Read the next chunk from a stream and fill in 'image' */ int read_chunk_2(Stream *st, struct image *image, StreamReadFlags srf) { @@ -1150,32 +1183,13 @@ int read_chunk_2(Stream *st, struct image *image, StreamReadFlags srf) if ( strncmp(line, "hdf5", 3) == 0 ) { - char **new_fields; - - if ( image->stuff_from_stream == NULL ) { - image->stuff_from_stream = - malloc(sizeof(struct stuff_from_stream)); - if ( image->stuff_from_stream == NULL) { - ERROR("Failed reading hdf5 entries from " - "stream\n"); - return 1; - } - image->stuff_from_stream->fields = NULL; - image->stuff_from_stream->n_fields = 0; - } + int fail; - new_fields = realloc(image->stuff_from_stream->fields, - (1+image->stuff_from_stream->n_fields)* - sizeof(char *)); - if ( new_fields == NULL ) { - ERROR("Failed reading hdf5 entries from stream\n"); + fail = read_and_store_hdf5_field(image, line); + if ( fail ) { + ERROR("Failed to read hd5 fields from stream.\n"); return 1; } - image->stuff_from_stream->fields = new_fields; - image->stuff_from_stream->fields[image->stuff_from_stream->n_fields] - = strdup(line); - image->stuff_from_stream->n_fields++; - } if ( (srf & STREAM_READ_PEAKS) -- cgit v1.2.3