Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*VRABIE-DINU DARIAN 313CB*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "structures.h"
- #define SINGULAR 1
- #define MAX_INSTRUCTION_LENGHT 50
- #define MAX_INSTRUCTIONS_NUMBER 20
- #define MAX_STATUS_LENGHT 256
- #define FLOAT_PRECISION 7
- #define END_DB (strcmp(instruction_set[0], "DELETE_DB") == 0)
- #define BLANK_SPACE(len) (30 - len)
- int CONV_STRING_TO_INT(char *str)
- {
- int i, len = strlen(str);
- int num = 0;
- if(str[0] == '-')
- {
- num = num * 10 + ((int)(str[1]) - 48);
- i = 2;
- }
- else
- {
- num = num * 10 + ((int)(str[0]) - 48);
- i = 1;
- }
- for(; i < len; i++)
- {
- num = num * 10 + ((int)(str[i]) - 48);
- }
- if(str[0] == '-')
- num *= -1;
- return num;
- }
- float CONV_STRING_TO_FLOAT(char *str)
- {
- int i, len = strlen(str);
- float num = 0.0, p = 10.0;
- if(str[0] == '-')
- {
- num = num * 10 + ((int)(str[1]) - 48);
- i = 2;
- }
- else
- {
- num = num * 10 + ((int)(str[0]) - 48);
- i = 1;
- }
- for(; i < len; i++)
- {
- if(str[i] == '.')
- {
- i++;
- break;
- }
- num = num * 10.0 + ((int)(str[i]) - 48.0);
- }
- for(; i < len; i++)
- {
- num = num + (((int)(str[i]) - 48.0) / p);
- p *= 10.0;
- }
- if(str[0] == '-')
- num *= -1;
- return num;
- }
- int CHAR_NUM(int val)
- {
- int len = 0;
- while(val != 0)
- {
- len++;
- val /= 10;
- }
- return len;
- }
- t_db *INIT_DB(const char* new_db_name)
- {
- t_db *new_db = (t_db*)calloc(SINGULAR, sizeof(t_db));
- if(!new_db)
- {
- fprintf(stderr, "Error initializing database.\n");
- return NULL;
- }
- strcpy(new_db -> name, new_db_name);
- new_db -> tables = NULL;
- return new_db;
- }
- t_column *CREATE_COLUMN(char* name)
- {
- t_column *new_col = (t_column*)calloc(SINGULAR, sizeof(t_column));
- if(!new_col)
- return NULL;
- strcpy(new_col -> name, name);
- new_col -> name[strlen(new_col -> name)] = '\0';
- new_col -> next = NULL;
- return new_col;
- }
- void INSERT_COLUMN(t_table *table, t_column *column)
- {
- t_column *wp;
- if(table -> columns == NULL)
- {
- table -> columns = column;
- return;
- }
- for(wp = table -> columns; wp -> next != NULL; wp = wp -> next);
- wp -> next = column;
- return;
- }
- void *CREATE_LINE(char **instruction_set, int num_param, unsigned int type)
- {
- int i;
- if(type == 0)
- {
- t_intLine *new_int_line = (t_intLine*)calloc(SINGULAR, sizeof(t_intLine));
- if(!new_int_line)
- return NULL;
- new_int_line -> cells = (t_intCell*)calloc(SINGULAR, sizeof(t_intCell));
- if(!(new_int_line -> cells))
- {
- free(new_int_line);
- return NULL;
- }
- new_int_line -> next = NULL;
- t_intCell *last = new_int_line -> cells;
- for(i = 2; i < num_param; i++)
- {
- last -> value = CONV_STRING_TO_INT(instruction_set[i]);
- if(i != num_param - 1)
- {
- last -> next = (t_intCell*)calloc(SINGULAR, sizeof(t_intCell));
- if(!(last -> next))
- return NULL;
- last = last -> next;
- last -> next = NULL;
- }
- else
- {
- last -> next = NULL;
- }
- }
- return (void*)(new_int_line);
- }
- else if(type == 1)
- {
- t_floatLine *new_float_line = (t_floatLine*)calloc(SINGULAR, sizeof(t_floatLine));
- if(!new_float_line)
- return NULL;
- new_float_line -> cells = (t_floatCell*)calloc(SINGULAR, sizeof(t_floatCell));
- if(!(new_float_line -> cells))
- {
- free(new_float_line);
- return NULL;
- }
- new_float_line -> next = NULL;
- t_floatCell *last = new_float_line -> cells;
- for(i = 2; i < num_param; i++)
- {
- last -> value = CONV_STRING_TO_FLOAT(instruction_set[i]);
- if(i != num_param - 1)
- {
- last -> next = (t_floatCell*)calloc(SINGULAR, sizeof(t_floatCell));
- if(!(last -> next))
- return NULL;
- last = last -> next;
- last -> next = NULL;
- }
- else
- {
- last -> next = NULL;
- }
- }
- return (void*)(new_float_line);
- }
- else if(type == 2)
- {
- t_stringLine *new_string_line = (t_stringLine*)calloc(SINGULAR, sizeof(t_stringLine));
- if(!new_string_line)
- return NULL;
- new_string_line -> cells = (t_stringCell*)calloc(SINGULAR, sizeof(t_stringCell));
- if(!(new_string_line -> cells))
- {
- free(new_string_line);
- return NULL;
- }
- new_string_line -> next = NULL;
- t_stringCell *last = new_string_line -> cells;
- for(i = 2; i < num_param; i++)
- {
- last -> value = strdup(instruction_set[i]);
- if(i != num_param - 1)
- {
- last -> next = (t_stringCell*)calloc(SINGULAR, sizeof(t_stringCell));
- if(!(last -> next))
- return NULL;
- last = last -> next;
- last -> next = NULL;
- }
- else
- {
- last -> next = NULL;
- }
- }
- return (void*)(new_string_line);
- }
- return NULL;
- }
- int TABLE_EXISTS(t_db *db, char *table_name)
- {
- if(db -> tables == NULL)
- return 0;
- t_table *wp;
- for(wp = (db -> tables); wp != NULL; wp = (wp -> next))
- {
- if(strcmp(wp -> name, table_name) == 0)
- return 1;
- }
- return 0;
- }
- void INSERT_TABLE(t_db *db, t_table *table)
- {
- t_table *wp;
- if(db -> tables == NULL)
- {
- db -> tables = table;
- return;
- }
- for(wp = db -> tables; wp -> next != NULL; wp = wp -> next);
- wp -> next = table;
- return;
- }
- char CREATE_TABLE(t_db *db, char **instruction_set, int num_param, t_cellType type)
- {
- if(db == NULL)
- return 'n';
- int i;
- if(TABLE_EXISTS(db, instruction_set[1]))
- {
- return 'a';
- }
- else
- {
- t_table *new_table = (t_table*)calloc(SINGULAR, sizeof(t_table));
- if(!new_table)
- {
- return 'n';
- }
- strcpy(new_table -> name, instruction_set[1]);
- new_table -> type = type;
- new_table -> lines = NULL;
- new_table -> next = NULL;
- //columns
- for(i = 3; i < num_param; i++)
- {
- t_column *new_col = CREATE_COLUMN(instruction_set[i]);
- if(!new_col)
- {
- return 'n';
- }
- INSERT_COLUMN(new_table, new_col);
- }
- INSERT_TABLE(db, new_table);
- }
- return 'k';
- }
- void PRINT_SEPARATOR_ROW(int colmun_count)
- {
- int i;
- for(i = 0; i < colmun_count; i++)
- {
- printf("------------------------------");
- if(i != (colmun_count - 1))
- printf(" ");
- else
- {
- printf("\n");
- }
- }
- }
- void ADD_IN_TABLE(t_db *db, char **instruction_set, int num_param)
- {
- t_table *wpt;
- for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
- {
- if(strcmp(wpt-> name, instruction_set[1]) == 0)
- break;
- }
- if(wpt == NULL)
- {
- printf("Table \"%s\" not found in database.\n", instruction_set[1]);
- return;
- }
- unsigned int type = (unsigned int)(wpt -> type);
- void *new_line = CREATE_LINE(instruction_set, num_param, type);
- if(type == 0)
- {
- t_intLine *new_int_line = (t_intLine*)new_line;
- new_int_line -> next = NULL;
- t_intLine *wpi;
- if((t_intLine*)(wpt -> lines) == NULL)
- {
- wpt -> lines = new_int_line;
- return;
- }
- for(wpi = (t_intLine*)(wpt -> lines); (t_intLine*)(wpi -> next) != NULL; wpi = (t_intLine*)(wpi -> next))
- {
- continue;
- }
- wpi -> next = new_int_line;
- }
- else if(type == 1)
- {
- t_floatLine *new_float_line = (t_floatLine*)new_line;
- new_float_line -> next = NULL;
- t_floatLine *wpf;
- if((t_floatLine*)(wpt -> lines) == NULL)
- {
- wpt -> lines = new_float_line;
- return;
- }
- for(wpf = (t_floatLine*)(wpt -> lines); (t_floatLine*)(wpf -> next) != NULL; wpf = (t_floatLine*)(wpf -> next))
- {
- continue;
- }
- wpf -> next = new_float_line;
- }
- else if(type == 2)
- {
- t_stringLine *new_string_line = (t_stringLine*)new_line;
- new_string_line -> next = NULL;
- t_stringLine *wps;
- if((t_stringLine*)(wpt -> lines) == NULL)
- {
- wpt -> lines = new_string_line;
- return;
- }
- for(wps = (t_stringLine*)(wpt -> lines); (t_stringLine*)(wps -> next) != NULL; wps = (t_stringLine*)(wps -> next))
- {
- continue;
- }
- wps -> next = new_string_line;
- }
- return;
- }
- void PRINT_TABLE(t_db *db, char* table_name)
- {
- int i, len, colmun_count = 0;
- t_table *wpt;
- t_column *wpc;
- for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
- {
- if(strcmp(wpt -> name, table_name) == 0)
- {
- printf("TABLE: %s\n", wpt -> name);
- for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
- {
- printf("%s", wpc -> name);
- len = strlen(wpc -> name);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- colmun_count++;
- }
- printf("\n");
- for(i = 0; i < colmun_count; i++)
- {
- printf("------------------------------");
- if(i != (colmun_count - 1))
- printf(" ");
- else
- {
- printf("\n");
- }
- }
- if(wpt -> type == 0)
- {
- t_intLine *wpi;
- t_intCell *wpic;
- for(wpi = (t_intLine*)(wpt -> lines); (t_intLine*)(wpi) != NULL; wpi = (t_intLine*)(wpi -> next))
- {
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- printf("%d", wpic -> value);
- if(wpic -> next != NULL)
- {
- len = CHAR_NUM(wpic -> value);
- if(wpic -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- }
- else if(wpt -> type == 1)
- {
- t_floatLine *wpf;
- t_floatCell *wpfc;
- for(wpf = (t_floatLine*)(wpt -> lines); (t_floatLine*)(wpf) != NULL; wpf = (t_floatLine*)(wpf -> next))
- {
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- printf("%f", wpfc -> value);
- if(wpfc -> next != NULL)
- {
- len = CHAR_NUM((int)(wpfc -> value)) + FLOAT_PRECISION;
- if(wpfc -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- }
- else if(wpt -> type == 2)
- {
- t_stringLine *wps;
- t_stringCell *wpsc;
- for(wps = (t_stringLine*)(wpt -> lines); (t_stringLine*)(wps) != NULL; wps = (t_stringLine*)(wps -> next))
- {
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- printf("%s", wpsc -> value);
- if(wpsc -> next != NULL)
- {
- len = strlen(wpsc -> value);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- }
- printf("\n");
- break;
- }
- }
- if(wpt == NULL)
- printf("Table \"%s\" not found in database.\n", table_name);
- }
- void PRINT_DB(t_db *db)
- {
- printf("DATABASE: %s\n\n", db -> name);
- t_table *wpt;
- t_column *wpc;
- int i, len, colmun_count;
- for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
- {
- colmun_count = 0;
- printf("TABLE: %s\n", wpt -> name);
- for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
- {
- printf("%s", wpc -> name);
- len = strlen(wpc -> name);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- colmun_count++;
- }
- printf("\n");
- PRINT_SEPARATOR_ROW(colmun_count);
- if(wpt -> type == 0)
- {
- t_intLine *wpi;
- t_intCell *wpic;
- for(wpi = (t_intLine*)(wpt -> lines); (t_intLine*)(wpi) != NULL; wpi = (t_intLine*)(wpi -> next))
- {
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- printf("%d", wpic -> value);
- if(wpic -> next != NULL)
- {
- len = CHAR_NUM(wpic -> value);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- }
- else if(wpt -> type == 1)
- {
- t_floatLine *wpf;
- t_floatCell *wpfc;
- for(wpf = (t_floatLine*)(wpt -> lines); (t_floatLine*)(wpf) != NULL; wpf = (t_floatLine*)(wpf -> next))
- {
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- printf("%f", wpfc -> value);
- if(wpfc -> next != NULL)
- {
- len = CHAR_NUM((int)(wpfc -> value)) + FLOAT_PRECISION;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- }
- else if(wpt -> type == 2)
- {
- t_stringLine *wps;
- t_stringCell *wpsc;
- for(wps = (t_stringLine*)(wpt -> lines); (t_stringLine*)(wps) != NULL; wps = (t_stringLine*)(wps -> next))
- {
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- printf("%s", wpsc -> value);
- if(wpsc -> next != NULL)
- {
- len = strlen(wpsc -> value);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- }
- printf("\n");
- }
- }
- void SEARCH_IN_TABLE(t_db *db, char **instruction_set)
- {
- int colmun_count = 0, relevant_column = 0;
- int icc, len, i;
- t_table *wpt, *relevant_table;
- t_column *wpc;
- for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
- {
- if(strcmp(wpt -> name, instruction_set[1]) == 0)
- {
- relevant_table = wpt;
- printf("TABLE: %s\n", wpt -> name);
- for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
- {
- printf("%s", wpc -> name);
- len = strlen(wpc -> name);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- colmun_count++;
- }
- break;
- }
- }
- if(wpt == NULL)
- {
- printf("Table \"%s\" not found in database.\n", instruction_set[1]);
- return;
- }
- for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
- {
- if(strcmp(wpc -> name, instruction_set[2]) == 0)
- {
- relevant_column++;
- break;
- }
- relevant_column++;
- }
- if(wpc == NULL)
- {
- printf("Table \"%s\" does not contain column \"%s\".\n", wpt -> name, instruction_set[2]);
- return;
- }
- printf("\n");
- PRINT_SEPARATOR_ROW(colmun_count);
- if(relevant_table -> type == 0)
- {
- t_intLine *wpi;
- t_intCell *wpic;
- int cmp_val = CONV_STRING_TO_INT(instruction_set[4]);
- for(wpi = (t_intLine*)(relevant_table -> lines); wpi != NULL; wpi = wpi -> next)
- {
- icc = 0;
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- icc++;
- if(icc == relevant_column)
- {
- if(strcmp("<", instruction_set[3]) == 0)
- {
- if(wpic -> value < cmp_val)
- {
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- printf("%d", wpic -> value);
- if(wpic -> next != NULL)
- {
- len = CHAR_NUM(wpic -> value);
- if(wpic -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp("<=", instruction_set[3]) == 0)
- {
- if(wpic -> value <= cmp_val)
- {
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- printf("%d", wpic -> value);
- if(wpic -> next != NULL)
- {
- len = CHAR_NUM(wpic -> value);
- if(wpic -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp("==", instruction_set[3]) == 0)
- {
- if(wpic -> value == cmp_val)
- {
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- printf("%d", wpic -> value);
- if(wpic -> next != NULL)
- {
- len = CHAR_NUM(wpic -> value);
- if(wpic -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp("!=", instruction_set[3]) == 0)
- {
- if(wpic -> value != cmp_val)
- {
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- printf("%d", wpic -> value);
- if(wpic -> next != NULL)
- {
- len = CHAR_NUM(wpic -> value);
- if(wpic -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp(">=", instruction_set[3]) == 0)
- {
- if(wpic -> value >= cmp_val)
- {
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- printf("%d", wpic -> value);
- if(wpic -> next != NULL)
- {
- len = CHAR_NUM(wpic -> value);
- if(wpic -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp(">", instruction_set[3]) == 0)
- {
- if(wpic -> value > cmp_val)
- {
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- printf("%d", wpic -> value);
- if(wpic -> next != NULL)
- {
- len = CHAR_NUM(wpic -> value);
- if(wpic -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- }
- }
- }
- return;
- }
- else if(relevant_table -> type == 1)
- {
- t_floatLine *wpf;
- t_floatCell *wpfc;
- float cmp_val = CONV_STRING_TO_FLOAT(instruction_set[4]);
- for(wpf = (t_floatLine*)(relevant_table -> lines); wpf != NULL; wpf = wpf -> next)
- {
- icc = 0;
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- icc++;
- if(icc == relevant_column)
- {
- if(strcmp("<", instruction_set[3]) == 0)
- {
- if(wpfc -> value < cmp_val)
- {
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- printf("%f", wpfc -> value);
- if(wpfc -> next != NULL)
- {
- len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
- if(wpfc -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp("<=", instruction_set[3]) == 0)
- {
- if(wpfc -> value <= cmp_val)
- {
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- printf("%f", wpfc -> value);
- if(wpfc -> next != NULL)
- {
- len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
- if(wpfc -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp("==", instruction_set[3]) == 0)
- {
- if(wpfc -> value == cmp_val)
- {
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- printf("%f", wpfc -> value);
- if(wpfc -> next != NULL)
- {
- len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
- if(wpfc -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp("!=", instruction_set[3]) == 0)
- {
- if(wpfc -> value != cmp_val)
- {
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- printf("%f", wpfc -> value);
- if(wpfc -> next != NULL)
- {
- len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
- if(wpfc -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp(">=", instruction_set[3]) == 0)
- {
- if(wpfc -> value >= cmp_val)
- {
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- printf("%f", wpfc -> value);
- if(wpfc -> next != NULL)
- {
- len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
- if(wpfc -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp(">", instruction_set[3]) == 0)
- {
- if(wpfc -> value > cmp_val)
- {
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- printf("%f", wpfc -> value);
- if(wpfc -> next != NULL)
- {
- len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
- if(wpfc -> value < 0)
- len++;
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- }
- }
- }
- return;
- }
- else if(relevant_table -> type == 2)
- {
- t_stringLine *wps;
- t_stringCell *wpsc;
- char* cmp_val = strdup(instruction_set[4]);
- for(wps = (t_stringLine*)(relevant_table -> lines); wps != NULL; wps = wps -> next)
- {
- icc = 0;
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- icc++;
- if(icc == relevant_column)
- {
- if(strcmp("<", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) < 0)
- {
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- printf("%s", wpsc -> value);
- if(wpsc -> next != NULL)
- {
- len = strlen(wpsc -> value);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp("<=", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) <= 0)
- {
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- printf("%s", wpsc -> value);
- if(wpsc -> next != NULL)
- {
- len = strlen(wpsc -> value);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp("==", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) == 0)
- {
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- printf("%s", wpsc -> value);
- if(wpsc -> next != NULL)
- {
- len = strlen(wpsc -> value);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp("!=", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) != 0)
- {
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- printf("%s", wpsc -> value);
- if(wpsc -> next != NULL)
- {
- len = strlen(wpsc -> value);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp(">=", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) >= 0)
- {
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- printf("%s", wpsc -> value);
- if(wpsc -> next != NULL)
- {
- len = strlen(wpsc -> value);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- else if(strcmp(">", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) > 0)
- {
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- printf("%s", wpsc -> value);
- if(wpsc -> next != NULL)
- {
- len = strlen(wpsc -> value);
- for(i = 0; i <= BLANK_SPACE(len); i++)
- printf(" ");
- }
- }
- printf("\n");
- }
- break;
- }
- }
- }
- }
- return;
- }
- return;
- }
- void DELETE_IN_TABLE(t_db *db, char **instruction_set)
- {
- int colmun_count = 0, icc;
- char flag;
- t_table *wpt;
- t_column *wpc;
- for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
- {
- if(strcmp(wpt -> name, instruction_set[1]) == 0)
- {
- break;
- }
- }
- if(wpt == NULL)
- {
- printf("Table \"%s\" not found in database.\n", instruction_set[1]);
- return;
- }
- for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
- {
- if(strcmp(wpc -> name, instruction_set[2]) == 0)
- {
- colmun_count++;
- break;
- }
- colmun_count++;
- }
- if(wpc == NULL)
- {
- printf("Table \"%s\" does not contain column \"%s\".\n", wpt -> name, instruction_set[2]);
- return;
- }
- if(wpt -> type == 0)
- {
- t_intLine *wpi, *previ = NULL;
- void *auxl;
- t_intCell *wpic, *auxc;
- int cmp_val = CONV_STRING_TO_INT(instruction_set[4]);
- for(wpi = (t_intLine*)(wpt -> lines); wpi != NULL;)
- {
- icc = 0;
- for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
- {
- flag = 0;
- icc++;
- if(icc == colmun_count)
- {
- if(strcmp("<", instruction_set[3]) == 0)
- {
- if(wpic -> value < cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp("<=", instruction_set[3]) == 0)
- {
- if(wpic -> value <= cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp("==", instruction_set[3]) == 0)
- {
- if(wpic -> value == cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp("!=", instruction_set[3]) == 0)
- {
- if(wpic -> value != cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp(">=", instruction_set[3]) == 0)
- {
- if(wpic -> value >= cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp(">", instruction_set[3]) == 0)
- {
- if(wpic -> value > cmp_val)
- {
- flag = 1;
- break;
- }
- }
- }
- }
- if(flag == 1)
- {
- while(wpi -> cells != NULL)
- {
- auxc = wpi -> cells;
- wpi -> cells = wpi -> cells -> next;
- free(auxc);
- }
- if(previ == NULL)
- {
- auxl = (void*)wpi;
- wpt -> lines = (void*)(wpi -> next);
- wpi = (t_intLine*)(wpt -> lines);
- free(auxl);
- }
- else
- {
- auxl = (void*)wpi;
- previ -> next = wpi -> next;
- wpi = wpi -> next;
- free(auxl);
- }
- }
- else if(flag == 0)
- {
- previ = wpi;
- wpi = wpi -> next;
- }
- }
- }
- else if(wpt -> type == 1)
- {
- t_floatLine *wpf, *auxl, *prevf = NULL;
- t_floatCell *wpfc, *auxc;
- float cmp_val = CONV_STRING_TO_FLOAT(instruction_set[4]);
- for(wpf = (t_floatLine*)(wpt -> lines); wpf != NULL;)
- {
- icc = 0;
- for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
- {
- flag = 0;
- icc++;
- if(icc == colmun_count)
- {
- if(strcmp("<", instruction_set[3]) == 0)
- {
- if(wpfc -> value < cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp("<=", instruction_set[3]) == 0)
- {
- if(wpfc -> value <= cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp("==", instruction_set[3]) == 0)
- {
- if(wpfc -> value == cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp("!=", instruction_set[3]) == 0)
- {
- if(wpfc -> value != cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp(">=", instruction_set[3]) == 0)
- {
- if(wpfc -> value >= cmp_val)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp(">", instruction_set[3]) == 0)
- {
- if(wpfc -> value > cmp_val)
- {
- flag = 1;
- break;
- }
- }
- }
- }
- if(flag == 1)
- {
- while(wpf -> cells != NULL)
- {
- auxc = wpf -> cells;
- wpf -> cells = wpf -> cells -> next;
- free(auxc);
- }
- if(prevf == NULL)
- {
- auxl = (void*)wpf;
- wpt -> lines = (void*)(wpf -> next);
- wpf = (t_floatLine*)(wpt -> lines);
- free(auxl);
- }
- else
- {
- auxl = (void*)wpf;
- prevf -> next = wpf -> next;
- wpf = wpf -> next;
- free(auxl);
- }
- }
- else if(flag == 0)
- {
- prevf = wpf;
- wpf = wpf -> next;
- }
- }
- return;
- }
- else if(wpt -> type == 2)
- {
- t_stringLine *wps, *auxl, *prevs = NULL;
- t_stringCell *wpsc, *auxc;
- char* cmp_val = strdup(instruction_set[4]);
- for(wps = (t_stringLine*)(wpt -> lines); wps != NULL;)
- {
- icc = 0;
- for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
- {
- flag = 0;
- icc++;
- if(icc == colmun_count)
- {
- if(strcmp("<", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) < 0)
- {
- if(strcmp(wpsc -> value, cmp_val) < 0)
- {
- flag = 1;
- break;
- }
- }
- }
- else if(strcmp("<=", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) <= 0)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp("==", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) == 0)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp("!=", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) != 0)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp(">=", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) >= 0)
- {
- flag = 1;
- break;
- }
- }
- else if(strcmp(">", instruction_set[3]) == 0)
- {
- if(strcmp(wpsc -> value, cmp_val) > 0)
- {
- flag = 1;
- break;
- }
- }
- }
- }
- if(flag == 1)
- {
- while(wps -> cells != NULL)
- {
- auxc = wps -> cells;
- wps -> cells = wps -> cells -> next;
- free(auxc -> value);
- free(auxc);
- }
- if(prevs == NULL)
- {
- auxl = (void*)wps;
- wpt -> lines = (void*)(wps -> next);
- wps = (t_stringLine*)(wpt -> lines);
- free(auxl);
- }
- else
- {
- auxl = (void*)wps;
- prevs -> next = wps -> next;
- wps = wps -> next;
- free(auxl);
- }
- }
- else if(flag == 0)
- {
- prevs = wps;
- wps = wps -> next;
- }
- }
- return;
- }
- return;
- }
- void DELETE_TABLE(t_db *db, char *table_name)
- {
- t_table *wpt, *prevt;
- for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
- {
- if(strcmp(wpt -> name, table_name) == 0)
- {
- if(wpt == db -> tables)
- {
- db -> tables = db -> tables -> next;
- return;
- }
- else
- {
- prevt -> next = wpt -> next;
- return;
- }
- }
- prevt = wpt;
- }
- if(wpt == NULL)
- {
- printf("Table \"%s\" not found in database.\n", table_name);
- return;
- }
- }
- void CLEAR_TABLE(t_db *db, char *table_name)
- {
- t_table *wpt;
- for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
- {
- if(strcmp(wpt -> name, table_name) == 0)
- break;
- }
- if(wpt == NULL)
- {
- printf("Table \"%s\" not found in database.\n", table_name);
- return;
- }
- if(wpt -> type == 0)
- {
- t_intLine *wpi;
- t_intCell *auxc;
- while(wpt -> lines != NULL)
- {
- wpi = (t_intLine*)(wpt -> lines);
- while((wpi -> cells) != NULL)
- {
- auxc = wpi -> cells;
- wpi -> cells = wpi -> cells -> next;
- free(auxc);
- }
- wpt -> lines = (void*)(wpi -> next);
- free(wpi);
- }
- }
- else if(wpt -> type == 1)
- {
- t_floatLine *wpf;
- t_floatCell *auxc;
- while(wpt -> lines != NULL)
- {
- wpf = (t_floatLine*)(wpt -> lines);
- while((wpf -> cells) != NULL)
- {
- auxc = wpf -> cells;
- wpf -> cells = wpf -> cells -> next;
- free(auxc);
- }
- wpt -> lines = (void*)(wpf -> next);
- free(wpf);
- }
- }
- else if(wpt -> type == 2)
- {
- t_stringLine *wps;
- t_stringCell *auxc;
- while(wpt -> lines != NULL)
- {
- wps = (t_stringLine*)(wpt -> lines);
- while((wps -> cells) != NULL)
- {
- auxc = wps -> cells;
- wps -> cells = wps -> cells -> next;
- free(auxc -> value);
- free(auxc);
- }
- wpt -> lines = (void*)(wps -> next);
- free(wps);
- }
- }
- return;
- }
- int get_instruction_set(char **instruction_set, char *raw_instruction)
- {
- int num_param;
- num_param = 0;
- gets(raw_instruction);
- char *wp = strtok(raw_instruction, " ");
- while(wp != NULL)
- {
- strcpy(instruction_set[num_param], wp);
- instruction_set[num_param][strlen(instruction_set[num_param])] = '\0';
- wp = strtok(NULL, " ");
- num_param++;
- }
- return num_param;
- }
- int main(void)
- {
- int i, j;
- char *raw_instruction = (char*)calloc(MAX_INSTRUCTION_LENGHT, sizeof(char));
- if(!raw_instruction)
- {
- fprintf(stderr, "Error getting instruction set.\n");
- return -1;
- }
- char **instruction_set = (char**)calloc(MAX_INSTRUCTIONS_NUMBER, sizeof(char*));
- if(!instruction_set)
- {
- fprintf(stderr, "Error getting instruction set.\n");
- return -1;
- }
- for(i = 0; i < MAX_INSTRUCTIONS_NUMBER; i++)
- {
- instruction_set[i] = (char*)calloc(MAX_INSTRUCTION_LENGHT, sizeof(char));
- if(!instruction_set[i])
- {
- fprintf(stderr, "Error getting instruction set.\n");
- for(j = 0; j < i; j++)
- free(instruction_set[j]);
- free(instruction_set);
- return -1;
- }
- }
- t_db *database = NULL;
- while(!END_DB)
- {
- int num_param = get_instruction_set(instruction_set, raw_instruction);
- if(num_param == -1)
- {
- return -1;
- }
- if(strcmp(instruction_set[0], "INIT_DB") == 0)
- {
- database = INIT_DB(instruction_set[1]);
- }
- else if(strcmp(instruction_set[0], "CREATE") == 0)
- {
- if(strcmp(instruction_set[2], "INT") == 0)
- {
- CREATE_TABLE(database, instruction_set, num_param, INT);
- }
- else if(strcmp(instruction_set[2], "FLOAT") == 0)
- {
- CREATE_TABLE(database, instruction_set, num_param, FLOAT);
- }
- else if(strcmp(instruction_set[2], "STRING") == 0)
- {
- CREATE_TABLE(database, instruction_set, num_param, STRING);
- }
- else
- {
- printf("Unknown data type: \"%s\"\n", instruction_set[2]);
- }
- }
- else if(strcmp(instruction_set[0], "ADD") == 0)
- {
- ADD_IN_TABLE(database, instruction_set, num_param);
- }
- else if(strcmp(instruction_set[0], "PRINT") == 0)
- {
- PRINT_TABLE(database, instruction_set[1]);
- }
- else if(strcmp(instruction_set[0], "SEARCH") == 0)
- {
- SEARCH_IN_TABLE(database, instruction_set);
- }
- else if(strcmp(instruction_set[0], "DELETE") == 0 && num_param > 2)
- {
- DELETE_IN_TABLE(database, instruction_set);
- }
- else if(strcmp(instruction_set[0], "DELETE") == 0 && !(num_param > 2))
- {
- DELETE_TABLE(database, instruction_set[1]);
- }
- else if(strcmp(instruction_set[0], "CLEAR") == 0)
- {
- CLEAR_TABLE(database, instruction_set[1]);
- }
- else if(strcmp(instruction_set[0], "PRINT_DB") == 0)
- {
- PRINT_DB(database);
- }
- else if(!END_DB)
- {
- printf("Unknown command: \"%s\".\n", raw_instruction);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement