aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/func_config.cc
blob: bd1ce32568a87a8ab9decb62a8eb59f4a34dfb0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632

// Copyright (c) 2020, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#include <ctype.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "func_config.h"
#include "func_debug.h"

#define MAX_NAME_LEN 128
#define MAX_DESC_LEN 128

#ifndef ARG_ERROR
#define ARG_ERROR(...)                                                                                                 \
    fprintf(stderr, "ERROR: ");                                                                                        \
    fprintf(stderr, __VA_ARGS__);                                                                                      \
    fprintf(stderr, "\n");                                                                                             \
    return 1;
#endif

// Parameter base name string table
const char* config_base_name_table[] = {
#define DEF_UNIT_START(UNIT)
#define DEF_UNIT_END(UNIT)
#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT) #NAME,
#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT) #NAME,
#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT) #NAME,
#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, LEN, DEFAULT) #NAME,
#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR
#undef DEF_UNIT_OPTION
};

// Parameter description table
const char* config_param_desc_table[] = {
#define DEF_UNIT_START(UNIT)
#define DEF_UNIT_END(UNIT)
#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT) #DESC,
#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT) #DESC,
#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT) #DESC,
#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, LEN, DEFAULT) #DESC,
#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_UNIT_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR
};

// String table and enum for the option hierarchy level/sub-levels
// (no leaf options).  Attribute at the top level have "BASE" as their
// enum value and an empty string for the value.
const char* config_hier_str_table[] = {
    "",
#define DEF_UNIT_START(UNIT) #UNIT,
#define DEF_UNIT_END(UNIT)                                    /**/
#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT)            /**/
#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT)              /**/
#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT) /**/
#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, LEN, DEFAULT)   /**/
#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_UNIT_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR
};

typedef enum config_hier_enum_t
{
    BASE,
#define DEF_UNIT_START(UNIT) CURRENT_UNIT,
#define DEF_UNIT_END(UNIT)                                    /**/
#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT)            /**/
#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT)              /**/
#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT) /**/
#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, LEN, DEFAULT)   /**/
#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_UNIT_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR

    MAX_CONFIG_HIER
} config_hier_enum_t;

// Mapping from a leaf parameter index to the
// position in the hierarchy.
config_hier_enum_t config_hierarchy_map[] = {
#define DEF_UNIT_START(UNIT)
#define DEF_UNIT_END(UNIT)
#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT) BASE,
#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT) BASE,
#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT) CURRENT_UNIT,
#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, LEN, DEFAULT) CURRENT_UNIT,
#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_UNIT_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR
};

#define CONFIG_PARAMETER_COUNT (sizeof(config_hierarchy_map) / sizeof(config_hier_enum_t))

// Dynamically generated at initialization
char** config_param_str_table = nullptr;

// Initialize the configuration data structures
int func_model_init_config()
{
    // Initialize string table (builds the hierarchical names)
    config_param_str_table = (char**)calloc(CONFIG_PARAMETER_COUNT, sizeof(char*));
    ASSERT_MEM(config_param_str_table);

    for (uint32_t i = 0; i < CONFIG_PARAMETER_COUNT; i++)
    {
        size_t len = strlen(config_base_name_table[i]) + 1;
        if (config_hierarchy_map[i] != BASE)
        {
            ASSERT_MSG(config_hierarchy_map[i] <= MAX_CONFIG_HIER,
                       "Configuration parameter\'s hierarchy is out of bounds");
            len += strlen(config_hier_str_table[config_hierarchy_map[i]]) + 1;
        }
        config_param_str_table[i] = (char*)calloc(len, 1);
        ASSERT_MEM(config_param_str_table[i]);
        ASSERT_MSG(len < MAX_NAME_LEN, "option expanded name is too long: %s", config_base_name_table[i]);

        if (config_hierarchy_map[i] != BASE)
        {
            snprintf(config_param_str_table[i], len, "%s.%s", config_hier_str_table[config_hierarchy_map[i]],
                     config_base_name_table[i]);
        }
        else
        {
            snprintf(config_param_str_table[i], len, "%s", config_base_name_table[i]);
        }
    }

    return 0;
}

int func_model_set_default_config(func_config_t* func_config)
{
    // Set default values in the global configuration data structure
    bzero(func_config, sizeof(*func_config));

#define DEF_UNIT_START(UNIT)
#define DEF_UNIT_END(UNIT)
#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT) func_config->NAME = (DEFAULT);
#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT) strncpy(func_config->NAME, (DEFAULT), (LEN)-1);
#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT) func_config->UNIT.NAME = (DEFAULT);
#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, LEN, DEFAULT) strncpy(func_config->UNIT.NAME, (DEFAULT), (LEN)-1);
#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_UNIT_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR

    return 0;
}

