Mark constant strings as const

It's hard to trace back, which methods in option parser require freeing
the result and which prohibit it. Marking all non-freeable values as
const lets this problem solve the compiler.
This commit is contained in:
Benedikt Heine 2017-09-25 00:45:04 +02:00
parent 4316c85e18
commit 554c873808
5 changed files with 81 additions and 80 deletions

View File

@ -395,7 +395,7 @@ int dunst_main(int argc, char *argv[])
void usage(int exit_status) void usage(int exit_status)
{ {
puts("usage:\n"); puts("usage:\n");
char *us = cmdline_create_usage(); const char *us = cmdline_create_usage();
puts(us); puts(us);
exit(exit_status); exit(exit_status);
} }

View File

@ -25,21 +25,21 @@ typedef struct _section_t {
static int section_count = 0; static int section_count = 0;
static section_t *sections; static section_t *sections;
static section_t *new_section(char *name); static section_t *new_section(const char *name);
static section_t *get_section(char *name); static section_t *get_section(const char *name);
static void add_entry(char *section_name, char *key, char *value); static void add_entry(const char *section_name, const char *key, const char *value);
static char *get_value(char *section, char *key); static const char *get_value(const char *section, const char *key);
static char *clean_value(char *value); static char *clean_value(const char *value);
static int cmdline_argc; static int cmdline_argc;
static char **cmdline_argv; static char **cmdline_argv;
static char *usage_str = NULL; static char *usage_str = NULL;
static void cmdline_usage_append(char *key, char *type, char *description); static void cmdline_usage_append(const char *key, const char *type, const char *description);
static int cmdline_find_option(char *key); static int cmdline_find_option(const char *key);
section_t *new_section(char *name) section_t *new_section(const char *name)
{ {
for (int i = 0; i < section_count; i++) { for (int i = 0; i < section_count; i++) {
if(!strcmp(name, sections[i].name)) { if(!strcmp(name, sections[i].name)) {
@ -70,7 +70,7 @@ void free_ini(void)
sections = NULL; sections = NULL;
} }
section_t *get_section(char *name) section_t *get_section(const char *name)
{ {
for (int i = 0; i < section_count; i++) { for (int i = 0; i < section_count; i++) {
if (strcmp(sections[i].name, name) == 0) if (strcmp(sections[i].name, name) == 0)
@ -80,7 +80,7 @@ section_t *get_section(char *name)
return NULL; return NULL;
} }
void add_entry(char *section_name, char *key, char *value) void add_entry(const char *section_name, const char *key, const char *value)
{ {
section_t *s = get_section(section_name); section_t *s = get_section(section_name);
if (s == NULL) { if (s == NULL) {
@ -94,7 +94,7 @@ void add_entry(char *section_name, char *key, char *value)
s->entries[s->entry_count - 1].value = clean_value(value); s->entries[s->entry_count - 1].value = clean_value(value);
} }
char *get_value(char *section, char *key) const char *get_value(const char *section, const char *key)
{ {
section_t *s = get_section(section); section_t *s = get_section(section);
if (!s) { if (!s) {
@ -109,44 +109,44 @@ char *get_value(char *section, char *key)
return NULL; return NULL;
} }
char *ini_get_path(char *section, char *key, const char *def) char *ini_get_path(const char *section, const char *key, const char *def)
{ {
return string_to_path(ini_get_string(section, key, def)); return string_to_path(ini_get_string(section, key, def));
} }
char *ini_get_string(char *section, char *key, const char *def) char *ini_get_string(const char *section, const char *key, const char *def)
{ {
char *value = get_value(section, key); const char *value = get_value(section, key);
if (value) if (value)
return g_strdup(value); return g_strdup(value);
return def ? g_strdup(def) : NULL; return def ? g_strdup(def) : NULL;
} }
int ini_get_int(char *section, char *key, int def) int ini_get_int(const char *section, const char *key, int def)
{ {
char *value = get_value(section, key); const char *value = get_value(section, key);
if (value == NULL) if (value == NULL)
return def; return def;
else else
return atoi(value); return atoi(value);
} }
double ini_get_double(char *section, char *key, double def) double ini_get_double(const char *section, const char *key, double def)
{ {
char *value = get_value(section, key); const char *value = get_value(section, key);
if (value == NULL) if (value == NULL)
return def; return def;
else else
return atof(value); return atof(value);
} }
bool ini_is_set(char *ini_section, char *ini_key) bool ini_is_set(const char *ini_section, const char *ini_key)
{ {
return get_value(ini_section, ini_key) != NULL; return get_value(ini_section, ini_key) != NULL;
} }
char *next_section(char *section) const char *next_section(const char *section)
{ {
if (section_count == 0) if (section_count == 0)
return NULL; return NULL;
@ -166,9 +166,9 @@ char *next_section(char *section)
return NULL; return NULL;
} }
int ini_get_bool(char *section, char *key, int def) int ini_get_bool(const char *section, const char *key, int def)
{ {
char *value = get_value(section, key); const char *value = get_value(section, key);
if (value == NULL) if (value == NULL)
return def; return def;
else { else {
@ -191,7 +191,7 @@ int ini_get_bool(char *section, char *key, int def)
} }
} }
char *clean_value(char *value) char *clean_value(const char *value)
{ {
char *s; char *s;
@ -296,7 +296,7 @@ void cmdline_load(int argc, char *argv[])
cmdline_argv = argv; cmdline_argv = argv;
} }
int cmdline_find_option(char *key) int cmdline_find_option(const char *key)
{ {
if (!key) { if (!key) {
return -1; return -1;
@ -331,7 +331,7 @@ int cmdline_find_option(char *key)
return -1; return -1;
} }
static char *cmdline_get_value(char *key) static const char *cmdline_get_value(const char *key)
{ {
int idx = cmdline_find_option(key); int idx = cmdline_find_option(key);
if (idx < 0) { if (idx < 0) {
@ -347,10 +347,10 @@ static char *cmdline_get_value(char *key)
return cmdline_argv[idx + 1]; return cmdline_argv[idx + 1];
} }
char *cmdline_get_string(char *key, const char *def, char *description) char *cmdline_get_string(const char *key, const char *def, const char *description)
{ {
cmdline_usage_append(key, "string", description); cmdline_usage_append(key, "string", description);
char *str = cmdline_get_value(key); const char *str = cmdline_get_value(key);
if (str) if (str)
return g_strdup(str); return g_strdup(str);
@ -360,10 +360,10 @@ char *cmdline_get_string(char *key, const char *def, char *description)
return g_strdup(def); return g_strdup(def);
} }
char *cmdline_get_path(char *key, const char *def, char *description) char *cmdline_get_path(const char *key, const char *def, const char *description)
{ {
cmdline_usage_append(key, "string", description); cmdline_usage_append(key, "string", description);
char *str = cmdline_get_value(key); const char *str = cmdline_get_value(key);
if (str) if (str)
return string_to_path(g_strdup(str)); return string_to_path(g_strdup(str));
@ -371,10 +371,10 @@ char *cmdline_get_path(char *key, const char *def, char *description)
return string_to_path(g_strdup(def)); return string_to_path(g_strdup(def));
} }
int cmdline_get_int(char *key, int def, char *description) int cmdline_get_int(const char *key, int def, const char *description)
{ {
cmdline_usage_append(key, "int", description); cmdline_usage_append(key, "int", description);
char *str = cmdline_get_value(key); const char *str = cmdline_get_value(key);
if (str == NULL) if (str == NULL)
return def; return def;
@ -382,33 +382,35 @@ int cmdline_get_int(char *key, int def, char *description)
return atoi(str); return atoi(str);
} }
double cmdline_get_double(char *key, double def, char *description) double cmdline_get_double(const char *key, double def, const char *description)
{ {
cmdline_usage_append(key, "double", description); cmdline_usage_append(key, "double", description);
char *str = cmdline_get_value(key); const char *str = cmdline_get_value(key);
if (str == NULL) if (str == NULL)
return def; return def;
else else
return atof(str); return atof(str);
} }
int cmdline_get_bool(char *key, int def, char *description) int cmdline_get_bool(const char *key, int def, const char *description)
{ {
cmdline_usage_append(key, "", description); cmdline_usage_append(key, "", description);
int idx = cmdline_find_option(key); int idx = cmdline_find_option(key);
if (idx > 0) if (idx > 0)
return true; return true;
else else
return def; return def;
} }
bool cmdline_is_set(char *key) bool cmdline_is_set(const char *key)
{ {
return cmdline_get_value(key) != NULL; return cmdline_get_value(key) != NULL;
} }
char *option_get_path(char *ini_section, char *ini_key, char *cmdline_key, char *option_get_path(const char *ini_section, const char *ini_key, const char *cmdline_key,
const char *def, char *description) const char *def, const char *description)
{ {
char *val = NULL; char *val = NULL;
@ -425,8 +427,8 @@ char *option_get_path(char *ini_section, char *ini_key, char *cmdline_key,
} }
char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, char *option_get_string(const char *ini_section, const char *ini_key, const char *cmdline_key,
const char *def, char *description) const char *def, const char *description)
{ {
char *val = NULL; char *val = NULL;
@ -443,11 +445,11 @@ char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key,
} }
int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, int option_get_int(const char *ini_section, const char *ini_key, const char *cmdline_key,
char *description) int def, const char *description)
{ {
/* *str is only used to check wether the cmdline option is actually set. */ /* *str is only used to check wether the cmdline option is actually set. */
char *str = cmdline_get_value(cmdline_key); const char *str = cmdline_get_value(cmdline_key);
/* we call cmdline_get_int even when the option isn't set in order to /* we call cmdline_get_int even when the option isn't set in order to
* add the usage info */ * add the usage info */
@ -459,10 +461,10 @@ int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def,
return val; return val;
} }
double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, double option_get_double(const char *ini_section, const char *ini_key, const char *cmdline_key,
double def, char *description) double def, const char *description)
{ {
char *str = cmdline_get_value(cmdline_key); const char *str = cmdline_get_value(cmdline_key);
double val = cmdline_get_double(cmdline_key, def, description); double val = cmdline_get_double(cmdline_key, def, description);
if (!str) if (!str)
@ -471,8 +473,8 @@ double option_get_double(char *ini_section, char *ini_key, char *cmdline_key,
return val; return val;
} }
int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, int option_get_bool(const char *ini_section, const char *ini_key, const char *cmdline_key,
int def, char *description) int def, const char *description)
{ {
int val = false; int val = false;
@ -488,7 +490,7 @@ int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key,
return ini_get_bool(ini_section, ini_key, def); return ini_get_bool(ini_section, ini_key, def);
} }
void cmdline_usage_append(char *key, char *type, char *description) void cmdline_usage_append(const char *key, const char *type, const char *description)
{ {
char *key_type; char *key_type;
if (type && strlen(type) > 0) if (type && strlen(type) > 0)
@ -513,9 +515,9 @@ void cmdline_usage_append(char *key, char *type, char *description)
} }
char *cmdline_create_usage(void) const char *cmdline_create_usage(void)
{ {
return g_strdup(usage_str); return usage_str;
} }
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -6,40 +6,40 @@
#include <stdio.h> #include <stdio.h>
int load_ini_file(FILE *); int load_ini_file(FILE *);
char *ini_get_path(char *section, char *key, const char *def); char *ini_get_path(const char *section, const char *key, const char *def);
char *ini_get_string(char *section, char *key, const char *def); char *ini_get_string(const char *section, const char *key, const char *def);
int ini_get_int(char *section, char *key, int def); int ini_get_int(const char *section, const char *key, int def);
double ini_get_double(char *section, char *key, double def); double ini_get_double(const char *section, const char *key, double def);
int ini_get_bool(char *section, char *key, int def); int ini_get_bool(const char *section, const char *key, int def);
bool ini_is_set(char *ini_section, char *ini_key); bool ini_is_set(const char *ini_section, const char *ini_key);
void free_ini(void); void free_ini(void);
void cmdline_load(int argc, char *argv[]); void cmdline_load(int argc, char *argv[]);
/* for all cmdline_get_* key can be either "-key" or "-key/-longkey" */ /* for all cmdline_get_* key can be either "-key" or "-key/-longkey" */
char *cmdline_get_string(char *key, const char *def, char *description); char *cmdline_get_string(const char *key, const char *def, const char *description);
char *cmdline_get_path(char *key, const char *def, char *description); char *cmdline_get_path(const char *key, const char *def, const char *description);
int cmdline_get_int(char *key, int def, char *description); int cmdline_get_int(const char *key, int def, const char *description);
double cmdline_get_double(char *key, double def, char *description); double cmdline_get_double(const char *key, double def, const char *description);
int cmdline_get_bool(char *key, int def, char *description); int cmdline_get_bool(const char *key, int def, const char *description);
bool cmdline_is_set(char *key); bool cmdline_is_set(const char *key);
char *cmdline_create_usage(void); const char *cmdline_create_usage(void);
char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, char *option_get_string(const char *ini_section, const char *ini_key, const char *cmdline_key,
const char *def, char *description); const char *def, const char *description);
char *option_get_path(char *ini_section, char *ini_key, char *cmdline_key, char *option_get_path(const char *ini_section, const char *ini_key, const char *cmdline_key,
const char *def, char *description); const char *def, const char *description);
int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, int option_get_int(const char *ini_section, const char *ini_key, const char *cmdline_key, int def,
char *description); const char *description);
double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, double option_get_double(const char *ini_section, const char *ini_key, const char *cmdline_key,
double def, char *description); double def, const char *description);
int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, int option_get_bool(const char *ini_section, const char *ini_key, const char *cmdline_key,
int def, char *description); int def, const char *description);
/* returns the next known section. /* returns the next known section.
* if section == NULL returns first section. * if section == NULL returns first section.
* returns NULL if no more sections are available * returns NULL if no more sections are available
*/ */
char *next_section(char *section); const char *next_section(const char *section);
#endif #endif
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -47,7 +47,7 @@ static enum markup_mode parse_markup_mode(const char *mode)
} }
} }
static int ini_get_urgency(char *section, char *key, int def) static int ini_get_urgency(const char *section, const char *key, const int def)
{ {
int ret = def; int ret = def;
char *urg = ini_get_string(section, key, ""); char *urg = ini_get_string(section, key, "");
@ -603,7 +603,7 @@ void load_settings(char *cmdline_config_path)
rules = g_slist_insert(rules, &(default_rules[i]), -1); rules = g_slist_insert(rules, &(default_rules[i]), -1);
} }
char *cur_section = NULL; const char *cur_section = NULL;
for (;;) { for (;;) {
cur_section = next_section(cur_section); cur_section = next_section(cur_section);
if (!cur_section) if (!cur_section)

View File

@ -7,7 +7,7 @@
TEST test_next_section(void) TEST test_next_section(void)
{ {
char *section = NULL; const char *section = NULL;
ASSERT_STR_EQ("bool", (section = next_section(section))); ASSERT_STR_EQ("bool", (section = next_section(section)));
ASSERT_STR_EQ("string", (section = next_section(section))); ASSERT_STR_EQ("string", (section = next_section(section)));
ASSERT_STR_EQ("path", (section = next_section(section))); ASSERT_STR_EQ("path", (section = next_section(section)));
@ -173,7 +173,7 @@ TEST test_cmdline_create_usage(void)
cmdline_get_int("-msgint/-mi", 0, "An int to test usage creation"); cmdline_get_int("-msgint/-mi", 0, "An int to test usage creation");
cmdline_get_double("-msgdouble/-md", 0, "A double to test usage creation"); cmdline_get_double("-msgdouble/-md", 0, "A double to test usage creation");
cmdline_get_bool("-msgbool/-mb", false, "A bool to test usage creation"); cmdline_get_bool("-msgbool/-mb", false, "A bool to test usage creation");
char *usage = cmdline_create_usage(); const char *usage = cmdline_create_usage();
ASSERT(strstr(usage, "-msgstring/-ms")); ASSERT(strstr(usage, "-msgstring/-ms"));
ASSERT(strstr(usage, "A string to test usage creation")); ASSERT(strstr(usage, "A string to test usage creation"));
ASSERT(strstr(usage, "-msgint/-mi")); ASSERT(strstr(usage, "-msgint/-mi"));
@ -182,7 +182,6 @@ TEST test_cmdline_create_usage(void)
ASSERT(strstr(usage, "A double to test usage creation")); ASSERT(strstr(usage, "A double to test usage creation"));
ASSERT(strstr(usage, "-msgbool/-mb")); ASSERT(strstr(usage, "-msgbool/-mb"));
ASSERT(strstr(usage, "A bool to test usage creation")); ASSERT(strstr(usage, "A bool to test usage creation"));
free(usage);
PASS(); PASS();
} }