2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2022-01-05 08:42:46 +00:00
|
|
|
* Copyright © 2017-2022 The Looking Glass Authors
|
2021-06-06 01:26:18 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2019-05-09 12:06:58 +00:00
|
|
|
|
|
|
|
#include "common/option.h"
|
|
|
|
#include "common/debug.h"
|
2019-05-21 01:31:31 +00:00
|
|
|
#include "common/stringutils.h"
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2021-08-15 02:11:59 +00:00
|
|
|
#include <ctype.h>
|
2021-08-28 10:04:46 +00:00
|
|
|
#include <math.h>
|
2019-05-09 12:06:58 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
2019-05-11 08:22:01 +00:00
|
|
|
#include <stdio.h>
|
2019-05-09 12:06:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct OptionGroup
|
|
|
|
{
|
|
|
|
const char * module;
|
|
|
|
struct Option ** options;
|
|
|
|
int count;
|
|
|
|
int pad;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct State
|
|
|
|
{
|
2021-05-05 18:10:46 +00:00
|
|
|
enum doHelpMode doHelp;
|
2019-05-16 23:25:57 +00:00
|
|
|
struct Option ** options;
|
2019-05-09 12:06:58 +00:00
|
|
|
int oCount;
|
|
|
|
struct OptionGroup * groups;
|
|
|
|
int gCount;
|
|
|
|
};
|
|
|
|
|
2019-05-11 09:07:10 +00:00
|
|
|
static struct State state =
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2021-05-05 18:10:46 +00:00
|
|
|
.doHelp = DOHELP_MODE_NO,
|
2019-05-09 12:06:58 +00:00
|
|
|
.options = NULL,
|
|
|
|
.oCount = 0,
|
|
|
|
.groups = NULL,
|
|
|
|
.gCount = 0
|
|
|
|
};
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
static bool int_parser(struct Option * opt, const char * str)
|
|
|
|
{
|
|
|
|
opt->value.x_int = atol(str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool bool_parser(struct Option * opt, const char * str)
|
|
|
|
{
|
|
|
|
opt->value.x_bool =
|
2021-07-30 22:35:41 +00:00
|
|
|
strcasecmp(str, "1" ) == 0 ||
|
|
|
|
strcasecmp(str, "on" ) == 0 ||
|
|
|
|
strcasecmp(str, "yes" ) == 0 ||
|
|
|
|
strcasecmp(str, "true") == 0;
|
2019-05-11 10:58:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-09 15:09:40 +00:00
|
|
|
static bool float_parser(struct Option * opt, const char * str)
|
|
|
|
{
|
|
|
|
opt->value.x_float = atof(str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
static bool string_parser(struct Option * opt, const char * str)
|
|
|
|
{
|
|
|
|
free(opt->value.x_string);
|
|
|
|
opt->value.x_string = strdup(str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * int_toString(struct Option * opt)
|
|
|
|
{
|
|
|
|
int len = snprintf(NULL, 0, "%d", opt->value.x_int);
|
|
|
|
char * ret = malloc(len + 1);
|
2022-03-06 23:13:54 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
sprintf(ret, "%d", opt->value.x_int);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * bool_toString(struct Option * opt)
|
|
|
|
{
|
|
|
|
return strdup(opt->value.x_bool ? "yes" : "no");
|
|
|
|
}
|
|
|
|
|
2021-08-09 15:09:40 +00:00
|
|
|
static char * float_toString(struct Option * opt)
|
|
|
|
{
|
|
|
|
int len = snprintf(NULL, 0, "%f", opt->value.x_float);
|
|
|
|
char * ret = malloc(len + 1);
|
2022-03-06 23:13:54 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-09 15:09:40 +00:00
|
|
|
sprintf(ret, "%f", opt->value.x_float);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
static char * string_toString(struct Option * opt)
|
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
if (!opt->value.x_string)
|
|
|
|
return NULL;
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
return strdup(opt->value.x_string);
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
bool option_register(struct Option options[])
|
|
|
|
{
|
|
|
|
int new = 0;
|
2019-05-11 10:58:49 +00:00
|
|
|
for(int i = 0; options[i].type != OPTION_TYPE_NONE; ++i)
|
2019-05-09 12:06:58 +00:00
|
|
|
++new;
|
|
|
|
|
|
|
|
state.options = realloc(
|
|
|
|
state.options,
|
2021-08-15 16:18:12 +00:00
|
|
|
sizeof(*state.options) * (state.oCount + new)
|
2019-05-09 12:06:58 +00:00
|
|
|
);
|
|
|
|
|
2022-03-06 23:13:54 +00:00
|
|
|
if (!state.options)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
for(int i = 0; options[i].type != OPTION_TYPE_NONE; ++i)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2022-03-06 23:13:54 +00:00
|
|
|
struct Option * o =
|
|
|
|
state.options[state.oCount + i] = malloc(sizeof(**state.options));
|
|
|
|
if (!o)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:18:12 +00:00
|
|
|
memcpy(o, &options[i], sizeof(*o));
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
if (!o->parser)
|
|
|
|
{
|
|
|
|
switch(o->type)
|
|
|
|
{
|
|
|
|
case OPTION_TYPE_INT:
|
|
|
|
o->parser = int_parser;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_TYPE_STRING:
|
|
|
|
o->parser = string_parser;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_TYPE_BOOL:
|
|
|
|
o->parser = bool_parser;
|
|
|
|
break;
|
|
|
|
|
2021-08-09 15:09:40 +00:00
|
|
|
case OPTION_TYPE_FLOAT:
|
|
|
|
o->parser = float_parser;
|
|
|
|
break;
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
default:
|
|
|
|
DEBUG_ERROR("BUG: Non int/string/bool option types must have a parser");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!o->toString)
|
|
|
|
{
|
|
|
|
switch(o->type)
|
|
|
|
{
|
|
|
|
case OPTION_TYPE_INT:
|
|
|
|
o->toString = int_toString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_TYPE_STRING:
|
|
|
|
o->toString = string_toString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_TYPE_BOOL:
|
|
|
|
o->toString = bool_toString;
|
|
|
|
break;
|
|
|
|
|
2021-08-09 15:09:40 +00:00
|
|
|
case OPTION_TYPE_FLOAT:
|
|
|
|
o->toString = float_toString;
|
|
|
|
break;
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
default:
|
|
|
|
DEBUG_ERROR("BUG: Non int/string/bool option types must implement toString");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
// ensure the string is locally allocated
|
2019-05-11 10:58:49 +00:00
|
|
|
if (o->type == OPTION_TYPE_STRING)
|
2019-05-21 01:31:31 +00:00
|
|
|
{
|
|
|
|
if (o->value.x_string)
|
2022-03-06 23:13:54 +00:00
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
o->value.x_string = strdup(o->value.x_string);
|
2022-03-06 23:13:54 +00:00
|
|
|
if (!o->value.x_string)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-05-21 01:31:31 +00:00
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
|
|
|
// add the option to the correct group for help printout
|
|
|
|
bool found = false;
|
|
|
|
for(int g = 0; g < state.gCount; ++g)
|
|
|
|
{
|
|
|
|
struct OptionGroup * group = &state.groups[g];
|
|
|
|
if (strcmp(group->module, o->module) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
group->options = realloc(
|
|
|
|
group->options,
|
2021-08-15 16:18:12 +00:00
|
|
|
sizeof(*group->options) * (group->count + 1)
|
2019-05-09 12:06:58 +00:00
|
|
|
);
|
2022-03-06 23:13:54 +00:00
|
|
|
if (!group->options)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
group->options[group->count] = o;
|
|
|
|
|
|
|
|
int len = strlen(o->name);
|
|
|
|
if (len > group->pad)
|
|
|
|
group->pad = len;
|
|
|
|
|
|
|
|
++group->count;
|
2019-05-16 23:25:57 +00:00
|
|
|
break;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
state.groups = realloc(
|
|
|
|
state.groups,
|
2021-08-15 16:18:12 +00:00
|
|
|
sizeof(*state.groups) * (state.gCount + 1)
|
2019-05-09 12:06:58 +00:00
|
|
|
);
|
2022-03-06 23:13:54 +00:00
|
|
|
if (!state.groups)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
|
|
|
struct OptionGroup * group = &state.groups[state.gCount];
|
|
|
|
++state.gCount;
|
|
|
|
|
|
|
|
group->module = o->module;
|
2021-08-15 16:18:12 +00:00
|
|
|
group->options = malloc(sizeof(*group->options));
|
2019-05-09 12:06:58 +00:00
|
|
|
group->options[0] = o;
|
|
|
|
group->count = 1;
|
|
|
|
group->pad = strlen(o->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.oCount += new;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
void option_free(void)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
|
|
|
for(int i = 0; i < state.oCount; ++i)
|
|
|
|
{
|
2019-05-16 23:25:57 +00:00
|
|
|
struct Option * o = state.options[i];
|
2022-03-06 23:13:54 +00:00
|
|
|
if (o->type == OPTION_TYPE_STRING && o->value.x_string)
|
2019-05-11 10:58:49 +00:00
|
|
|
free(o->value.x_string);
|
2019-05-16 23:25:57 +00:00
|
|
|
free(o);
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
free(state.options);
|
|
|
|
state.options = NULL;
|
|
|
|
state.oCount = 0;
|
|
|
|
|
2021-01-24 08:39:00 +00:00
|
|
|
for(int g = 0; g < state.gCount; ++g)
|
|
|
|
{
|
|
|
|
struct OptionGroup * group = &state.groups[g];
|
|
|
|
if (group->options)
|
|
|
|
free(group->options);
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
free(state.groups);
|
|
|
|
state.groups = NULL;
|
|
|
|
state.gCount = 0;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
static bool option_set(struct Option * opt, const char * value)
|
2019-05-11 08:22:01 +00:00
|
|
|
{
|
2019-05-12 06:51:37 +00:00
|
|
|
if (!opt->parser(opt, value))
|
|
|
|
{
|
|
|
|
opt->failed_set = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
opt->failed_set = false;
|
|
|
|
return true;
|
2019-05-11 08:22:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
bool option_parse(int argc, char * argv[])
|
|
|
|
{
|
|
|
|
for(int a = 1; a < argc; ++a)
|
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
struct Option * o = NULL;
|
|
|
|
char * value = NULL;
|
|
|
|
|
|
|
|
// emulate getopt for backwards compatability
|
|
|
|
if (argv[a][0] == '-')
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2021-07-30 22:35:41 +00:00
|
|
|
if (strcasecmp(argv[a], "-h") == 0 || strcasecmp(argv[a], "--help") == 0)
|
2019-05-21 01:31:31 +00:00
|
|
|
{
|
2021-05-05 18:10:46 +00:00
|
|
|
state.doHelp = DOHELP_MODE_YES;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-30 22:35:41 +00:00
|
|
|
if (strcasecmp(argv[a], "--rst-help") == 0)
|
2021-05-05 18:10:46 +00:00
|
|
|
{
|
|
|
|
state.doHelp = DOHELP_MODE_RST;
|
2019-05-21 01:31:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
if (strlen(argv[a]) != 2)
|
|
|
|
{
|
2021-07-30 22:35:41 +00:00
|
|
|
DEBUG_WARN("Ignored invalid argument: %s", argv[a]);
|
2019-05-21 01:31:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
for(int i = 0; i < state.oCount; ++i)
|
|
|
|
{
|
|
|
|
if (state.options[i]->shortopt == argv[a][1])
|
|
|
|
{
|
|
|
|
o = state.options[i];
|
2019-05-21 01:34:50 +00:00
|
|
|
if (o->type != OPTION_TYPE_BOOL && a < argc - 1)
|
|
|
|
{
|
2023-05-02 02:27:17 +00:00
|
|
|
char * v = argv[a];
|
|
|
|
|
|
|
|
//ltrim
|
|
|
|
while(*v && isspace(*v))
|
|
|
|
++v;
|
|
|
|
|
|
|
|
int valueLen = strlen(v);
|
|
|
|
//rtrim
|
|
|
|
while (valueLen > 1 && isspace(v[valueLen-1]))
|
|
|
|
--valueLen;
|
|
|
|
|
|
|
|
value = strndup(v, valueLen);
|
2019-05-21 01:34:50 +00:00
|
|
|
}
|
2019-05-21 01:31:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
char * arg = strdup(argv[a]);
|
|
|
|
char * module = strtok(arg , ":");
|
|
|
|
char * name = strtok(NULL, "=");
|
|
|
|
value = strtok(NULL, "" );
|
|
|
|
|
|
|
|
if (!module || !name)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("Ignored invalid argument: %s", argv[a]);
|
|
|
|
free(arg);
|
2019-05-09 12:06:58 +00:00
|
|
|
continue;
|
2019-05-21 01:31:31 +00:00
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
o = option_get(module, name);
|
|
|
|
if (value)
|
2023-05-02 02:27:17 +00:00
|
|
|
{
|
|
|
|
//ltrim
|
|
|
|
while(*value && isspace(*value))
|
|
|
|
++value;
|
|
|
|
|
|
|
|
int valueLen = strlen(value);
|
|
|
|
//rtrim
|
|
|
|
while (valueLen > 1 && isspace(value[valueLen-1]))
|
|
|
|
--valueLen;
|
|
|
|
|
|
|
|
value = strndup(value, valueLen);
|
|
|
|
}
|
2019-05-21 01:31:31 +00:00
|
|
|
|
|
|
|
free(arg);
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 03:24:28 +00:00
|
|
|
if (!o)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("Ignored unknown argument: %s", argv[a]);
|
|
|
|
free(value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
if (!value)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
if (o->type == OPTION_TYPE_BOOL)
|
|
|
|
{
|
2019-05-21 02:58:53 +00:00
|
|
|
o->value.x_bool = !o->value.x_bool;
|
2019-05-21 01:31:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (o->type != OPTION_TYPE_CUSTOM)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("Ignored invalid argument, missing value: %s", argv[a]);
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
option_set(o, value);
|
|
|
|
free(value);
|
2019-05-11 08:22:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * file_parse_module(FILE * fp)
|
|
|
|
{
|
|
|
|
char * module = NULL;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
for(int c = fgetc(fp); !feof(fp); c = fgetc(fp))
|
|
|
|
{
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case ']':
|
|
|
|
if (module)
|
|
|
|
module[len] = '\0';
|
|
|
|
return module;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
free(module);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (len % 32 == 0)
|
2021-09-11 00:38:07 +00:00
|
|
|
{
|
|
|
|
char * p = realloc(module, len + 32 + 1);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
free(module);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
module = p;
|
|
|
|
}
|
2019-05-11 08:22:01 +00:00
|
|
|
module[len++] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (module)
|
|
|
|
free(module);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-07 07:09:31 +00:00
|
|
|
static bool process_option_line(const char * module, const char * name,
|
|
|
|
char * value, int valueLen, int lineno)
|
|
|
|
{
|
|
|
|
if (!module)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, module not specified for option", lineno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Option * o = option_get(module, name);
|
|
|
|
if (!o)
|
|
|
|
DEBUG_WARN("Ignored unknown option %s:%s", module, name);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value)
|
2023-05-02 02:27:17 +00:00
|
|
|
{
|
|
|
|
//rtrim
|
|
|
|
while (valueLen > 1 && isspace(name[valueLen-1]))
|
|
|
|
--valueLen;
|
|
|
|
|
2021-06-07 07:09:31 +00:00
|
|
|
value[valueLen] = '\0';
|
2023-05-02 02:27:17 +00:00
|
|
|
}
|
2021-06-07 07:09:31 +00:00
|
|
|
|
|
|
|
if (!option_set(o, value))
|
|
|
|
DEBUG_ERROR("Failed to set the option value");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
bool option_load(const char * filename)
|
|
|
|
{
|
|
|
|
FILE * fp = fopen(filename, "r");
|
|
|
|
if (!fp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
int lineno = 1;
|
|
|
|
char * module = NULL;
|
|
|
|
bool line = true;
|
2020-01-12 11:44:41 +00:00
|
|
|
bool comment = false;
|
2019-05-11 08:22:01 +00:00
|
|
|
bool expectLine = false;
|
|
|
|
bool expectValue = false;
|
|
|
|
char * name = NULL;
|
|
|
|
int nameLen = 0;
|
|
|
|
char * value = NULL;
|
|
|
|
int valueLen = 0;
|
|
|
|
|
|
|
|
char ** p = &name;
|
|
|
|
int * len = &nameLen;
|
|
|
|
|
|
|
|
for(int c = fgetc(fp); !feof(fp); c = fgetc(fp))
|
|
|
|
{
|
2020-01-12 11:44:41 +00:00
|
|
|
if (comment && c != '\n')
|
|
|
|
continue;
|
2020-01-21 05:35:21 +00:00
|
|
|
comment = false;
|
2020-01-12 11:44:41 +00:00
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
switch(c)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-11 08:22:01 +00:00
|
|
|
case '[':
|
|
|
|
if (expectLine)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, expected new line", lineno);
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line)
|
|
|
|
{
|
|
|
|
free(module);
|
|
|
|
module = file_parse_module(fp);
|
|
|
|
if (!module)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, failed to parse the module", lineno);
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
line = false;
|
|
|
|
expectLine = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*len % 32 == 0)
|
2021-09-11 00:38:07 +00:00
|
|
|
{
|
|
|
|
char * tmp = realloc(*p, *len + 32 + 1);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
*p = tmp;
|
|
|
|
}
|
2019-05-11 08:22:01 +00:00
|
|
|
(*p)[(*len)++] = c;
|
2019-05-09 12:06:58 +00:00
|
|
|
break;
|
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
case '\r':
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case '\n':
|
2021-06-07 07:09:31 +00:00
|
|
|
if (name && !process_option_line(module, name, value, valueLen, lineno))
|
2019-05-11 08:22:01 +00:00
|
|
|
{
|
2021-06-07 07:09:31 +00:00
|
|
|
result = false;
|
|
|
|
goto exit;
|
2019-05-11 08:22:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
line = true;
|
|
|
|
expectLine = false;
|
|
|
|
expectValue = false;
|
|
|
|
++lineno;
|
|
|
|
|
|
|
|
p = &name;
|
|
|
|
len = &nameLen;
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
name = NULL;
|
|
|
|
nameLen = 0;
|
|
|
|
free(value);
|
|
|
|
value = NULL;
|
|
|
|
valueLen = 0;
|
2019-05-09 12:06:58 +00:00
|
|
|
break;
|
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
case '=':
|
|
|
|
if (!expectValue)
|
|
|
|
{
|
|
|
|
if (!name)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, expected option name", lineno);
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-05-21 02:34:41 +00:00
|
|
|
//rtrim
|
2021-08-15 02:11:59 +00:00
|
|
|
while (nameLen > 1 && isspace(name[nameLen-1]))
|
2019-05-21 02:34:41 +00:00
|
|
|
--nameLen;
|
2019-05-11 08:22:01 +00:00
|
|
|
name[nameLen] = '\0';
|
|
|
|
expectValue = true;
|
|
|
|
|
|
|
|
p = &value;
|
|
|
|
len = &valueLen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*len % 32 == 0)
|
2021-09-11 00:38:07 +00:00
|
|
|
{
|
|
|
|
char * tmp = realloc(*p, *len + 32 + 1);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
*p = tmp;
|
|
|
|
}
|
2019-05-11 08:22:01 +00:00
|
|
|
(*p)[(*len)++] = c;
|
2019-05-09 12:06:58 +00:00
|
|
|
break;
|
|
|
|
|
2020-01-12 11:44:41 +00:00
|
|
|
case ';':
|
|
|
|
if (line)
|
|
|
|
{
|
|
|
|
comment = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fallthrough
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
default:
|
2021-06-07 01:29:17 +00:00
|
|
|
// ignore non-typeable ascii characters
|
|
|
|
if (c < 32 || c > 126)
|
|
|
|
continue;
|
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
if (expectLine)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, expected new line", lineno);
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
line = false;
|
|
|
|
|
2019-05-21 02:34:41 +00:00
|
|
|
//ltrim
|
2021-08-15 02:11:59 +00:00
|
|
|
if (*len == 0 && isspace(c))
|
2019-05-21 02:34:41 +00:00
|
|
|
break;
|
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
if (*len % 32 == 0)
|
2021-09-11 00:38:07 +00:00
|
|
|
{
|
|
|
|
char * tmp = realloc(*p, *len + 32 + 1);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
*p = tmp;
|
|
|
|
}
|
2019-05-11 08:22:01 +00:00
|
|
|
(*p)[(*len)++] = c;
|
2019-05-09 12:06:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-05-11 01:21:00 +00:00
|
|
|
}
|
2019-05-09 13:05:33 +00:00
|
|
|
|
2021-06-07 07:09:31 +00:00
|
|
|
if (name && !process_option_line(module, name, value, valueLen, lineno))
|
|
|
|
result = false;
|
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
exit:
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
free(module);
|
|
|
|
free(name );
|
|
|
|
free(value );
|
|
|
|
|
|
|
|
return result;
|
2019-05-11 01:35:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
bool option_validate(void)
|
2019-05-11 01:35:17 +00:00
|
|
|
{
|
2021-05-05 18:10:46 +00:00
|
|
|
if (state.doHelp != DOHELP_MODE_NO)
|
2019-05-11 01:50:26 +00:00
|
|
|
{
|
|
|
|
option_print();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-11 01:21:00 +00:00
|
|
|
// validate the option values
|
|
|
|
bool ok = true;
|
|
|
|
for(int i = 0; i < state.oCount; ++i)
|
|
|
|
{
|
2019-05-16 23:25:57 +00:00
|
|
|
struct Option * o = state.options[i];
|
2019-05-11 01:35:17 +00:00
|
|
|
const char * error = NULL;
|
2019-05-12 06:51:37 +00:00
|
|
|
bool invalid = o->failed_set;
|
2019-05-11 01:35:17 +00:00
|
|
|
|
2019-05-12 06:51:37 +00:00
|
|
|
if (!invalid && o->validator)
|
|
|
|
invalid = !o->validator(o, &error);
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-12 06:51:37 +00:00
|
|
|
if (invalid)
|
|
|
|
{
|
|
|
|
printf("\nInvalid value provided to the option: %s:%s\n", o->module, o->name);
|
2019-05-12 06:13:50 +00:00
|
|
|
|
2019-05-12 06:51:37 +00:00
|
|
|
if (error)
|
|
|
|
printf("\n Error: %s\n", error);
|
|
|
|
|
|
|
|
if (o->getValues)
|
|
|
|
{
|
|
|
|
StringList values = o->getValues(o);
|
|
|
|
printf("\nValid values are:\n\n");
|
|
|
|
for(unsigned int v = 0; v < stringlist_count(values); ++v)
|
|
|
|
printf(" * %s\n", stringlist_at(values, v));
|
|
|
|
stringlist_free(&values);
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-12 06:51:37 +00:00
|
|
|
if (o->printHelp)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
o->printHelp();
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
2019-05-12 06:51:37 +00:00
|
|
|
|
|
|
|
ok = false;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-11 01:35:17 +00:00
|
|
|
if (!ok)
|
|
|
|
printf("\n");
|
|
|
|
|
2019-05-11 01:21:00 +00:00
|
|
|
return ok;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 18:10:46 +00:00
|
|
|
void option_print_hrule(char * headerLine, int maxLen, char ruleChar)
|
|
|
|
{
|
|
|
|
printf(" +%c", ruleChar);
|
|
|
|
for (int i = 0; i < maxLen; i++)
|
|
|
|
{
|
|
|
|
if(i < strlen(headerLine))
|
|
|
|
{
|
|
|
|
if (headerLine[i] == '|')
|
|
|
|
{
|
|
|
|
putc('+', stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putc(ruleChar, stdout);
|
|
|
|
}
|
|
|
|
printf("%c+\n", ruleChar);
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
void option_print(void)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"The following is a complete list of options accepted by this application\n\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
for(int g = 0; g < state.gCount; ++g)
|
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
StringList lines = stringlist_new(true);
|
|
|
|
StringList values = stringlist_new(true);
|
|
|
|
int len;
|
|
|
|
int maxLen;
|
|
|
|
int valueLen = 5;
|
|
|
|
char * line;
|
2021-05-05 18:10:46 +00:00
|
|
|
char * headerLine;
|
2019-05-21 01:31:31 +00:00
|
|
|
|
|
|
|
// ensure the pad length is atleast as wide as the heading
|
|
|
|
if (state.groups[g].pad < 4)
|
|
|
|
state.groups[g].pad = 4;
|
|
|
|
|
|
|
|
// get the values and the max value length
|
2019-05-09 12:06:58 +00:00
|
|
|
for(int i = 0; i < state.groups[g].count; ++i)
|
|
|
|
{
|
|
|
|
struct Option * o = state.groups[g].options[i];
|
2021-08-30 08:17:04 +00:00
|
|
|
if (o->preset)
|
|
|
|
continue;
|
2019-05-11 10:58:49 +00:00
|
|
|
char * value = o->toString(o);
|
2019-05-21 01:31:31 +00:00
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
value = strdup("NULL");
|
|
|
|
len = 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
len = strlen(value);
|
|
|
|
|
|
|
|
if (len > valueLen)
|
|
|
|
valueLen = len;
|
|
|
|
|
|
|
|
stringlist_push(values, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the heading
|
|
|
|
maxLen = alloc_sprintf(
|
|
|
|
&line,
|
|
|
|
"%-*s | Short | %-*s | Description",
|
2020-01-10 04:23:49 +00:00
|
|
|
(int)(strlen(state.groups[g].module) + state.groups[g].pad + 1),
|
2019-05-21 01:31:31 +00:00
|
|
|
"Long",
|
|
|
|
valueLen,
|
|
|
|
"Value"
|
|
|
|
);
|
|
|
|
|
2021-08-13 23:32:37 +00:00
|
|
|
DEBUG_ASSERT(maxLen > 0);
|
2021-05-05 18:10:46 +00:00
|
|
|
headerLine = line;
|
2019-05-21 01:31:31 +00:00
|
|
|
stringlist_push(lines, line);
|
|
|
|
|
|
|
|
for(int i = 0; i < state.groups[g].count; ++i)
|
|
|
|
{
|
|
|
|
struct Option * o = state.groups[g].options[i];
|
2021-08-30 08:17:04 +00:00
|
|
|
if (o->preset)
|
|
|
|
continue;
|
2019-05-21 01:31:31 +00:00
|
|
|
|
2021-08-30 08:17:04 +00:00
|
|
|
char * value = stringlist_at(values, i);
|
2019-05-21 01:31:31 +00:00
|
|
|
len = alloc_sprintf(
|
|
|
|
&line,
|
|
|
|
"%s:%-*s | %c%c | %-*s | %s",
|
|
|
|
o->module,
|
|
|
|
state.groups[g].pad,
|
|
|
|
o->name,
|
|
|
|
o->shortopt ? '-' : ' ',
|
|
|
|
o->shortopt ? o->shortopt : ' ',
|
|
|
|
valueLen,
|
|
|
|
value,
|
|
|
|
o->description
|
|
|
|
);
|
|
|
|
|
2021-08-13 23:32:37 +00:00
|
|
|
DEBUG_ASSERT(len > 0);
|
2019-05-21 01:31:31 +00:00
|
|
|
stringlist_push(lines, line);
|
|
|
|
if (len > maxLen)
|
|
|
|
maxLen = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
stringlist_free(&values);
|
|
|
|
|
2021-08-30 08:17:04 +00:00
|
|
|
if (stringlist_count(lines) <= 1)
|
|
|
|
{
|
|
|
|
stringlist_free(&lines);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
// print out the lines
|
|
|
|
for(int i = 0; i < stringlist_count(lines); ++i)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
{
|
2021-05-05 18:10:46 +00:00
|
|
|
option_print_hrule(headerLine, maxLen, '-');
|
2019-05-21 01:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char * line = stringlist_at(lines, i);
|
|
|
|
printf(" | %-*s |\n", maxLen, line);
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
2021-05-05 18:10:46 +00:00
|
|
|
option_print_hrule(headerLine, maxLen, state.doHelp == DOHELP_MODE_RST ? '=' : '-');
|
|
|
|
}
|
|
|
|
else if (state.doHelp == DOHELP_MODE_RST && i < stringlist_count(lines) - 1)
|
|
|
|
{
|
|
|
|
option_print_hrule(headerLine, maxLen, '-');
|
2019-05-21 01:31:31 +00:00
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
2019-05-21 01:31:31 +00:00
|
|
|
|
2021-05-05 18:10:46 +00:00
|
|
|
option_print_hrule(headerLine, maxLen, '-');
|
2019-05-21 01:31:31 +00:00
|
|
|
|
|
|
|
stringlist_free(&lines);
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 06:30:17 +00:00
|
|
|
// dump the options in ini format into the file
|
2021-08-30 08:23:09 +00:00
|
|
|
bool option_dump_preset(FILE * file)
|
2021-08-27 06:30:17 +00:00
|
|
|
{
|
2021-08-30 08:23:09 +00:00
|
|
|
for (int g = 0; g < state.gCount; ++g)
|
2021-08-27 06:30:17 +00:00
|
|
|
{
|
2021-08-30 08:23:09 +00:00
|
|
|
bool hasPreset = false;
|
|
|
|
for (int i = 0; i < state.groups[g].count; ++i)
|
|
|
|
hasPreset |= state.groups[g].options[i]->preset;
|
|
|
|
if (!hasPreset)
|
2021-08-27 06:30:17 +00:00
|
|
|
continue;
|
2021-08-30 08:23:09 +00:00
|
|
|
|
|
|
|
fprintf(file, "[%s]\n", state.groups[g].module);
|
|
|
|
|
|
|
|
for (int i = 0; i < state.groups[g].count; ++i)
|
|
|
|
{
|
|
|
|
struct Option * o = state.groups[g].options[i];
|
|
|
|
if (!o->preset)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char * value = o->toString(o);
|
|
|
|
fprintf(file, "%s=%s\n", o->name, value);
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
fputc('\n', file);
|
2021-08-27 06:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
struct Option * option_get(const char * module, const char * name)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
|
|
|
for(int i = 0; i < state.oCount; ++i)
|
|
|
|
{
|
2019-05-16 23:25:57 +00:00
|
|
|
struct Option * o = state.options[i];
|
2021-07-30 22:35:41 +00:00
|
|
|
if ((strcasecmp(o->module, module) == 0) && (strcasecmp(o->name, name) == 0))
|
2019-05-11 10:58:49 +00:00
|
|
|
return o;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int option_get_int(const char * module, const char * name)
|
|
|
|
{
|
2019-05-11 10:58:49 +00:00
|
|
|
struct Option * o = option_get(module, name);
|
2019-05-09 12:06:58 +00:00
|
|
|
if (!o)
|
2019-05-21 04:58:11 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("BUG: Failed to get the value for option %s:%s", module, name);
|
2019-05-09 12:06:58 +00:00
|
|
|
return -1;
|
2019-05-21 04:58:11 +00:00
|
|
|
}
|
2021-08-13 23:32:37 +00:00
|
|
|
DEBUG_ASSERT(o->type == OPTION_TYPE_INT);
|
2019-05-11 10:58:49 +00:00
|
|
|
return o->value.x_int;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * option_get_string(const char * module, const char * name)
|
|
|
|
{
|
2019-05-11 10:58:49 +00:00
|
|
|
struct Option * o = option_get(module, name);
|
2019-05-09 12:06:58 +00:00
|
|
|
if (!o)
|
2019-05-21 04:58:11 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("BUG: Failed to get the value for option %s:%s", module, name);
|
2019-05-09 12:06:58 +00:00
|
|
|
return NULL;
|
2019-05-21 04:58:11 +00:00
|
|
|
}
|
2021-08-13 23:32:37 +00:00
|
|
|
DEBUG_ASSERT(o->type == OPTION_TYPE_STRING);
|
2019-05-11 10:58:49 +00:00
|
|
|
return o->value.x_string;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool option_get_bool(const char * module, const char * name)
|
|
|
|
{
|
2019-05-11 10:58:49 +00:00
|
|
|
struct Option * o = option_get(module, name);
|
2019-05-09 12:06:58 +00:00
|
|
|
if (!o)
|
2019-05-21 04:58:11 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("BUG: Failed to get the value for option %s:%s", module, name);
|
2019-05-09 12:06:58 +00:00
|
|
|
return false;
|
2019-05-21 04:58:11 +00:00
|
|
|
}
|
2021-08-13 23:32:37 +00:00
|
|
|
DEBUG_ASSERT(o->type == OPTION_TYPE_BOOL);
|
2019-05-11 10:58:49 +00:00
|
|
|
return o->value.x_bool;
|
2020-01-21 05:35:21 +00:00
|
|
|
}
|
2021-08-09 15:09:40 +00:00
|
|
|
|
2021-08-09 15:36:12 +00:00
|
|
|
float option_get_float(const char * module, const char * name)
|
2021-08-09 15:09:40 +00:00
|
|
|
{
|
|
|
|
struct Option * o = option_get(module, name);
|
|
|
|
if (!o)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("BUG: Failed to get the value for option %s:%s", module, name);
|
2021-08-28 10:04:46 +00:00
|
|
|
return NAN;
|
2021-08-09 15:09:40 +00:00
|
|
|
}
|
2021-08-13 23:32:37 +00:00
|
|
|
DEBUG_ASSERT(o->type == OPTION_TYPE_FLOAT);
|
2021-08-09 15:09:40 +00:00
|
|
|
return o->value.x_float;
|
|
|
|
}
|
2021-08-28 10:08:00 +00:00
|
|
|
|
|
|
|
void option_set_int(const char * module, const char * name, int value)
|
|
|
|
{
|
|
|
|
struct Option * o = option_get(module, name);
|
|
|
|
if (!o)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("BUG: Failed to set the value for option %s:%s", module, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DEBUG_ASSERT(o->type == OPTION_TYPE_INT);
|
|
|
|
o->value.x_int = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void option_set_string(const char * module, const char * name, const char * value)
|
|
|
|
{
|
|
|
|
struct Option * o = option_get(module, name);
|
|
|
|
if (!o)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("BUG: Failed to set the value for option %s:%s", module, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DEBUG_ASSERT(o->type == OPTION_TYPE_STRING);
|
|
|
|
free(o->value.x_string);
|
|
|
|
o->value.x_string = strdup(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void option_set_bool(const char * module, const char * name, bool value)
|
|
|
|
{
|
|
|
|
struct Option * o = option_get(module, name);
|
|
|
|
if (!o)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("BUG: Failed to set the value for option %s:%s", module, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DEBUG_ASSERT(o->type == OPTION_TYPE_BOOL);
|
|
|
|
o->value.x_bool = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void option_set_float(const char * module, const char * name, float value)
|
|
|
|
{
|
|
|
|
struct Option * o = option_get(module, name);
|
|
|
|
if (!o)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("BUG: Failed to set the value for option %s:%s", module, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DEBUG_ASSERT(o->type == OPTION_TYPE_FLOAT);
|
|
|
|
o->value.x_float = value;
|
|
|
|
}
|