int func_model_config_cleanup()
{
    uint32_t i;

    if (!config_param_str_table)
        return 1;

    for (i = 0; i < CONFIG_PARAMETER_COUNT; i++)
    {
        free(config_param_str_table[i]);
    }

    free(config_param_str_table);
    config_param_str_table = nullptr;

    return 0;
}

int func_model_config_set_option(func_config_t* func_config, const char* name, const char* value)
{
    // Increment an index variable on each parameter position
    // so that we can index both the position struct through the macro and the
    // array of parameter names through a simple array of strings.
    int param_idx = 0;
    char* endptr;

    // TODO: does not handle strings yet.  Can set magic values on FMT to
    // choose a string copy vs strtoull
#define DEF_UNIT_START(UNIT)
#define DEF_UNIT_END(UNIT)
#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT)                                                                     \
    if (!strcmp(config_param_str_table[param_idx], name))                                                              \
    {                                                                                                                  \
        func_config->NAME = (uint64_t)strtoll(value, &endptr, 0);                                                      \
        if (endptr == value)                                                                                           \
        {                                                                                                              \
            ARG_ERROR("Cannot parse option: %s = %s", name, value);                                                    \
        }                                                                                                              \
        return 0;                                                                                                      \
    }                                                                                                                  \
    param_idx++;

#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT)                                                                       \
    if (!strcmp(config_param_str_table[param_idx], name))                                                              \
    {                                                                                                                  \
        if (strlen(value) >= LEN)                                                                                      \
        {                                                                                                              \
            ARG_ERROR("Option value is too long: %s = %s", name, value);                                               \
        }                                                                                                              \
        strncpy(func_config->NAME, value, (LEN)-1);                                                                    \
        return 0;                                                                                                      \
    }                                                                                                                  \
    param_idx++;

#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT)                                                          \
    if (!strcmp(config_param_str_table[param_idx], name))                                                              \
    {                                                                                                                  \
        func_config->UNIT.NAME = (uint64_t)strtoll(value, &endptr, 0);                                                 \
        if (endptr == value)                                                                                           \
        {                                                                                                              \
            ARG_ERROR("Cannot parse option: %s = %s", name, value);                                                    \
        }                                                                                                              \
        return 0;                                                                                                      \
    }                                                                                                                  \
    param_idx++;

#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, LEN, DEFAULT)                                                            \
    if (!strcmp(config_param_str_table[param_idx], name))                                                              \
    {                                                                                                                  \
        if (strlen(value) >= LEN)                                                                                      \
        {                                                                                                              \
            ARG_ERROR("Option value is too long: %s = %s", name, value);                                               \
        }                                                                                                              \
        strncpy(func_config->UNIT.NAME, value, (LEN)-1);                                                               \
        return 0;                                                                                                      \
    }                                                                                                                  \
    param_idx++;

#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_UNIT_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR

    // No match!
    ARG_ERROR("Cannot find option: %s", name);

    return 1;
}

int func_model_config_get_option_by_name(func_config_t* func_config, const char* name, uint64_t* val)
{
    // Increment an index variable on each parameter position
    // so that we can index both the position struct through the macro and the
    // array of parameter names through a simple array of strings.
    int param_idx = 0;

#define DEF_UNIT_START(UNIT)
#define DEF_UNIT_END(UNIT)

#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT) param_idx++;

#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, FMT, DEFAULT) param_idx++;

#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT)                                                                     \
    if (!strcmp(config_param_str_table[param_idx], name))                                                              \
    {                                                                                                                  \
        *val = func_config->NAME;                                                                                      \
        return 0;                                                                                                      \
    }                                                                                                                  \
    param_idx++;

#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT)                                                          \
    if (!strcmp(config_param_str_table[param_idx], name))                                                              \
    {                                                                                                                  \
        *val = func_config->UNIT.NAME;                                                                                 \
        return 0;                                                                                                      \
    }                                                                                                                  \
    param_idx++;

#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_UNIT_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR
    // No match!
    return 1;
}
int func_model_config_get_str_option_by_name(func_config_t* func_config,
                                             const char* name,
                                             char* value,
                                             const uint32_t len)
{
    // Increment an index variable on each parameter position
    // so that we can index both the position struct through the macro and the
    // array of parameter names through a simple array of strings.
    int param_idx = 0;

#define DEF_UNIT_START(UNIT)
#define DEF_UNIT_END(UNIT)
#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT)                                                                       \
    if (!strcmp(config_param_str_table[param_idx], name))                                                              \
    {                                                                                                                  \
        strncpy(value, func_config->NAME, len - 1);                                                                    \
        return 0;                                                                                                      \
    }                                                                                                                  \
    param_idx++;

#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, LEN, DEFAULT)                                                            \
    if (!strcmp(config_param_str_table[param_idx], name))                                                              \
    {                                                                                                                  \
        strncpy(value, func_config->UNIT.NAME, len - 1);                                                               \
        return 0;                                                                                                      \
    }                                                                                                                  \
    param_idx++;

#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT) param_idx++;

#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT) param_idx++;

#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_UNIT_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR
    // No match!
    return 1;
}

