You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.5 KiB

#include "dict.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// dict_t **dict = dict_alloc();
// dict_addItem(dict, "foo", "bar");
// (char *)dict_getItem(*dict, "foo")
// dict_size(*dict)
// dict_delItem(dict, "foo");
// dict_dealloc(*dict);
Dict ** dict_alloc() {
return malloc(sizeof(Dict));
}
void dict_dealloc(Dict *dict) {
Dict *ptr;
for (ptr = dict; ptr != NULL; ptr = ptr->next) {
free(ptr);
}
}
void *dict_getItem(Dict *dict, char *key) {
Dict *ptr;
for (ptr = dict; ptr != NULL; ptr = ptr->next) {
if (strcmp(ptr->key, key) == 0) {
return ptr->value;
}
}
return NULL;
}
void dict_addItem(Dict **dict, char *key, void *value) {
Dict *d = malloc(sizeof(Dict));
d->key = malloc(strlen(key)+1);
strcpy(d->key, key);
d->value = value;
d->next = *dict;
*dict = d;
}
int dict_size(Dict *dict) {
int size = 0;
Dict *ptr;
for (ptr = dict; ptr != NULL; ptr = ptr->next) {
size++;
}
return size;
}
char *sprintf_id(char *str, int id){
if(strlen(str) > 50){
printf("WARNING: char '%s' ist relativ lang (%ld Zeichen)! Evtl sprintf_id malloc anpassen!\n", str, strlen(str));
}
if(strlen(str) >= 63){
printf("Ok der war bisschen sehr lang, SIGSEV incoming :O :O :O, es ist alles kaputt, mach tmp lieber mal länger\n");
}
char *tmp = malloc(sizeof(char) * 64);
sprintf(tmp, "%s%d", str, id);
return tmp;
}