This commit is contained in:
Odweta
2023-12-21 13:31:10 +01:00
parent eb6b6c8343
commit 784fd0d35d
3 changed files with 34 additions and 51 deletions
+6 -19
View File
@@ -1,29 +1,16 @@
#include <odweta/vector.h> #include <odweta/vector.h>
#include <odweta/file.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) { int main(int argc, char **argv) {
vector_t *vv = vectorInit(VECTOR); vector_t *v = vectorInit(VECTOR);
vector_t *fv = vectorInit(FLOAT);
vectorPrint(vv);
vectorPush(fv, &(float){3.14}); vector_t *floatv = vectorInit(FLOAT);
vectorPush(fv, &(float){5.123});
vectorPush(vv, fv); vectorPush(floatv, &(float){3.14});
vectorPush(vv, fv); vectorPush(floatv, &(float){55.5});
vectorPush(vv, fv);
vectorPush(fv, &(float){156.1}); vectorPush(v, floatv);
vectorPush(fv, &(float){6.124});
vectorInsertAt(vv, 1, fv); vectorPrint(v);
vectorPrint(vv);
vectorCleanup(vv);
vectorCleanup(fv);
return 0; return 0;
} }
+12 -8
View File
@@ -2,23 +2,27 @@
#define FILE_H #define FILE_H
#include <odweta/vector.h> #include <odweta/vector.h>
#include <stdio.h>
typedef struct { typedef struct {
FILE* descriptor; // the file descriptor char* path; // the file descriptor
int length; // line count int length; // line count
vector_t* lines; // 2d array of chars vector_t* lines; // 2d array of chars
} file_t; } file_t;
file_t* fileInit(char* path, char* mode); // initialization function file_t* fileInit(char* path); // initialization function
// TODO: when there is not a '\n' at the end of a file, the last line is not loaded :(
void fileCleanup(file_t* f); // just to be memory safe... void fileCleanup(file_t* f); // just to be memory safe...
int fileCountLines(file_t* f); // int fileCountLines(file_t* f); // counts the lines in a file
void filePrint(file_t* f); void filePrint(file_t* f); // prints the file as a string
char* fileToString(file_t* f); char* fileToString(file_t* f); // returns a string with the file contents
#endif void fileWriteln(file_t* f, char* line); // appends a given string to the file on a newline
void fileWrite(file_t* f, char* line); // appends a given string to the file (not on a newline!)
void fileClear(file_t* f); // wipes the file contents
#endif
+16 -24
View File
@@ -8,26 +8,27 @@ int
fileCountLines(file_t* f) { fileCountLines(file_t* f) {
char ch; char ch;
int lines = 0; int lines = 0;
FILE* fd = fopen(f->path, "r");
while ((ch = fgetc(f->descriptor)) != EOF) { while ((ch = fgetc(fd)) != EOF) {
if (ch == '\n') { if (ch == '\n') {
lines++; lines++;
} }
} }
rewind(f->descriptor); fclose(fd);
return lines; return lines;
} }
void void
_fileLoadLines(file_t* f) { _fileLoadLines(file_t* f, FILE* fd) {
int lineLength; int lineLength;
char ch; char ch;
vector_t* line = vectorInit(CHAR); vector_t* line = vectorInit(CHAR);
for (int i = 0; i < f->length; i++) { for (int i = 0; i < f->length; i++) {
while ((ch = fgetc(f->descriptor)) != '\n' && ch != EOF) { while ((ch = fgetc(fd)) != '\n' && ch != EOF) {
vectorPush(line, &ch); vectorPush(line, &ch);
} }
@@ -40,8 +41,6 @@ _fileLoadLines(file_t* f) {
vectorPush(f->lines, line); vectorPush(f->lines, line);
line = vectorInit(CHAR); line = vectorInit(CHAR);
} }
rewind(f->descriptor);
} }
char* char*
@@ -50,40 +49,34 @@ fileToString(file_t* f) {
} }
file_t* file_t*
fileInit(char* path, char* mode) { fileInit(char* path) {
file_t* f = malloc(sizeof(file_t)); file_t* f = malloc(sizeof(file_t));
if (f == NULL) { if (f == NULL) {
perror("failed to allocate memory for the file\n"); perror("failed to allocate memory for the file\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
f->lines = malloc(sizeof(vector_t)); f->lines = malloc(sizeof(vector_t));
if (f->lines == NULL) { if (f->lines == NULL) {
perror("failed to allocate memory for the file contents vector\n"); perror("failed to allocate memory for the file contents vector\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int imode = 0;
if (strcmp(mode, "r") == 0) { f->path = path;
imode = 0;
} else if (strcmp(mode, "w") == 0) {
imode = 1;
} else if (strcmp(mode, "a") == 0) {
imode = 2;
} else {
perror("only allowed modes for file opening are \"r\", \"w\" and \"a\"\n");
exit(EXIT_FAILURE);
}
f->descriptor = fopen(path, mode); FILE* fd = fopen(f->path, "r");
if (f->descriptor == NULL) { if (fd == NULL) {
fprintf(stderr, "unable to open file %s with mode %s\n", path, mode); fprintf(stderr, "unable to open file %s with mode \"r\"\n", path);
fclose(fd);
fileCleanup(f); fileCleanup(f);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
f->length = fileCountLines(f); f->length = fileCountLines(f);
f->lines = vectorInit(VECTOR); // vector of vectors of chars = vector of strings f->lines = vectorInit(VECTOR); // vector of vectors of chars = vector of strings
if (imode == 0) _fileLoadLines(f); _fileLoadLines(f, fd);
fclose(fd);
return f; return f;
} }
@@ -91,7 +84,6 @@ fileInit(char* path, char* mode) {
void void
fileCleanup(file_t* f) { fileCleanup(file_t* f) {
vectorCleanup(f->lines); vectorCleanup(f->lines);
fclose(f->descriptor);
free(f); free(f);
} }
@@ -102,4 +94,4 @@ filePrint(file_t* f) {
line = (vector_t *)vectorGetAt(f->lines, i); line = (vector_t *)vectorGetAt(f->lines, i);
vectorPrintAsString(line); vectorPrintAsString(line);
} }
} }