int func_config_print_config_help(FILE* out)
{
    fprintf(out, "%-40s %s\n", "Option", "Description");
    fprintf(out, "%-40s %s\n", "------", "-----------");

    for (uint32_t i = 0; i < CONFIG_PARAMETER_COUNT; i++)
    {
        fprintf(out, "-C%-40s %s\n", config_param_str_table[i], config_param_desc_table[i]);
    }

    fprintf(out, "\n");

    return 0;
}

int func_model_print_config(func_config_t* func_config, FILE* out)
{
#define DEF_UNIT_START(UNIT)
#define DEF_UNIT_END(UNIT)
#define DEF_OPTION(NAME, DESC, TYPE, FMT, DEFAULT) fprintf(out, "%-40s = " FMT "\n", #NAME, func_config->NAME);
#define DEF_UNIT_OPTION(UNIT, NAME, DESC, TYPE, FMT, DEFAULT)                                                          \
    fprintf(out, "%-40s = " FMT "\n", #UNIT "." #NAME, func_config->UNIT.NAME);
#define DEF_OPTION_STR(NAME, DESC, LEN, DEFAULT) fprintf(out, "%-40s = %s\n", #NAME, func_config->NAME);
#define DEF_UNIT_OPTION_STR(UNIT, NAME, DESC, LEN, DEFAULT)                                                            \
    fprintf(out, "%-40s = %s\n", #UNIT "." #NAME, func_config->UNIT.NAME);

#define FOF_HEX "0x%llx"
#define FOF_DEC "%" PRIu32
#define FOF_DECU64 "%" PRIu64

#include "func_config.def"
#undef DEF_UNIT_START
#undef DEF_UNIT_END
#undef DEF_OPTION
#undef DEF_UNIT_OPTION
#undef DEF_OPTION_STR
#undef DEF_UNIT_OPTION_STR

    return 0;
}

static const char* programname;

void func_model_print_debug_masks(FILE* out)
{
    fprintf(out, "\t  List of components:\n");
#define DEBUG_MODE(string, value) fprintf(out, "\t\t" #string "\n");
#include "debug_modes.def"
#undef DEBUG_MODE
}

int func_model_print_help(FILE* out)
{
    fprintf(out, "TOSA Reference Model help\n\n");

    fprintf(out,
            "Usage: %s [-c] [-C <name=value>] [-d <Debug Mask>] [-h] [-i <uscriptfile>] [-l <verbosity>] [-F "
            "<flatconfig>]\n",
            programname);
    fprintf(out, "\t-c - Print list of config options\n");
    fprintf(out, "\t-C <name=value> - modify config option <name> to <value>\n");
    fprintf(out, "\t-d <Debug Mask - set component debug mask\n");
    func_model_print_debug_masks(out);
    fprintf(out, "\t-F <flatconfig> - parse <flatconfig> as file of config options\n");
    fprintf(out, "\t-h - show this help message and exit\n");
    fprintf(
        out,
        "\t-i <input_tensor_name>,<filename> - set input tensor <input_tensor_name> to the values from <filename>\n");
    fprintf(out, "\t-l <verbosity> - set log verbosity\n");
    fprintf(out, "\t-o <debuglog> - set debug log file\n");
    fprintf(out, "\n");

    func_config_print_config_help(stdout);

    return 0;
}

static const char* get_arg_text(int& index, const int argc, const char** argv)
{
    if (strlen(argv[index]) > 2)
    {
        return argv[index] + 2;
    }

    if ((index + 1 == argc) || (argv[index + 1][0] == '-'))
    {
        fprintf(stderr, "No option value found for option %s\n", argv[index]);
        return "";
    }

    index++;
    return argv[index];
}

// Read the command line arguments
int func_model_parse_cmd_line(func_config_t* func_config, func_debug_t* func_debug, const int argc, const char** argv)
{
    int i;
    programname = argv[0];
    for (i = 1; i < argc; i++)
    {
        // All command line arguments must begin with -X  where X is a recognized character
        if (strlen(argv[i]) < 2 || argv[i][0] != '-')
        {
            func_model_print_help(stderr);
            ARG_ERROR("Command line argument at position %d not valid: %s", i, argv[i]);
        }

        switch (argv[i][1])
        {
                // Model parameters may be overridden with the -Cname=value switch
            case 'c':
                func_config_print_config_help(stderr);
                return 1;

            case 'C':
            {
                const char *name = nullptr, *value = nullptr;

                // Break the string into name and value parts
                name  = get_arg_text(i, argc, argv);
                value = strchr(name, '=');

                if (value == nullptr)
                {
                    func_model_print_help(stderr);
                    ARG_ERROR("Cannot parse -C argument at position %d: %s", i, argv[i]);
                }

                *const_cast<char*>(value) = 0;

                if (func_model_config_set_option(func_config, name, value + 1))
                {
                    func_model_print_help(stderr);
                    ARG_ERROR("Cannot parse -C argument at position %d: %s", i, argv[i]);
                }
                break;
            }

            case 'd':
            case 'D':
            {
                func_debug_set_mask(func_debug, get_arg_text(i, argc, argv));
                break;
            }
            case 'F':
            {
                // Read a flat configuration file
                if (func_model_parse_flat_config_file(func_config, get_arg_text(i, argc, argv)))
                    return 1;

                break;
            }
            case 'h':
                func_model_print_help(stderr);
                return 1;

            case 'i':
            {
                // shortcut for '-Cinput_tensor='
                if (func_model_config_set_option(func_config, "input_tensor", get_arg_text(i, argc, argv)))
                {
                    func_model_print_help(stderr);
                    ARG_ERROR("Cannot set input tensor config value");
                }
                break;
            }
            case 'l':
            {
                // Debug verbosity/logging level
                func_debug_set_verbosity(func_debug, get_arg_text(i, argc, argv));
                break;
            }
            case 'o':
            {
                func_debug_set_file(func_debug, get_arg_text(i, argc, argv));
                break;
            }
            default:
                func_model_print_help(stderr);
                ARG_ERROR("Unrecognized argument at position %d: %s", i, argv[i]);
        }
    }

    return 0;
}

int func_model_parse_flat_config_file(func_config_t* func_config, const char* filename)
{
    const int MAX_LINE_LEN = 1024;

    FILE* infile = nullptr;
    char line_buf[MAX_LINE_LEN];
    int line = 1;

    infile = fopen(filename, "r");

    if (infile == nullptr)
    {
        ARG_ERROR("Cannot open config file: %s\n", filename);
    }

    while (fgets(line_buf, MAX_LINE_LEN - 1, infile) != nullptr)
    {
        char *name = line_buf, *value = nullptr, *comment = nullptr, *ptr = nullptr;

        // Remove comments
        comment = strchr(line_buf, '#');

        if (comment)
            *comment = 0;

        // Break the string into name and value parts
        name = line_buf;

        // Remove leading whitespace
        while (*name && isspace(*name))
            name++;

        // Empty line?
        if (*name == 0)
        {
            line++;
            continue;
        }

        value = strchr(name, '=');

        // Missing value
        if (value == nullptr)
        {
            ARG_ERROR("Cannot parse parameter in %s at line %d: %s", filename, line, line_buf);
        }

        // Remove the =
        *value = 0;
        value++;

        // Trim off any whitespace at the end of the value
        ptr = value;
        while (*ptr != 0 && !isspace(*ptr))
            ptr++;
        *ptr = 0;

        // Include a nested file
        if (!strcmp(name, "include"))
        {
            if (func_model_parse_flat_config_file(func_config, value))
                return 1;
            line++;
            continue;
        }

        if (func_model_config_set_option(func_config, name, value))
        {
            func_model_print_help(stderr);
            ARG_ERROR("Cannot set parameter in %s at line %d: %s", filename, line, line_buf)
        }

        line++;
    }

    fclose(infile);

    return 0;
}