| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/encode/inifiles.h" | ||
| 30 | #include "gate/comparers.h" | ||
| 31 | #include "gate/results.h" | ||
| 32 | #include "gate/debugging.h" | ||
| 33 | |||
| 34 | 71 | static gate_flatmap_iterator_t gate_inifile_store_use_section(gate_inifile_store_t* store, gate_string_t const* section) | |
| 35 | { | ||
| 36 | 71 | gate_flatmap_iterator_t iter = gate_flatmap_get(&store->sections, section); | |
| 37 | 71 | gate_flatmap_t content = GATE_INIT_EMPTY; | |
| 38 | |||
| 39 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 2 taken 43 times.
|
71 | if (!gate_flatmap_iterator_valid(&store->sections, iter)) |
| 40 | { | ||
| 41 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | if (NULL != gate_flatmap_create(&content, &gate_compare_string, |
| 42 | sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor, | ||
| 43 | sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor)) | ||
| 44 | { | ||
| 45 | 28 | iter = gate_flatmap_add(&store->sections, section, &content); | |
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!iter) |
| 47 | { | ||
| 48 | ✗ | gate_flatmap_destroy(&content); | |
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | 71 | return iter; | |
| 53 | } | ||
| 54 | |||
| 55 | 14 | gate_result_t gate_inifile_store_create(gate_inifile_store_t* store) | |
| 56 | { | ||
| 57 | 14 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 58 | gate_flatmap_iterator_t global_section_entry; | ||
| 59 | static gate_string_t const empty_string = GATE_STRING_INIT_EMPTY; | ||
| 60 | |||
| 61 | do | ||
| 62 | { | ||
| 63 | 14 | gate_mem_clear(store, sizeof(gate_inifile_store_t)); | |
| 64 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
|
14 | if (NULL == gate_flatmap_create(&store->sections, &gate_compare_string, |
| 65 | sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor, | ||
| 66 | sizeof(gate_flatmap_t), &gate_flatmap_copy_constructor, &gate_flatmap_destructor)) | ||
| 67 | { | ||
| 68 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 69 | ✗ | break; | |
| 70 | } | ||
| 71 | |||
| 72 | 14 | global_section_entry = gate_inifile_store_use_section(store, &empty_string); | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (NULL == global_section_entry) |
| 74 | { | ||
| 75 | ✗ | gate_flatmap_destroy(&store->sections); | |
| 76 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 77 | ✗ | break; | |
| 78 | } | ||
| 79 | |||
| 80 | 14 | ret = GATE_RESULT_OK; | |
| 81 | } while (0); | ||
| 82 | |||
| 83 | 14 | return ret; | |
| 84 | } | ||
| 85 | 2 | gate_result_t gate_inifile_store_copy(gate_inifile_store_t* store, gate_inifile_store_t const* source) | |
| 86 | { | ||
| 87 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (NULL == gate_flatmap_copy(&store->sections, &source->sections)) |
| 88 | { | ||
| 89 | ✗ | return GATE_RESULT_OUTOFMEMORY; | |
| 90 | } | ||
| 91 | 2 | return GATE_RESULT_OK; | |
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | 16 | gate_result_t gate_inifile_store_destroy(gate_inifile_store_t* store) | |
| 96 | { | ||
| 97 | 16 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 98 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | if (store) |
| 99 | { | ||
| 100 | 16 | gate_flatmap_destroy(&store->sections); | |
| 101 | 16 | ret = GATE_RESULT_OK; | |
| 102 | } | ||
| 103 | 16 | return ret; | |
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | 13 | gate_result_t gate_inifile_store_add_section(gate_inifile_store_t* store, gate_string_t const* section) | |
| 108 | { | ||
| 109 | 13 | gate_flatmap_iterator_t section_entry = gate_inifile_store_use_section(store, section); | |
| 110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (section_entry == NULL) |
| 111 | { | ||
| 112 | ✗ | return GATE_RESULT_OUTOFMEMORY; | |
| 113 | } | ||
| 114 | else | ||
| 115 | { | ||
| 116 | 13 | return GATE_RESULT_OK; | |
| 117 | } | ||
| 118 | } | ||
| 119 | 3 | gate_result_t gate_inifile_store_remove_section(gate_inifile_store_t* store, gate_string_t const* section) | |
| 120 | { | ||
| 121 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if (gate_flatmap_remove(&store->sections, section)) |
| 122 | { | ||
| 123 | 2 | return GATE_RESULT_OK; | |
| 124 | } | ||
| 125 | else | ||
| 126 | { | ||
| 127 | 1 | return GATE_RESULT_OK_UNCHANGED; | |
| 128 | } | ||
| 129 | } | ||
| 130 | 44 | gate_result_t gate_inifile_store_set(gate_inifile_store_t* store, gate_string_t const* section, | |
| 131 | gate_string_t const* key, gate_string_t const* value) | ||
| 132 | { | ||
| 133 | 44 | gate_result_t ret = GATE_RESULT_OK; | |
| 134 | |||
| 135 | do | ||
| 136 | { | ||
| 137 | gate_flatmap_iterator_t section_entry; | ||
| 138 | gate_flatmap_t* entry_map; | ||
| 139 | |||
| 140 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
|
44 | if (gate_string_is_empty(key)) |
| 141 | { | ||
| 142 | /* a key MUST NOT be empty */ | ||
| 143 | ✗ | ret = GATE_RESULT_INVALIDARG; | |
| 144 | ✗ | break; | |
| 145 | } | ||
| 146 | 44 | section_entry = gate_inifile_store_use_section(store, section); | |
| 147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (NULL == section_entry) |
| 148 | { | ||
| 149 | /* failed to access or create section */ | ||
| 150 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 151 | ✗ | break; | |
| 152 | } | ||
| 153 | 44 | entry_map = (gate_flatmap_t*)gate_flatmap_iterator_value(section_entry); | |
| 154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (NULL == entry_map) |
| 155 | { | ||
| 156 | /* section entry is invalid */ | ||
| 157 | ✗ | ret = GATE_RESULT_INVALIDCONTENT; | |
| 158 | ✗ | break; | |
| 159 | } | ||
| 160 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
|
44 | if (NULL == gate_flatmap_add(entry_map, key, value)) |
| 161 | { | ||
| 162 | /* failed to add */ | ||
| 163 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 164 | ✗ | break; | |
| 165 | } | ||
| 166 | /* new value successfully attached */ | ||
| 167 | 44 | ret = GATE_RESULT_OK; | |
| 168 | } while (0); | ||
| 169 | |||
| 170 | 44 | return ret; | |
| 171 | } | ||
| 172 | 8 | gate_result_t gate_inifile_store_get(gate_inifile_store_t const* store, gate_string_t const* section, | |
| 173 | gate_string_t const* key, gate_string_t* return_value) | ||
| 174 | { | ||
| 175 | 8 | gate_result_t ret = GATE_RESULT_OK; | |
| 176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | GATE_DEBUG_ASSERT(store != NULL); |
| 177 | |||
| 178 | do | ||
| 179 | { | ||
| 180 | 8 | gate_flatmap_iterator_t section_entry = gate_flatmap_get(&store->sections, section); | |
| 181 | gate_flatmap_t* entry_map; | ||
| 182 | gate_flatmap_iterator_t entry_iter; | ||
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (NULL == section_entry) |
| 184 | { | ||
| 185 | /* failed to access or create section */ | ||
| 186 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 187 | ✗ | break; | |
| 188 | } | ||
| 189 | 8 | entry_map = (gate_flatmap_t*)gate_flatmap_iterator_value(section_entry); | |
| 190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (NULL == entry_map) |
| 191 | { | ||
| 192 | /* section entry is invalid */ | ||
| 193 | ✗ | ret = GATE_RESULT_INVALIDCONTENT; | |
| 194 | ✗ | break; | |
| 195 | } | ||
| 196 | 8 | entry_iter = gate_flatmap_get(entry_map, key); | |
| 197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (NULL == entry_iter) |
| 198 | { | ||
| 199 | /* key does not exist in section */ | ||
| 200 | ✗ | ret = GATE_RESULT_NOMATCH; | |
| 201 | ✗ | break; | |
| 202 | } | ||
| 203 | |||
| 204 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (NULL != return_value) |
| 205 | { | ||
| 206 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
8 | if (NULL == gate_string_duplicate(return_value, (gate_string_t const*)gate_flatmap_iterator_value(entry_iter))) |
| 207 | { | ||
| 208 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 209 | ✗ | break; | |
| 210 | } | ||
| 211 | } | ||
| 212 | /* value successfully retrieved */ | ||
| 213 | 8 | ret = GATE_RESULT_OK; | |
| 214 | } while (0); | ||
| 215 | |||
| 216 | 8 | return ret; | |
| 217 | } | ||
| 218 | |||
| 219 | 9 | gate_result_t gate_inifile_store_remove(gate_inifile_store_t* store, gate_string_t const* section, gate_string_t const* key) | |
| 220 | { | ||
| 221 | 9 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | GATE_DEBUG_ASSERT(store != NULL); |
| 223 | |||
| 224 | do | ||
| 225 | { | ||
| 226 | 9 | gate_flatmap_iterator_t section_entry = gate_flatmap_get(&store->sections, section); | |
| 227 | gate_flatmap_t* entry_map; | ||
| 228 | |||
| 229 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
|
9 | if (NULL == section_entry) |
| 230 | { | ||
| 231 | /* failed to access or create section */ | ||
| 232 | 1 | ret = GATE_RESULT_NOMATCH; | |
| 233 | 1 | break; | |
| 234 | } | ||
| 235 | 8 | entry_map = (gate_flatmap_t*)gate_flatmap_iterator_value(section_entry); | |
| 236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (NULL == entry_map) |
| 237 | { | ||
| 238 | /* section entry is invalid */ | ||
| 239 | ✗ | ret = GATE_RESULT_INVALIDCONTENT; | |
| 240 | ✗ | break; | |
| 241 | } | ||
| 242 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if (!gate_flatmap_remove(entry_map, key)) |
| 243 | { | ||
| 244 | /* entry key does not exist */ | ||
| 245 | ✗ | ret = GATE_RESULT_NOMATCH; | |
| 246 | ✗ | break; | |
| 247 | } | ||
| 248 | |||
| 249 | /* entry successfully removed */ | ||
| 250 | 8 | ret = GATE_RESULT_OK; | |
| 251 | } while (0); | ||
| 252 | |||
| 253 | 9 | return ret; | |
| 254 | } | ||
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | 13 | gate_result_t gate_inifile_store_load(gate_inifile_store_t* inistore, gate_stream_t* input) | |
| 261 | { | ||
| 262 | 13 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 263 | 13 | gate_string_t current_section = GATE_STRING_INIT_EMPTY; | |
| 264 | char buffer[8192]; | ||
| 265 | 13 | gate_uint64_t comment_counter = 0; | |
| 266 | 13 | char comment_id[128] = ";;;;;;;;;;;;;;;;"; | |
| 267 | gate_size_t linelen; | ||
| 268 | 13 | gate_string_t comment_key = GATE_STRING_INIT_EMPTY; | |
| 269 | 13 | gate_string_t current_line = GATE_STRING_INIT_EMPTY; | |
| 270 | 13 | gate_string_t current_key = GATE_STRING_INIT_EMPTY; | |
| 271 | 13 | gate_string_t current_value = GATE_STRING_INIT_EMPTY; | |
| 272 | gate_size_t pos; | ||
| 273 | gate_char8_t chr; | ||
| 274 | gate_char8_t chr_last; | ||
| 275 | |||
| 276 | do | ||
| 277 | { | ||
| 278 | 13 | ret = GATE_RESULT_OK; | |
| 279 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | while (GATE_SUCCEEDED(ret)) |
| 280 | { | ||
| 281 | 80 | ret = gate_stream_read_line(input, buffer, sizeof(buffer), &linelen); | |
| 282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | GATE_BREAK_IF_FAILED(ret); |
| 283 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 67 times.
|
80 | if (linelen == 0) |
| 284 | { | ||
| 285 | /* end of stream reached */ | ||
| 286 | 13 | break; | |
| 287 | } | ||
| 288 | |||
| 289 | 67 | gate_string_create_static_len(¤t_line, buffer, linelen); | |
| 290 | 67 | gate_string_rtrim(¤t_line, ¤t_line); | |
| 291 | 67 | gate_string_ltrim(¤t_line, ¤t_line); | |
| 292 | |||
| 293 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 47 times.
|
67 | if (gate_string_is_empty(¤t_line)) |
| 294 | { | ||
| 295 | 20 | continue; | |
| 296 | } | ||
| 297 | |||
| 298 | 47 | chr = gate_string_char_at(¤t_line, 0); | |
| 299 |
2/3✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
|
47 | switch (chr) |
| 300 | { | ||
| 301 | 11 | case '[': | |
| 302 | { | ||
| 303 | /* new section */ | ||
| 304 | 11 | chr_last = gate_string_char_at(¤t_line, gate_string_length(¤t_line) - 1); | |
| 305 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (chr_last != ']') |
| 306 | { | ||
| 307 | ✗ | ret = GATE_RESULT_INVALIDINPUT; | |
| 308 | ✗ | break; | |
| 309 | } | ||
| 310 | 11 | gate_string_release(¤t_section); | |
| 311 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
|
11 | if (NULL == gate_string_create(¤t_section, gate_string_ptr(¤t_line, 1), gate_string_length(¤t_line) - 2)) |
| 312 | { | ||
| 313 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 314 | ✗ | break; | |
| 315 | } | ||
| 316 | 11 | ret = gate_inifile_store_add_section(inistore, ¤t_section); | |
| 317 | 11 | break; | |
| 318 | } | ||
| 319 | ✗ | case '#': | |
| 320 | case ';': | ||
| 321 | { | ||
| 322 | /* comment line */ | ||
| 323 | ✗ | gate_str_print_uint64(&comment_id[1], sizeof(comment_id) - 1, ++comment_counter); | |
| 324 | ✗ | gate_string_create_static(&comment_key, comment_id); | |
| 325 | ✗ | gate_inifile_store_set(inistore, ¤t_section, &comment_key, ¤t_line); | |
| 326 | ✗ | gate_string_release(&comment_key); | |
| 327 | ✗ | break; | |
| 328 | } | ||
| 329 | 36 | default: | |
| 330 | { | ||
| 331 | /* key-value entry */ | ||
| 332 | 36 | pos = gate_string_char_pos(¤t_line, '=', 0); | |
| 333 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (pos == GATE_STR_NPOS) |
| 334 | { | ||
| 335 | ✗ | gate_string_duplicate(¤t_key, ¤t_line); | |
| 336 | ✗ | gate_string_create_empty(¤t_value); | |
| 337 | } | ||
| 338 | else | ||
| 339 | { | ||
| 340 | 36 | gate_string_substr(¤t_key, ¤t_line, 0, pos); | |
| 341 | 36 | gate_string_rtrim(¤t_key, ¤t_key); | |
| 342 | 36 | gate_string_substr(¤t_value, ¤t_line, pos + 1, GATE_STR_NPOS); | |
| 343 | 36 | gate_string_ltrim(¤t_value, ¤t_value); | |
| 344 | } | ||
| 345 | 36 | ret = gate_inifile_store_set(inistore, ¤t_section, ¤t_key, ¤t_value); | |
| 346 | 36 | gate_string_release(¤t_value); | |
| 347 | 36 | gate_string_release(¤t_key); | |
| 348 | 36 | break; | |
| 349 | } | ||
| 350 | } | ||
| 351 | 47 | gate_string_release(¤t_line); | |
| 352 | } | ||
| 353 | |||
| 354 | } while (0); | ||
| 355 | |||
| 356 | 13 | gate_string_release(¤t_section); | |
| 357 | |||
| 358 | 13 | return ret; | |
| 359 | } | ||
| 360 | |||
| 361 | 9 | gate_result_t gate_inifile_store_save(gate_inifile_store_t const* inistore, gate_stream_t* output) | |
| 362 | { | ||
| 363 | 9 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 364 | gate_flatmap_iterator_t section_iter; | ||
| 365 | gate_flatmap_iterator_t entry_iter; | ||
| 366 | gate_string_t const* ptr_section_name; | ||
| 367 | gate_flatmap_t const* ptr_section_content; | ||
| 368 | gate_string_t const* ptr_key; | ||
| 369 | gate_string_t const* ptr_value; | ||
| 370 | gate_size_t entry_count; | ||
| 371 | |||
| 372 | do | ||
| 373 | { | ||
| 374 | 9 | section_iter = gate_flatmap_first(&inistore->sections); | |
| 375 |
2/2✓ Branch 1 taken 17 times.
✓ Branch 2 taken 9 times.
|
26 | while (gate_flatmap_iterator_valid(&inistore->sections, section_iter)) |
| 376 | { | ||
| 377 | 17 | ptr_section_name = (gate_string_t const*)gate_flatmap_iterator_key(section_iter); | |
| 378 | 17 | ptr_section_content = (gate_flatmap_t const*)gate_flatmap_iterator_value(section_iter); | |
| 379 |
2/4✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
17 | if (ptr_section_name && ptr_section_content) |
| 380 | { | ||
| 381 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 9 times.
|
17 | if (!gate_string_is_empty(ptr_section_name)) |
| 382 | { | ||
| 383 | 8 | gate_stream_print(output, | |
| 384 | GATE_PRINT_CHAR, '[', | ||
| 385 | GATE_PRINT_STRING, ptr_section_name, | ||
| 386 | GATE_PRINT_CHAR, ']', | ||
| 387 | GATE_PRINT_NEWLINE, | ||
| 388 | GATE_PRINT_END); | ||
| 389 | } | ||
| 390 | 17 | entry_count = gate_flatmap_count(ptr_section_content); | |
| 391 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5 times.
|
17 | if (entry_count > 0) |
| 392 | { | ||
| 393 | 12 | entry_iter = gate_flatmap_first(ptr_section_content); | |
| 394 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 12 times.
|
32 | while (gate_flatmap_iterator_valid(ptr_section_content, entry_iter)) |
| 395 | { | ||
| 396 | 20 | ptr_key = (gate_string_t const*)gate_flatmap_iterator_key(entry_iter); | |
| 397 | 20 | ptr_value = (gate_string_t const*)gate_flatmap_iterator_value(entry_iter); | |
| 398 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
|
20 | if (gate_string_starts_with_char(ptr_key, ';')) |
| 399 | { | ||
| 400 | ✗ | gate_stream_print(output, | |
| 401 | GATE_PRINT_STRING, ptr_value, | ||
| 402 | GATE_PRINT_NEWLINE, | ||
| 403 | GATE_PRINT_END); | ||
| 404 | } | ||
| 405 | else | ||
| 406 | { | ||
| 407 | 20 | gate_stream_print(output, | |
| 408 | GATE_PRINT_STRING, ptr_key, | ||
| 409 | GATE_PRINT_CSTR, " = ", | ||
| 410 | GATE_PRINT_STRING, ptr_value, | ||
| 411 | GATE_PRINT_NEWLINE, | ||
| 412 | GATE_PRINT_END); | ||
| 413 | } | ||
| 414 | 20 | entry_iter = gate_flatmap_iterator_next(entry_iter); | |
| 415 | } | ||
| 416 | 12 | gate_stream_print(output, | |
| 417 | GATE_PRINT_NEWLINE, | ||
| 418 | GATE_PRINT_END); | ||
| 419 | } | ||
| 420 | } | ||
| 421 | 17 | section_iter = gate_flatmap_iterator_next(section_iter); | |
| 422 | } | ||
| 423 | 9 | ret = GATE_RESULT_OK; | |
| 424 | } while (0); | ||
| 425 | |||
| 426 | 9 | return ret; | |
| 427 | } | ||
| 428 | |||
| 429 | ✗ | static gate_result_t gate_inifile_store_import_tree(gate_inifile_store_t* inistore, gate_property_t const* property_tree, gate_string_t const* section) | |
| 430 | { | ||
| 431 | ✗ | gate_result_t ret = GATE_RESULT_FAILED; | |
| 432 | gate_size_t count, ndx; | ||
| 433 | ✗ | gate_array_t names = GATE_INIT_EMPTY; | |
| 434 | gate_string_t const* ptr_name; | ||
| 435 | gate_property_t const* ptr_prop; | ||
| 436 | gate_property_typeid_t prop_type; | ||
| 437 | gate_string_t prop_value; | ||
| 438 | gate_strbuilder_t builder; | ||
| 439 | gate_string_t sub_section; | ||
| 440 | |||
| 441 | do | ||
| 442 | { | ||
| 443 | ✗ | if (NULL == gate_property_member_names(property_tree, &names)) | |
| 444 | { | ||
| 445 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 446 | ✗ | break; | |
| 447 | } | ||
| 448 | ✗ | count = gate_array_length(&names); | |
| 449 | ✗ | ret = GATE_RESULT_OK; | |
| 450 | ✗ | for (ndx = 0; GATE_SUCCEEDED(ret) && (ndx != count); ++ndx) | |
| 451 | { | ||
| 452 | ✗ | ptr_name = (gate_string_t const*)gate_array_get(&names, ndx); | |
| 453 | ✗ | if (!ptr_name) | |
| 454 | { | ||
| 455 | ✗ | continue; | |
| 456 | } | ||
| 457 | ✗ | ptr_prop = gate_property_member_get(property_tree, ptr_name); | |
| 458 | ✗ | if (!ptr_prop) | |
| 459 | { | ||
| 460 | ✗ | continue; | |
| 461 | } | ||
| 462 | ✗ | prop_type = gate_property_get_type(ptr_prop); | |
| 463 | ✗ | switch (prop_type) | |
| 464 | { | ||
| 465 | ✗ | case GATE_PROPERTY_TYPE_BOOL: | |
| 466 | case GATE_PROPERTY_TYPE_INT: | ||
| 467 | case GATE_PROPERTY_TYPE_REAL: | ||
| 468 | case GATE_PROPERTY_TYPE_STRING: | ||
| 469 | { | ||
| 470 | ✗ | ret = gate_property_get_string(ptr_prop, &prop_value); | |
| 471 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 472 | ✗ | ret = gate_inifile_store_set(inistore, section, ptr_name, &prop_value); | |
| 473 | ✗ | gate_string_release(&prop_value); | |
| 474 | ✗ | break; | |
| 475 | } | ||
| 476 | ✗ | case GATE_PROPERTY_TYPE_ARRAY: | |
| 477 | { | ||
| 478 | ✗ | break; | |
| 479 | } | ||
| 480 | ✗ | case GATE_PROPERTY_TYPE_OBJECT: | |
| 481 | { | ||
| 482 | ✗ | gate_strbuilder_create(&builder, 0); | |
| 483 | ✗ | gate_strbuilder_append_string(&builder, section); | |
| 484 | ✗ | if (gate_strbuilder_length(&builder) != 0) | |
| 485 | { | ||
| 486 | ✗ | gate_strbuilder_append_cstr(&builder, "/"); | |
| 487 | } | ||
| 488 | ✗ | gate_strbuilder_append_string(&builder, ptr_name); | |
| 489 | ✗ | if (NULL == gate_strbuilder_to_string(&builder, &sub_section)) | |
| 490 | { | ||
| 491 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 492 | } | ||
| 493 | ✗ | gate_strbuilder_release(&builder); | |
| 494 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 495 | ✗ | ret = gate_inifile_store_import_tree(inistore, ptr_prop, &sub_section); | |
| 496 | ✗ | gate_string_release(&sub_section); | |
| 497 | ✗ | break; | |
| 498 | } | ||
| 499 | } | ||
| 500 | ✗ | } | |
| 501 | |||
| 502 | } while (0); | ||
| 503 | |||
| 504 | ✗ | gate_array_release(&names); | |
| 505 | |||
| 506 | ✗ | return ret; | |
| 507 | } | ||
| 508 | |||
| 509 | ✗ | gate_result_t gate_inifile_store_import(gate_inifile_store_t* inistore, gate_property_t const* property_tree) | |
| 510 | { | ||
| 511 | ✗ | gate_string_t section = GATE_STRING_INIT_EMPTY; | |
| 512 | ✗ | if (gate_property_get_type(property_tree) != GATE_PROPERTY_TYPE_OBJECT) | |
| 513 | { | ||
| 514 | ✗ | return GATE_RESULT_INVALIDINPUT; | |
| 515 | } | ||
| 516 | else | ||
| 517 | { | ||
| 518 | ✗ | return gate_inifile_store_import_tree(inistore, property_tree, §ion); | |
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | ✗ | gate_result_t gate_inifile_store_export(gate_inifile_store_t* inistore, gate_property_t* property_tree) | |
| 523 | { | ||
| 524 | /* TODO */ | ||
| 525 | ✗ | return GATE_RESULT_NOTIMPLEMENTED; | |
| 526 | } | ||
| 527 | |||
| 528 | |||
| 529 | |||
| 530 | ✗ | gate_result_t gate_inifile_parse_string(gate_string_t const* source, gate_property_t* property_tree) | |
| 531 | { | ||
| 532 | ✗ | gate_memstream_impl_t mem_stream = GATE_INIT_EMPTY; | |
| 533 | ✗ | gate_memstream_create_static_unmanaged_readonly(&mem_stream, gate_string_ptr(source, 0), gate_string_length(source), 0); | |
| 534 | ✗ | return gate_inifile_parse((gate_stream_t*)&mem_stream, property_tree); | |
| 535 | } | ||
| 536 | ✗ | gate_result_t gate_inifile_build_string(gate_property_t const* property_tree, gate_strbuilder_t* dest_builder) | |
| 537 | { | ||
| 538 | gate_result_t ret; | ||
| 539 | ✗ | gate_stringstream_t* stream = gate_stringstream_create_builder(dest_builder); | |
| 540 | ✗ | if (NULL == stream) | |
| 541 | { | ||
| 542 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 543 | } | ||
| 544 | else | ||
| 545 | { | ||
| 546 | ✗ | ret = gate_inifile_build(property_tree, (gate_stream_t*)stream); | |
| 547 | ✗ | gate_object_release(stream); | |
| 548 | } | ||
| 549 | ✗ | return ret; | |
| 550 | } | ||
| 551 | |||
| 552 | ✗ | gate_result_t gate_inifile_parse(gate_stream_t* source, gate_property_t* property_tree) | |
| 553 | { | ||
| 554 | ✗ | gate_result_t ret = GATE_RESULT_FAILED; | |
| 555 | ✗ | gate_inifile_store_t store = GATE_INIT_EMPTY; | |
| 556 | do | ||
| 557 | { | ||
| 558 | ✗ | ret = gate_inifile_store_create(&store); | |
| 559 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 560 | |||
| 561 | ✗ | ret = gate_inifile_store_load(&store, source); | |
| 562 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 563 | |||
| 564 | ✗ | ret = gate_inifile_store_export(&store, property_tree); | |
| 565 | } while (0); | ||
| 566 | |||
| 567 | ✗ | gate_inifile_store_destroy(&store); | |
| 568 | |||
| 569 | ✗ | return ret; | |
| 570 | } | ||
| 571 | ✗ | gate_result_t gate_inifile_build(gate_property_t const* property_tree, gate_stream_t* output) | |
| 572 | { | ||
| 573 | ✗ | gate_result_t ret = GATE_RESULT_FAILED; | |
| 574 | ✗ | gate_inifile_store_t store = GATE_INIT_EMPTY; | |
| 575 | do | ||
| 576 | { | ||
| 577 | ✗ | ret = gate_inifile_store_create(&store); | |
| 578 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 579 | |||
| 580 | ✗ | ret = gate_inifile_store_import(&store, property_tree); | |
| 581 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 582 | |||
| 583 | ✗ | ret = gate_inifile_store_save(&store, output); | |
| 584 | } while (0); | ||
| 585 | |||
| 586 | ✗ | gate_inifile_store_destroy(&store); | |
| 587 | |||
| 588 | ✗ | return ret; | |
| 589 | } | ||
| 590 |