| 286 | } |
| 287 | |
| 288 | void Map_ToString |
| 289 | ( |
| 290 | SIValue map, // map to get string representation from |
| 291 | char **buf, // buffer to populate |
| 292 | size_t *bufferLen, // size of buffer |
| 293 | size_t *bytesWritten // length of string |
| 294 | ) { |
| 295 | ASSERT(SI_TYPE(map) & T_MAP); |
| 296 | ASSERT(buf != NULL); |
| 297 | ASSERT(bufferLen != NULL); |
| 298 | ASSERT(bytesWritten != NULL); |
| 299 | |
| 300 | // resize buffer if buffer length is less than 64 |
| 301 | if(*bufferLen - *bytesWritten < 64) str_ExtendBuffer(buf, bufferLen, 64); |
| 302 | |
| 303 | uint key_count = Map_KeyCount(map); |
| 304 | |
| 305 | // "{" marks the beginning of a map |
| 306 | *bytesWritten += snprintf(*buf + *bytesWritten, *bufferLen, "{"); |
| 307 | |
| 308 | for(uint i = 0; i < key_count; i ++) { |
| 309 | Pair p = map.map[i]; |
| 310 | // write the next key/value pair |
| 311 | SIValue_ToString(p.key, buf, bufferLen, bytesWritten); |
| 312 | if(*bufferLen - *bytesWritten < 64) str_ExtendBuffer(buf, bufferLen, 64); |
| 313 | *bytesWritten += snprintf(*buf + *bytesWritten, *bufferLen, ": "); |
| 314 | SIValue_ToString(p.val, buf, bufferLen, bytesWritten); |
| 315 | // if this is not the last element, add ", " |
| 316 | if(i != key_count - 1) { |
| 317 | if(*bufferLen - *bytesWritten < 64) str_ExtendBuffer(buf, bufferLen, 64); |
| 318 | *bytesWritten += snprintf(*buf + *bytesWritten, *bufferLen, ", "); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // make sure there's enough space for "}" |
| 323 | if(*bufferLen - *bytesWritten < 2) str_ExtendBuffer(buf, bufferLen, 2); |
| 324 | |
| 325 | // "}" marks the end of a map |
| 326 | *bytesWritten += snprintf(*buf + *bytesWritten, *bufferLen, "}"); |
| 327 | } |
| 328 | |
| 329 | // free map |
| 330 | void Map_Free |