Download schedule.h
//FUNCTIONS FOR READING SCHEDULE ON FILE.
//FILE STREAM POINTER fp OF OPEN FILE PASSED
//TO RELEVANT FUNCTIONS.
//
//Brian Smith 2022-04-10
/*Copyright (C) 2022 Brian R. Smith
* Contact: briansmith2865@gmail.com
**The source code in this file is free software: you can
**redistribute it and/or modify it under the terms of the GNU
**General Public License (GNU GPL) as published by the Free Software
**foundation, either version 3 of the License, or (at your option)
**any later version. The code is distributed WITHOUT ANY WARRANTY;
**without even the implied warranty of MERCHANTABILITY or FITNESS
**FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
**
**As additional permission under GNU GPL version 3 section 7, you
**may distribute non-source (e.g., minimized or compacted) forms of
**that code without the copy of the GNU GPL normally required by
**section 4, provided you include this license notice and a URL
**through which recipients can access the Corresponding Source.
**
** @licend The above is the entire license notice
**for the source code in this file.
*/
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"log_entry_ll.h"
#include"parse_log_entry.h"
//NOTSET, AUSK, LEKT, MEETING, CONVERSATION
enum SESSIONTYPE gettype(char buff[]){
if(strcmp(buff, "notset") == 0)
return NOTSET;
else if(strcmp(buff, "ausk") == 0)
return AUSK;
else if(strcmp(buff, "lekt") == 0)
return LEKT;
else if(strcmp(buff, "meet") == 0)
return MEETING;
else if(strcmp(buff, "conv") == 0)
return CONVERSATION;
else
return NOTSET;
}
char getwday(char buff[]) {
if(strcmp(buff, "sön") == 0)
return 0;
else if(strcmp(buff, "mån") == 0)
return 1;
else if(strcmp(buff, "tis") == 0)
return 2;
else if(strcmp(buff, "ons") == 0)
return 3;
else if(strcmp(buff, "tors") == 0)
return 4;
else if(strcmp(buff, "fre") == 0)
return 5;
else if(strcmp(buff, "lör") == 0)
return 6;
else
return -1;
}
LOG_ENTRY* nextEntry(FILE* fp) {
char* buff = (char*) malloc(64);
memset(buff, 0, 64);
size_t bffsz = 64;
LOG_ENTRY* entry = (LOG_ENTRY*) malloc(sizeof(LOG_ENTRY));
memset(entry, 0, sizeof(LOG_ENTRY));
getline(&buff, &bffsz, fp);
buff[strlen(buff)-1] = '\0';
entry->date.wday = getwday(buff);
getline(&buff, &bffsz, fp);
buff[strlen(buff)-1] = '\0';
gettime(&(entry->startTime), buff);
getline(&buff, &bffsz, fp);
buff[strlen(buff)-1] = '\0';
gettime(&(entry->endTime), buff);
getline(&buff, &bffsz, fp);
buff[strlen(buff)-1] = '\0';
entry->sessiontype = gettype(buff);
getline(&buff, &bffsz, fp);
buff[strlen(buff)-1] = '\0';
strcpy(entry->with, buff);
free(buff);
return entry;
}
LOG_INDEX* getSchedule(FILE* fp) {
if(fp == NULL)
return NULL;
char* empty = (char*) malloc(1); *empty = '\0';
char* buff = (char*) malloc(4);
char* s;
size_t bffsz;
int numread = 1;
LOG_INDEX* logindex = NULL;
LOG_INDEX* previndex = NULL;
LOG_INDEX* index_start = NULL;
char firstentry = 1;
while(numread > 0) {
numread = getline(&buff, &bffsz, fp);
buff[strlen(buff)-1] = '\0';
s = strip(buff);
if(strcmp(s, "ENTRY") == 0) {
logindex = (LOG_INDEX*) malloc(sizeof(LOG_INDEX));
logindex->comments = empty;
logindex->entryPtr = nextEntry(fp);
if(firstentry) {
index_start = logindex;
firstentry = 0;
}
if(previndex != NULL)
previndex->next = logindex;
previndex = logindex;
}
}
logindex->next = NULL;
return index_start;
}
#endif