str.h#

#include <sif/utils/str.h>
SIF_UTILS_STR_H#

String helpers for parsing ASCII catalogue and field files.

These back the ASCII readers, which cannot use scanf: the column layout is not known until runtime, and the files are large enough that the parse has to run once over the buffer without allocating per field.

enum sif_col_target_t#

What a column of an ASCII file holds.

enumerator SIF_COL_IGNORE = 0#

Present but not read.

enumerator SIF_COL_M#

Per-particle weight; see sif_field_t::weights.

int sif_str_decode_format(const char *fmt, sif_col_target_t *targets_out, int max_cols)#

Translate a column format string into an array of column targets.

One character per column, case-insensitive:

x y z

position components

u v w

velocity components

m

per-particle weight (m for mass, its usual meaning)

* /

ignored column

So "xyz*m" describes a file whose first three columns are the position, whose fourth is skipped, and whose fifth is the weight.

Parameters:
  • fmt – Format string as above.

  • targets_out – Written with one entry per decoded column. Must have room for max_cols entries.

  • max_cols – Capacity of targets_out; decoding stops there.

Returns:

The number of columns decoded. This is a count, not a status code.

Warning

Characters outside the table are skipped silently rather than rejected, so a typo in fmt yields a shorter layout instead of an error. Compare the result against the column count you expect.

int sif_str_extract_next_real(char **cursor, char delimiter, sif_real *out_val)#

Read the next numeric field and advance the cursor past it.

Skips leading whitespace, parses one number, and leaves cursor on the character after the field’s trailing delimiter, ready for the next call.

Parameters:
  • cursor – Address of the read pointer; advanced in place.

  • delimiter – Field separator. Use ' ' for whitespace-separated files.

  • out_val – Written with the parsed value.

Returns:

1 if a field was consumed, 0 at end of line or end of string.

Warning

The return value reports that a field was consumed, not that it parsed as a number. A malformed field yields 0.0 in out_val, returns 1, and skips to the next delimiter, so a text column inside a numeric file reads as zeros rather than failing.