Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <conio.h>
- typedef struct wpis {
- char* time;
- char* data;
- char* tags;
- } wpis;
- typedef struct node {
- wpis* w;
- struct node* next;
- } node;
- void read(char** target, char stop, FILE* in) {
- static char word[2048];
- int i = 0;
- char c;
- while ((c = getc(in)) != stop && c != EOF)
- word[i++] = c;
- word[i] = 0;
- int len = strlen(word);
- *target = (char*)malloc((len + 1) * sizeof(char));
- strcpy(*target, word);
- }
- void add(node* list) {
- wpis* w = (wpis*) malloc(sizeof(wpis));
- read(&w->time, ';', stdin);
- read(&w->data, ';', stdin);
- read(&w->tags, '\n', stdin);
- node* tmp = (node*)malloc(sizeof(node));
- tmp->next = list->next;
- tmp->w = w;
- list->next = tmp;
- }
- void print_all(node* root, FILE* out) {
- node* iter = root->next;
- while (iter != NULL) {
- wpis* w = iter->w;
- fprintf(out, "\n%s;%s;%s", w->time, w->data, w->tags);
- iter = iter->next;
- }
- }
- void save(node* root) {
- FILE* data = fopen("data.db", "w");
- print_all(root, data);
- fclose(data);
- }
- void read_from_file(node* list) {
- FILE* data = fopen("data.db", "r");
- if (data == NULL) return;
- getc(data);
- while (!feof(data)) {
- wpis* w = (wpis*) malloc(sizeof(wpis));
- read(&w->time, ';', data);
- read(&w->data, ';', data);
- read(&w->tags, '\n', data);
- node* tmp = (node*)malloc(sizeof(node));
- tmp->w = w;
- list->next = tmp;
- list = tmp;
- tmp->next = NULL;
- }
- fclose(data);
- }
- void show_all(node* root) {
- print_all(root, stdout);
- printf("\n");
- }
- void print_menu() {
- printf("1. Add\n");
- printf("2. Show All\n");
- printf("3. Show Tag\n");
- printf("4. Quit and save\n");
- }
- void show_tag(node* root, char* tag) {
- node* iter = root->next;
- while (iter != NULL) {
- wpis* w = iter->w;
- char* res = strstr(w->tags, tag);
- if (res != NULL) {
- printf("%s;%s;%s\n", w->time, w->data, w->tags);
- }
- iter = iter->next;
- }
- }
- bool file_exist() {
- FILE *file;
- if (file = fopen("data.db", "r")) {
- fclose(file);
- return true;
- }
- return false;
- }
- int main() {
- node* list = (node*) malloc(sizeof(node));
- list->next = NULL;
- read_from_file(list);
- bool run = true;
- while (run) {
- print_menu();
- int dec;
- scanf("%d", &dec);
- while (getchar() != '\n');
- if (dec == 1) {
- add(list);
- save(list);
- system("cls");
- }
- else if (dec == 2) {
- show_all(list);
- getchar();
- system("cls");
- }
- else if (dec == 3) {
- char* tag;
- read(&tag, '\n', stdin);
- show_tag(list, tag);
- getchar();
- system("cls");
- }
- else if (dec == 4) break;
- }
- save(list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement