static void array_print(pdf_obj *a) { int i; printf("%s ", "["); for (i = 0; i < a->value.a.len; i++) { pdf_obj *o = &a->value.a.items[i]; switch (o->t) { case eRef: printf("ref(%d,%d) ", o->value.r.num, o->value.r.gen); break; case eKey: case eName: printf("name(%s) ", o->value.k); break; case eString: case eHexString: printf("str("); {int i; for (i = 0; i < o->value.s.len; i++) printf("%c", o->value.s.buf[i]); } printf(")"); break; case eInt: printf("i(%d) ", o->value.i); break; case eReal: printf("f(%f) ", o->value.f); break; case eArray: { array_print(o); } break; case eDict: printf("<< "); dict_dump(o->value.d.dict); printf(">> "); break; default: printf("%d ", o->t); break; } } printf("%s ", "]"); }
static void dict_print_keyval(char *key, void *val, void *x) { if (!val) printf("dict_print_key: %s\n", key); else { pdf_obj *o = (pdf_obj*) val; switch (o->t) { case eRef: printf("%s = ref(%d,%d)\n", key, o->value.r.num, o->value.r.gen); break; case eKey: case eName: printf("%s = name(%s)\n", key, o->value.k); break; case eString: case eHexString: printf("%s = str(%s)\n", key, o->value.s.buf); // could out run buffer break; case eInt: printf("%s = i(%d)\n", key, o->value.i); break; case eReal: printf("%s = f(%f)\n", key, o->value.f); break; case eArray: printf("%s = ", key); array_print(o); printf("\n"); break; case eDict: printf("%s = << ", key); dict_dump(o->value.d.dict); printf(">>\n"); break; case eBool: printf("%s = B(%d)\n", key, o->value.i); break; default: printf("%s:%d\n", key, o->t); break; } } }
int main(void) { char *option = NULL, *word = NULL, *def = NULL, *filename = NULL; bool exit = false; dict_t dict = NULL,copy = NULL; dict = dict_empty(); do { option = print_menu(); switch (*option) { case ADD: printf("\tPlease enter the word to add to the dict: "); word = readline_from_stdin(); if (!dict_exists(dict, word)) { printf("\tPlease enter the definition: "); def = readline_from_stdin(); dict_add(dict,word,def); printf("\t-> The word and definition were added.\n"); free(def); def = NULL; } else { printf("\t-> The word %s already exists.\n", word); } assert(dict_exists(dict,word)); free(word); word = NULL; break; case COPY: copy = dict_copy(dict); printf("\t-> The dictionary was duplicated. Showing the duplicated dict:\n"); printf("{\n"); dict_dump(copy, stdout); printf("\n}\n"); copy = dict_destroy(copy); break; case DELETE: printf("\tPlease enter the word to delete from the dict: "); word = readline_from_stdin(); if(dict_exists(dict,word)) { dict = dict_remove(dict,word); printf("\t-> The word was successfully removed.\n"); } else { printf("\t-> The word %s does not exist.\n", word); } assert(!dict_exists(dict, word)); free(word); word = NULL; break; case DUMP: printf("\tPlease enter the filename to dump the dict to: "); filename = readline_from_stdin(); dict_to_file(dict,filename); printf("\t-> The dictionary was successfully dumped.\n"); free(filename); filename = NULL; break; case EMPTY: dict = dict_destroy(dict); dict = dict_empty(); printf("\t-> The dictionary was successfully emptied.\n"); break; case EXIT: printf("\t-> Exiting.\n"); exit = true; break; case LOAD: printf("\tPlease enter the filename to load the dict from: "); filename = readline_from_stdin(); copy = dict_from_file(filename); if (copy == NULL) { printf("Can not load dict from filename %s\n", filename); } else { dict = dict_destroy(dict); dict = copy; printf("\t-> The dictionary was successfully loaded.\n"); } free(filename); filename = NULL; break; case SEARCH: printf("\tPlease enter the word to search in the dict: "); word = readline_from_stdin(); if(dict_exists(dict,word)) { def = dict_search(dict,word); printf("\t-> The definition for \"%s\" is \"%s\".\n", word, def); free(def); def = NULL; } else { printf("\t-> The word \"%s\" does not exist in the dict.\n", word); } free(word); word = NULL; break; case SHOW: printf("{\n"); dict_dump(dict, stdout); printf("\n}\n"); break; case SIZE: printf("\t-> The size is %u.\n", dict_length(dict)); break; default: printf("\n\"%c\" is invalid. Please choose a valid option.\n\n", *option); } free(option); option = NULL; } while (!exit); dict = dict_destroy(dict); return (0); }