diff --git a/modules/map.c b/modules/map.c new file mode 100644 index 0000000..55ecbb3 --- /dev/null +++ b/modules/map.c @@ -0,0 +1,54 @@ +// +// Készítette: Toldi Balázs Ádám +// Dátum: 2019. 10. 16. +// + +#include "map.h" + +void map_init(map *m) { + m->length = 0; + m->size = 0; + m->pairs = 0; +} + +bool map_isFound(map map, char* key){ + for (int i = 0; i < map.length; ++i) { + if(strcmp(map.pairs[i].key,key) == 0) + return true; + } + return false; +} + +char* map_getValue(map m, char* key){ + for (int i = 0; i < m.length; ++i) { + if(strcmp(m.pairs[i].key,key) == 0) + return m.pairs[i].value; + } + return "UNDEFINED"; +} +void map_dump(map m) { + for (int i = 0; i length >= m->size) + { + assert(m->length == m->size); + size_t new_size = (m->size + 2) * 2; + pair *new_list = realloc(m->pairs, new_size * sizeof(pair)); + if (new_list == 0) + printf("OUT OF MEMORY!"); + m->size = new_size; + m->pairs = new_list; + } + m->pairs[m->length++] = p; +} + +pair map_make_pair(char *key,char *value) { + pair result; + strcpy(result.key,key); + strcpy(result.value,value); + return result; +} diff --git a/modules/map.h b/modules/map.h new file mode 100644 index 0000000..6420ee9 --- /dev/null +++ b/modules/map.h @@ -0,0 +1,39 @@ +// +// Készítette: Toldi Balázs Ádám +// Dátum: 2019. 10. 16. +// + +#pragma once +#include +#include +#include +#include +#include +#include + +typedef struct Pair{ + char key[65]; + char value[65]; +}pair; + +typedef struct Map +{ + size_t size; + size_t length; + pair *pairs; +} map,config; + +void initMap(map * m); + +bool map_isFound(map m, char* key); + +char* map_getValue(map m, char* key); + +void map_addPair(map *m, pair p); + +pair map_make_pair(char *key,char *value); + +void map_init(map *m); + +//Debug-hoz hasznos +void map_dump(map m); \ No newline at end of file diff --git a/utility.c b/utility.c index 836bbb1..9175e87 100644 --- a/utility.c +++ b/utility.c @@ -19,7 +19,9 @@ char* generateSeed(int len){ result[len] = '\0'; return result; } -map* getHandshakeData(char* text,int* len){ +map getHandshakeData(char* text){ + map result; + map_init(&result); if (text[0] == '@') memmove(text, text+1, strlen(text)); int count = 0; @@ -27,8 +29,6 @@ map* getHandshakeData(char* text,int* len){ if(text[i] == '&') count++; } - *len = count+1; - map * m = (map*) malloc(sizeof(map)*(*len)); int i =0; const char c[2] = "&"; char *tmp; @@ -36,31 +36,16 @@ map* getHandshakeData(char* text,int* len){ while (tmp != NULL && i #include #include #include #include #include +#include #include #include -#endif +#include "modules/map.h" #define DEFAULT_BUFLEN 1024 char* generateSeed(int len); -typedef struct Map{ - char key[50]; - char value[50]; -} map; - -map* getHandshakeData(char* text,int *len); - -bool map_isFound(map m[],int len, char* key); - -char* map_getValue(map m[],int len, char* key); - -//Debug-hoz hasznos -void map_dump(map m[],int len); +map getHandshakeData(char* text); void md5(char *string, char outputBuffer[33]);