void SG_EXPORT _sgJSONFreeValue(SGJSONValue* value) { if(!value) return; SGJSONSetItem* titem; switch(value->type) { case SG_JSON_TYPE_NULL: case SG_JSON_TYPE_BOOLEAN: case SG_JSON_TYPE_NUMBER: break; // nothing to do! case SG_JSON_TYPE_STRING: case SG_JSON_TYPE_COMMENT: free(value->v.string); break; case SG_JSON_TYPE_ARRAY: while(value->v.array->head) _sgJSONFreeValue(sgListPopFirst(value->v.array)); sgListDestroy(value->v.array); break; case SG_JSON_TYPE_OBJECT: while(value->v.object->root) { titem = sgSetPopRoot(value->v.object); free(titem->key); _sgJSONFreeValue(titem->val); free(titem); } sgSetDestroy(value->v.object); break; } free(value->strbuf); free(value); }
int main(void) { char buf[1024]; char* ptr; SGSet* set = sgSetCreate(_setCmp, NULL); printf("Example of SIEGE sets"); printf("----------------------------------------\n"); int option; do { printf("Select a set operation:\n"); printf("+<elem> -- Add a new element\n"); printf("-<elem> -- Delete an element\n"); printf("?<elem> -- Query whether an element is present\n"); printf("p -- Print all elements in (strcmp) order\n"); printf("pp -- Pretty-print the set's search tree\n"); printf("s -- Print the set's tree in a compact s-expression form\n"); // yes, I was bored. printf("ss -- Pretty print the set's tree in a compact s-expression form\n"); // ...very bored. printf("\n"); printf("q -- Quit the program\n"); printf("----------------------------------------\n"); printf("Selection: "); fflush(stdout); fgets(buf, sizeof(buf), stdin); printf("\n"); trimNL(buf); if(strstr(buf, "+") == buf) option = '+'; else if(strstr(buf, "-") == buf) option = '-'; else if(strstr(buf, "?") == buf) option = '?'; else if(!strcmp(buf, "p") || !strcmp(buf, "P")) option = 'p'; else if(!strcmp(buf, "pp") || !strcmp(buf, "pP") || !strcmp(buf, "Pp") || !strcmp(buf, "PP")) option = 'P'; else if(!strcmp(buf, "s") || !strcmp(buf, "S")) option = 's'; else if(!strcmp(buf, "ss") || !strcmp(buf, "sS") || !strcmp(buf, "Ss") || !strcmp(buf, "SS")) option = 'S'; else if(!strcmp(buf, "q") || !strcmp(buf, "Q")) option = 'q'; else option = 0; ptr = skipSpace(buf + 1); switch(option) { case '+': if(!strlen(ptr)) { printf("Error -- element must be a non-empty string!\n"); break; } switch(insertItem(set, ptr)) { case 0: printf("Element '%s' already exists in the set!\n", ptr); break; case 1: printf("Element '%s' successfully added!\n", ptr); break; default: printf("Error adding element '%s' -- probably out of memory!\n", ptr); } break; case '-': if(!strlen(ptr)) { printf("Error -- element must be a non-empty string!\n"); break; } switch(removeItem(set, ptr)) { case 0: printf("There is no element '%s' in the set!\n", ptr); break; case 1: printf("Element '%s' successfully removed!\n", ptr); break; default: printf("Error removing element '%s'!\n", ptr); } break; case '?': if(!strlen(ptr)) { printf("Error -- element must be a non-empty string!\n"); break; } if(sgSetSearch(set, ptr)) printf("Element '%s' is present in the set!\n", ptr); else printf("Element '%s' is *NOT* present in the set!\n", ptr); break; case 'p': printSet(set); break; case 'P': pprintSet(set); break; case 's': sprintSet(set, 0); break; case 'S': sprintSet(set, 1); break; case 'q': break; /* handled by the while() */ default: printf("Invalid option '%s'!\n", trimSpace(buf)); break; } printf("\n"); fflush(stdout); } while(option != 'q'); while(set->root) removeItem(set, set->root->item); sgSetDestroy(set); return 0; }