Created
September 21, 2022 20:43
-
-
Save epshteinmatthew/d751bdea5237f09af50970cdab91edd9 to your computer and use it in GitHub Desktop.
hvdAU;KL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <rmsutils.h> //utils! | |
| #include <jsmn.h> //json | |
| #define INT_A 1306858826 | |
| #define INT_B 1130263739 | |
| #define INT_LE 2147483647 | |
| /* | |
| stuff to do: | |
| entry strcut: key, values, hash | |
| philosophy: | |
| Have a function to initialize that loads specified file into RAM as a list of KV's | |
| Hashing(?) | |
| Manipulations happens only in RAM, then saves on progream exit | |
| */ | |
| #define PB_DIR "/home/matthew/Documents/pdftomd/phonebook" // change per user | |
| #define ENTRY_AMOUNT 5000 | |
| struct arr_entry{ | |
| char *key; | |
| char *value; | |
| }; | |
| struct phonebook_entry | |
| { | |
| unsigned int hash; | |
| struct arr_entry *data; | |
| }; | |
| unsigned int hash(char *key, int size, int a, int b, int prime) | |
| { | |
| unsigned int sum = 0; | |
| int index = 0; | |
| while (key[index] != '\0') | |
| { | |
| sum += (int)key[index]; | |
| index++; | |
| } | |
| sum = (((a * sum) + b) % prime) % size; | |
| return sum; | |
| } | |
| char *lookup(char *key, struct phonebook_entry *holder){ | |
| int pos = hash(key, ENTRY_AMOUNT, INT_A, INT_B, INT_LE); | |
| if(holder[pos].data){ | |
| for (int i = 0; holder[pos].data[i].key; i++) | |
| { | |
| if(strcmp(holder[pos].data[i].key, key) == 0){ | |
| return holder[pos].data[i].value; | |
| } | |
| } | |
| return "e"; | |
| } | |
| } | |
| int initpb(struct phonebook_entry *holder) | |
| { | |
| static const char *JSON_STRING = | |
| "{\"user\": \"johndoe\", \"admin\": false,\"admin\": true, \"uid\": 1000,\n " | |
| "\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}"; | |
| holder = malloc(sizeof(struct phonebook_entry) * ENTRY_AMOUNT); | |
| jsmn_parser parser; | |
| jsmn_init(&parser); | |
| int count = jsmn_parse(&parser, JSON_STRING, strlen(JSON_STRING), NULL, 0); | |
| jsmntok_t tokens[count]; | |
| parser.pos = 0; | |
| int obj = jsmn_parse(&parser, JSON_STRING, strlen(JSON_STRING), tokens, sizeof(tokens)/sizeof(tokens)[0]); | |
| if (obj < 0) | |
| { | |
| printf("Failed to parse JSON: %d\n", obj); | |
| return 1; | |
| } | |
| /* Assume the top-level element is an object */ | |
| if (obj < 1 || tokens[0].type != JSMN_OBJECT) | |
| { | |
| printf("Failed to parse JSON: %d\n", parser); | |
| printf("Object expected\n"); | |
| return 1; | |
| } | |
| for (int i = 1; i < obj; i+= 2) | |
| { | |
| char *key = strndup(JSON_STRING + tokens[i].start, tokens[i].end - tokens[i].start); | |
| int pos = hash(key,ENTRY_AMOUNT, INT_A, INT_B, INT_LE); | |
| holder[pos].hash = pos; | |
| if(holder[pos].data){ | |
| int sizearr = sizeof(holder[pos].data)/sizeof(struct arr_entry); | |
| printf("%d\n", sizearr); | |
| holder[pos].data = realloc(holder[pos].data, (1+sizearr)*sizeof(struct arr_entry)); | |
| holder[pos].data[sizearr].key = key; | |
| holder[pos].data[sizearr].value = strndup(JSON_STRING + tokens[i + 1].start, tokens[i+1].end - tokens[i+1].start); | |
| continue; | |
| } | |
| holder[pos].data = malloc(sizeof(struct arr_entry)); | |
| holder[pos].data[0].key = key; | |
| puts(holder[pos].data[0].key); | |
| holder[pos].data[0].value = strndup(JSON_STRING + tokens[i + 1].start, tokens[i+1].end - tokens[i+1].start); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment