added writing capacilities to files and made the appropriate changes to the filedemo.c file

This commit is contained in:
Odweta
2023-12-23 18:28:58 +01:00
parent b9c93116bc
commit 0c68944a1d
9 changed files with 172 additions and 96 deletions
+96 -30
View File
@@ -4,11 +4,15 @@
#include <stdlib.h>
#include <stdio.h>
int
int
fileCountLines(file_t* f) {
char ch;
int lines = 0;
FILE* fd = fopen(f->path, "r");
if (!fd) {
fprintf(stderr, "unable to open file");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(fd)) != EOF) {
if (ch == '\n') {
@@ -21,26 +25,44 @@ fileCountLines(file_t* f) {
return lines;
}
void
_fileLoadLines(file_t* f, FILE* fd) {
int lineLength;
void
_fileLoadLines(file_t* f) {
char ch;
vector_t* line = vectorInit(CHAR);
FILE* fd = fopen(f->path, "r");
if (!fd) {
fprintf(stderr, "unable to open file %s with mode \"r\"\n", f->path);
fclose(fd);
fileCleanup(f);
exit(EXIT_FAILURE);
}
char prev;
for (int i = 0; i < f->length; i++) {
vector_t* line = vectorInit(CHAR); // Create a new vector for each line
while ((ch = fgetc(fd)) != '\n' && ch != EOF) {
vectorPush(line, &ch);
}
// Skip the newline character if it exists
if (ch == '\n') {
char* empty = "";
vectorPush(line, empty);
char* empty = "";
vectorPush(line, empty);
}
vectorPush(f->lines, line);
line = vectorInit(CHAR);
prev = ch;
// Don't free the line vector here, as it's now owned by the f->lines vector
}
if (prev != '\n') {
f->not_newline_terminated = 1; // \n is NOT at the end of a file
} else {
f->not_newline_terminated = 0; // \n IS at the end of a file
}
fclose(fd);
}
char*
@@ -51,47 +73,91 @@ fileToString(file_t* f) {
file_t*
fileInit(char* path) {
file_t* f = malloc(sizeof(file_t));
if (f == NULL) {
perror("failed to allocate memory for the file\n");
exit(EXIT_FAILURE);
}
f->lines = malloc(sizeof(vector_t));
if (f->lines == NULL) {
perror("failed to allocate memory for the file contents vector\n");
if (!f) {
fprintf(stderr, "failed to allocate memory for the file\n");
exit(EXIT_FAILURE);
}
f->path = path;
FILE* fd = fopen(f->path, "r");
if (fd == NULL) {
fprintf(stderr, "unable to open file %s with mode \"r\"\n", path);
fclose(fd);
fileCleanup(f);
exit(EXIT_FAILURE);
}
f->length = fileCountLines(f);
f->lines = vectorInit(VECTOR); // vector of vectors of chars = vector of strings
_fileLoadLines(f, fd);
fclose(fd);
f->lines = vectorInit(VECTOR); // vector of vectors of chars = vector of strings
_fileLoadLines(f);
return f;
}
void
fileCleanup(file_t* f) {
if (f->not_newline_terminated == 1) {
FILE* fd = fopen(f->path, "a");
fprintf(fd, "\n");
fclose(fd);
f->not_newline_terminated = 0; // just for piece of mind
}
vectorCleanup(f->lines);
free(f);
}
void
filePrint(file_t* f) {
vector_t* line = vectorInit(CHAR);
for (int i = 0; i < f->length; i++) {
line = (vector_t *)vectorGetAt(f->lines, i);
vectorPrintAsString(line);
vectorPrintAsString((vector_t *)vectorGetAt(f->lines, i));
}
}
void
fileWriteln(file_t* f, char* payload) {
FILE* fd = fopen(f->path, "a");
if (!fd) {
fprintf(stderr, "unable to open file %s with mode \"a\"\n", f->path);
fclose(fd);
fileCleanup(f);
exit(EXIT_FAILURE);
}
fprintf(fd, "%s\n", payload);
fclose(fd);
// Update the file structure without re-initializing
if (f->not_newline_terminated == 0) {
vector_t* line = vectorInit(CHAR);
for (int i = 0; i < strlen(payload); i++) {
vectorPush(line, &payload[i]);
}
vectorPush(f->lines, line);
f->length++;
f->not_newline_terminated = 0;
} else if (f->not_newline_terminated == 1) {
for (int i = 0; i < strlen(payload); i++) {
vectorPush((vector_t *)vectorGetAt(f->lines, f->length-1), &payload[i]);
}
f->not_newline_terminated = 0;
}
}
void
fileWrite(file_t* f, char* payload) {
FILE* fd = fopen(f->path, "a");
if (!fd) {
fprintf(stderr, "unable to open file %s with mode \"a\"\n", f->path);
fclose(fd);
fileCleanup(f);
exit(EXIT_FAILURE);
}
fprintf(fd, "%s", payload);
fclose(fd);
// Update the file structure without re-initializing
vector_t* line = vectorInit(CHAR);
for (int i = 0; i < strlen(payload); i++) {
vectorPush(line, &payload[i]);
}
vectorPush(f->lines, line);
f->length++;
f->not_newline_terminated = 1;
}