diff options
author | Thomas White <taw@physics.org> | 2021-03-31 17:29:53 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2021-03-31 17:29:53 +0200 |
commit | 1778bbe31f50857ad5e8a9297ff2e1dc36cc6330 (patch) | |
tree | 77d6c45e271b04e4a17007f77ddaa061b64d9201 | |
parent | 504f6465c3b872792fa17aa6c01ce6045fb06922 (diff) |
FromFile indexer, stream2sol.py: Handle spaces in filename
-rw-r--r-- | libcrystfel/src/indexers/fromfile.c | 61 | ||||
-rwxr-xr-x | scripts/stream2sol.py | 13 |
2 files changed, 57 insertions, 17 deletions
diff --git a/libcrystfel/src/indexers/fromfile.c b/libcrystfel/src/indexers/fromfile.c index 7fc502e2..7d95baae 100644 --- a/libcrystfel/src/indexers/fromfile.c +++ b/libcrystfel/src/indexers/fromfile.c @@ -185,6 +185,28 @@ static int set_lattice(UnitCell *cell, const char *ltsym) } +static void unescape(char *line) +{ + size_t len; + int i, w; + int esc = 0; + + len = strlen(line); + w = 0; + for ( i=0; i<len; i++ ) { + if ( esc ) { + line[w++] = line[i]; + esc = 0; + } else if ( line[i] == '\\' ) { + esc = 1; + } else { + line[w++] = line[i]; + } + } + line[w] = '\0'; +} + + void *fromfile_prepare(IndexingMethod *indm, struct fromfile_options *opts) { FILE *fh; @@ -228,47 +250,62 @@ void *fromfile_prepare(IndexingMethod *indm, struct fromfile_options *opts) struct fromfile_key key; Crystal *cr; UnitCell *cell; + size_t len; + int n_sp; struct fromfile_entry *item = NULL; rval = fgets(line, 1023, fh); if ( rval == NULL ) break; - /* FIXME: Replace this with something that can handle quoted - * filenames with possible spaces */ chomp(line); notrail(line); - n = assplode(line, " \t,", &bits, ASSPLODE_NONE); - if ( n < 14 ) { + + len = strlen(line); + n_sp = 0; + for ( i=len-1; i>0; i-- ) { + if ( line[i] == ' ' ) { + n_sp++; + if ( n_sp == 13 ) { + line[i] = '\0'; + break; + } + } + } + + n = assplode(line+i+1, " \t,", &bits, ASSPLODE_NONE); + if ( n < 13 ) { ERROR("Badly formatted line '%s'\n", line); return NULL; } /* filename, event, asx, asy, asz, bsx, bsy, bsz, csx, csy, csz, * det_shift_x, det_shift_y, latticetype+centering */ - for ( i=2; i<13; i++ ) { - if (sscanf(bits[i], "%f", &vals[i-2]) != 1) + for ( i=1; i<12; i++ ) { + if (sscanf(bits[i], "%f", &vals[i-1]) != 1) { ERROR("Invalid value for number %i\n", i); return NULL; } } - if ( make_key(&key, bits[0], bits[1]) ) { + unescape(line); + + if ( make_key(&key, line, bits[0]) ) { ERROR("Failed to make key for %s %s\n", - bits[0], bits[1]); + line, bits[0]); continue; } item = add_unique(&dp->sol_hash, key); if ( item == NULL ) { ERROR("Failed to add/find entry for %s %s\n", - bits[0], bits[1]); + line, bits[0]); continue; } if ( item->n_crystals == MAX_CRYSTALS ) { - ERROR("Too many crystals for %s %s\n", bits[0], bits[1]); + ERROR("Too many crystals for %s %s\n", line, bits[0]); } else { @@ -281,8 +318,8 @@ void *fromfile_prepare(IndexingMethod *indm, struct fromfile_options *opts) cell_set_reciprocal(cell, vals[0]*1e9, vals[1]*1e9, vals[2]*1e9, vals[3]*1e9, vals[4]*1e9, vals[5]*1e9, vals[6]*1e9, vals[7]*1e9, vals[8]*1e9); - if ( set_lattice(cell, bits[13]) ) { - ERROR("Invalid lattice type '%s'\n", bits[13]); + if ( set_lattice(cell, bits[12]) ) { + ERROR("Invalid lattice type '%s'\n", bits[12]); } else { crystal_set_cell(cr, cell); item->crystals[item->n_crystals++] = cr; diff --git a/scripts/stream2sol.py b/scripts/stream2sol.py index 1a9f31ec..76aa0068 100755 --- a/scripts/stream2sol.py +++ b/scripts/stream2sol.py @@ -81,6 +81,9 @@ class Crystal: cs += ' ' + self.lattice_type_sym return cs +def escape_spaces(str): + return str.replace('\\', '\\\\').replace(' ', '\\ ') + class Chunk: def __init__(self, line): @@ -113,7 +116,7 @@ class Chunk: # return '\n'.join([' '.join([self.file, *self.Event.split('//'), str(cryst)]) # for ii, cryst in enumerate(self.crystals)]) # new-style (not working yet) - return '\n'.join([' '.join([self.file, self.Event, str(cryst)]) + return '\n'.join([' '.join([escape_spaces(self.file), self.Event, str(cryst)]) for ii, cryst in enumerate(self.crystals)]) @@ -168,7 +171,7 @@ def parse_stream(stream, sol=None, return_meta=True, curr_chunk = None elif l.startswith(file_label): - curr_chunk.file = l.split(' ')[-1].strip() + curr_chunk.file = l.split(' ', 2)[-1].strip() elif l.startswith(event_label): curr_chunk.Event = l.split(' ')[-1].strip() @@ -201,13 +204,13 @@ def parse_stream(stream, sol=None, return_meta=True, curr_cryst.cstar = parse_vec(l) elif l.startswith('lattice_type'): - curr_cryst.lattice_type = l.split(' ')[2].rstrip('\r\n') + curr_cryst.lattice_type = l.split(' ')[2].strip() elif l.startswith('centering'): - curr_cryst.centering = l.split(' ')[2].rstrip('\r\n') + curr_cryst.centering = l.split(' ')[2].strip() elif l.startswith('unique_axis'): - curr_cryst.unique_axis = l.split(' ')[2].rstrip('\r\n') + curr_cryst.unique_axis = l.split(' ')[2].strip() elif l.startswith('predict_refine/det_shift'): curr_cryst.det_shift = parse_vec(l) |