Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include "stringdb.h"
- #include "tree.h"
- #include "table.h"
- #include "file.h"
- #include "calculate.h"
- #include "initialize.h"
- #include "IOfunctions.h"
- static int newEntrys=0; // To keep track if/how many entrys user has entered
- void IO_getSearchStructs(TABLE tmptable,TREE tmpsearchtree)
- {
- searchtree = tmpsearchtree;
- table = tmptable;
- }
- void IO_printDate(int year, int month)
- {
- printf("\n");
- for(int i=1;i <= 31;i++)
- {
- int key=year*10000 + month*100 +i;
- int index= tree_find(searchtree,key);
- if(index != KEY_NOT_FOUND)
- printf("%s\n",stringdb_get(index) );
- }
- printf("\n");
- }
- void IO_printDay(int month,int day)
- {
- int key = (month-1)*31 + day-1;
- int matches = table_count(table,key);
- if(matches)
- {
- printf("\n");
- for(int i = 1; i <= matches; i++)
- {
- printf("%s\n",stringdb_get(table_find(table,key,i)) );
- }
- printf("\n");
- }
- }
- void IO_searchMenu(void)
- {
- int firstInput=0;
- int secondInput=0;
- while(firstInput<1||secondInput<1||secondInput>12)
- {
- printf("Enter (year and month) or (day and month) #> ");
- scanf(" %d %d",&firstInput,&secondInput);
- }
- if(firstInput > 31)
- IO_printDate(firstInput,secondInput);
- else
- IO_printDay(secondInput,firstInput);
- }
- void IO_addeventMenu(void)
- {
- int year,month,day;
- char newEvent[300];
- char entry[317];
- while(month<1||month>12||day<1||day>31||year<32) // To reduce inputs that will not work
- {
- printf("Enter date of event (YYYY/MM/DD) #> ");
- scanf("%d/%d/%d",&year,&month,&day);
- }
- while(getchar()!='\n'); // To clear input buffer
- printf("Enter what happened on that day #> ");
- fgets(newEvent,300,stdin);
- convert_date(day,month,year,entry);
- strcat(entry,newEvent); // stored the strings separately, then fuse them together
- insertEvent(entry);
- newEntrys++;
- }
- void IO_entrytofile()
- {
- char choice='n';
- printf("Want to save your new entrys?(y/n) #> ");
- scanf(" %c",&choice);
- int size = stringdb_size();
- while(newEntrys-- && choice == 'y')
- {
- file_writeline(stringdb_get(size));
- size--;
- }
- }
- int searchIO(void)
- {
- int choice;
- printf("Welcome to historychannel\n1.) Shutdown program\n2.) Search database\n3.) Add historic events\n");
- printf("Your choice #> ");
- scanf(" %d",&choice);
- switch(choice)
- {
- case 1:
- if(newEntrys)
- IO_entrytofile();
- return 0;
- case 2:
- IO_searchMenu();
- break;
- case 3:
- IO_addeventMenu();
- break;
- }
- return 1;
- }
Add Comment
Please, Sign In to add comment