refactoring done, small bugfixes included, vectors and lists of non-primitive types are not working though
This commit is contained in:
+107
-133
@@ -5,14 +5,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
fileCountLines(file_t* f) {
|
||||
int file_count_lines(file_t *this) {
|
||||
char ch;
|
||||
int lines = 0;
|
||||
FILE* fd = fopen(f->path, "r");
|
||||
if (!fd) {
|
||||
fprintf(stderr, "unable to count lines in file %s\n", f->path);
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
FILE *fd = fopen(this->path, "r");
|
||||
if (fd == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ((ch = fgetc(fd)) != EOF) {
|
||||
@@ -26,118 +25,108 @@ fileCountLines(file_t* f) {
|
||||
return lines;
|
||||
}
|
||||
|
||||
void
|
||||
_fileLoadLines(file_t* f) {
|
||||
void _file_load_lines(file_t *this) {
|
||||
char ch;
|
||||
|
||||
FILE* fd = fopen(f->path, "r");
|
||||
if (!fd) {
|
||||
fprintf(stderr, "unable to open file %s with mode \"r\"\n", f->path);
|
||||
FILE *fd = fopen(this->path, "r");
|
||||
if (fd == NULL) {
|
||||
fclose(fd);
|
||||
fileCleanup(f);
|
||||
exit(EXIT_FAILURE);
|
||||
file_cleanup(this);
|
||||
return;
|
||||
}
|
||||
|
||||
char prev;
|
||||
for (int i = 0; i < f->length; i++) {
|
||||
vector_t* line = vectorInit(CHAR); // Create a new vector for each line
|
||||
for (int i = 0; i < this->length; i++) {
|
||||
vector_t *line = vector_new(CHAR); // Create a new vector for each line
|
||||
|
||||
while ((ch = fgetc(fd)) != '\n' && ch != EOF) {
|
||||
vectorPush(line, &ch);
|
||||
vector_push(line, &ch);
|
||||
}
|
||||
|
||||
// Skip the newline character if it exists
|
||||
if (ch == '\n') {
|
||||
char* empty = "";
|
||||
vectorPush(line, empty);
|
||||
char *empty = "";
|
||||
vector_push(line, empty);
|
||||
}
|
||||
|
||||
vectorPush(f->lines, line);
|
||||
vector_push(this->lines, line);
|
||||
|
||||
prev = ch;
|
||||
// Don't free the line vector here, as it's now owned by the f->lines vector
|
||||
// Don't free the line vector here, as it's now owned by the this->lines vector
|
||||
}
|
||||
if (prev != '\n') {
|
||||
f->not_newline_terminated = 1; // \n is NOT at the end of a file
|
||||
this->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
|
||||
this->not_newline_terminated = 0; // \n IS at the end of a file
|
||||
}
|
||||
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
char*
|
||||
fileToString(file_t* f) {
|
||||
return (char *)vectorToString(f->lines);
|
||||
char *file_to_string(file_t *this) {
|
||||
return vector_to_string(this->lines);
|
||||
}
|
||||
|
||||
int
|
||||
fileExists(file_t* f) {
|
||||
FILE* fd = fopen(f->path, "r");
|
||||
if (!fd) {
|
||||
return 0; // file does not exist
|
||||
} else {
|
||||
return 1; // file exists
|
||||
}
|
||||
bool file_exists(file_t *this) {
|
||||
FILE *fd = fopen(this->path, "r");
|
||||
|
||||
bool retval = (fd == NULL) ? false : true;
|
||||
|
||||
fclose(fd);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void
|
||||
fileCreateEmpty(char* path) {
|
||||
void file_create(char *path) {
|
||||
fclose(fopen(path, "w"));
|
||||
}
|
||||
|
||||
file_t*
|
||||
fileInit(char* path) {
|
||||
file_t* f = malloc(sizeof(file_t));
|
||||
if (!f) {
|
||||
fprintf(stderr, "failed to allocate memory for the file\n");
|
||||
exit(EXIT_FAILURE);
|
||||
file_t *file_new(char *path) {
|
||||
file_t *this = (file_t *)malloc(sizeof(file_t));
|
||||
if (this == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
f->path = path;
|
||||
this->path = path;
|
||||
|
||||
if (fileExists(f) == 0) {
|
||||
// if file 'f' does not exist
|
||||
fileCreateEmpty(f->path);
|
||||
return fileInit(f->path);
|
||||
if (file_exists(this) == false) {
|
||||
// if file 'this' does not exist
|
||||
file_create(this->path);
|
||||
return file_new(this->path);
|
||||
}
|
||||
|
||||
f->length = fileCountLines(f);
|
||||
this->length = file_count_lines(this);
|
||||
|
||||
f->lines = vectorInit(VECTOR); // vector of vectors of chars = vector of strings
|
||||
_fileLoadLines(f);
|
||||
this->lines = vector_new(VECTOR); // vector of vectors of chars = vector of strings
|
||||
_file_load_lines(this);
|
||||
|
||||
return f;
|
||||
return this;
|
||||
}
|
||||
|
||||
void
|
||||
fileCleanup(file_t* f) {
|
||||
if (f->not_newline_terminated == 1) {
|
||||
FILE* fd = fopen(f->path, "a");
|
||||
void file_cleanup(file_t *this) {
|
||||
if (this->not_newline_terminated == 1) {
|
||||
FILE *fd = fopen(this->path, "a");
|
||||
fprintf(fd, "\n");
|
||||
fclose(fd);
|
||||
f->not_newline_terminated = 0; // just for piece of mind
|
||||
this->not_newline_terminated = 0; // just for piece of mind
|
||||
}
|
||||
vectorCleanup(f->lines);
|
||||
free(f);
|
||||
|
||||
vector_cleanup(this->lines);
|
||||
free(this);
|
||||
}
|
||||
|
||||
void
|
||||
filePrint(file_t* f) {
|
||||
for (int i = 0; i < f->length; i++) {
|
||||
vectorPrintAsString((vector_t *)vectorGetAt(f->lines, i));
|
||||
void file_print(file_t *this) {
|
||||
for (int i = 0; i < this->length; i++) {
|
||||
vector_print_as_string((vector_t *)vector_get_at(this->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);
|
||||
void file_writeln(file_t *this, char *payload) {
|
||||
FILE *fd = fopen(this->path, "a");
|
||||
if (fd == NULL) {
|
||||
fclose(fd);
|
||||
fileCleanup(f);
|
||||
exit(EXIT_FAILURE);
|
||||
file_cleanup(this);
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(fd, "%s\n", payload);
|
||||
@@ -145,29 +134,28 @@ fileWriteln(file_t* f, char* payload) {
|
||||
fclose(fd);
|
||||
|
||||
// Update the file structure without re-initializing
|
||||
if (f->not_newline_terminated == 0) {
|
||||
vector_t* line = vectorInit(CHAR);
|
||||
if (this->not_newline_terminated == 0) {
|
||||
vector_t *line = vector_new(CHAR);
|
||||
for (int i = 0; i < strlen(payload); i++) {
|
||||
vectorPush(line, &payload[i]);
|
||||
vector_push(line, &payload[i]);
|
||||
}
|
||||
vectorPush(f->lines, line);
|
||||
f->length++;
|
||||
f->not_newline_terminated = 0;
|
||||
} else if (f->not_newline_terminated == 1) {
|
||||
vector_push(this->lines, line);
|
||||
this->length++;
|
||||
this->not_newline_terminated = 0;
|
||||
} else if (this->not_newline_terminated == 1) {
|
||||
for (int i = 0; i < strlen(payload); i++) {
|
||||
vectorPush((vector_t *)vectorGetAt(f->lines, f->length-1), &payload[i]);
|
||||
vector_push((vector_t *)vector_get_at(this->lines, this->length-1), &payload[i]);
|
||||
}
|
||||
f->not_newline_terminated = 0;
|
||||
this->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);
|
||||
void file_write(file_t *this, char *payload) {
|
||||
FILE *fd = fopen(this->path, "a");
|
||||
if (fd) {
|
||||
fprintf(stderr, "unable to open file %s with mode \"a\"\n", this->path);
|
||||
fclose(fd);
|
||||
fileCleanup(f);
|
||||
file_cleanup(this);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -176,28 +164,26 @@ fileWrite(file_t* f, char* payload) {
|
||||
fclose(fd);
|
||||
|
||||
// Update the file structure without re-initializing
|
||||
vector_t* line = vectorInit(CHAR);
|
||||
vector_t *line = vector_new(CHAR);
|
||||
for (int i = 0; i < strlen(payload); i++) {
|
||||
vectorPush(line, &payload[i]);
|
||||
vector_push(line, &payload[i]);
|
||||
}
|
||||
vectorPush(f->lines, line);
|
||||
f->length++;
|
||||
f->not_newline_terminated = 1;
|
||||
vector_push(this->lines, line);
|
||||
this->length++;
|
||||
this->not_newline_terminated = 1;
|
||||
}
|
||||
|
||||
void
|
||||
fileCopy(file_t* src, char* dst) {
|
||||
FILE* fd = fopen(dst, "w");
|
||||
if (!fd) {
|
||||
fprintf(stderr, "could not open file for writing\n");
|
||||
void file_copy(file_t *src, char *dst) {
|
||||
FILE *fd = fopen(dst, "w");
|
||||
if (fd == NULL) {
|
||||
fclose(fd);
|
||||
exit(EXIT_FAILURE);
|
||||
return;
|
||||
}
|
||||
|
||||
char ch;
|
||||
for (int i = 0; i < src->length; i++) {
|
||||
for (int j = 0; j < ((vector_t *)vectorGetAt(src->lines, i))->size; j++) {
|
||||
ch = *((char *)vectorGetAt((vector_t *)vectorGetAt(src->lines, i), j));
|
||||
for (int j = 0; j < ((vector_t *)vector_get_at(src->lines, i))->size; j++) {
|
||||
ch = *((char *)vector_get_at((vector_t *)vector_get_at(src->lines, i), j));
|
||||
fprintf(fd, "%c", ch);
|
||||
}
|
||||
fprintf(fd, "\n");
|
||||
@@ -206,68 +192,56 @@ fileCopy(file_t* src, char* dst) {
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
void
|
||||
fileDelete(file_t* f) {
|
||||
int retval = remove(f->path); // try to remove/delete a file
|
||||
|
||||
if (retval != 0) { // if it could not do so, raise an error
|
||||
fprintf(stderr, "could not delete file\n");
|
||||
// fileCleanup(f); // don't know if this is necessary... :shrug:
|
||||
// exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fileCleanup(f);
|
||||
void file_delete(file_t *this) {
|
||||
if (remove(this->path) == 0)
|
||||
file_cleanup(this);
|
||||
}
|
||||
|
||||
void
|
||||
fileMove(file_t* src, char* dst) {
|
||||
fileCopy(src, dst);
|
||||
void file_move(file_t *src, char *dst) {
|
||||
file_copy(src, dst);
|
||||
|
||||
fileDelete(src);
|
||||
file_delete(src);
|
||||
}
|
||||
|
||||
void
|
||||
fileNumberLines(file_t* f) {
|
||||
FILE* fd = fopen(f->path, "w");
|
||||
if (!fd) {
|
||||
fprintf(stderr, "could not open file for writing\n");
|
||||
void file_number_lines(file_t *this) {
|
||||
FILE *fd = fopen(this->path, "w");
|
||||
if (fd == NULL) {
|
||||
fclose(fd);
|
||||
exit(EXIT_FAILURE);
|
||||
return;
|
||||
}
|
||||
|
||||
int mlp = maxLowerPowerOf10(f->length);
|
||||
int mlp = max_lower_power_of_10(this->length);
|
||||
|
||||
for (int i = 0; i < f->length; i++) {
|
||||
for (int i = 0; i < this->length; i++) {
|
||||
// print to the file descriptor
|
||||
fprintf(fd, "%d %s\n", i+1, (char*)vectorToString((vector_t *)vectorGetAt(f->lines, i)));
|
||||
fprintf(fd, "%d %s\n", i+1, vector_to_string((vector_t *)vector_get_at(this->lines, i)));
|
||||
|
||||
// print to f->lines
|
||||
vectorInsertAt((vector_t*)vectorGetAt(f->lines, i), 0, &(char){' '});
|
||||
char* string = intToString(i+1);
|
||||
// print to this->lines
|
||||
vector_insert_at((vector_t*)vector_get_at(this->lines, i), 0, &(char){' '});
|
||||
char *string = int_to_string(i+1);
|
||||
for (int j = strlen(string)-1; j >= 0; j--) {
|
||||
vectorInsertAt((vector_t*)vectorGetAt(f->lines, i), 0, &(char){string[j]});
|
||||
vector_insert_at((vector_t*)vector_get_at(this->lines, i), 0, &(char){string[j]});
|
||||
}
|
||||
|
||||
// prepend the correct amount of ' '
|
||||
for (int j = 0; j < strlen(intToString(mlp))-strlen(intToString(i+1)); j++) {
|
||||
vectorInsertAt((vector_t*)vectorGetAt(f->lines, i), 0, &(char){' '});
|
||||
for (int j = 0; j < strlen(int_to_string(mlp))-strlen(int_to_string(i+1)); j++) {
|
||||
vector_insert_at((vector_t*)vector_get_at(this->lines, i), 0, &(char){' '});
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
int
|
||||
fileCountWords(file_t* f) {
|
||||
int file_count_words(file_t *this) {
|
||||
int num_words = 0;
|
||||
char prev;
|
||||
for (int i = 0; i < f->length; i++) {
|
||||
for (int j = 0; j < ((vector_t*)vectorGetAt(f->lines, i))->size; j++) {
|
||||
char ch = *((char*)vectorGetAt((vector_t*)vectorGetAt(f->lines, i), j));
|
||||
for (int i = 0; i < this->length; i++) {
|
||||
for (int j = 0; j < ((vector_t*)vector_get_at(this->lines, i))->size; j++) {
|
||||
char ch = *((char*)vector_get_at((vector_t*)vector_get_at(this->lines, i), j));
|
||||
if (
|
||||
(ch == ' ' && prev != ' ') ||
|
||||
(i == f->length-1 && j == ((vector_t*)vectorGetAt(f->lines, i))->size-1) ||
|
||||
(j == ((vector_t*)vectorGetAt(f->lines, i))->size-1 && j != 0)
|
||||
(i == this->length-1 && j == ((vector_t*)vector_get_at(this->lines, i))->size-1) ||
|
||||
(j == ((vector_t*)vector_get_at(this->lines, i))->size-1 && j != 0)
|
||||
) {
|
||||
num_words++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user