Skip to content

Commit 1926727

Browse files
committed
input/csv: slightly shuffle text routines, add bin/hex/oct doc
Add documentation to the bin/hex/oct text parse routines, and move the bin/hex/oct dispatcher to the location where its invoked routines are. Stick with a TODO comment for parse_line() to reduce the diff size.
1 parent 9eab443 commit 1926727

File tree

1 file changed

+77
-21
lines changed

1 file changed

+77
-21
lines changed

src/input/csv.c

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ struct context {
174174
GSList *prev_sr_channels;
175175
};
176176

177+
/*
178+
* Primitive operations for text input: Strip comments off text lines.
179+
* Split text lines into columns. Process input text for individual
180+
* columns.
181+
*/
182+
177183
static void strip_comment(char *buf, const GString *prefix)
178184
{
179185
char *ptr;
@@ -187,6 +193,20 @@ static void strip_comment(char *buf, const GString *prefix)
187193
}
188194
}
189195

196+
/* TODO Move parse_line() here. */
197+
198+
/**
199+
* @brief Parse a text field into multiple bits, binary presentation.
200+
*
201+
* @param[in] str The input text, a run of 0/1 digits.
202+
* @param[in] inc The input module's context.
203+
*
204+
* @retval SR_OK Success.
205+
* @retval SR_ERR Invalid input data (empty, or format error).
206+
*
207+
* This routine modifies the logic levels in the current sample set,
208+
* based on the text input which consists of binary digits.
209+
*/
190210
static int parse_binstr(const char *str, struct context *inc)
191211
{
192212
gsize i, j, length;
@@ -217,6 +237,18 @@ static int parse_binstr(const char *str, struct context *inc)
217237
return SR_OK;
218238
}
219239

240+
/**
241+
* @brief Parse a text field into multiple bits, hexadecimal presentation.
242+
*
243+
* @param[in] str The input text, a run of hex digits.
244+
* @param[in] inc The input module's context.
245+
*
246+
* @retval SR_OK Success.
247+
* @retval SR_ERR Invalid input data (empty, or format error).
248+
*
249+
* This routine modifies the logic levels in the current sample set,
250+
* based on the text input which consists of hexadecimal digits.
251+
*/
220252
static int parse_hexstr(const char *str, struct context *inc)
221253
{
222254
gsize i, j, k, length;
@@ -261,6 +293,18 @@ static int parse_hexstr(const char *str, struct context *inc)
261293
return SR_OK;
262294
}
263295

296+
/**
297+
* @brief Parse a text field into multiple bits, octal presentation.
298+
*
299+
* @param[in] str The input text, a run of oct digits.
300+
* @param[in] inc The input module's context.
301+
*
302+
* @retval SR_OK Success.
303+
* @retval SR_ERR Invalid input data (empty, or format error).
304+
*
305+
* This routine modifies the logic levels in the current sample set,
306+
* based on the text input which consists of octal digits.
307+
*/
264308
static int parse_octstr(const char *str, struct context *inc)
265309
{
266310
gsize i, j, k, length;
@@ -305,6 +349,20 @@ static int parse_octstr(const char *str, struct context *inc)
305349
return SR_OK;
306350
}
307351

352+
static int parse_single_column(const char *column, struct context *inc)
353+
{
354+
switch (inc->format) {
355+
case FORMAT_BIN:
356+
return parse_binstr(column, inc);
357+
case FORMAT_HEX:
358+
return parse_hexstr(column, inc);
359+
case FORMAT_OCT:
360+
return parse_octstr(column, inc);
361+
}
362+
363+
return SR_ERR;
364+
}
365+
308366
/**
309367
* @brief Splits a text line into a set of columns.
310368
*
@@ -313,6 +371,12 @@ static int parse_octstr(const char *str, struct context *inc)
313371
* @param[in] max_cols The maximum column count, negative to get all of them.
314372
*
315373
* @returns An array of strings, representing the columns' text.
374+
*
375+
* This routine splits a text line on previously determined separators.
376+
* A previously determined set of columns gets isolated (starting at a
377+
* first position and spanning a given number of columns). A negative
378+
* value for the maximum number of columns results in no restriction on
379+
* the result set's length (the first columns still get trimmed off).
316380
*/
317381
static char **parse_line(char *buf, struct context *inc, ssize_t max_cols)
318382
{
@@ -359,6 +423,19 @@ static char **parse_line(char *buf, struct context *inc, ssize_t max_cols)
359423
return columns;
360424
}
361425

426+
/**
427+
* @brief Picks logic levels from multiple binary colomns, one channel per column.
428+
*
429+
* @param[in] columns The text fields which are kept in the columns.
430+
* @param[in] inc The input module's context.
431+
*
432+
* @retval SR_OK Success.
433+
* @retval SR_ERR Insufficient input, or syntax errors.
434+
*
435+
* This routine exclusively handles binary input where one logic channel
436+
* occupies one column each. All channels are expected to reside in one
437+
* consequtive run of columns.
438+
*/
362439
static int parse_multi_columns(char **columns, struct context *inc)
363440
{
364441
gsize i;
@@ -386,27 +463,6 @@ static int parse_multi_columns(char **columns, struct context *inc)
386463
return SR_OK;
387464
}
388465

389-
static int parse_single_column(const char *column, struct context *inc)
390-
{
391-
int res;
392-
393-
res = SR_ERR;
394-
395-
switch (inc->format) {
396-
case FORMAT_BIN:
397-
res = parse_binstr(column, inc);
398-
break;
399-
case FORMAT_HEX:
400-
res = parse_hexstr(column, inc);
401-
break;
402-
case FORMAT_OCT:
403-
res = parse_octstr(column, inc);
404-
break;
405-
}
406-
407-
return res;
408-
}
409-
410466
static int flush_samples(const struct sr_input *in)
411467
{
412468
struct context *inc;

0 commit comments

Comments
 (0)