Download log_entry_def.h
//THIS FILE CONTAINS DEFITION OF STRUCTURE LOG_ENTRY
//FOR ENCACPSULATING INFORMATION FOR A SINGLE LOG ENTRY.
//A LOG_ENTRY CONTAINS MEMBER VARIABLES:
//date -- a structure of type 'mydate', also defined below.
//startTime, endTime -- structures of type mytime, also defined below.
//sessiontype -- an enumeration.
//with -- a char array(20 bytes).
//IN ADDITION THERE ARE SOME FUNCTIONS FOR COMPARISON AND PRINTING.
//
//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 LOG_ENTRY_DEF_H
#define LOG_ENTRY_DEF_H
#include<stdio.h>
#include<stdlib.h>
#include<time.h> //struct tm: used in setWday to get day of week.
#include<string.h> //strcpy, memset
//BEGIN: MY TIME STRUCTURE **********************************************************
struct mytime {
char hour;
char minute;
};
int numMinutes(struct mytime* time1, struct mytime* time2) { //Redundant -- could replace with -cmptm()
return (int)(time2->hour-time1->hour)*60 + (int)time2->minute-time1->minute;
}
//Next func. returns time1-time2 in minutes.
int cmptm(struct mytime* time1, struct mytime* time2) {
return (int)(time1->hour-time2->hour)*60 + (int)time1->minute-time2->minute;
}
//MY TIME STRUCTURE *****************************************************
//BEGIN: MY DATE STRUCTURE ***********
struct mydate {
char year;
char month;
char day;
char wday; //Sunday is day 0.
};
//Next func. returns -1 if date1 < date2, 1 if date1 > date2 and 0 if =.
int cmpdt(struct mydate* date1, struct mydate* date2) {
if (date1 -> year - date2 -> year < 0)
return -1;
if(date1 -> year - date2 -> year > 0)
return 1;
if (date1 -> month - date2 -> month < 0)
return -1;
if(date1 -> month - date2 -> month > 0)
return 1;
if (date1 -> day - date2 -> day < 0)
return -1;
if(date1 -> day - date2 -> day > 0)
return 1;
return 0;
}
//END: MY DATE STRUCTURE *************
//BEGIN: ENUMERATATION FOR SESSION TYPE.
enum SESSIONTYPE {NOTSET, AUSK, LEKT, MEETING, CONVERSATION};
//NOTSET: unspecified.
//AUSK: lesson audit.
//LEKT: lession.
//MEETING: ... duh
//CONVERSATION: ... duh.
void printType(enum SESSIONTYPE cat){
switch (cat) {
case NOTSET:
printf("Session");
break;
case AUSK:
printf("Auskultation");
break;
case LEKT:
printf("Lektion");
break;
case MEETING:
printf("Möte ");
break;
case CONVERSATION:
printf("Samtal ");
break;
default:
printf("Hell if I know");
break;
}
}
//Next func: write string for session type in buffer.
void typeToString(char buff[], enum SESSIONTYPE cat){
switch (cat) {
case NOTSET:
strcpy(buff, "Session");
break;
case AUSK:
strcpy(buff, "Auskultation");
break;
case LEKT:
strcpy(buff, "Lektion");
break;
case MEETING:
strcpy(buff, "Möte ");
break;
case CONVERSATION:
strcpy(buff, "Samtal ");
break;
default:
strcpy(buff, "Hell if I know");
break;
}
}
//Given string (Swedish) name of session type in s[], return appropriate SESSIONSTYPE.
enum SESSIONTYPE fromString(char s[]) {
if(strcmp(s, "ausk")==0)
return AUSK;
else if (strcmp(s,"lekt")==0)
return LEKT;
else if (strcmp(s, "meet")==0)
return MEETING;
else if (strcmp(s, "conv")==0)
return CONVERSATION;
else
return NOTSET;
}
//END: ENUMERATION FOR SESSION TYPE.
//BEGIN: LOG_ENTRY ** LOG_ENTRY ** LOG_ENTRY ** LOG_ENTRY ** LOG_ENTRY
typedef struct LOG_ENTRY {
struct mydate date; //4 bytes.
struct mytime startTime; //2 bytes.
struct mytime endTime; //2 bytes.
enum SESSIONTYPE sessiontype; //4 bytes.
char with[20]; //20 bytes.
//32 bytes total.
} LOG_ENTRY;
void init_entry(LOG_ENTRY* entry) {
memset(entry, 0, sizeof(LOG_ENTRY)); //Set all bytes to 0.
entry -> sessiontype = NOTSET; //Probably redundant.
}
int numMin(LOG_ENTRY* entry) { //Get LOG_ENTRY number of minutes.
return cmptm(&(entry->endTime), &(entry->startTime));
}
//Next func: compare two log entries e1, e2.
//if e1 < e2 returns -1, e1 > e2 returns 1. else 0.
int cmpntrydt(LOG_ENTRY* entry1, LOG_ENTRY* entry2) {
if(cmpdt(&(entry1->date), &(entry2->date)) < 0)
return -1;
if(cmpdt(&(entry1->date), &(entry2->date)) > 0)
return 1;
if(cmptm(&(entry1->startTime), &(entry2->startTime)) < 0)
return -1;
if(cmptm(&(entry1->startTime), &(entry2->startTime)) > 0)
return 1;
return 0;
}
//Next func: returns 1 if startTime < endTime (times ok). 0 otherwsie
int startBeforeEnd(LOG_ENTRY* entry) {
if(cmptm(&(entry->startTime), &(entry->endTime)) < 0)
return 1;
return 0;
}
//Next func: tests if start time or end time of entry1 lies in time interval of entry2.
int overlap2(LOG_ENTRY* entry1, LOG_ENTRY* entry2) { //OBS: another overlap func (overlap()) in log_entry_ll.h
if(cmpdt(&(entry1->date), &(entry2->date)) != 0)
return 0;
if(cmptm(&(entry1->startTime), &(entry2->startTime)) >= 0 && cmptm(&(entry1->startTime), &(entry2->endTime)) < 0)
return 1;
if(cmptm(&(entry1->endTime), &(entry2->startTime)) > 0 && cmptm(&(entry1->endTime), &(entry2->endTime)) <= 0)
return 1;
return 0;
}
//Next func: convert tm_wday to string date(Swedish)
//weekDayToStr(target buffer for string, int day of week (0-6))
void weekDayToStr(char buff[], int dow) {
switch(dow){
case 1:
strcpy(buff, "Mån");
break;
case 2:
strcpy(buff, "Tis");
break;
case 3:
strcpy(buff, "Ons");
break;
case 4:
strcpy(buff, "Tor");
break;
case 5:
strcpy(buff, "Fre");
break;
case 6:
strcpy(buff, "Lör");
break;
case 0:
strcpy(buff, "Sön");
break;
default:
strcpy(buff, "ERR");
}
}
//Next func: given log_entry structure with numeric date filled in, set wday(day of week) member in date.
void setWday(LOG_ENTRY* entry) {
struct tm time; memset(&time, 0, sizeof(struct tm));
time.tm_year = entry->date.year - 1900; //Don't know why this works.
time.tm_mon = entry->date.month-1;
time.tm_mday = entry->date.day;
mktime(&time);
(entry -> date).wday = time.tm_wday;
}
//Next func: print log_entry structure data to screen.
//printEntry(pointer to log_entry structure)
void printEntry(LOG_ENTRY* entry, char comments[]) {
char wday[8];
weekDayToStr(wday, entry->date.wday);
if(entry == NULL) {
printf("\033[0;31mNULL POINTER PASSED TO 'printEntry'.\033[0m\n\n");
return;
}
printf("\033[0;33m\033[1m%s %02d-%02d-%02d (%02d:%02d-%02d:%02d)\tANTAL MIN: %d\033[1;36m\n\t",
wday,
entry->date.year, entry->date.month, entry->date.day,
entry->startTime.hour, entry->startTime.minute, entry->endTime.hour, entry->endTime.minute,
numMinutes(&(entry->startTime), &(entry->endTime) ) );
printType(entry->sessiontype);
if(*(entry->with) == '\0')
printf(" med ???\n");
else
printf(" med %s.\n", entry->with);
if(*comments == '\0')
printf("\tKOMMENTARER: inga.\n");
else
printf("\tKOMMENTARER: %s\n\n", comments);
printf("\033[0m");
}
//********************************************************************************************************
#endif