Skip to content

Instantly share code, notes, and snippets.

@astronomersiva
Created November 23, 2015 05:51
Show Gist options
  • Save astronomersiva/e02457113601c0fd15f2 to your computer and use it in GitHub Desktop.
Save astronomersiva/e02457113601c0fd15f2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Values {
char *value;
int counter;
struct Values* next_value;
};
struct Keys {
char *key;
struct Values* values;
struct Keys* next_key;
};
typedef struct Keys Keys;
typedef struct Values Values;
char *strlwr(char *str)
{
size_t i;
size_t len = strlen(str);
for(i = 0; i < len; i++) {
str[i] = tolower((unsigned char)str[i]);
}
return str;
}
int main(int argc, char const *argv[])
{
const char space[2] = " ";
char user_input[20];
char *token, *operand, *input_value, *print_input;
int len, begin_count = 0, i, search_flag = 0;
Keys *keys_root = NULL;
Keys *keys_iterator, *keys_head, *keys_temp, *keys_node;
while (1) {
fgets(user_input, 20, stdin);
//remove trailing '\n'
len = strlen(user_input);
if (user_input[len - 1] == '\n') {
user_input[len - 1] = '\0';
}
//first token - the command or invalid input
token = strlwr(strtok(user_input, space));
if (strcmp(token, "set") == 0) {
operand = strtok(NULL, space);
input_value = strtok(NULL, space);
//initially empty case
if (keys_root == NULL) {
keys_root = (Keys*)malloc(sizeof(Keys));
Values *values_root = (Values*)malloc(sizeof(Values));
/*
* copying operand and input_value directly to
* keys_root -> key and values_root -> value
* using strcpy() causes memory links with the
* next input.
*/
char temp_operand[20], temp_value[20];
strcpy(temp_operand, operand);
keys_root -> key = (char*) temp_operand;
strcpy(temp_value, input_value);
values_root -> value = (char*) temp_value;
values_root -> counter = begin_count;
values_root -> next_value = NULL;
keys_root -> values = values_root;
keys_root -> next_key = NULL;
keys_head = keys_root;
} else {
//search for key
keys_temp = keys_head;
while (keys_temp != NULL) {
if (strcmp(keys_temp -> key, operand) == 0) {
search_flag = 1;
break;
}
keys_temp = keys_temp -> next_key;
}
if (search_flag == 1) {
//key exists
Values *values_node = (Values*)malloc(sizeof(Values));
Values *values_temp = (Values*)malloc(sizeof(Values));
keys_node = (Keys*)malloc(sizeof(Keys));
keys_node = keys_temp;
values_node = keys_node -> values;
while (values_node -> next_value != NULL) {
values_node = values_node -> next_value;
}
/*
* strcpy() causes segmentation faults.
* creating a duplicate and assigning works.
*/
char *temp_value_1;
temp_value_1 = strdup(input_value);
values_temp -> value = (char*)temp_value_1;
values_temp -> counter = begin_count;
values_temp -> next_value = NULL;
values_node -> next_value = values_temp;
search_flag = 0;
} else {
//key doesnt exist
keys_node = (Keys*)malloc(sizeof(Keys));
Values *values_root = (Values*)malloc(sizeof(Values));
/*
* strcpy() causes segmentation faults.
* creating a duplicate and assigning works.
*/
char *temp_operand_1, *temp_value_1;
temp_operand_1 = strdup(operand);
keys_node -> key = (char*) temp_operand_1;
temp_value_1 = strdup(input_value);
values_root -> value = (char*)temp_value_1;
values_root -> counter = begin_count;
values_root -> next_value = NULL;
keys_node -> values = values_root;
keys_node -> next_key = NULL;
keys_iterator = keys_head;
while (keys_iterator -> next_key != NULL) {
keys_iterator = keys_iterator -> next_key;
}
keys_iterator -> next_key = keys_node;
keys_temp = NULL;
}
}
} else if (strcmp(token, "print") == 0) {
print_input = strtok(NULL, space);
keys_temp = keys_head;
int matches = 0;
while (keys_temp != NULL) {
Values *values_temp = (Values*)malloc(sizeof(Values));
values_temp = keys_temp -> values;
while (values_temp -> next_value != NULL) {
values_temp = values_temp -> next_value;
}
if (values_temp -> value) {
if (strcmp(values_temp -> value, input_value) == 0) {
matches++;
}
keys_temp = keys_temp -> next_key;
} else {
keys_temp = keys_temp -> next_key;
}
}
if (matches == 0) {
printf("Value not found.\n");
} else {
printf("%d Matches found.\n", matches);
}
} else if (strcmp(token, "begin") == 0) {
begin_count++;
} else if (strcmp(token, "rollback") == 0) {
//delete all values with counter as begin_count
keys_temp = keys_head;
int matches = 0;
while (keys_temp != NULL) {
Values *values_temp = (Values*)malloc(sizeof(Values));
Values *values_prev = (Values*)malloc(sizeof(Values));
if (keys_temp -> values) {
values_temp = keys_temp -> values;
while (values_temp != NULL) {
if (values_temp -> counter == begin_count) {
//delete the value
values_prev -> next_value = NULL;
free(values_temp);
break;
}
values_prev = values_temp;
values_temp = values_temp -> next_value;
}
keys_temp = keys_temp -> next_key;
}
}
begin_count--;
} else if (strcmp(token, "exit") == 0) {
break;
} else {
printf("Invalid input.\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment