new demo
This commit is contained in:
@@ -1,29 +1,16 @@
|
||||
#include <odweta/vector.h>
|
||||
#include <odweta/file.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
vector_t *vv = vectorInit(VECTOR);
|
||||
vector_t *fv = vectorInit(FLOAT);
|
||||
vectorPrint(vv);
|
||||
vector_t *v = vectorInit(VECTOR);
|
||||
|
||||
vectorPush(fv, &(float){3.14});
|
||||
vectorPush(fv, &(float){5.123});
|
||||
vector_t *floatv = vectorInit(FLOAT);
|
||||
|
||||
vectorPush(vv, fv);
|
||||
vectorPush(vv, fv);
|
||||
vectorPush(vv, fv);
|
||||
vectorPush(floatv, &(float){3.14});
|
||||
vectorPush(floatv, &(float){55.5});
|
||||
|
||||
vectorPush(fv, &(float){156.1});
|
||||
vectorPush(fv, &(float){6.124});
|
||||
vectorPush(v, floatv);
|
||||
|
||||
vectorInsertAt(vv, 1, fv);
|
||||
|
||||
vectorPrint(vv);
|
||||
|
||||
vectorCleanup(vv);
|
||||
vectorCleanup(fv);
|
||||
vectorPrint(v);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+11
-7
@@ -2,23 +2,27 @@
|
||||
#define FILE_H
|
||||
|
||||
#include <odweta/vector.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
FILE* descriptor; // the file descriptor
|
||||
char* path; // the file descriptor
|
||||
int length; // line count
|
||||
vector_t* lines; // 2d array of chars
|
||||
} file_t;
|
||||
|
||||
file_t* fileInit(char* path, char* mode); // initialization function
|
||||
// TODO: when there is not a '\n' at the end of a file, the last line is not loaded :(
|
||||
file_t* fileInit(char* path); // initialization function
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
+15
-23
@@ -8,26 +8,27 @@ int
|
||||
fileCountLines(file_t* f) {
|
||||
char ch;
|
||||
int lines = 0;
|
||||
FILE* fd = fopen(f->path, "r");
|
||||
|
||||
while ((ch = fgetc(f->descriptor)) != EOF) {
|
||||
while ((ch = fgetc(fd)) != EOF) {
|
||||
if (ch == '\n') {
|
||||
lines++;
|
||||
}
|
||||
}
|
||||
|
||||
rewind(f->descriptor);
|
||||
fclose(fd);
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
void
|
||||
_fileLoadLines(file_t* f) {
|
||||
_fileLoadLines(file_t* f, FILE* fd) {
|
||||
int lineLength;
|
||||
char ch;
|
||||
vector_t* line = vectorInit(CHAR);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -40,8 +41,6 @@ _fileLoadLines(file_t* f) {
|
||||
vectorPush(f->lines, line);
|
||||
line = vectorInit(CHAR);
|
||||
}
|
||||
|
||||
rewind(f->descriptor);
|
||||
}
|
||||
|
||||
char*
|
||||
@@ -50,40 +49,34 @@ fileToString(file_t* f) {
|
||||
}
|
||||
|
||||
file_t*
|
||||
fileInit(char* path, char* mode) {
|
||||
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");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int imode = 0;
|
||||
|
||||
if (strcmp(mode, "r") == 0) {
|
||||
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->path = path;
|
||||
|
||||
f->descriptor = fopen(path, mode);
|
||||
if (f->descriptor == NULL) {
|
||||
fprintf(stderr, "unable to open file %s with mode %s\n", path, mode);
|
||||
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
|
||||
if (imode == 0) _fileLoadLines(f);
|
||||
_fileLoadLines(f, fd);
|
||||
|
||||
fclose(fd);
|
||||
|
||||
return f;
|
||||
}
|
||||
@@ -91,7 +84,6 @@ fileInit(char* path, char* mode) {
|
||||
void
|
||||
fileCleanup(file_t* f) {
|
||||
vectorCleanup(f->lines);
|
||||
fclose(f->descriptor);
|
||||
free(f);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user