Download log_entry_files.h
//read_log() reads data in files INDEX_DATA, COMMENTS_DATA.
//If success, *index_pp will point
//to start of linked list of LOG_ENTRYs.
//Returns number of elements read.
//
//write_log() goes the other way: writes data
//to INDEX_DATA, COMMENTS_DATA. WRITES int NUM_ELEMS AS
//FIRST 4 BYTES IN FILE INDEX_DATA.
//
//
//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_FILES_H
#define LOG_ENTRY_FILES_H
#include"log_entry_ll.h"
#include"log_entry_copy_files.h" //Contains copy_files() which makes back up copies of prev. data.
int read_log(LOG_INDEX** index_pp) {
char* commentsp = NULL;
LOG_ENTRY* pt_fArr;
int NUM_ELEMS = 0;
size_t COMMENTS_SIZE;
FILE * fp = NULL;
FILE * cfp = NULL;
if(copy_files("INDEX_DATA_BACKUP", "INDEX_DATA") == 0 && copy_files("COMMENTS_DATA_BACKUP", "COMMENTS_DATA") == 0) {
fp = fopen("INDEX_DATA", "rb");
cfp = fopen("COMMENTS_DATA", "rb"); fseek(cfp, 0L, SEEK_END); COMMENTS_SIZE = ftell(cfp); fseek(cfp, 0, SEEK_SET);
if(fp != NULL && cfp != NULL) {
if(fread(&NUM_ELEMS, sizeof(int),1,fp) == 1) {
pt_fArr = (LOG_ENTRY*) malloc(NUM_ELEMS*sizeof(LOG_ENTRY));
commentsp = (char*) malloc(COMMENTS_SIZE);
if(fread(pt_fArr, sizeof(LOG_ENTRY), NUM_ELEMS,fp) == NUM_ELEMS) {
if(fread(commentsp, COMMENTS_SIZE, 1,cfp) == 1){
*(commentsp + COMMENTS_SIZE-1) = '\0';
*index_pp = makeindex(pt_fArr, commentsp, NUM_ELEMS);
}
}
else printf("\033[0;31mProblem reading from files.\033[0m\n\n");
}
else printf("\033[0;31mProblem getting num elements in file.\033[0m\n\n");
}
else printf("\033[0;31mCould not open 'dagbok.log' for reading into array.\033[0m\n\n");
if(fp != NULL) fclose(fp); if(cfp != NULL) fclose(cfp);
}
else
printf("\033[0;31mCould not backup data. If you are making a new logbok this is normal.\033[0m\n\n");
return NUM_ELEMS;
}
int write_log(LOG_INDEX* index_start, int NUM_ELEMS) {
printf("\033[0;31mWRITING TO FILE AND EXITING.\033[0m\n");
FILE * fp = fopen("INDEX_DATA", "wb");
FILE * cfp = fopen("COMMENTS_DATA", "w");
if(fp != NULL && cfp != NULL) {
fwrite(&NUM_ELEMS, sizeof(int), 1, fp);
LOG_INDEX* p = index_start;
while(p != NULL) {
fwrite(p -> entryPtr, sizeof(LOG_ENTRY), 1, fp);
fprintf(cfp, "%s\n", p->comments);
p = p -> next;
}
if(fp != NULL) fclose(fp); if(cfp != NULL) fclose(cfp);
printf("\033[0;32mENTRIES WRITTEN TO FILE.\033[0m\n");
}
else
printf("\033[0;31mPROBLEM WRITING TO FILE.\033[0m\n");
}
#endif