59 } else { |
60 } else { |
60 outbuf[outpos++] = inbuf[inpos++]; |
61 outbuf[outpos++] = inbuf[inpos++]; |
61 } |
62 } |
62 } |
63 } |
63 outbuf[outpos] = 0; |
64 outbuf[outpos] = 0; |
64 return outbuf; |
65 char *shrunk = realloc(outbuf, outpos+1); |
|
66 return shrunk ? shrunk : outbuf; |
65 } |
67 } |
66 |
68 |
67 char *inihelper_createDictKey(const char *sectionName, const char *keyName) { |
69 char *inihelper_createDictKey(const char *sectionName, const char *keyName) { |
68 if(!sectionName || !keyName) { |
70 if(!sectionName || !keyName) { |
|
71 flib_log_e("null parameter in inihelper_createDictKey"); |
69 return NULL; |
72 return NULL; |
70 } |
73 } |
71 return flib_asprintf("%s:%s", sectionName, keyName); |
74 return flib_asprintf("%s:%s", sectionName, keyName); |
72 } |
75 } |
73 |
76 |
74 char *inihelper_getstring(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) { |
77 char *inihelper_getstring(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) { |
75 if(!inifile || !sectionName || !keyName) { |
78 if(!inifile || !sectionName || !keyName) { |
|
79 flib_log_e("null parameter in inihelper_getstring"); |
76 *error = true; |
80 *error = true; |
77 return NULL; |
81 return NULL; |
78 } |
82 } |
79 char *extendedkey = inihelper_createDictKey(sectionName, keyName); |
83 char *extendedkey = inihelper_createDictKey(sectionName, keyName); |
80 if(!extendedkey) { |
84 if(!extendedkey) { |
100 return 0; |
104 return 0; |
101 } else { |
105 } else { |
102 errno = 0; |
106 errno = 0; |
103 long val = strtol(value, NULL, 10); |
107 long val = strtol(value, NULL, 10); |
104 if(errno!=0) { |
108 if(errno!=0) { |
|
109 flib_log_w("Cannot parse ini setting %s/%s = \"%s\" as integer.", sectionName, keyName, value); |
105 *error = true; |
110 *error = true; |
106 return 0; |
111 return 0; |
107 } |
112 } |
108 if(val<INT_MIN || val>INT_MAX) { |
113 if(val<INT_MIN || val>INT_MAX) { |
|
114 flib_log_w("ini setting %s/%s = \"%s\" is too large or too small.", sectionName, keyName, value); |
109 *error = true; |
115 *error = true; |
110 return 0; |
116 return 0; |
111 } |
117 } |
112 return (int)val; |
118 return (int)val; |
113 } |
119 } |
119 return false; |
125 return false; |
120 } else { |
126 } else { |
121 bool trueval = strchr("1tTyY", value[0]); |
127 bool trueval = strchr("1tTyY", value[0]); |
122 bool falseval = strchr("0fFnN", value[0]); |
128 bool falseval = strchr("0fFnN", value[0]); |
123 if(!trueval && !falseval) { |
129 if(!trueval && !falseval) { |
|
130 flib_log_w("ini setting %s/%s = \"%s\" is not a recognized truth value.", sectionName, keyName, value); |
124 *error = true; |
131 *error = true; |
125 return false; |
132 return false; |
126 } else { |
133 } else { |
127 return trueval; |
134 return trueval; |
128 } |
135 } |
130 } |
137 } |
131 |
138 |
132 int inihelper_setstr(dictionary *dict, const char *sectionName, const char *keyName, const char *value) { |
139 int inihelper_setstr(dictionary *dict, const char *sectionName, const char *keyName, const char *value) { |
133 int result = -1; |
140 int result = -1; |
134 if(!dict || !sectionName || !keyName || !value) { |
141 if(!dict || !sectionName || !keyName || !value) { |
135 flib_log_e("inihelper_setstr called with bad parameters"); |
142 flib_log_e("null parameter in inihelper_setstr"); |
136 } else { |
143 } else { |
137 char *extendedkey = inihelper_createDictKey(sectionName, keyName); |
144 char *extendedkey = inihelper_createDictKey(sectionName, keyName); |
138 if(extendedkey) { |
145 if(extendedkey) { |
139 result = iniparser_set(dict, extendedkey, value); |
146 result = iniparser_set(dict, extendedkey, value); |
140 free(extendedkey); |
|
141 } |
147 } |
|
148 free(extendedkey); |
142 } |
149 } |
143 return result; |
150 return result; |
144 } |
151 } |
145 |
152 |
146 int inihelper_setint(dictionary *dict, const char *sectionName, const char *keyName, int value) { |
153 int inihelper_setint(dictionary *dict, const char *sectionName, const char *keyName, int value) { |
147 int result = -1; |
154 int result = -1; |
148 if(!dict || !sectionName || !keyName) { |
155 if(!dict || !sectionName || !keyName) { |
149 flib_log_e("inihelper_setint called with bad parameters"); |
156 flib_log_e("null parameter in inihelper_setint"); |
150 } else { |
157 } else { |
151 char *strvalue = flib_asprintf("%i", value); |
158 char *strvalue = flib_asprintf("%i", value); |
152 if(strvalue) { |
159 if(strvalue) { |
153 result = inihelper_setstr(dict, sectionName, keyName, strvalue); |
160 result = inihelper_setstr(dict, sectionName, keyName, strvalue); |
154 free(strvalue); |
161 free(strvalue); |
158 } |
165 } |
159 |
166 |
160 int inihelper_setbool(dictionary *dict, const char *sectionName, const char *keyName, bool value) { |
167 int inihelper_setbool(dictionary *dict, const char *sectionName, const char *keyName, bool value) { |
161 int result = -1; |
168 int result = -1; |
162 if(!dict || !sectionName || !keyName) { |
169 if(!dict || !sectionName || !keyName) { |
163 flib_log_e("inihelper_setint called with bad parameters"); |
170 flib_log_e("null parameter in inihelper_setbool"); |
164 } else { |
171 } else { |
165 result = inihelper_setstr(dict, sectionName, keyName, value ? "true" : "false"); |
172 result = inihelper_setstr(dict, sectionName, keyName, value ? "true" : "false"); |
166 } |
173 } |
167 return result; |
174 return result; |
168 } |
175 } |