15db71995Sopenharmony_ci/*
25db71995Sopenharmony_ci  Copyright (c) 2009 Dave Gamble
35db71995Sopenharmony_ci  Copyright (c) 2015-2021 The Khronos Group Inc.
45db71995Sopenharmony_ci  Copyright (c) 2015-2021 Valve Corporation
55db71995Sopenharmony_ci  Copyright (c) 2015-2021 LunarG, Inc.
65db71995Sopenharmony_ci
75db71995Sopenharmony_ci  Permission is hereby granted, free of charge, to any person obtaining a copy
85db71995Sopenharmony_ci  of this software and associated documentation files (the "Software"), to deal
95db71995Sopenharmony_ci  in the Software without restriction, including without limitation the rights
105db71995Sopenharmony_ci  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
115db71995Sopenharmony_ci  copies of the Software, and to permit persons to whom the Software is
125db71995Sopenharmony_ci  furnished to do so, subject to the following conditions:
135db71995Sopenharmony_ci
145db71995Sopenharmony_ci  The above copyright notice and this permission notice shall be included in
155db71995Sopenharmony_ci  all copies or substantial portions of the Software.
165db71995Sopenharmony_ci
175db71995Sopenharmony_ci  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
185db71995Sopenharmony_ci  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195db71995Sopenharmony_ci  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
205db71995Sopenharmony_ci  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
215db71995Sopenharmony_ci  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
225db71995Sopenharmony_ci  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
235db71995Sopenharmony_ci  THE SOFTWARE.
245db71995Sopenharmony_ci*/
255db71995Sopenharmony_ci
265db71995Sopenharmony_ci#pragma once
275db71995Sopenharmony_ci
285db71995Sopenharmony_ci#include <stdint.h>
295db71995Sopenharmony_ci
305db71995Sopenharmony_ci#include <vulkan/vulkan_core.h>
315db71995Sopenharmony_ci
325db71995Sopenharmony_ci/* cJSON Types: */
335db71995Sopenharmony_ci#define cJSON_False 0
345db71995Sopenharmony_ci#define cJSON_True 1
355db71995Sopenharmony_ci#define cJSON_NULL 2
365db71995Sopenharmony_ci#define cJSON_Number 3
375db71995Sopenharmony_ci#define cJSON_String 4
385db71995Sopenharmony_ci#define cJSON_Array 5
395db71995Sopenharmony_ci#define cJSON_Object 6
405db71995Sopenharmony_ci
415db71995Sopenharmony_ci#define cJSON_IsReference 256
425db71995Sopenharmony_ci#define cJSON_StringIsConst 512
435db71995Sopenharmony_ci
445db71995Sopenharmony_ci/* The cJSON structure: */
455db71995Sopenharmony_citypedef struct cJSON {
465db71995Sopenharmony_ci    struct cJSON *next, *prev; /* next/prev allow you to walk array/object
475db71995Sopenharmony_ci                                  chains. Alternatively, use
485db71995Sopenharmony_ci                                  GetArraySize/GetArrayItem/GetObjectItem */
495db71995Sopenharmony_ci    struct cJSON *child;       /* An array or object item will have a child pointer
505db71995Sopenharmony_ci                                  pointing to a chain of the items in the
515db71995Sopenharmony_ci                                  array/object. */
525db71995Sopenharmony_ci
535db71995Sopenharmony_ci    int type; /* The type of the item, as above. */
545db71995Sopenharmony_ci
555db71995Sopenharmony_ci    char *valuestring;  /* The item's string, if type==cJSON_String */
565db71995Sopenharmony_ci    int valueint;       /* The item's number, if type==cJSON_Number */
575db71995Sopenharmony_ci    double valuedouble; /* The item's number, if type==cJSON_Number */
585db71995Sopenharmony_ci
595db71995Sopenharmony_ci    char *string; /* The item's name string, if this item is the child of, or is
605db71995Sopenharmony_ci                     in the list of subitems of an object. */
615db71995Sopenharmony_ci    /* pointer to the allocation callbacks to use */
625db71995Sopenharmony_ci    VkAllocationCallbacks *pAllocator;
635db71995Sopenharmony_ci} cJSON;
645db71995Sopenharmony_ci
655db71995Sopenharmony_ci/* Render a cJSON entity to text for transfer/storage. Free the char* when
665db71995Sopenharmony_ci * finished. */
675db71995Sopenharmony_cichar *loader_cJSON_Print(cJSON *item);
685db71995Sopenharmony_ci/* Render a cJSON entity to text for transfer/storage without any formatting.
695db71995Sopenharmony_ci * Free the char* when finished. */
705db71995Sopenharmony_cichar *loader_cJSON_PrintUnformatted(cJSON *item);
715db71995Sopenharmony_ci/* Delete a cJSON entity and all subentities. */
725db71995Sopenharmony_civoid loader_cJSON_Delete(cJSON *c);
735db71995Sopenharmony_ci
745db71995Sopenharmony_ci/* Returns the number of items in an array (or object). */
755db71995Sopenharmony_ciint loader_cJSON_GetArraySize(cJSON *array);
765db71995Sopenharmony_ci/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful.
775db71995Sopenharmony_ci */
785db71995Sopenharmony_cicJSON *loader_cJSON_GetArrayItem(cJSON *array, int item);
795db71995Sopenharmony_ci/* Get item "string" from object. Case insensitive. */
805db71995Sopenharmony_cicJSON *loader_cJSON_GetObjectItem(cJSON *object, const char *string);
815db71995Sopenharmony_ci
825db71995Sopenharmony_ci/* When assigning an integer value, it needs to be propagated to valuedouble
835db71995Sopenharmony_ci * too. */
845db71995Sopenharmony_ci#define cJSON_SetIntValue(object, val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val))
855db71995Sopenharmony_ci#define cJSON_SetNumberValue(object, val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val))
865db71995Sopenharmony_ci
875db71995Sopenharmony_ci// Helper functions to using JSON
885db71995Sopenharmony_ci
895db71995Sopenharmony_cistruct loader_instance;
905db71995Sopenharmony_cistruct loader_string_list;
915db71995Sopenharmony_ci
925db71995Sopenharmony_ci// Read a JSON file into a buffer.
935db71995Sopenharmony_ci//
945db71995Sopenharmony_ci// @return -  A pointer to a cJSON object representing the JSON parse tree.
955db71995Sopenharmony_ci//            This returned buffer should be freed by caller.
965db71995Sopenharmony_ciVkResult loader_get_json(const struct loader_instance *inst, const char *filename, cJSON **json);
975db71995Sopenharmony_ci
985db71995Sopenharmony_ci// Given a cJSON object, find the string associated with the key and puts an pre-allocated string into out_string.
995db71995Sopenharmony_ci// Length is given by out_str_len, and this function truncates the string with a null terminator if it the provided space isn't
1005db71995Sopenharmony_ci// large enough.
1015db71995Sopenharmony_ciVkResult loader_parse_json_string_to_existing_str(const struct loader_instance *inst, cJSON *object, const char *key,
1025db71995Sopenharmony_ci                                                  size_t out_str_len, char *out_string);
1035db71995Sopenharmony_ci
1045db71995Sopenharmony_ci// Given a cJSON object, find the string associated with the key and puts an allocated string into out_string.
1055db71995Sopenharmony_ci// It is the callers responsibility to free out_string.
1065db71995Sopenharmony_ciVkResult loader_parse_json_string(cJSON *object, const char *key, char **out_string);
1075db71995Sopenharmony_ci
1085db71995Sopenharmony_ci// Given a cJSON object, find the array of strings associated with they key and writes the count into out_count and data into
1095db71995Sopenharmony_ci// out_array_of_strings. It is the callers responsibility to free out_array_of_strings.
1105db71995Sopenharmony_ciVkResult loader_parse_json_array_of_strings(const struct loader_instance *inst, cJSON *object, const char *key,
1115db71995Sopenharmony_ci                                            struct loader_string_list *string_list);
112