Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 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. wpis* w = (wpis*) malloc(sizeof(wpis));
  62. read(&w->time, ';', data);
  63. read(&w->data, ';', data);
  64. read(&w->tags, '\n', data);
  65. node* tmp = (node*)malloc(sizeof(node));
  66. tmp->w = w;
  67. list->next = tmp;
  68. list = tmp;
  69. tmp->next = NULL;
  70. }
  71. fclose(data);
  72. }
  73.  
  74. void show_all(node* root) {
  75. print_all(root, stdout);
  76. printf("\n");
  77. }
  78.  
  79. void print_menu() {
  80. printf("1. Add\n");
  81. printf("2. Show All\n");
  82. printf("3. Show Tag\n");
  83. printf("4. Quit and save\n");
  84. }
  85.  
  86. void show_tag(node* root, char* tag) {
  87. node* iter = root->next;
  88. while (iter != NULL) {
  89. wpis* w = iter->w;
  90. char* res = strstr(w->tags, tag);
  91. if (res != NULL) {
  92. printf("%s;%s;%s\n", w->time, w->data, w->tags);
  93. }
  94. iter = iter->next;
  95. }
  96. }
  97.  
  98. bool file_exist() {
  99. FILE *file;
  100. if (file = fopen("data.db", "r")) {
  101. fclose(file);
  102. return true;
  103. }
  104. return false;
  105. }
  106.  
  107. int main() {
  108. node* list = (node*) malloc(sizeof(node));
  109. list->next = NULL;
  110. read_from_file(list);
  111.  
  112. bool run = true;
  113. while (run) {
  114. print_menu();
  115. int dec;
  116. scanf("%d", &dec);
  117. while (getchar() != '\n');
  118. if (dec == 1) {
  119. add(list);
  120. save(list);
  121. system("cls");
  122. }
  123. else if (dec == 2) {
  124. show_all(list);
  125. getchar();
  126. system("cls");
  127. }
  128. else if (dec == 3) {
  129. char* tag;
  130. read(&tag, '\n', stdin);
  131. show_tag(list, tag);
  132. getchar();
  133. system("cls");
  134. }
  135. else if (dec == 4) break;
  136. }
  137.  
  138. save(list);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement