Download log_entry_ll.h
//DECLARATION OF type LOG_INDEX
// -- A STRUCTURE FOR LINKING LOG_ENTIRES
//IN A LINKED LIST.
//
//NOTE: Had a comment in insertEntry() that was not quite true about index_start being modified.
//
//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_LL_H
#define LOG_ENTRY_LL_H
#include"log_entry_def.h"
int NUM_ELEMS = 0; //ONLY MACRO VARIABLE: NUMBER OF ELEMENTS IN LIST ... needs to be known globally.
typedef struct LOG_INDEX {
LOG_ENTRY* entryPtr; //Pointer to an entry.
char* comments; //Pointer to the accompanying comments.
struct LOG_INDEX* next; //pointer next index in list.
} LOG_INDEX;
//Comments are read from file in one long string separated
//by newlines, '\n'. Next function reads a comment up to \n,
//replaces newline with null char. Return value is pointer to next
//comment -- one char after the \n just replaced by 0.
char* nextComment(char* commentsp){
while(*commentsp != '\n')
++commentsp;
*commentsp = '\0';
++commentsp;
return commentsp;
}
//Given an array of LOG_ENTRYs and a string of comments separated by newlines, the next function
//makes a linked list out of them. This function is used when reading data from file.
LOG_INDEX* makeindex (LOG_ENTRY myentries[], char* commentsp, int NUM_ELEMS) {
if(myentries == NULL || NUM_ELEMS == 0 || commentsp == NULL) {
printf("\033[0;31mNULL POINTER PASSED TO 'makeindex()'. RETURNING NULL POINTER.\033[0m\n\n");
return NULL;
}
LOG_INDEX* index_start = NULL;
LOG_INDEX* prev = NULL;
for(int i = 0; i < NUM_ELEMS ; ++i) {
LOG_INDEX* index = (LOG_INDEX*) malloc(sizeof(LOG_INDEX));
if(i == 0)
index_start = index;
index -> next = NULL;
index -> entryPtr = myentries + i;
index -> comments = commentsp;
if(i< NUM_ELEMS-1)
commentsp = nextComment(commentsp);
if(prev != NULL)
prev -> next = index;
prev = index;
}
return index_start;
}
//NOTE: I tried to clean up prev. function, but managed to introduce a very, very
//mysterious bug. See folders DGbug and DBgugb.
//Print all the entries(with comments) in a linked list.
void printLogList(LOG_INDEX* index) {
if(index == NULL) {
printf("\033[0;31mNULL POINTER PASSED TO printLogList().\033[0m\n\n");
return;
}
while(index != NULL) {
printEntry(index -> entryPtr, index -> comments); putchar('\n');
index = index->next;
}
}
//If entry1 < entry2 pass to this function to check for overlap.
//Returns 1 if entry1.endTime > entry2.startTime
int overlap(LOG_ENTRY* entry1, LOG_ENTRY* entry2){ //OBS: another overlap func (overlap2) in log_entry_def.h
if(cmpdt(&(entry1->date), &(entry2->date)) == 0) {
if(cmptm(&(entry1->endTime), &(entry2->startTime)) > 0)
return 1;
}
return 0;
}
//Given pointer to new entry 'entry' with comments 'comments', insert in list with
//index_start pointing to first index in list.
LOG_INDEX* insertentry(LOG_INDEX** index_start, LOG_ENTRY* entry, char* comments) {
//Bad arg name index_start ... in other files index_start is LOG_INDEX*
LOG_INDEX** pp = index_start;
LOG_INDEX* temp = (LOG_INDEX*) malloc(sizeof(LOG_INDEX));
temp -> next = NULL;
temp -> entryPtr = entry;
temp -> comments = comments;
if(*index_start == NULL) {
*index_start = temp;
return NULL;
//This is the only place EVER that index_start should be modified!!!!
// ... explicitly. Can also be modified in loop below.
}
while(*pp != NULL) {
int cmp_val = cmpntrydt(entry, (*pp)-> entryPtr);
if( cmp_val < 0) {
if(overlap(entry, (*pp)-> entryPtr) != 0){
free(temp);
return *pp;
}
temp -> next = *pp;
break;
}
else if(cmp_val == 0) {
free(temp);
return *pp;
}
else {
if(overlap((*pp)-> entryPtr, entry) != 0){
free(temp);
return *pp;
}
}
pp = &((*pp) -> next);
}
*pp = temp;
return NULL;
}
//Next func: deletes index pointed to by theIndex -- associated LOG_ENTRY
int deleteEntry(LOG_INDEX* theIndex, LOG_INDEX** index_startP) {
for(LOG_INDEX** pp = index_startP; *pp != NULL; pp = &((*pp)->next)){
if(*pp == theIndex){
*pp = theIndex->next;
return 1;
//Note: shit still hanging around:-(
}
}
return 0;
}
//NOTE: I have not freed the associated memory, which in some circumstances
//would create a memory leak, but there should be few such deletions for each
//run. Dealocating memory here would be tricky since memory may be in the
//middle of an allocated array if this entry was gotten from file.
#endif