Skip to content

Commit

Permalink
Apply clang format.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkestene committed Jul 5, 2023
1 parent ea3e61b commit 40e0089
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 95 deletions.
19 changes: 10 additions & 9 deletions config/ConfigMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ ConfigMap::ConfigMap(std::string filename)

// =======================================================
// =======================================================
ConfigMap::~ConfigMap()
{
} // ConfigMap::~ConfigMap
ConfigMap::~ConfigMap() {} // ConfigMap::~ConfigMap

// =======================================================
// =======================================================
float ConfigMap::getFloat(std::string section, std::string name, float default_value) const
float
ConfigMap::getFloat(std::string section, std::string name, float default_value) const
{
std::string valstr = getString(section, name, "");
const char* value = valstr.c_str();
char* end;
std::string valstr = getString(section, name, "");
const char * value = valstr.c_str();
char * end;
// This parses "1234" (decimal) and also "0x4D2" (hex)
float valFloat = strtof(value, &end);
return end > value ? valFloat : default_value;
} // ConfigMap::getFloat

// =======================================================
// =======================================================
void ConfigMap::setFloat(std::string section, std::string name, float value)
void
ConfigMap::setFloat(std::string section, std::string name, float value)
{

std::stringstream ss;
Expand Down Expand Up @@ -73,7 +73,8 @@ ConfigMap::getBool(std::string section, std::string name, bool default_value) co

// =======================================================
// =======================================================
void ConfigMap::setBool(std::string section, std::string name, bool value)
void
ConfigMap::setBool(std::string section, std::string name, bool value)
{

if (value)
Expand Down
186 changes: 100 additions & 86 deletions config/inih/ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,111 +28,125 @@ rstrip(char * s)
}

/* Return pointer to first non-whitespace char in given string. */
static char* lskip(const char* s)
static char *
lskip(const char * s)
{
while (*s && isspace(*s))
s++;
return (char*)s;
while (*s && isspace(*s))
s++;
return (char *)s;
}

/* Return pointer to first char c or ';' ( or '#') comment in given
string, or pointer to null at end of string if neither found. ';' (or
'#') must be prefixed by a whitespace character to register as a comment. */
static char* find_char_or_comment(const char* s, char c)
static char *
find_char_or_comment(const char * s, char c)
{
int was_whitespace = 0;
while (*s && *s != c && !(was_whitespace && *s == ';')) {
was_whitespace = isspace(*s);
s++;
}
return (char*)s;
int was_whitespace = 0;
while (*s && *s != c && !(was_whitespace && *s == ';'))
{
was_whitespace = isspace(*s);
s++;
}
return (char *)s;
}

/* Version of strncpy that ensures dest (size bytes) is null-terminated. */
static char* strncpy0(char* dest, const char* src, size_t size)
static char *
strncpy0(char * dest, const char * src, size_t size)
{
strncpy(dest, src, size);
dest[size - 1] = '\0';
return dest;
strncpy(dest, src, size);
dest[size - 1] = '\0';
return dest;
}

/* See documentation in header file. */
int ini_parse(const char* filename,
int (*handler)(void*, const char*, const char*, const char*),
void* user)
int
ini_parse(const char * filename,
int (*handler)(void *, const char *, const char *, const char *),
void * user)
{
/* Uses a fair bit of stack (use heap instead if you need to) */
char line[MAX_LINE];
char section[MAX_SECTION] = "";
char prev_name[MAX_NAME] = "";

FILE* file;
char* start;
char* end;
char* name;
char* value;
int lineno = 0;
int error = 0;

file = fopen(filename, "r");
if (!file)
return -1;

/* Scan through file line by line */
while (fgets(line, sizeof(line), file) != NULL) {
lineno++;
start = lskip(rstrip(line));
/* Uses a fair bit of stack (use heap instead if you need to) */
char line[MAX_LINE];
char section[MAX_SECTION] = "";
char prev_name[MAX_NAME] = "";

FILE * file;
char * start;
char * end;
char * name;
char * value;
int lineno = 0;
int error = 0;

file = fopen(filename, "r");
if (!file)
return -1;

/* Scan through file line by line */
while (fgets(line, sizeof(line), file) != NULL)
{
lineno++;
start = lskip(rstrip(line));

#if INI_ALLOW_MULTILINE
if (*prev_name && *start && start > line) {
/* Non-black line with leading whitespace, treat as continuation
of previous name's value (as per Python ConfigParser). */
if (!handler(user, section, prev_name, start) && !error)
error = lineno;
}
else
if (*prev_name && *start && start > line)
{
/* Non-black line with leading whitespace, treat as continuation
of previous name's value (as per Python ConfigParser). */
if (!handler(user, section, prev_name, start) && !error)
error = lineno;
}
else
#endif
if (*start == ';' || *start == '#') {
/* Per Python ConfigParser, allow '#' comments at start of line */
}
else if (*start == '[') {
/* A "[section]" line */
end = find_char_or_comment(start + 1, ']');
if (*end == ']') {
*end = '\0';
strncpy0(section, start + 1, sizeof(section));
*prev_name = '\0';
}
else if (!error) {
/* No ']' found on section line */
error = lineno;
}
}
else if (*start && *start != ';') {
/* Not a comment, must be a name=value pair */
end = find_char_or_comment(start, '=');
if (*end == '=') {
*end = '\0';
name = rstrip(start);
value = lskip(end + 1);
end = find_char_or_comment(value, '\0');
if (*end == ';')
*end = '\0';
rstrip(value);

/* Valid name=value pair found, call handler */
strncpy0(prev_name, name, sizeof(prev_name));
if (!handler(user, section, name, value) && !error)
error = lineno;
}
else if (!error) {
/* No '=' found on name=value line */
error = lineno;
}
}
if (*start == ';' || *start == '#')
{
/* Per Python ConfigParser, allow '#' comments at start of line */
}
else if (*start == '[')
{
/* A "[section]" line */
end = find_char_or_comment(start + 1, ']');
if (*end == ']')
{
*end = '\0';
strncpy0(section, start + 1, sizeof(section));
*prev_name = '\0';
}
else if (!error)
{
/* No ']' found on section line */
error = lineno;
}
}
else if (*start && *start != ';')
{
/* Not a comment, must be a name=value pair */
end = find_char_or_comment(start, '=');
if (*end == '=')
{
*end = '\0';
name = rstrip(start);
value = lskip(end + 1);
end = find_char_or_comment(value, '\0');
if (*end == ';')
*end = '\0';
rstrip(value);

/* Valid name=value pair found, call handler */
strncpy0(prev_name, name, sizeof(prev_name));
if (!handler(user, section, name, value) && !error)
error = lineno;
}
else if (!error)
{
/* No '=' found on name=value line */
error = lineno;
}
}
}

fclose(file);
fclose(file);

return error;
return error;
}

0 comments on commit 40e0089

Please sign in to comment.