Advertisement
Guest User

dawasd

a guest
Mar 31st, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.14 KB | None | 0 0
  1. /*VRABIE-DINU DARIAN 313CB*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #include "structures.h"
  7.  
  8. #define SINGULAR 1
  9. #define MAX_INSTRUCTION_LENGHT 50
  10. #define MAX_INSTRUCTIONS_NUMBER 20
  11. #define MAX_STATUS_LENGHT 256
  12. #define FLOAT_PRECISION 7
  13.  
  14. #define END_DB (strcmp(instruction_set[0], "DELETE_DB") == 0)
  15. #define BLANK_SPACE(len) (30 - len)
  16.  
  17. int CONV_STRING_TO_INT(char *str)
  18. {
  19. int i, len = strlen(str);
  20. int num = 0;
  21.  
  22. if(str[0] == '-')
  23. {
  24. num = num * 10 + ((int)(str[1]) - 48);
  25. i = 2;
  26. }
  27. else
  28. {
  29. num = num * 10 + ((int)(str[0]) - 48);
  30. i = 1;
  31. }
  32.  
  33. for(; i < len; i++)
  34. {
  35. num = num * 10 + ((int)(str[i]) - 48);
  36. }
  37.  
  38. if(str[0] == '-')
  39. num *= -1;
  40.  
  41. return num;
  42. }
  43.  
  44. float CONV_STRING_TO_FLOAT(char *str)
  45. {
  46. int i, len = strlen(str);
  47. float num = 0.0, p = 10.0;
  48.  
  49. if(str[0] == '-')
  50. {
  51. num = num * 10 + ((int)(str[1]) - 48);
  52. i = 2;
  53. }
  54. else
  55. {
  56. num = num * 10 + ((int)(str[0]) - 48);
  57. i = 1;
  58. }
  59.  
  60. for(; i < len; i++)
  61. {
  62. if(str[i] == '.')
  63. {
  64. i++;
  65. break;
  66. }
  67.  
  68. num = num * 10.0 + ((int)(str[i]) - 48.0);
  69. }
  70. for(; i < len; i++)
  71. {
  72. num = num + (((int)(str[i]) - 48.0) / p);
  73. p *= 10.0;
  74. }
  75.  
  76. if(str[0] == '-')
  77. num *= -1;
  78.  
  79. return num;
  80. }
  81.  
  82. int CHAR_NUM(int val)
  83. {
  84. int len = 0;
  85. while(val != 0)
  86. {
  87. len++;
  88. val /= 10;
  89. }
  90.  
  91. return len;
  92. }
  93.  
  94. t_db *INIT_DB(const char* new_db_name)
  95. {
  96. t_db *new_db = (t_db*)calloc(SINGULAR, sizeof(t_db));
  97. if(!new_db)
  98. {
  99. fprintf(stderr, "Error initializing database.\n");
  100. return NULL;
  101. }
  102.  
  103. strcpy(new_db -> name, new_db_name);
  104.  
  105. new_db -> tables = NULL;
  106.  
  107. return new_db;
  108. }
  109.  
  110. t_column *CREATE_COLUMN(char* name)
  111. {
  112. t_column *new_col = (t_column*)calloc(SINGULAR, sizeof(t_column));
  113. if(!new_col)
  114. return NULL;
  115.  
  116. strcpy(new_col -> name, name);
  117. new_col -> name[strlen(new_col -> name)] = '\0';
  118.  
  119. new_col -> next = NULL;
  120.  
  121. return new_col;
  122. }
  123.  
  124. void INSERT_COLUMN(t_table *table, t_column *column)
  125. {
  126. t_column *wp;
  127.  
  128. if(table -> columns == NULL)
  129. {
  130. table -> columns = column;
  131. return;
  132. }
  133.  
  134.  
  135. for(wp = table -> columns; wp -> next != NULL; wp = wp -> next);
  136. wp -> next = column;
  137.  
  138. return;
  139. }
  140.  
  141. void *CREATE_LINE(char **instruction_set, int num_param, unsigned int type)
  142. {
  143. int i;
  144. if(type == 0)
  145. {
  146. t_intLine *new_int_line = (t_intLine*)calloc(SINGULAR, sizeof(t_intLine));
  147. if(!new_int_line)
  148. return NULL;
  149.  
  150. new_int_line -> cells = (t_intCell*)calloc(SINGULAR, sizeof(t_intCell));
  151. if(!(new_int_line -> cells))
  152. {
  153. free(new_int_line);
  154. return NULL;
  155. }
  156.  
  157. new_int_line -> next = NULL;
  158.  
  159. t_intCell *last = new_int_line -> cells;
  160.  
  161. for(i = 2; i < num_param; i++)
  162. {
  163. last -> value = CONV_STRING_TO_INT(instruction_set[i]);
  164. if(i != num_param - 1)
  165. {
  166. last -> next = (t_intCell*)calloc(SINGULAR, sizeof(t_intCell));
  167. if(!(last -> next))
  168. return NULL;
  169.  
  170. last = last -> next;
  171. last -> next = NULL;
  172. }
  173. else
  174. {
  175. last -> next = NULL;
  176. }
  177. }
  178.  
  179. return (void*)(new_int_line);
  180. }
  181. else if(type == 1)
  182. {
  183. t_floatLine *new_float_line = (t_floatLine*)calloc(SINGULAR, sizeof(t_floatLine));
  184. if(!new_float_line)
  185. return NULL;
  186.  
  187. new_float_line -> cells = (t_floatCell*)calloc(SINGULAR, sizeof(t_floatCell));
  188. if(!(new_float_line -> cells))
  189. {
  190. free(new_float_line);
  191. return NULL;
  192. }
  193.  
  194. new_float_line -> next = NULL;
  195.  
  196. t_floatCell *last = new_float_line -> cells;
  197.  
  198. for(i = 2; i < num_param; i++)
  199. {
  200. last -> value = CONV_STRING_TO_FLOAT(instruction_set[i]);
  201. if(i != num_param - 1)
  202. {
  203. last -> next = (t_floatCell*)calloc(SINGULAR, sizeof(t_floatCell));
  204. if(!(last -> next))
  205. return NULL;
  206.  
  207. last = last -> next;
  208. last -> next = NULL;
  209. }
  210. else
  211. {
  212. last -> next = NULL;
  213. }
  214. }
  215.  
  216. return (void*)(new_float_line);
  217. }
  218. else if(type == 2)
  219. {
  220. t_stringLine *new_string_line = (t_stringLine*)calloc(SINGULAR, sizeof(t_stringLine));
  221. if(!new_string_line)
  222. return NULL;
  223.  
  224. new_string_line -> cells = (t_stringCell*)calloc(SINGULAR, sizeof(t_stringCell));
  225. if(!(new_string_line -> cells))
  226. {
  227. free(new_string_line);
  228. return NULL;
  229. }
  230.  
  231. new_string_line -> next = NULL;
  232.  
  233. t_stringCell *last = new_string_line -> cells;
  234.  
  235. for(i = 2; i < num_param; i++)
  236. {
  237. last -> value = strdup(instruction_set[i]);
  238. if(i != num_param - 1)
  239. {
  240. last -> next = (t_stringCell*)calloc(SINGULAR, sizeof(t_stringCell));
  241. if(!(last -> next))
  242. return NULL;
  243.  
  244. last = last -> next;
  245. last -> next = NULL;
  246. }
  247. else
  248. {
  249. last -> next = NULL;
  250. }
  251.  
  252. }
  253.  
  254. return (void*)(new_string_line);
  255. }
  256.  
  257. return NULL;
  258. }
  259.  
  260. int TABLE_EXISTS(t_db *db, char *table_name)
  261. {
  262. if(db -> tables == NULL)
  263. return 0;
  264.  
  265. t_table *wp;
  266. for(wp = (db -> tables); wp != NULL; wp = (wp -> next))
  267. {
  268. if(strcmp(wp -> name, table_name) == 0)
  269. return 1;
  270. }
  271.  
  272. return 0;
  273. }
  274.  
  275. void INSERT_TABLE(t_db *db, t_table *table)
  276. {
  277. t_table *wp;
  278.  
  279. if(db -> tables == NULL)
  280. {
  281. db -> tables = table;
  282. return;
  283. }
  284.  
  285.  
  286. for(wp = db -> tables; wp -> next != NULL; wp = wp -> next);
  287. wp -> next = table;
  288.  
  289. return;
  290. }
  291.  
  292. char CREATE_TABLE(t_db *db, char **instruction_set, int num_param, t_cellType type)
  293. {
  294. if(db == NULL)
  295. return 'n';
  296.  
  297. int i;
  298.  
  299. if(TABLE_EXISTS(db, instruction_set[1]))
  300. {
  301. return 'a';
  302. }
  303. else
  304. {
  305. t_table *new_table = (t_table*)calloc(SINGULAR, sizeof(t_table));
  306. if(!new_table)
  307. {
  308. return 'n';
  309. }
  310.  
  311. strcpy(new_table -> name, instruction_set[1]);
  312. new_table -> type = type;
  313. new_table -> lines = NULL;
  314. new_table -> next = NULL;
  315.  
  316. //columns
  317. for(i = 3; i < num_param; i++)
  318. {
  319. t_column *new_col = CREATE_COLUMN(instruction_set[i]);
  320. if(!new_col)
  321. {
  322. return 'n';
  323. }
  324.  
  325. INSERT_COLUMN(new_table, new_col);
  326. }
  327.  
  328. INSERT_TABLE(db, new_table);
  329. }
  330.  
  331. return 'k';
  332. }
  333.  
  334. void PRINT_SEPARATOR_ROW(int colmun_count)
  335. {
  336. int i;
  337. for(i = 0; i < colmun_count; i++)
  338. {
  339. printf("------------------------------");
  340. if(i != (colmun_count - 1))
  341. printf(" ");
  342. else
  343. {
  344. printf("\n");
  345. }
  346. }
  347. }
  348.  
  349. void ADD_IN_TABLE(t_db *db, char **instruction_set, int num_param)
  350. {
  351. t_table *wpt;
  352.  
  353. for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
  354. {
  355. if(strcmp(wpt-> name, instruction_set[1]) == 0)
  356. break;
  357. }
  358.  
  359. if(wpt == NULL)
  360. {
  361. printf("Table \"%s\" not found in database.\n", instruction_set[1]);
  362. return;
  363. }
  364.  
  365. unsigned int type = (unsigned int)(wpt -> type);
  366.  
  367. void *new_line = CREATE_LINE(instruction_set, num_param, type);
  368.  
  369. if(type == 0)
  370. {
  371. t_intLine *new_int_line = (t_intLine*)new_line;
  372. new_int_line -> next = NULL;
  373.  
  374. t_intLine *wpi;
  375.  
  376. if((t_intLine*)(wpt -> lines) == NULL)
  377. {
  378. wpt -> lines = new_int_line;
  379. return;
  380. }
  381.  
  382. for(wpi = (t_intLine*)(wpt -> lines); (t_intLine*)(wpi -> next) != NULL; wpi = (t_intLine*)(wpi -> next))
  383. {
  384. continue;
  385. }
  386.  
  387.  
  388. wpi -> next = new_int_line;
  389. }
  390. else if(type == 1)
  391. {
  392. t_floatLine *new_float_line = (t_floatLine*)new_line;
  393. new_float_line -> next = NULL;
  394.  
  395. t_floatLine *wpf;
  396.  
  397. if((t_floatLine*)(wpt -> lines) == NULL)
  398. {
  399. wpt -> lines = new_float_line;
  400. return;
  401. }
  402.  
  403. for(wpf = (t_floatLine*)(wpt -> lines); (t_floatLine*)(wpf -> next) != NULL; wpf = (t_floatLine*)(wpf -> next))
  404. {
  405. continue;
  406. }
  407.  
  408. wpf -> next = new_float_line;
  409. }
  410. else if(type == 2)
  411. {
  412. t_stringLine *new_string_line = (t_stringLine*)new_line;
  413. new_string_line -> next = NULL;
  414.  
  415. t_stringLine *wps;
  416.  
  417. if((t_stringLine*)(wpt -> lines) == NULL)
  418. {
  419. wpt -> lines = new_string_line;
  420. return;
  421. }
  422.  
  423. for(wps = (t_stringLine*)(wpt -> lines); (t_stringLine*)(wps -> next) != NULL; wps = (t_stringLine*)(wps -> next))
  424. {
  425. continue;
  426. }
  427.  
  428. wps -> next = new_string_line;
  429. }
  430.  
  431. return;
  432. }
  433.  
  434. void PRINT_TABLE(t_db *db, char* table_name)
  435. {
  436. int i, len, colmun_count = 0;
  437. t_table *wpt;
  438. t_column *wpc;
  439.  
  440. for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
  441. {
  442. if(strcmp(wpt -> name, table_name) == 0)
  443. {
  444. printf("TABLE: %s\n", wpt -> name);
  445. for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
  446. {
  447. printf("%s", wpc -> name);
  448. len = strlen(wpc -> name);
  449.  
  450. for(i = 0; i <= BLANK_SPACE(len); i++)
  451. printf(" ");
  452.  
  453. colmun_count++;
  454. }
  455. printf("\n");
  456.  
  457. for(i = 0; i < colmun_count; i++)
  458. {
  459. printf("------------------------------");
  460. if(i != (colmun_count - 1))
  461. printf(" ");
  462. else
  463. {
  464. printf("\n");
  465. }
  466. }
  467.  
  468. if(wpt -> type == 0)
  469. {
  470. t_intLine *wpi;
  471. t_intCell *wpic;
  472. for(wpi = (t_intLine*)(wpt -> lines); (t_intLine*)(wpi) != NULL; wpi = (t_intLine*)(wpi -> next))
  473. {
  474. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  475. {
  476. printf("%d", wpic -> value);
  477.  
  478. if(wpic -> next != NULL)
  479. {
  480. len = CHAR_NUM(wpic -> value);
  481. if(wpic -> value < 0)
  482. len++;
  483.  
  484. for(i = 0; i <= BLANK_SPACE(len); i++)
  485. printf(" ");
  486. }
  487. }
  488. printf("\n");
  489. }
  490. }
  491. else if(wpt -> type == 1)
  492. {
  493. t_floatLine *wpf;
  494. t_floatCell *wpfc;
  495. for(wpf = (t_floatLine*)(wpt -> lines); (t_floatLine*)(wpf) != NULL; wpf = (t_floatLine*)(wpf -> next))
  496. {
  497. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  498. {
  499. printf("%f", wpfc -> value);
  500. if(wpfc -> next != NULL)
  501. {
  502. len = CHAR_NUM((int)(wpfc -> value)) + FLOAT_PRECISION;
  503. if(wpfc -> value < 0)
  504. len++;
  505.  
  506. for(i = 0; i <= BLANK_SPACE(len); i++)
  507. printf(" ");
  508. }
  509. }
  510. printf("\n");
  511. }
  512. }
  513. else if(wpt -> type == 2)
  514. {
  515. t_stringLine *wps;
  516. t_stringCell *wpsc;
  517. for(wps = (t_stringLine*)(wpt -> lines); (t_stringLine*)(wps) != NULL; wps = (t_stringLine*)(wps -> next))
  518. {
  519. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  520. {
  521. printf("%s", wpsc -> value);
  522. if(wpsc -> next != NULL)
  523. {
  524. len = strlen(wpsc -> value);
  525.  
  526. for(i = 0; i <= BLANK_SPACE(len); i++)
  527. printf(" ");
  528. }
  529. }
  530. printf("\n");
  531. }
  532. }
  533. printf("\n");
  534. break;
  535. }
  536. }
  537.  
  538. if(wpt == NULL)
  539. printf("Table \"%s\" not found in database.\n", table_name);
  540. }
  541.  
  542. void PRINT_DB(t_db *db)
  543. {
  544. printf("DATABASE: %s\n\n", db -> name);
  545.  
  546. t_table *wpt;
  547. t_column *wpc;
  548. int i, len, colmun_count;
  549.  
  550. for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
  551. {
  552. colmun_count = 0;
  553.  
  554. printf("TABLE: %s\n", wpt -> name);
  555. for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
  556. {
  557. printf("%s", wpc -> name);
  558. len = strlen(wpc -> name);
  559.  
  560. for(i = 0; i <= BLANK_SPACE(len); i++)
  561. printf(" ");
  562.  
  563. colmun_count++;
  564. }
  565. printf("\n");
  566.  
  567. PRINT_SEPARATOR_ROW(colmun_count);
  568.  
  569. if(wpt -> type == 0)
  570. {
  571. t_intLine *wpi;
  572. t_intCell *wpic;
  573. for(wpi = (t_intLine*)(wpt -> lines); (t_intLine*)(wpi) != NULL; wpi = (t_intLine*)(wpi -> next))
  574. {
  575. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  576. {
  577. printf("%d", wpic -> value);
  578.  
  579. if(wpic -> next != NULL)
  580. {
  581. len = CHAR_NUM(wpic -> value);
  582. for(i = 0; i <= BLANK_SPACE(len); i++)
  583. printf(" ");
  584. }
  585. }
  586. printf("\n");
  587. }
  588. }
  589. else if(wpt -> type == 1)
  590. {
  591. t_floatLine *wpf;
  592. t_floatCell *wpfc;
  593. for(wpf = (t_floatLine*)(wpt -> lines); (t_floatLine*)(wpf) != NULL; wpf = (t_floatLine*)(wpf -> next))
  594. {
  595. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  596. {
  597. printf("%f", wpfc -> value);
  598. if(wpfc -> next != NULL)
  599. {
  600. len = CHAR_NUM((int)(wpfc -> value)) + FLOAT_PRECISION;
  601. for(i = 0; i <= BLANK_SPACE(len); i++)
  602. printf(" ");
  603. }
  604. }
  605. printf("\n");
  606. }
  607. }
  608. else if(wpt -> type == 2)
  609. {
  610. t_stringLine *wps;
  611. t_stringCell *wpsc;
  612. for(wps = (t_stringLine*)(wpt -> lines); (t_stringLine*)(wps) != NULL; wps = (t_stringLine*)(wps -> next))
  613. {
  614. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  615. {
  616. printf("%s", wpsc -> value);
  617. if(wpsc -> next != NULL)
  618. {
  619. len = strlen(wpsc -> value);
  620. for(i = 0; i <= BLANK_SPACE(len); i++)
  621. printf(" ");
  622. }
  623. }
  624. printf("\n");
  625. }
  626. }
  627. printf("\n");
  628. }
  629. }
  630.  
  631. void SEARCH_IN_TABLE(t_db *db, char **instruction_set)
  632. {
  633. int colmun_count = 0, relevant_column = 0;
  634. int icc, len, i;
  635. t_table *wpt, *relevant_table;
  636. t_column *wpc;
  637. for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
  638. {
  639. if(strcmp(wpt -> name, instruction_set[1]) == 0)
  640. {
  641. relevant_table = wpt;
  642.  
  643. printf("TABLE: %s\n", wpt -> name);
  644. for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
  645. {
  646. printf("%s", wpc -> name);
  647. len = strlen(wpc -> name);
  648.  
  649. for(i = 0; i <= BLANK_SPACE(len); i++)
  650. printf(" ");
  651.  
  652. colmun_count++;
  653. }
  654. break;
  655. }
  656. }
  657.  
  658. if(wpt == NULL)
  659. {
  660. printf("Table \"%s\" not found in database.\n", instruction_set[1]);
  661. return;
  662. }
  663.  
  664. for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
  665. {
  666. if(strcmp(wpc -> name, instruction_set[2]) == 0)
  667. {
  668. relevant_column++;
  669. break;
  670. }
  671. relevant_column++;
  672. }
  673.  
  674. if(wpc == NULL)
  675. {
  676. printf("Table \"%s\" does not contain column \"%s\".\n", wpt -> name, instruction_set[2]);
  677. return;
  678. }
  679.  
  680. printf("\n");
  681. PRINT_SEPARATOR_ROW(colmun_count);
  682.  
  683. if(relevant_table -> type == 0)
  684. {
  685. t_intLine *wpi;
  686. t_intCell *wpic;
  687. int cmp_val = CONV_STRING_TO_INT(instruction_set[4]);
  688.  
  689. for(wpi = (t_intLine*)(relevant_table -> lines); wpi != NULL; wpi = wpi -> next)
  690. {
  691. icc = 0;
  692. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  693. {
  694. icc++;
  695. if(icc == relevant_column)
  696. {
  697. if(strcmp("<", instruction_set[3]) == 0)
  698. {
  699. if(wpic -> value < cmp_val)
  700. {
  701. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  702. {
  703. printf("%d", wpic -> value);
  704.  
  705. if(wpic -> next != NULL)
  706. {
  707. len = CHAR_NUM(wpic -> value);
  708. if(wpic -> value < 0)
  709. len++;
  710.  
  711. for(i = 0; i <= BLANK_SPACE(len); i++)
  712. printf(" ");
  713. }
  714. }
  715. printf("\n");
  716. }
  717. break;
  718. }
  719. else if(strcmp("<=", instruction_set[3]) == 0)
  720. {
  721. if(wpic -> value <= cmp_val)
  722. {
  723. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  724. {
  725. printf("%d", wpic -> value);
  726.  
  727. if(wpic -> next != NULL)
  728. {
  729. len = CHAR_NUM(wpic -> value);
  730. if(wpic -> value < 0)
  731. len++;
  732.  
  733. for(i = 0; i <= BLANK_SPACE(len); i++)
  734. printf(" ");
  735. }
  736. }
  737. printf("\n");
  738. }
  739. break;
  740. }
  741. else if(strcmp("==", instruction_set[3]) == 0)
  742. {
  743. if(wpic -> value == cmp_val)
  744. {
  745. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  746. {
  747. printf("%d", wpic -> value);
  748.  
  749. if(wpic -> next != NULL)
  750. {
  751. len = CHAR_NUM(wpic -> value);
  752. if(wpic -> value < 0)
  753. len++;
  754.  
  755. for(i = 0; i <= BLANK_SPACE(len); i++)
  756. printf(" ");
  757. }
  758. }
  759. printf("\n");
  760. }
  761. break;
  762. }
  763. else if(strcmp("!=", instruction_set[3]) == 0)
  764. {
  765. if(wpic -> value != cmp_val)
  766. {
  767. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  768. {
  769. printf("%d", wpic -> value);
  770.  
  771. if(wpic -> next != NULL)
  772. {
  773. len = CHAR_NUM(wpic -> value);
  774. if(wpic -> value < 0)
  775. len++;
  776.  
  777. for(i = 0; i <= BLANK_SPACE(len); i++)
  778. printf(" ");
  779. }
  780. }
  781. printf("\n");
  782. }
  783. break;
  784. }
  785. else if(strcmp(">=", instruction_set[3]) == 0)
  786. {
  787. if(wpic -> value >= cmp_val)
  788. {
  789. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  790. {
  791. printf("%d", wpic -> value);
  792.  
  793. if(wpic -> next != NULL)
  794. {
  795. len = CHAR_NUM(wpic -> value);
  796. if(wpic -> value < 0)
  797. len++;
  798.  
  799. for(i = 0; i <= BLANK_SPACE(len); i++)
  800. printf(" ");
  801. }
  802. }
  803. printf("\n");
  804. }
  805. break;
  806. }
  807. else if(strcmp(">", instruction_set[3]) == 0)
  808. {
  809. if(wpic -> value > cmp_val)
  810. {
  811. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  812. {
  813. printf("%d", wpic -> value);
  814.  
  815. if(wpic -> next != NULL)
  816. {
  817. len = CHAR_NUM(wpic -> value);
  818. if(wpic -> value < 0)
  819. len++;
  820.  
  821. for(i = 0; i <= BLANK_SPACE(len); i++)
  822. printf(" ");
  823. }
  824. }
  825. printf("\n");
  826. }
  827. break;
  828. }
  829. }
  830. }
  831. }
  832. return;
  833. }
  834. else if(relevant_table -> type == 1)
  835. {
  836. t_floatLine *wpf;
  837. t_floatCell *wpfc;
  838. float cmp_val = CONV_STRING_TO_FLOAT(instruction_set[4]);
  839.  
  840. for(wpf = (t_floatLine*)(relevant_table -> lines); wpf != NULL; wpf = wpf -> next)
  841. {
  842. icc = 0;
  843. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  844. {
  845. icc++;
  846. if(icc == relevant_column)
  847. {
  848. if(strcmp("<", instruction_set[3]) == 0)
  849. {
  850. if(wpfc -> value < cmp_val)
  851. {
  852. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  853. {
  854. printf("%f", wpfc -> value);
  855.  
  856. if(wpfc -> next != NULL)
  857. {
  858. len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
  859. if(wpfc -> value < 0)
  860. len++;
  861.  
  862. for(i = 0; i <= BLANK_SPACE(len); i++)
  863. printf(" ");
  864. }
  865. }
  866. printf("\n");
  867. }
  868. break;
  869. }
  870. else if(strcmp("<=", instruction_set[3]) == 0)
  871. {
  872. if(wpfc -> value <= cmp_val)
  873. {
  874. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  875. {
  876. printf("%f", wpfc -> value);
  877.  
  878. if(wpfc -> next != NULL)
  879. {
  880. len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
  881. if(wpfc -> value < 0)
  882. len++;
  883.  
  884. for(i = 0; i <= BLANK_SPACE(len); i++)
  885. printf(" ");
  886. }
  887. }
  888. printf("\n");
  889. }
  890. break;
  891. }
  892. else if(strcmp("==", instruction_set[3]) == 0)
  893. {
  894. if(wpfc -> value == cmp_val)
  895. {
  896. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  897. {
  898. printf("%f", wpfc -> value);
  899.  
  900. if(wpfc -> next != NULL)
  901. {
  902. len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
  903. if(wpfc -> value < 0)
  904. len++;
  905.  
  906. for(i = 0; i <= BLANK_SPACE(len); i++)
  907. printf(" ");
  908. }
  909. }
  910. printf("\n");
  911. }
  912. break;
  913. }
  914. else if(strcmp("!=", instruction_set[3]) == 0)
  915. {
  916. if(wpfc -> value != cmp_val)
  917. {
  918. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  919. {
  920. printf("%f", wpfc -> value);
  921.  
  922. if(wpfc -> next != NULL)
  923. {
  924. len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
  925. if(wpfc -> value < 0)
  926. len++;
  927.  
  928. for(i = 0; i <= BLANK_SPACE(len); i++)
  929. printf(" ");
  930. }
  931. }
  932. printf("\n");
  933. }
  934. break;
  935. }
  936. else if(strcmp(">=", instruction_set[3]) == 0)
  937. {
  938. if(wpfc -> value >= cmp_val)
  939. {
  940. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  941. {
  942. printf("%f", wpfc -> value);
  943.  
  944. if(wpfc -> next != NULL)
  945. {
  946. len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
  947. if(wpfc -> value < 0)
  948. len++;
  949.  
  950. for(i = 0; i <= BLANK_SPACE(len); i++)
  951. printf(" ");
  952. }
  953. }
  954. printf("\n");
  955. }
  956. break;
  957. }
  958. else if(strcmp(">", instruction_set[3]) == 0)
  959. {
  960. if(wpfc -> value > cmp_val)
  961. {
  962. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  963. {
  964. printf("%f", wpfc -> value);
  965.  
  966. if(wpfc -> next != NULL)
  967. {
  968. len = CHAR_NUM(wpfc -> value) + FLOAT_PRECISION;
  969. if(wpfc -> value < 0)
  970. len++;
  971.  
  972. for(i = 0; i <= BLANK_SPACE(len); i++)
  973. printf(" ");
  974. }
  975. }
  976. printf("\n");
  977. }
  978. break;
  979. }
  980. }
  981. }
  982. }
  983. return;
  984. }
  985. else if(relevant_table -> type == 2)
  986. {
  987. t_stringLine *wps;
  988. t_stringCell *wpsc;
  989. char* cmp_val = strdup(instruction_set[4]);
  990.  
  991. for(wps = (t_stringLine*)(relevant_table -> lines); wps != NULL; wps = wps -> next)
  992. {
  993. icc = 0;
  994. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  995. {
  996. icc++;
  997. if(icc == relevant_column)
  998. {
  999. if(strcmp("<", instruction_set[3]) == 0)
  1000. {
  1001. if(strcmp(wpsc -> value, cmp_val) < 0)
  1002. {
  1003. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  1004. {
  1005. printf("%s", wpsc -> value);
  1006.  
  1007. if(wpsc -> next != NULL)
  1008. {
  1009. len = strlen(wpsc -> value);
  1010.  
  1011. for(i = 0; i <= BLANK_SPACE(len); i++)
  1012. printf(" ");
  1013. }
  1014. }
  1015. printf("\n");
  1016. }
  1017. break;
  1018. }
  1019. else if(strcmp("<=", instruction_set[3]) == 0)
  1020. {
  1021. if(strcmp(wpsc -> value, cmp_val) <= 0)
  1022. {
  1023. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  1024. {
  1025. printf("%s", wpsc -> value);
  1026.  
  1027. if(wpsc -> next != NULL)
  1028. {
  1029. len = strlen(wpsc -> value);
  1030.  
  1031. for(i = 0; i <= BLANK_SPACE(len); i++)
  1032. printf(" ");
  1033. }
  1034. }
  1035. printf("\n");
  1036. }
  1037. break;
  1038. }
  1039. else if(strcmp("==", instruction_set[3]) == 0)
  1040. {
  1041. if(strcmp(wpsc -> value, cmp_val) == 0)
  1042. {
  1043. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  1044. {
  1045. printf("%s", wpsc -> value);
  1046.  
  1047. if(wpsc -> next != NULL)
  1048. {
  1049. len = strlen(wpsc -> value);
  1050.  
  1051. for(i = 0; i <= BLANK_SPACE(len); i++)
  1052. printf(" ");
  1053. }
  1054. }
  1055. printf("\n");
  1056. }
  1057. break;
  1058. }
  1059. else if(strcmp("!=", instruction_set[3]) == 0)
  1060. {
  1061. if(strcmp(wpsc -> value, cmp_val) != 0)
  1062. {
  1063. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  1064. {
  1065. printf("%s", wpsc -> value);
  1066.  
  1067. if(wpsc -> next != NULL)
  1068. {
  1069. len = strlen(wpsc -> value);
  1070.  
  1071. for(i = 0; i <= BLANK_SPACE(len); i++)
  1072. printf(" ");
  1073. }
  1074. }
  1075. printf("\n");
  1076. }
  1077. break;
  1078. }
  1079. else if(strcmp(">=", instruction_set[3]) == 0)
  1080. {
  1081. if(strcmp(wpsc -> value, cmp_val) >= 0)
  1082. {
  1083. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  1084. {
  1085. printf("%s", wpsc -> value);
  1086.  
  1087. if(wpsc -> next != NULL)
  1088. {
  1089. len = strlen(wpsc -> value);
  1090.  
  1091. for(i = 0; i <= BLANK_SPACE(len); i++)
  1092. printf(" ");
  1093. }
  1094. }
  1095. printf("\n");
  1096. }
  1097. break;
  1098. }
  1099. else if(strcmp(">", instruction_set[3]) == 0)
  1100. {
  1101. if(strcmp(wpsc -> value, cmp_val) > 0)
  1102. {
  1103. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  1104. {
  1105. printf("%s", wpsc -> value);
  1106.  
  1107. if(wpsc -> next != NULL)
  1108. {
  1109. len = strlen(wpsc -> value);
  1110.  
  1111. for(i = 0; i <= BLANK_SPACE(len); i++)
  1112. printf(" ");
  1113. }
  1114. }
  1115. printf("\n");
  1116. }
  1117. break;
  1118. }
  1119. }
  1120. }
  1121. }
  1122. return;
  1123. }
  1124.  
  1125. return;
  1126. }
  1127.  
  1128. void DELETE_IN_TABLE(t_db *db, char **instruction_set)
  1129. {
  1130. int colmun_count = 0, icc;
  1131. char flag;
  1132. t_table *wpt;
  1133. t_column *wpc;
  1134. for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
  1135. {
  1136. if(strcmp(wpt -> name, instruction_set[1]) == 0)
  1137. {
  1138. break;
  1139. }
  1140. }
  1141.  
  1142. if(wpt == NULL)
  1143. {
  1144. printf("Table \"%s\" not found in database.\n", instruction_set[1]);
  1145. return;
  1146. }
  1147.  
  1148. for(wpc = wpt -> columns; wpc != NULL; wpc = wpc -> next)
  1149. {
  1150. if(strcmp(wpc -> name, instruction_set[2]) == 0)
  1151. {
  1152. colmun_count++;
  1153. break;
  1154. }
  1155. colmun_count++;
  1156. }
  1157.  
  1158. if(wpc == NULL)
  1159. {
  1160. printf("Table \"%s\" does not contain column \"%s\".\n", wpt -> name, instruction_set[2]);
  1161. return;
  1162. }
  1163.  
  1164. if(wpt -> type == 0)
  1165. {
  1166. t_intLine *wpi, *previ = NULL;
  1167. void *auxl;
  1168. t_intCell *wpic, *auxc;
  1169. int cmp_val = CONV_STRING_TO_INT(instruction_set[4]);
  1170.  
  1171. for(wpi = (t_intLine*)(wpt -> lines); wpi != NULL;)
  1172. {
  1173. icc = 0;
  1174. for(wpic = wpi -> cells; wpic != NULL; wpic = wpic -> next)
  1175. {
  1176. flag = 0;
  1177. icc++;
  1178. if(icc == colmun_count)
  1179. {
  1180. if(strcmp("<", instruction_set[3]) == 0)
  1181. {
  1182. if(wpic -> value < cmp_val)
  1183. {
  1184. flag = 1;
  1185. break;
  1186. }
  1187. }
  1188. else if(strcmp("<=", instruction_set[3]) == 0)
  1189. {
  1190. if(wpic -> value <= cmp_val)
  1191. {
  1192. flag = 1;
  1193. break;
  1194. }
  1195. }
  1196. else if(strcmp("==", instruction_set[3]) == 0)
  1197. {
  1198. if(wpic -> value == cmp_val)
  1199. {
  1200. flag = 1;
  1201. break;
  1202. }
  1203. }
  1204. else if(strcmp("!=", instruction_set[3]) == 0)
  1205. {
  1206. if(wpic -> value != cmp_val)
  1207. {
  1208. flag = 1;
  1209. break;
  1210. }
  1211. }
  1212. else if(strcmp(">=", instruction_set[3]) == 0)
  1213. {
  1214. if(wpic -> value >= cmp_val)
  1215. {
  1216. flag = 1;
  1217. break;
  1218. }
  1219. }
  1220. else if(strcmp(">", instruction_set[3]) == 0)
  1221. {
  1222. if(wpic -> value > cmp_val)
  1223. {
  1224. flag = 1;
  1225. break;
  1226. }
  1227. }
  1228. }
  1229. }
  1230.  
  1231. if(flag == 1)
  1232. {
  1233. while(wpi -> cells != NULL)
  1234. {
  1235. auxc = wpi -> cells;
  1236. wpi -> cells = wpi -> cells -> next;
  1237. free(auxc);
  1238. }
  1239.  
  1240. if(previ == NULL)
  1241. {
  1242. auxl = (void*)wpi;
  1243. wpt -> lines = (void*)(wpi -> next);
  1244. wpi = (t_intLine*)(wpt -> lines);
  1245. free(auxl);
  1246. }
  1247. else
  1248. {
  1249. auxl = (void*)wpi;
  1250. previ -> next = wpi -> next;
  1251. wpi = wpi -> next;
  1252. free(auxl);
  1253. }
  1254. }
  1255. else if(flag == 0)
  1256. {
  1257. previ = wpi;
  1258. wpi = wpi -> next;
  1259. }
  1260. }
  1261.  
  1262. }
  1263. else if(wpt -> type == 1)
  1264. {
  1265. t_floatLine *wpf, *auxl, *prevf = NULL;
  1266. t_floatCell *wpfc, *auxc;
  1267. float cmp_val = CONV_STRING_TO_FLOAT(instruction_set[4]);
  1268.  
  1269. for(wpf = (t_floatLine*)(wpt -> lines); wpf != NULL;)
  1270. {
  1271. icc = 0;
  1272. for(wpfc = wpf -> cells; wpfc != NULL; wpfc = wpfc -> next)
  1273. {
  1274. flag = 0;
  1275. icc++;
  1276. if(icc == colmun_count)
  1277. {
  1278. if(strcmp("<", instruction_set[3]) == 0)
  1279. {
  1280. if(wpfc -> value < cmp_val)
  1281. {
  1282. flag = 1;
  1283. break;
  1284. }
  1285. }
  1286. else if(strcmp("<=", instruction_set[3]) == 0)
  1287. {
  1288. if(wpfc -> value <= cmp_val)
  1289. {
  1290. flag = 1;
  1291. break;
  1292. }
  1293. }
  1294. else if(strcmp("==", instruction_set[3]) == 0)
  1295. {
  1296. if(wpfc -> value == cmp_val)
  1297. {
  1298. flag = 1;
  1299. break;
  1300. }
  1301. }
  1302. else if(strcmp("!=", instruction_set[3]) == 0)
  1303. {
  1304. if(wpfc -> value != cmp_val)
  1305. {
  1306. flag = 1;
  1307. break;
  1308. }
  1309. }
  1310. else if(strcmp(">=", instruction_set[3]) == 0)
  1311. {
  1312. if(wpfc -> value >= cmp_val)
  1313. {
  1314. flag = 1;
  1315. break;
  1316. }
  1317. }
  1318. else if(strcmp(">", instruction_set[3]) == 0)
  1319. {
  1320. if(wpfc -> value > cmp_val)
  1321. {
  1322. flag = 1;
  1323. break;
  1324. }
  1325. }
  1326. }
  1327. }
  1328.  
  1329. if(flag == 1)
  1330. {
  1331. while(wpf -> cells != NULL)
  1332. {
  1333. auxc = wpf -> cells;
  1334. wpf -> cells = wpf -> cells -> next;
  1335. free(auxc);
  1336. }
  1337.  
  1338. if(prevf == NULL)
  1339. {
  1340. auxl = (void*)wpf;
  1341. wpt -> lines = (void*)(wpf -> next);
  1342. wpf = (t_floatLine*)(wpt -> lines);
  1343. free(auxl);
  1344. }
  1345. else
  1346. {
  1347. auxl = (void*)wpf;
  1348. prevf -> next = wpf -> next;
  1349. wpf = wpf -> next;
  1350. free(auxl);
  1351. }
  1352. }
  1353. else if(flag == 0)
  1354. {
  1355. prevf = wpf;
  1356. wpf = wpf -> next;
  1357. }
  1358. }
  1359. return;
  1360. }
  1361. else if(wpt -> type == 2)
  1362. {
  1363. t_stringLine *wps, *auxl, *prevs = NULL;
  1364. t_stringCell *wpsc, *auxc;
  1365. char* cmp_val = strdup(instruction_set[4]);
  1366.  
  1367. for(wps = (t_stringLine*)(wpt -> lines); wps != NULL;)
  1368. {
  1369. icc = 0;
  1370. for(wpsc = wps -> cells; wpsc != NULL; wpsc = wpsc -> next)
  1371. {
  1372. flag = 0;
  1373. icc++;
  1374. if(icc == colmun_count)
  1375. {
  1376. if(strcmp("<", instruction_set[3]) == 0)
  1377. {
  1378. if(strcmp(wpsc -> value, cmp_val) < 0)
  1379. {
  1380. if(strcmp(wpsc -> value, cmp_val) < 0)
  1381. {
  1382. flag = 1;
  1383. break;
  1384. }
  1385. }
  1386. }
  1387. else if(strcmp("<=", instruction_set[3]) == 0)
  1388. {
  1389. if(strcmp(wpsc -> value, cmp_val) <= 0)
  1390. {
  1391. flag = 1;
  1392. break;
  1393. }
  1394. }
  1395. else if(strcmp("==", instruction_set[3]) == 0)
  1396. {
  1397. if(strcmp(wpsc -> value, cmp_val) == 0)
  1398. {
  1399. flag = 1;
  1400. break;
  1401. }
  1402. }
  1403. else if(strcmp("!=", instruction_set[3]) == 0)
  1404. {
  1405. if(strcmp(wpsc -> value, cmp_val) != 0)
  1406. {
  1407. flag = 1;
  1408. break;
  1409. }
  1410. }
  1411. else if(strcmp(">=", instruction_set[3]) == 0)
  1412. {
  1413. if(strcmp(wpsc -> value, cmp_val) >= 0)
  1414. {
  1415. flag = 1;
  1416. break;
  1417. }
  1418. }
  1419. else if(strcmp(">", instruction_set[3]) == 0)
  1420. {
  1421. if(strcmp(wpsc -> value, cmp_val) > 0)
  1422. {
  1423. flag = 1;
  1424. break;
  1425. }
  1426. }
  1427. }
  1428. }
  1429.  
  1430. if(flag == 1)
  1431. {
  1432. while(wps -> cells != NULL)
  1433. {
  1434. auxc = wps -> cells;
  1435. wps -> cells = wps -> cells -> next;
  1436. free(auxc -> value);
  1437. free(auxc);
  1438. }
  1439.  
  1440. if(prevs == NULL)
  1441. {
  1442. auxl = (void*)wps;
  1443. wpt -> lines = (void*)(wps -> next);
  1444. wps = (t_stringLine*)(wpt -> lines);
  1445. free(auxl);
  1446. }
  1447. else
  1448. {
  1449. auxl = (void*)wps;
  1450. prevs -> next = wps -> next;
  1451. wps = wps -> next;
  1452. free(auxl);
  1453. }
  1454. }
  1455. else if(flag == 0)
  1456. {
  1457. prevs = wps;
  1458. wps = wps -> next;
  1459. }
  1460. }
  1461. return;
  1462. }
  1463.  
  1464. return;
  1465. }
  1466.  
  1467. void DELETE_TABLE(t_db *db, char *table_name)
  1468. {
  1469. t_table *wpt, *prevt;
  1470. for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
  1471. {
  1472. if(strcmp(wpt -> name, table_name) == 0)
  1473. {
  1474. if(wpt == db -> tables)
  1475. {
  1476. db -> tables = db -> tables -> next;
  1477. return;
  1478. }
  1479. else
  1480. {
  1481. prevt -> next = wpt -> next;
  1482. return;
  1483. }
  1484. }
  1485. prevt = wpt;
  1486. }
  1487.  
  1488. if(wpt == NULL)
  1489. {
  1490. printf("Table \"%s\" not found in database.\n", table_name);
  1491. return;
  1492. }
  1493. }
  1494.  
  1495. void CLEAR_TABLE(t_db *db, char *table_name)
  1496. {
  1497. t_table *wpt;
  1498. for(wpt = db -> tables; wpt != NULL; wpt = wpt -> next)
  1499. {
  1500. if(strcmp(wpt -> name, table_name) == 0)
  1501. break;
  1502. }
  1503.  
  1504. if(wpt == NULL)
  1505. {
  1506. printf("Table \"%s\" not found in database.\n", table_name);
  1507. return;
  1508. }
  1509.  
  1510. if(wpt -> type == 0)
  1511. {
  1512. t_intLine *wpi;
  1513. t_intCell *auxc;
  1514.  
  1515. while(wpt -> lines != NULL)
  1516. {
  1517. wpi = (t_intLine*)(wpt -> lines);
  1518. while((wpi -> cells) != NULL)
  1519. {
  1520. auxc = wpi -> cells;
  1521. wpi -> cells = wpi -> cells -> next;
  1522. free(auxc);
  1523. }
  1524.  
  1525. wpt -> lines = (void*)(wpi -> next);
  1526. free(wpi);
  1527. }
  1528. }
  1529. else if(wpt -> type == 1)
  1530. {
  1531. t_floatLine *wpf;
  1532. t_floatCell *auxc;
  1533.  
  1534. while(wpt -> lines != NULL)
  1535. {
  1536. wpf = (t_floatLine*)(wpt -> lines);
  1537. while((wpf -> cells) != NULL)
  1538. {
  1539. auxc = wpf -> cells;
  1540. wpf -> cells = wpf -> cells -> next;
  1541. free(auxc);
  1542. }
  1543.  
  1544. wpt -> lines = (void*)(wpf -> next);
  1545. free(wpf);
  1546. }
  1547. }
  1548. else if(wpt -> type == 2)
  1549. {
  1550. t_stringLine *wps;
  1551. t_stringCell *auxc;
  1552.  
  1553. while(wpt -> lines != NULL)
  1554. {
  1555. wps = (t_stringLine*)(wpt -> lines);
  1556. while((wps -> cells) != NULL)
  1557. {
  1558. auxc = wps -> cells;
  1559. wps -> cells = wps -> cells -> next;
  1560. free(auxc -> value);
  1561. free(auxc);
  1562. }
  1563.  
  1564. wpt -> lines = (void*)(wps -> next);
  1565. free(wps);
  1566. }
  1567. }
  1568. return;
  1569. }
  1570.  
  1571. int get_instruction_set(char **instruction_set, char *raw_instruction)
  1572. {
  1573. int num_param;
  1574.  
  1575. num_param = 0;
  1576. gets(raw_instruction);
  1577.  
  1578. char *wp = strtok(raw_instruction, " ");
  1579.  
  1580.  
  1581. while(wp != NULL)
  1582. {
  1583. strcpy(instruction_set[num_param], wp);
  1584. instruction_set[num_param][strlen(instruction_set[num_param])] = '\0';
  1585.  
  1586. wp = strtok(NULL, " ");
  1587.  
  1588. num_param++;
  1589. }
  1590.  
  1591. return num_param;
  1592. }
  1593.  
  1594. int main(void)
  1595. {
  1596. int i, j;
  1597.  
  1598. char *raw_instruction = (char*)calloc(MAX_INSTRUCTION_LENGHT, sizeof(char));
  1599. if(!raw_instruction)
  1600. {
  1601. fprintf(stderr, "Error getting instruction set.\n");
  1602. return -1;
  1603. }
  1604.  
  1605. char **instruction_set = (char**)calloc(MAX_INSTRUCTIONS_NUMBER, sizeof(char*));
  1606. if(!instruction_set)
  1607. {
  1608. fprintf(stderr, "Error getting instruction set.\n");
  1609. return -1;
  1610. }
  1611.  
  1612.  
  1613. for(i = 0; i < MAX_INSTRUCTIONS_NUMBER; i++)
  1614. {
  1615. instruction_set[i] = (char*)calloc(MAX_INSTRUCTION_LENGHT, sizeof(char));
  1616. if(!instruction_set[i])
  1617. {
  1618. fprintf(stderr, "Error getting instruction set.\n");
  1619. for(j = 0; j < i; j++)
  1620. free(instruction_set[j]);
  1621. free(instruction_set);
  1622. return -1;
  1623. }
  1624. }
  1625.  
  1626. t_db *database = NULL;
  1627.  
  1628. while(!END_DB)
  1629. {
  1630. int num_param = get_instruction_set(instruction_set, raw_instruction);
  1631. if(num_param == -1)
  1632. {
  1633. return -1;
  1634. }
  1635.  
  1636. if(strcmp(instruction_set[0], "INIT_DB") == 0)
  1637. {
  1638. database = INIT_DB(instruction_set[1]);
  1639. }
  1640. else if(strcmp(instruction_set[0], "CREATE") == 0)
  1641. {
  1642. if(strcmp(instruction_set[2], "INT") == 0)
  1643. {
  1644. CREATE_TABLE(database, instruction_set, num_param, INT);
  1645. }
  1646. else if(strcmp(instruction_set[2], "FLOAT") == 0)
  1647. {
  1648. CREATE_TABLE(database, instruction_set, num_param, FLOAT);
  1649. }
  1650. else if(strcmp(instruction_set[2], "STRING") == 0)
  1651. {
  1652. CREATE_TABLE(database, instruction_set, num_param, STRING);
  1653. }
  1654. else
  1655. {
  1656. printf("Unknown data type: \"%s\"\n", instruction_set[2]);
  1657. }
  1658. }
  1659. else if(strcmp(instruction_set[0], "ADD") == 0)
  1660. {
  1661. ADD_IN_TABLE(database, instruction_set, num_param);
  1662. }
  1663. else if(strcmp(instruction_set[0], "PRINT") == 0)
  1664. {
  1665. PRINT_TABLE(database, instruction_set[1]);
  1666. }
  1667. else if(strcmp(instruction_set[0], "SEARCH") == 0)
  1668. {
  1669. SEARCH_IN_TABLE(database, instruction_set);
  1670. }
  1671. else if(strcmp(instruction_set[0], "DELETE") == 0 && num_param > 2)
  1672. {
  1673. DELETE_IN_TABLE(database, instruction_set);
  1674. }
  1675. else if(strcmp(instruction_set[0], "DELETE") == 0 && !(num_param > 2))
  1676. {
  1677. DELETE_TABLE(database, instruction_set[1]);
  1678. }
  1679. else if(strcmp(instruction_set[0], "CLEAR") == 0)
  1680. {
  1681. CLEAR_TABLE(database, instruction_set[1]);
  1682. }
  1683. else if(strcmp(instruction_set[0], "PRINT_DB") == 0)
  1684. {
  1685. PRINT_DB(database);
  1686. }
  1687. else if(!END_DB)
  1688. {
  1689. printf("Unknown command: \"%s\".\n", raw_instruction);
  1690. }
  1691. }
  1692.  
  1693. return 0;
  1694. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement