Skip to content

Commit c6aa987

Browse files
committed
input/csv: eliminate magic numbers in options declaration
The CSV input module has grown so many options, that counting them by hand became tedious and error prone. Eliminate the magic numbers in the associated code paths. This also has the side effect that the set is easy to re-order just by adjusting the enum, no other code is affected. Help text and default values is much easier to verify and adjust with the symbolic references. [ see 'git diff --word-diff' for the essence of the change ]
1 parent ad6a2be commit c6aa987

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/input/csv.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -965,38 +965,51 @@ static int reset(struct sr_input *in)
965965
return SR_OK;
966966
}
967967

968+
enum option_index {
969+
OPT_SINGLE_COL,
970+
OPT_NUM_LOGIC,
971+
OPT_DELIM,
972+
OPT_FORMAT,
973+
OPT_COMMENT,
974+
OPT_RATE,
975+
OPT_FIRST_LOGIC,
976+
OPT_HEADER,
977+
OPT_START,
978+
OPT_MAX,
979+
};
980+
968981
static struct sr_option options[] = {
969-
{ "single-column", "Single column", "Enable single-column mode, using the specified column (>= 1); 0: multi-col. mode", NULL, NULL },
970-
{ "numchannels", "Number of logic channels", "The number of (logic) channels (single-col. mode: number of bits beginning at 'first channel', LSB-first)", NULL, NULL },
971-
{ "delimiter", "Column delimiter", "The column delimiter (>= 1 characters)", NULL, NULL },
972-
{ "format", "Data format (single-col. mode)", "The numeric format of the data (single-col. mode): bin, hex, oct", NULL, NULL },
973-
{ "comment", "Comment character(s)", "The comment prefix character(s)", NULL, NULL },
974-
{ "samplerate", "Samplerate (Hz)", "The sample rate (used during capture) in Hz", NULL, NULL },
975-
{ "first-channel", "First channel", "The column number of the first channel (multi-col. mode); bit position for the first channel (single-col. mode)", NULL, NULL },
976-
{ "header", "Interpret first line as header (multi-col. mode)", "Treat the first line as header with channel names (multi-col. mode)", NULL, NULL },
977-
{ "startline", "Start line", "The line number at which to start processing samples (>= 1)", NULL, NULL },
978-
ALL_ZERO
982+
[OPT_SINGLE_COL] = { "single-column", "Single column", "Enable single-column mode, using the specified column (>= 1); 0: multi-col. mode", NULL, NULL },
983+
[OPT_NUM_LOGIC] = { "numchannels", "Number of logic channels", "The number of (logic) channels (single-col. mode: number of bits beginning at 'first channel', LSB-first)", NULL, NULL },
984+
[OPT_DELIM] = { "delimiter", "Column delimiter", "The column delimiter (>= 1 characters)", NULL, NULL },
985+
[OPT_FORMAT] = { "format", "Data format (single-col. mode)", "The numeric format of the data (single-col. mode): bin, hex, oct", NULL, NULL },
986+
[OPT_COMMENT] = { "comment", "Comment character(s)", "The comment prefix character(s)", NULL, NULL },
987+
[OPT_RATE] = { "samplerate", "Samplerate (Hz)", "The sample rate (used during capture) in Hz", NULL, NULL },
988+
[OPT_FIRST_LOGIC] = { "first-channel", "First channel", "The column number of the first channel (multi-col. mode); bit position for the first channel (single-col. mode)", NULL, NULL },
989+
[OPT_HEADER] = { "header", "Interpret first line as header (multi-col. mode)", "Treat the first line as header with channel names (multi-col. mode)", NULL, NULL },
990+
[OPT_START] = { "startline", "Start line", "The line number at which to start processing samples (>= 1)", NULL, NULL },
991+
[OPT_MAX] = ALL_ZERO,
979992
};
980993

981994
static const struct sr_option *get_options(void)
982995
{
983996
GSList *l;
984997

985998
if (!options[0].def) {
986-
options[0].def = g_variant_ref_sink(g_variant_new_int32(0));
987-
options[1].def = g_variant_ref_sink(g_variant_new_int32(0));
988-
options[2].def = g_variant_ref_sink(g_variant_new_string(","));
989-
options[3].def = g_variant_ref_sink(g_variant_new_string("bin"));
999+
options[OPT_SINGLE_COL].def = g_variant_ref_sink(g_variant_new_int32(0));
1000+
options[OPT_NUM_LOGIC].def = g_variant_ref_sink(g_variant_new_int32(0));
1001+
options[OPT_DELIM].def = g_variant_ref_sink(g_variant_new_string(","));
1002+
options[OPT_FORMAT].def = g_variant_ref_sink(g_variant_new_string("bin"));
9901003
l = NULL;
9911004
l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("bin")));
9921005
l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("hex")));
9931006
l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("oct")));
994-
options[3].values = l;
995-
options[4].def = g_variant_ref_sink(g_variant_new_string(";"));
996-
options[5].def = g_variant_ref_sink(g_variant_new_uint64(0));
997-
options[6].def = g_variant_ref_sink(g_variant_new_int32(0));
998-
options[7].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
999-
options[8].def = g_variant_ref_sink(g_variant_new_int32(1));
1007+
options[OPT_FORMAT].values = l;
1008+
options[OPT_COMMENT].def = g_variant_ref_sink(g_variant_new_string(";"));
1009+
options[OPT_RATE].def = g_variant_ref_sink(g_variant_new_uint64(0));
1010+
options[OPT_FIRST_LOGIC].def = g_variant_ref_sink(g_variant_new_int32(0));
1011+
options[OPT_HEADER].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
1012+
options[OPT_START].def = g_variant_ref_sink(g_variant_new_int32(1));
10001013
}
10011014

10021015
return options;

0 commit comments

Comments
 (0)