smallObjectsUsage.c (4402B)
1 // 2 3 #include "../release/libsheepyObject.h" 4 5 #define internal static 6 7 #include <stdlib.h> 8 #include <stdbool.h> 9 #include <string.h> 10 #include <stdio.h> 11 12 #ifndef unitTest 13 #endif 14 int MAIN(int ARGC, char** ARGV); 15 16 int argc; char **argv; 17 18 #ifndef unitTest 19 // Remove main when running the unit tests 20 #define MAIN main 21 #endif 22 int MAIN(int ARGC, char** ARGV) { 23 const char **list = NULL; 24 char *s = NULL; 25 26 argc = ARGC; argv = ARGV;;// Manipulate dictionaries and arrays 27 28 // Result: 29 30 // Array content: 31 // [255,"generic",1.000000e+01,null] 32 // Array element types: 33 // int, string, double, undefined 34 35 // [-1,"push any type"] 36 37 // gotArray content: 38 // [256,"GENERIC",1.000000e+01,-1,"push any type"] 39 40 // Dictionary content: 41 // {"s": "LIBSHEEPY","array": [256,"GENERIC",1.000000e+01,-1,"push any type"],"Dict2": {"undefinedValue": null}} 42 // Dictionary element types: 43 // {"s": "string","array": "array","Dict2": "dict"} 44 45 // Json content: 46 // --- 47 // s: LIBSHEEPY 48 // array: 49 // - 256 50 // - GENERIC 51 // - 1.000000e+01 52 // - -1 53 // - push any type 54 // Dict2: 55 // undefinedValue: null 56 // usefull: null 57 // libsheepy: c11 58 // bool: true 59 60 // Steps 61 // create objects 62 // fill array and dict 63 // set a dictionary in a dict 64 // delete an element in the dictionary Dict2 65 // retrieve and modify s 66 // retrieve and modify the array in dict 67 // delete last element 68 // append 69 70 // create objects 71 smallBoolt *cBool = allocSmallBool(true); 72 smallDictt *dict = allocSmallDict(); 73 smallDoublet *cDouble = allocSmallDouble(10); 74 smallIntt *cInt = allocSmallInt(255); 75 smallJsont *json = allocSmallJson(); 76 smallStringt *string = allocSmallString(strdup("libsheepy")); 77 smallArrayt *tommy = allocSmallArray(); 78 undefinedt *undef = allocUndefined(); 79 80 // fill array and dict 81 pushNFreeG(tommy, cBool); 82 pushG(tommy, "generic"); 83 pushNFreeG(tommy, cDouble); 84 pushG(tommy, NULL); 85 setNFreeG(tommy, 0, cInt); 86 87 printf("\nArray content:"); 88 printf("\n"); 89 putsG(tommy); 90 // Array content: 91 // [255,"generic",1.000000e+01,null] 92 printf("Array element types:"); 93 printf("\n"); 94 list = typeStringsG(tommy); 95 s = join((char**)list, ", "); 96 // smallArrays return list of static strings, use free instead of listFreeS 97 free(list); 98 logNFree(s); 99 // Array element types: 100 // int, string, double, undefined 101 102 setNFreeG(dict, "s", string); 103 setNFreeG(dict, "array", tommy); 104 105 // set a dictionary in a dict 106 createAllocateSmallDict(Dict2); 107 setNFreeG(Dict2, "undefinedValue", undef); 108 setG(Dict2, "pi", 3.14); 109 110 // delete an element in the dictionary Dict2 111 delG(Dict2, "pi", 0); 112 113 setNFreeG(dict, "Dict2", Dict2); 114 115 // retrieve and modify s 116 smallStringt *S = NULL; 117 S = getG(dict, S, "s"); 118 upperG(S); 119 finishG(S); 120 121 // retrieve and modify the array in dict 122 smallArrayt *gotArray = NULL; 123 gotArray = getG(dict, gotArray, "array"); 124 125 int *I = NULL; 126 I = getG(gotArray, I, 0); 127 (*I)++; 128 129 S = getG(gotArray, S,1); 130 upperG(S); 131 finishG(S); 132 133 // delete last element 134 delG(gotArray, -1, 0); 135 136 // append 137 createAllocateSmallArray(a); 138 pushG(a, -1); 139 pushG(a, "push any type"); 140 putsG(a); 141 // [-1,"push any type"] 142 143 appendNSmashG(gotArray, a); 144 145 printf("\ngotArray content:"); 146 printf("\n"); 147 putsG(gotArray); 148 // gotArray content: 149 // [256,"GENERIC",1.000000e+01,-1,"push any type"] 150 151 setNFreePG(dict, "array", gotArray); 152 153 printf("\nDictionary content:"); 154 printf("\n"); 155 putsG(dict); 156 // Dictionary content: 157 // {"s": "LIBSHEEPY","array": [256,"GENERIC",1.000000e+01,-1,"push any type"],"Dict2": {"undefinedValue": null}} 158 printf("Dictionary element types:"); 159 printf("\n"); 160 smallDictt *d = typeStringsG(dict); 161 putsG(d); 162 // Dictionary element types: 163 // {"s": "string","array": "array","Dict2": "dict"} 164 terminateG(d); 165 166 setTopNFreeG(json, (baset*)dict); 167 168 setG(json, "usefull", NULL); 169 setG(json, "libsheepy", "c11"); 170 setG(json, "bool", (bool)true); 171 172 printf("\nJson content:"); 173 printf("\n"); 174 s = toYMLG(json, 2);; 175 logNFree(s); 176 // Json content: 177 // --- 178 // s: LIBSHEEPY 179 // array: 180 // - 256 181 // - GENERIC 182 // - 1.000000e+01 183 // - -1 184 // - push any type 185 // Dict2: 186 // undefinedValue: null 187 // usefull: null 188 // libsheepy: c11 189 // bool: true 190 191 // free all objects 192 terminateG(json); 193 194 finalizeLibsheepy(); 195 }