diff --git a/praxis3/CMakeLists.txt b/praxis3/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/praxis3/key_value_pair.c b/praxis3/key_value_pair.c new file mode 100644 index 0000000..48d657f --- /dev/null +++ b/praxis3/key_value_pair.c @@ -0,0 +1,135 @@ +#include "key_value_pair.h" + +#include +#include +#include + +key_value_pair *head = NULL; +key_value_pair *tail = NULL; + +char *key_value_pair_to_string(key_value_pair *kv) +{ + char *str = malloc(sizeof(char) * (strlen(kv->key) + strlen(kv->value) + 1)); + if (str == NULL) return NULL; + + strcpy(str, kv->key); + strcat(str, kv->value); + + return str; +} + +// initializes a new instance of key_value_pair with given values on given pointer +key_value_pair *init_key_value_pair(const char *key, const char *value) +{ + key_value_pair *kv = (key_value_pair*)malloc(sizeof(key_value_pair)); + if (kv == NULL) + { + perror("Failed to allocate memory for key_value_pair"); + return NULL; + } + kv->key = strdup(key); + if (kv->key == NULL) + { + free(kv); + perror("Failed to allocate memory for key_value_pair->key"); + return NULL; + } + kv->value = strdup(value); + if (kv->value == NULL) + { + free(kv->key); + free(kv); + perror("Failed to allocate memory for key_value_pair->value"); + return NULL; + } + + kv->next = NULL; + kv->toString = key_value_pair_to_string; + + return kv; +} + +// frees the given instance of struct key_value_pair +void free_key_value_pair(key_value_pair **kv) +{ + if (*kv == NULL) return; + + free((*kv)->key); + free((*kv)->value); + free(*kv); + + *kv = NULL; +} + +// frees whole key_value_pair list structure +void free_key_value_pairs() +{ + while (head != NULL) + { + key_value_pair *toFree = head; + head = head->next; + free_key_value_pair(&toFree); + } + tail = NULL; +} + + +// adds given key_value_pair to list +int add_key_value_pair(key_value_pair *kv) +{ + if (head == NULL) + { + head = kv; + tail = kv; + } + else + { + key_value_pair *iterator = head; + while (iterator->next != NULL) + { + if (strcmp(kv->key, iterator->key) == 0) + { + char *bak = iterator->value; + iterator->value = (char*)realloc(iterator->value, strlen(iterator->value) + strlen(kv->value) + 1); + if (iterator->value == NULL) + { + iterator->value = bak; + return -1; + } + strcat(iterator->value, kv->value); + return 0; + } + iterator = iterator->next; + } + iterator->next = kv; + tail = kv; + } + return 0; +} + +// removes given key_value_pair from list +void remove_key_value_pair(key_value_pair *kv) +{ + if (head == NULL || kv == NULL) return; + if (head == kv) + { + head = head->next; + if (tail == kv) tail = NULL; + return; + } + + key_value_pair *iterator = head; + if (iterator == NULL) return; + + while (iterator != NULL) + { + if (iterator->next == kv) + { + iterator->next = kv->next; + if (tail == kv) tail = iterator; + return; + } + iterator = iterator->next; + } +} + diff --git a/praxis3/key_value_pair.h b/praxis3/key_value_pair.h new file mode 100644 index 0000000..d9c010e --- /dev/null +++ b/praxis3/key_value_pair.h @@ -0,0 +1,20 @@ +#ifndef MAP_REDUCE_H +#define MAP_REDUCE_H + +// Constants + +typedef struct key_value_pair{ + char *key; + char value; + struct key_value_pair *next; +} key_value_pair; + +static key_value_pair *head = NULL; + +// Functions +void init_key_value_pair(key_value_pair *kv, char *key, char *value); +void free_key_value_pair(key_value_pair *kv); + +void add_key_value_pair(key_value_pair *kv, char *key, char *value); + +#endif //MAP_REDUCE_H diff --git a/praxis3/zmq_distributor.c b/praxis3/zmq_distributor.c new file mode 100644 index 0000000..2c072e7 --- /dev/null +++ b/praxis3/zmq_distributor.c @@ -0,0 +1,6 @@ +#include + +int main() { + printf("Hello, World!\n"); +} + diff --git a/praxis3/zmq_worker.c b/praxis3/zmq_worker.c new file mode 100644 index 0000000..7560393 --- /dev/null +++ b/praxis3/zmq_worker.c @@ -0,0 +1 @@ +#include "worker.h"