From 9d57a73df4c5f905428923339620046b212f4bdb Mon Sep 17 00:00:00 2001 From: Thomas White Date: Tue, 24 Apr 2018 16:43:40 +0200 Subject: Read audit information from stream --- libcrystfel/src/stream.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ libcrystfel/src/stream.h | 5 ++-- 2 files changed, 69 insertions(+), 2 deletions(-) (limited to 'libcrystfel') diff --git a/libcrystfel/src/stream.c b/libcrystfel/src/stream.c index d7d997cf..db6c6199 100644 --- a/libcrystfel/src/stream.c +++ b/libcrystfel/src/stream.c @@ -65,6 +65,7 @@ struct _stream int major_version; int minor_version; + char *audit_info; long long int ln; @@ -1335,6 +1336,64 @@ void write_stream_header(FILE *ofh, int argc, char *argv[]) } +char *stream_audit_info(Stream *st) +{ + if ( st->audit_info == NULL ) return NULL; + return strdup(st->audit_info); +} + + +static void read_audit_lines(Stream *st) +{ + int done = 0; + size_t len = 0; + int first = 1; + + st->audit_info = malloc(4096); + if ( st->audit_info == NULL ) { + ERROR("Failed to allocate memory for audit information\n"); + return; + } + + /* Read lines from stream until one of them starts with "-----", + * then rewind to the start of that line */ + do { + + char line[1024]; + char *rval; + long pos; + + pos = ftell(st->fh); + + rval = fgets(line, 1023, st->fh); + if ( rval == NULL ) { + ERROR("Failed to read stream audit info.\n"); + close_stream(st); + return; + } + + if ( strncmp(line, "-----", 5) == 0 ) { + fseek(st->fh, pos, SEEK_SET); + done = 1; + } else { + chomp(line); + len += strlen(line); + if ( len > 4090 ) { + ERROR("Too much audit information.\n"); + return; + } else { + if ( !first ) { + strcat(st->audit_info, "\n"); + } + first = 0; + strcat(st->audit_info, line); + } + } + + } while ( !done ); +} + + Stream *open_stream_for_read(const char *filename) { Stream *st; @@ -1342,6 +1401,7 @@ Stream *open_stream_for_read(const char *filename) st = malloc(sizeof(struct _stream)); if ( st == NULL ) return NULL; st->old_indexers = 0; + st->audit_info = NULL; if ( strcmp(filename, "-") == 0 ) { st->fh = stdin; @@ -1384,6 +1444,8 @@ Stream *open_stream_for_read(const char *filename) st->ln = 1; + read_audit_lines(st); + return st; } @@ -1409,6 +1471,7 @@ Stream *open_stream_fd_for_write(int fd) st = malloc(sizeof(struct _stream)); if ( st == NULL ) return NULL; st->old_indexers = 0; + st->audit_info = NULL; st->fh = fdopen(fd, "w"); if ( st->fh == NULL ) { @@ -1459,6 +1522,7 @@ Stream *open_stream_for_write_3(const char *filename, st = malloc(sizeof(struct _stream)); if ( st == NULL ) return NULL; st->old_indexers = 0; + st->audit_info = NULL; st->fh = fopen(filename, "w"); if ( st->fh == NULL ) { @@ -1511,6 +1575,7 @@ Stream *open_stream_for_write_2(const char *filename, st = malloc(sizeof(struct _stream)); if ( st == NULL ) return NULL; st->old_indexers = 0; + st->audit_info = NULL; st->fh = fopen(filename, "w"); if ( st->fh == NULL ) { @@ -1577,6 +1642,7 @@ int get_stream_fd(Stream *st) void close_stream(Stream *st) { + free(st->audit_info); fclose(st->fh); free(st); } diff --git a/libcrystfel/src/stream.h b/libcrystfel/src/stream.h index 2ed7b050..b50da832 100644 --- a/libcrystfel/src/stream.h +++ b/libcrystfel/src/stream.h @@ -3,11 +3,11 @@ * * Stream tools * - * Copyright © 2013-2017 Deutsches Elektronen-Synchrotron DESY, + * Copyright © 2013-2018 Deutsches Elektronen-Synchrotron DESY, * a research centre of the Helmholtz Association. * * Authors: - * 2010-2017 Thomas White + * 2010-2018 Thomas White * 2014 Valerio Mariani * 2011 Andrew Aquila * @@ -121,6 +121,7 @@ extern void write_command(Stream *st, int argc, char *argv[]); extern void write_geometry_file(Stream *st, const char *geom_filename); extern int rewind_stream(Stream *st); extern int is_stream(const char *filename); +extern char *stream_audit_info(Stream *st); #ifdef __cplusplus } -- cgit v1.2.3