Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <conio.h>
  6.  
  7. typedef struct wpis {
  8. char* time;
  9. char* data;
  10. char* tags;
  11. } wpis;
  12.  
  13. typedef struct node {
  14. wpis* w;
  15. struct node* next;
  16. } node;
  17.  
  18. void read(char** target, char stop, FILE* in) {
  19. static char word[2048];
  20. int i = 0;
  21. char c;
  22. while ((c = getc(in)) != stop && c != EOF)
  23. word[i++] = c;
  24. word[i] = 0;
  25. int len = strlen(word);
  26. *target = (char*)malloc((len + 1) * sizeof(char));
  27. strcpy(*target, word);
  28. }
  29.  
  30. void add(node* list) {
  31. wpis* w = (wpis*) malloc(sizeof(wpis));
  32. read(&w->time, ';', stdin);
  33. read(&w->data, ';', stdin);
  34. read(&w->tags, '\n', stdin);
  35. node* tmp = (node*)malloc(sizeof(node));
  36. tmp->next = list->next;
  37. tmp->w = w;
  38. list->next = tmp;
  39. }
  40.  
  41. void print_all(node* root, FILE* out) {
  42. node* iter = root->next;
  43. while (iter != NULL) {
  44. wpis* w = iter->w;
  45. fprintf(out, "\n%s;%s;%s", w->time, w->data, w->tags);
  46. iter = iter->next;
  47. }
  48. }
  49.  
  50. void save(node* root) {
  51. FILE* data = fopen("data.db", "w");
  52. print_all(root, data);
  53. fclose(data);
  54. }
  55.  
  56. void read_from_file(node* list) {
  57. FILE* data = fopen("data.db", "r");
  58. if (data == NULL) return;
  59. getc(data);
  60. while (!feof(data)) {
  61. getc(data);
  62. wpis* w = (wpis*) malloc(sizeof(wpis));
  63. read(&w->time, ';', data);
  64. read(&w->data, ';', data);
  65. read(&w->tags, '\n', data);
  66. node* tmp = (node*)malloc(sizeof(node));
  67. tmp->w = w;
  68. list->next = tmp;
  69. list = tmp;
  70. tmp->next = NULL;
  71. }
  72. fclose(data);
  73. }
  74.  
  75. void show_all(node* root) {
  76. print_all(root, stdout);
  77. printf("\n");
  78. }
  79.  
  80. void print_menu() {
  81. printf("1. Add\n");
  82. printf("2. Show All\n");
  83. printf("3. Show Tag\n");
  84. printf("4. Quit and save\n");
  85. }
  86.  
  87. void show_tag(node* root, char* tag) {
  88. node* iter = root->next;
  89. while (iter != NULL) {
  90. wpis* w = iter->w;
  91. char* res = strstr(w->tags, tag);
  92. if (res != NULL) {
  93. printf("%s;%s;%s\n", w->time, w->data, w->tags);
  94. }
  95. iter = iter->next;
  96. }
  97. }
  98.  
  99. bool file_exist() {
  100. FILE *file;
  101. if (file = fopen("data.db", "r")) {
  102. fclose(file);
  103. return true;
  104. }
  105. return false;
  106. }
  107.  
  108. int main() {
  109. node* list = (node*) malloc(sizeof(node));
  110. list->next = NULL;
  111. read_from_file(list);
  112.  
  113. bool run = true;
  114. while (run) {
  115. print_menu();
  116. int dec;
  117. scanf("%d", &dec);
  118. while (getchar() != '\n');
  119. if (dec == 1) {
  120. add(list);
  121. save(list);
  122. system("cls");
  123. }
  124. else if (dec == 2) {
  125. show_all(list);
  126. getchar();
  127. system("cls");
  128. }
  129. else if (dec == 3) {
  130. char* tag;
  131. read(&tag, '\n', stdin);
  132. show_tag(list, tag);
  133. getchar();
  134. system("cls");
  135. }
  136. else if (dec == 4) break;
  137. }
  138.  
  139. save(list);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement