Download edit_entry.h
//FUNCTIONS FOR EDITING AN EXISTING LOG_ENTRY IN LIST.
//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 EDIT_ENTRY_H
#define EDIT_ENTRY_H


#include"parse_log_entry.h"


LOG_INDEX * findEntry(LOG_INDEX* indexPtr, struct mydate* datep, struct mytime* startTimePtr);
void  editLogEntry(LOG_INDEX* p);

//Get date of entry from user. 
LOG_INDEX* dateStart(LOG_INDEX** index_startP) {
	LOG_INDEX* index_start = *index_startP;
	char buff[20];
	char* s;
	struct mydate date;
	LOG_INDEX* p = NULL;
	
	while(1) {
		LOG_INDEX* list_start = NULL;
		p = index_start;

		printf("Date: ");
		getmaline(buff);
		s = strip(buff);
		if(strcmp(s, "back") == 0)
			return NULL;

		if(getdate(&date, s) == 0){
			while(p != NULL){
				if(cmpdt(&((p->entryPtr)->date), &date) == 0) {
					if(list_start == NULL)
						list_start = p;
					printEntry(p->entryPtr, p->comments);
				}
				p = p->next;
			}
		}

		if(list_start == NULL)
			printf("\033[0;33mNo entries on that date.\033[0m\n");
		else {
			printf("Write 'time' to continue or 'cancel' to return to main menu."
				"Any other option repeats date query process.\n");
			getmaline(buff);
			s = strip(buff);

			if(strcmp(s, "back") == 0) 
				return NULL;
			if(strcmp(s, "time") == 0){
				struct mytime startTime;
				printf("Time: ");
				getmaline(buff);  s = strip(buff);
				if(strcmp(s, "back") != 0) { 
					gettime(&startTime, s);
					LOG_INDEX* theIndex = findEntry(list_start, &date, &startTime);
					if(theIndex != NULL) {
						while(1) {
							printf("Edit or delete?\n");
							getmaline(buff); s = strip(buff);
							if(strcmp(s, "back") == 0)
								break;
							else if(strcmp(s, "edit") == 0) {
								editLogEntry(theIndex);
								//break;
							}
							else if(strcmp(s, "delete") == 0) {
								printf("\033[0;31mARE YOUR SURE (y/n) \033[0m");
								getmaline(buff); s = strip(buff);
								if(strcmp(s,"n") == 0)
									break;
								else if(strcmp(s, "y") == 0){
								       if(deleteEntry(theIndex, index_startP)) {
									       --NUM_ELEMS;
									       printf("\033[0;31mLOG ENTRY DELETED\n\033[0m");
									       //Note: if only one entry on file, deleting that 
									       //entry wont remove file: need to have a remove file
									       //if NUM_ELEMS goes from +ve to 0.
								       }
								       else 
									       printf("\033[0;31mERROR: NO ENTRY DELETED\n\033[0m");
								       break;
								}
								else
									printf("\033[0;31mINVALID KEYWORD.\033[0m\n");	
							}
							else 
								printf("\033[0;31mINVALID KEYWORD.\033[0m\n");	
						}
					}
					else
						printf("\033[0;33mNo entry found at that time and date\n\033[0m");
				}
				
			}
		}
	}
}

LOG_INDEX * findEntry(LOG_INDEX* indexPtr, struct mydate* datep, struct mytime* startTimePtr) {
	for(LOG_INDEX* p = indexPtr; (p != NULL) && cmpdt(&((indexPtr->entryPtr)->date), datep) == 0; p = p->next) {
		if(cmptm(&((p->entryPtr)->startTime), startTimePtr) == 0)
			return p;
	}
	
	return NULL;

}

//Note: should check that time fields do not conflict if an attempt is made at change!!!!!
//Write no conflict func in log_entry_ll.h?
void  editLogEntry(LOG_INDEX* p) {
	char* s;
	char comments[2048];
	memset(comments, 0, 2048);
	char w = 0, t2 = 0, c = 0, tp = 0;

	char buff[2048]; 
	int ret_code= 0; 

	char no_colon;
	while(1) {
		printf("\033[0;35m>>>>>>>>\033[0m ");
		getmaline(buff);
		char* rest = toColon(&no_colon, buff);
		if(no_colon) 
			printf("Colon expected.\n");
		else {
			char* keyword = strip(buff);

			if (strcmp(keyword, "cancel") == 0) {
				return;
			}
			else if(strcmp(keyword, "print") == 0)
				printEntry((p->entryPtr), comments);
			else if(strcmp(keyword, "clear") == 0)
				system("clear");
			else if(strcmp(keyword, "date")==0) {
				printf("\033{0;31mTO CHANGE THE DATE YOU MUST DELETE THIS ENTRY AND MAKE A NEW.\033[0m\n");	
			}
			else if(strcmp(keyword, "start")==0) 
				printf("\033[0;31mTO CHANGE THE START TIME YOU MUST DELETE THIS ENTRY AND MAKE A NEW.\033[0m\n");
			else if(strcmp(keyword, "end")==0) {
				struct mytime newEndTime;
				if(gettime(&newEndTime, rest) != 0)
					printf("INVALID TIME\n");
				else if(p->next != NULL && cmptm(&newEndTime, &(((p->next)->entryPtr)->startTime))>0)
					printf("\033[0;31mNOT ALLOWED: WOULD CAUSE OVERLAP.\033[0m\n");
				else if(cmptm(&((p->entryPtr)->startTime), &newEndTime) >= 0)
					printf("\033[0;31mATTEMPTED END TIME BEFORE OR SAME AS START TIME.\033[0m\n");
				else {
					(p->entryPtr)->endTime = newEndTime;
					t2 = 1;
				}

			}
			else if(strcmp(keyword, "type")==0) {
				rest = strip(rest);
				(p->entryPtr)->sessiontype = fromString(rest);
				tp = 1;
			}
			else if (strcmp(keyword, "with") == 0) {
				rest = strip(rest);
				if(strlen(rest) >= 20)
					printf("\033[0;31mONLY 20 BYTES ALLOWED IN FIELD.\033[0m\n");
				else {
					strcpy((p->entryPtr)->with, rest);
					w = 1;
				}
			}
			else if (strcmp(keyword, "comments") == 0) { 
				rest = strip(rest);
				strcpy(comments, rest);
				c = 1;
			}
			else if (strcmp(keyword, "done") == 0) {
				char* new_comments = NULL;
				int comm_len = strlen(comments); 
				if(comm_len > 0) {
					new_comments = (char*)malloc(comm_len+1);
					strcpy(new_comments, comments);
					p -> comments = new_comments;
					//Note: we have just pointed the comments pointer
					//to a new string, but we can't de-allocate the old string.
				}
				else new_comments=EMPTY_STRING;
				if (t2 || w || tp || c) {
					printf("t2:%d, w:%d, tp:%d, c:%d\n", t2, w, tp, c);	
					printf("\033[0;32mENTRY EDITED\033[0m\n");
				}
				else {
				        printf("t2:%d, w:%d, tp:%d, c:%d\n", t2, w, tp, c);	
					printf("\033[0;33mNO CHANGES WERE MADE\033[0m\n");
				}
				break;
			}
			else {
				printf("INVALD KEYWORD\n");
			}
		}
	}
}

#endif