added writing capacilities to files and made the appropriate changes to the filedemo.c file
This commit is contained in:
+96
-30
@@ -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;
|
||||
}
|
||||
|
||||
+1
-1
@@ -49,7 +49,7 @@ listPush(list_t* l, dataType_e type, void* element) {
|
||||
vectorPush((vector_t *)l->data, newVec);
|
||||
break;
|
||||
default:
|
||||
perror("specify a valid vector type\n");
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
+35
-30
@@ -1,9 +1,9 @@
|
||||
#include <odweta/vector.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string.h>
|
||||
|
||||
void
|
||||
void
|
||||
vectorCleanup(vector_t* v) {
|
||||
free(v->data);
|
||||
free(v);
|
||||
@@ -18,7 +18,7 @@ vectorReset(vector_t* v) {
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
void
|
||||
_printVectorRecursive(vector_t* v, int depth) {
|
||||
int i;
|
||||
fprintf(stdout, "[");
|
||||
@@ -38,7 +38,7 @@ _printVectorRecursive(vector_t* v, int depth) {
|
||||
_printVectorRecursive((vector_t *)(v->data + i * v->elementSize), depth + 1);
|
||||
break;
|
||||
default:
|
||||
perror("tried to print unsupported data type\n");
|
||||
fprintf(stderr, "tried to print unsupported data type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ _printVectorRecursive(vector_t* v, int depth) {
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
void
|
||||
vectorPrint(vector_t* v) {
|
||||
fprintf(stdout, "[");
|
||||
int i;
|
||||
@@ -74,7 +74,7 @@ vectorPrint(vector_t* v) {
|
||||
_printVectorRecursive((vector_t *)(v->data + i * v->elementSize), 0);
|
||||
break;
|
||||
default:
|
||||
perror("tried to print unsupported data type\n");
|
||||
fprintf(stderr, "tried to print unsupported data type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ vectorPrint(vector_t* v) {
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
void
|
||||
_printVectorAsStringRecursive(vector_t* v, int depth) {
|
||||
int i;
|
||||
for (i = 0; i < v->size; i++) {
|
||||
@@ -103,14 +103,14 @@ _printVectorAsStringRecursive(vector_t* v, int depth) {
|
||||
_printVectorAsStringRecursive((vector_t *)(v->data + i * v->elementSize), depth + 1);
|
||||
break;
|
||||
default:
|
||||
perror("tried to print a non-char type vector as string\n");
|
||||
fprintf(stderr, "tried to print a non-char type vector as string\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
if (depth != 0) fprintf(stdout, "\n"); // do not print the last additional new line
|
||||
}
|
||||
|
||||
void
|
||||
void
|
||||
vectorPrintAsString(vector_t* v) {
|
||||
switch (v->type) {
|
||||
case CHAR:
|
||||
@@ -123,16 +123,16 @@ vectorPrintAsString(vector_t* v) {
|
||||
_printVectorAsStringRecursive(v, 0);
|
||||
break;
|
||||
default:
|
||||
perror("tried to print a non-char type vector as string\n");
|
||||
fprintf(stderr, "tried to print a non-char type vector as string | type: %d\n", v->type);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
vector_t*
|
||||
vector_t*
|
||||
vectorInit(dataType_e type) {
|
||||
vector_t *v = malloc(sizeof(vector_t));
|
||||
if (v == NULL) {
|
||||
perror("failed to allocate memory for the vector\n");
|
||||
if (!v) {
|
||||
fprintf(stderr, "failed to allocate memory for the vector\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -153,20 +153,20 @@ vectorInit(dataType_e type) {
|
||||
v->elementSize = sizeof(vector_t);
|
||||
break;
|
||||
default:
|
||||
perror("specify a valid vector type\n");
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
v->data = malloc(v->elementSize * v->capacity);
|
||||
|
||||
if (v->data == NULL) {
|
||||
perror("failed to allocate memory for vector data\n");
|
||||
fprintf(stderr, "failed to allocate memory for vector data\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
void
|
||||
void
|
||||
vectorPush(vector_t* v, void* element) {
|
||||
switch (v->type) {
|
||||
case INT:
|
||||
@@ -176,28 +176,33 @@ vectorPush(vector_t* v, void* element) {
|
||||
*((float *)(v->data) + v->size) = *((float *)element);
|
||||
break;
|
||||
case CHAR:
|
||||
// Allocate memory for the character
|
||||
v->data = realloc(v->data, (v->size + 1) * v->elementSize);
|
||||
if (!v->data) {
|
||||
fprintf(stderr, "failed to allocate memory for vector data\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*((char *)(v->data) + v->size) = *((char *)element);
|
||||
break;
|
||||
case VECTOR:
|
||||
// Instead of reallocating memory for vector_t pointers, allocate memory for the vector data
|
||||
if (v->size == 0) {
|
||||
// Allocate memory for the vector data for the first time
|
||||
v->data = malloc(v->elementSize * v->capacity);
|
||||
v->data = realloc(v->data, v->elementSize);
|
||||
} else {
|
||||
// Reallocate memory for the vector data
|
||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
||||
v->data = realloc(v->data, v->elementSize * (v->size + 1));
|
||||
}
|
||||
if (v->data == NULL) {
|
||||
perror("failed to allocate memory for vector data\n");
|
||||
if (!v->data) {
|
||||
fprintf(stderr, "failed to allocate memory for vector data\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Copy the vector data from the element to the vector
|
||||
memcpy((vector_t *)(v->data) + v->size, (vector_t *)element, sizeof(vector_t));
|
||||
|
||||
break;
|
||||
default:
|
||||
perror("specify a valid vector type\n");
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -239,7 +244,7 @@ vectorPop(vector_t* v) {
|
||||
|
||||
return vretval;
|
||||
default:
|
||||
perror("specify a valid vector type\n");
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
}
|
||||
@@ -249,7 +254,7 @@ vectorPop(vector_t* v) {
|
||||
}
|
||||
}
|
||||
|
||||
char*
|
||||
char*
|
||||
vectorToString(vector_t* v) {
|
||||
char* retString;
|
||||
int totalSize = 0;
|
||||
@@ -312,7 +317,7 @@ vectorGetAt(vector_t* v, int index) {
|
||||
case VECTOR:
|
||||
return (vector_t *)(v->data) + index;
|
||||
default:
|
||||
perror("specify a valid vector type\n");
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
@@ -320,7 +325,7 @@ vectorGetAt(vector_t* v, int index) {
|
||||
void
|
||||
vectorSetAt(vector_t* v, int index, void* element) {
|
||||
if (index >= v->size) {
|
||||
perror("index out of range\n");
|
||||
fprintf(stderr, "index out of range\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -338,15 +343,15 @@ vectorSetAt(vector_t* v, int index, void* element) {
|
||||
*((vector_t *)(v->data) + index) = *((vector_t *)element);
|
||||
break;
|
||||
default:
|
||||
perror("specify a valid vector type\n");
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
void
|
||||
vectorInsertAt(vector_t* v, int index, void* element) {
|
||||
if (index >= v->size) {
|
||||
perror("index out of range\n");
|
||||
fprintf(stderr, "index out of range\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -384,7 +389,7 @@ vectorInsertAt(vector_t* v, int index, void* element) {
|
||||
*((vector_t *)(v->data) + index) = *((vector_t *)element);
|
||||
break;
|
||||
default:
|
||||
perror("specify a valid vector type\n");
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user