parent
f9a24a52c2
commit
10d320b29d
@ -0,0 +1,66 @@
|
||||
#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;
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
#ifndef DICT_H_
|
||||
#define DICT_H_
|
||||
|
||||
typedef struct Dict {
|
||||
char *key;
|
||||
void *value;
|
||||
struct Dict *next;
|
||||
} Dict;
|
||||
|
||||
char *sprintf_id(char *str, int id);
|
||||
Dict **dict_alloc();
|
||||
void dict_dealloc(Dict *dict);
|
||||
void *dict_getItem(Dict *dict, char *key);
|
||||
void dict_delItem(Dict **dict, char *key);
|
||||
void dict_addItem(Dict **dict, char *key, void *value);
|
||||
int dict_size(Dict *dict);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Loading…
Reference in new issue