immersive
This commit is contained in:
25
cfg.h
25
cfg.h
@@ -99,6 +99,28 @@ bool string_compare(string* a, string* b) {
|
||||
return strncmp(a->str, b->str, a->len) == 0;
|
||||
}
|
||||
|
||||
ssize_t string_count_char(string* s, char c) {
|
||||
int i;
|
||||
ssize_t res = 0;
|
||||
for (i = 0; i < s->len; i++) if (s->str[i] == c) res++;
|
||||
return res;
|
||||
}
|
||||
|
||||
string string_quote(string* replace) {
|
||||
int i = 0;
|
||||
ssize_t quotes = string_count_char(replace, '"');
|
||||
string res = new_string_sized(replace->len+quotes);
|
||||
res.len = res.cap;
|
||||
|
||||
quotes = 0;
|
||||
for (i = 0; i < replace->len; i++) {
|
||||
if (replace->str[i] == '"') res.str[i+(quotes++)] = '\\';
|
||||
res.str[i+quotes] = replace->str[i];
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// FNV-1a
|
||||
size_t string_hash(string* s) {
|
||||
size_t hash = 2166136261;
|
||||
@@ -348,7 +370,8 @@ string config_value_str(config_value* c, unsigned int indent) {
|
||||
return res;
|
||||
case config_value_string:
|
||||
string_cappend(&res, "\"");
|
||||
string_append(&res, &c->s);
|
||||
sub = string_quote(&c->s);
|
||||
string_append(&res, &sub);
|
||||
string_cappend(&res, "\"");
|
||||
break;
|
||||
case config_value_number:
|
||||
|
@@ -13,7 +13,7 @@ int main() {
|
||||
config_add_list(&c, string_from_cstr("mylist"), &l);
|
||||
config sub = new_config();
|
||||
config_add_number(&sub, string_from_cstr("mysecondnum"), 23);
|
||||
config_add_string(&sub, string_from_cstr("mysecondstring"), string_from_cstr("inner"));
|
||||
config_add_string(&sub, string_from_cstr("mysecondstring"), string_from_cstr("\"inner\"\"quoted\""));
|
||||
config_add_section(&c, string_from_cstr("mysection"), &sub);
|
||||
|
||||
string s = config_str(&c, 0);
|
||||
|
Reference in New Issue
Block a user