MCPcopy Create free account
hub / github.com/KhronosGroup/KTX-Software / printKVDataJSON

Function printKVDataJSON

lib/info.c:233–346  ·  view source on GitHub ↗

* @internal * @~English * @brief Prints a list of the keys & values found in a KTX2 file. * * @param [in] pKvd pointer to serialized key/value data. * @param [in] kvdLen length of the serialized key/value data. * @param [in] base_indent The number of indentations to include at the front of every line * @param [in] indent_width The number of spaces to add with

Source from the content-addressed store, hash-verified

231 * @param [in] minified Specifies whether the JSON output should be minified
232 */
233void
234printKVDataJSON(ktx_uint8_t* pKvd, ktx_uint32_t kvdLen, ktx_uint32_t base_indent, ktx_uint32_t indent_width, bool minified)
235{
236 const char* space = minified ? "" : " ";
237 const char* nl = minified ? "" : "\n";
238
239 KTX_error_code result;
240 ktxHashList kvDataHead = 0;
241
242 assert(pKvd != NULL && kvdLen > 0);
243
244 result = ktxHashList_Deserialize(&kvDataHead, kvdLen, pKvd);
245 if (result != KTX_SUCCESS) {
246 // Logging while printing JSON is not possible, we rely on the validation step to provide meaningful errors
247 // fprintf(stdout, "Failed to parse or not enough memory to build list of key/value pairs.\n");
248 return;
249 }
250
251 if (kvDataHead == NULL)
252 return;
253
254 int entryIndex = 0;
255 ktxHashListEntry* entry = kvDataHead;
256 bool firstPrint = true; // Marks if the first print did not occur yet (first print != first entry)
257 for (; entry != NULL && entryIndex < MAX_NUM_KVD_ENTRIES; entry = ktxHashList_Next(entry), ++entryIndex) {
258 char* key;
259 char* value;
260 ktx_uint32_t keyLen, valueLen;
261
262 ktxHashListEntry_GetKey(entry, &keyLen, &key);
263 ktxHashListEntry_GetValue(entry, &valueLen, (void**)&value);
264 // Keys must be NUL terminated.
265 if (!value) {
266 if (!isKnownKeyValue(key)) {
267 // Known keys are not be printed with null
268 if (!firstPrint)
269 fprintf(stdout, ",%s", nl);
270 firstPrint = false;
271 PRINT_INDENT(0, "\"%s\":%snull", key, space)
272 }
273 } else {
274 if (strcmp(key, "KTXglFormat") == 0) {
275 if (valueLen == 3 * sizeof(ktx_uint32_t)) {
276 if (!firstPrint)
277 fprintf(stdout, ",%s", nl);
278 firstPrint = false;
279 ktx_uint32_t glInternalformat = *(const ktx_uint32_t*) (value + 0);
280 ktx_uint32_t glFormat = *(const ktx_uint32_t*) (value + 4);
281 ktx_uint32_t glType = *(const ktx_uint32_t*) (value + 8);
282 PRINT_INDENT(0, "\"%s\":%s{%s", key, space, nl)
283 PRINT_INDENT(1, "\"glInternalformat\":%s%u,%s", space, glInternalformat, nl)
284 PRINT_INDENT(1, "\"glFormat\":%s%u,%s", space, glFormat, nl)
285 PRINT_INDENT(1, "\"glType\":%s%u%s", space, glType, nl)
286 PRINT_INDENT_NOARG(0, "}")
287 }
288 } else if (strcmp(key, "KTXanimData") == 0) {
289 if (valueLen == 3 * sizeof(ktx_uint32_t)) {
290 if (!firstPrint)

Callers 1

printKTX2Info2JSONFunction · 0.85

Calls 10

assertFunction · 0.85
ktxHashList_DeserializeFunction · 0.85
ktxHashList_NextFunction · 0.85
ktxHashListEntry_GetKeyFunction · 0.85
isKnownKeyValueFunction · 0.85
fprintfFunction · 0.85
isKnownKeyValueUINT32Function · 0.85
isKnownKeyValueStringFunction · 0.85
ktxHashList_DestructFunction · 0.85

Tested by

no test coverage detected