fixed some errors

This commit is contained in:
Odweta
2024-02-06 22:04:47 +01:00
parent e40b61a21b
commit f9e820efbc
10 changed files with 355 additions and 355 deletions
+70 -70
View File
@@ -5,11 +5,11 @@
#include <stdlib.h>
#include <stdio.h>
int file_count_lines(file_t *this) {
int file_count_lines(file_t *self) {
char ch;
int lines = 0;
FILE *fd = fopen(this->path, "r");
FILE *fd = fopen(self->path, "r");
if (fd == NULL) {
return -1;
}
@@ -25,18 +25,18 @@ int file_count_lines(file_t *this) {
return lines;
}
void _file_load_lines(file_t *this) {
void _file_load_lines(file_t *self) {
char ch;
FILE *fd = fopen(this->path, "r");
FILE *fd = fopen(self->path, "r");
if (fd == NULL) {
fclose(fd);
file_cleanup(this);
file_cleanup(self);
return;
}
char prev;
for (int i = 0; i < this->length; i++) {
for (int i = 0; i < self->length; i++) {
vector_t *line = vector_new(CHAR); // Create a new vector for each line
while ((ch = fgetc(fd)) != '\n' && ch != EOF) {
@@ -49,28 +49,28 @@ void _file_load_lines(file_t *this) {
vector_push(line, empty);
}
vector_push(this->lines, line);
vector_push(self->lines, line);
prev = ch;
// Don't free the line vector here, as it's now owned by the this->lines vector
// Don't free the line vector here, as it's now owned by the self->lines vector
}
if (prev != '\n') {
this->not_newline_terminated = 1; // \n is NOT at the end of a file
self->not_newline_terminated = 1; // \n is NOT at the end of a file
} else {
this->not_newline_terminated = 0; // \n IS at the end of a file
self->not_newline_terminated = 0; // \n IS at the end of a file
}
fclose(fd);
}
char *file_to_string(file_t *this) {
return vector_to_string(this->lines);
char *file_to_string(file_t *self) {
return vector_to_string(self->lines);
}
bool file_exists(file_t *this) {
FILE *fd = fopen(this->path, "r");
bool_t file_exists(file_t *self) {
FILE *fd = fopen(self->path, "r");
bool retval = (fd == NULL) ? false : true;
bool_t retval = (fd == NULL) ? FALSE : TRUE;
fclose(fd);
@@ -82,50 +82,50 @@ void file_create(char *path) {
}
file_t *file_new(char *path) {
file_t *this = (file_t *)malloc(sizeof(file_t));
if (this == NULL) {
file_t *self = (file_t *)malloc(sizeof(file_t));
if (self == NULL) {
return NULL;
}
this->path = path;
self->path = path;
if (file_exists(this) == false) {
// if file 'this' does not exist
file_create(this->path);
return file_new(this->path);
if (file_exists(self) == FALSE) {
// if file 'self' does not exist
file_create(self->path);
return file_new(self->path);
}
this->length = file_count_lines(this);
self->length = file_count_lines(self);
this->lines = vector_new(VECTOR); // vector of vectors of chars = vector of strings
_file_load_lines(this);
self->lines = vector_new(VECTOR); // vector of vectors of chars = vector of strings
_file_load_lines(self);
return this;
return self;
}
void file_cleanup(file_t *this) {
if (this->not_newline_terminated == 1) {
FILE *fd = fopen(this->path, "a");
void file_cleanup(file_t *self) {
if (self->not_newline_terminated == 1) {
FILE *fd = fopen(self->path, "a");
fprintf(fd, "\n");
fclose(fd);
this->not_newline_terminated = 0; // just for piece of mind
self->not_newline_terminated = 0; // just for piece of mind
}
vector_cleanup(this->lines);
free(this);
vector_cleanup(self->lines);
free(self);
}
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 file_print(file_t *self) {
for (int i = 0; i < self->length; i++) {
vector_print_as_string((vector_t *)vector_get_at(self->lines, i));
}
}
void file_writeln(file_t *this, char *payload) {
FILE *fd = fopen(this->path, "a");
void file_writeln(file_t *self, char *payload) {
FILE *fd = fopen(self->path, "a");
if (fd == NULL) {
fclose(fd);
file_cleanup(this);
file_cleanup(self);
return;
}
@@ -134,28 +134,28 @@ void file_writeln(file_t *this, char *payload) {
fclose(fd);
// Update the file structure without re-initializing
if (this->not_newline_terminated == 0) {
if (self->not_newline_terminated == 0) {
vector_t *line = vector_new(CHAR);
for (int i = 0; i < strlen(payload); i++) {
vector_push(line, &payload[i]);
}
vector_push(this->lines, line);
this->length++;
this->not_newline_terminated = 0;
} else if (this->not_newline_terminated == 1) {
vector_push(self->lines, line);
self->length++;
self->not_newline_terminated = 0;
} else if (self->not_newline_terminated == 1) {
for (int i = 0; i < strlen(payload); i++) {
vector_push((vector_t *)vector_get_at(this->lines, this->length-1), &payload[i]);
vector_push((vector_t *)vector_get_at(self->lines, self->length-1), &payload[i]);
}
this->not_newline_terminated = 0;
self->not_newline_terminated = 0;
}
}
void file_write(file_t *this, char *payload) {
FILE *fd = fopen(this->path, "a");
void file_write(file_t *self, char *payload) {
FILE *fd = fopen(self->path, "a");
if (fd) {
fprintf(stderr, "unable to open file %s with mode \"a\"\n", this->path);
fprintf(stderr, "unable to open file %s with mode \"a\"\n", self->path);
fclose(fd);
file_cleanup(this);
file_cleanup(self);
exit(EXIT_FAILURE);
}
@@ -168,9 +168,9 @@ void file_write(file_t *this, char *payload) {
for (int i = 0; i < strlen(payload); i++) {
vector_push(line, &payload[i]);
}
vector_push(this->lines, line);
this->length++;
this->not_newline_terminated = 1;
vector_push(self->lines, line);
self->length++;
self->not_newline_terminated = 1;
}
void file_copy(file_t *src, char *dst) {
@@ -192,9 +192,9 @@ void file_copy(file_t *src, char *dst) {
fclose(fd);
}
void file_delete(file_t *this) {
if (remove(this->path) == 0)
file_cleanup(this);
void file_delete(file_t *self) {
if (remove(self->path) == 0)
file_cleanup(self);
}
void file_move(file_t *src, char *dst) {
@@ -203,45 +203,45 @@ void file_move(file_t *src, char *dst) {
file_delete(src);
}
void file_number_lines(file_t *this) {
FILE *fd = fopen(this->path, "w");
void file_number_lines(file_t *self) {
FILE *fd = fopen(self->path, "w");
if (fd == NULL) {
fclose(fd);
return;
}
int mlp = max_lower_power_of_10(this->length);
int mlp = max_lower_power_of_10(self->length);
for (int i = 0; i < this->length; i++) {
for (int i = 0; i < self->length; i++) {
// print to the file descriptor
fprintf(fd, "%d %s\n", i+1, vector_to_string((vector_t *)vector_get_at(this->lines, i)));
fprintf(fd, "%d %s\n", i+1, vector_to_string((vector_t *)vector_get_at(self->lines, i)));
// print to this->lines
vector_insert_at((vector_t*)vector_get_at(this->lines, i), 0, &(char){' '});
// print to self->lines
vector_insert_at((vector_t*)vector_get_at(self->lines, i), 0, &(char){' '});
char *string = int_to_string(i+1);
for (int j = strlen(string)-1; j >= 0; j--) {
vector_insert_at((vector_t*)vector_get_at(this->lines, i), 0, &(char){string[j]});
vector_insert_at((vector_t*)vector_get_at(self->lines, i), 0, &(char){string[j]});
}
// prepend the correct amount of ' '
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){' '});
vector_insert_at((vector_t*)vector_get_at(self->lines, i), 0, &(char){' '});
}
}
fclose(fd);
}
int file_count_words(file_t *this) {
int file_count_words(file_t *self) {
int num_words = 0;
char prev;
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));
for (int i = 0; i < self->length; i++) {
for (int j = 0; j < ((vector_t*)vector_get_at(self->lines, i))->size; j++) {
char ch = *((char*)vector_get_at((vector_t*)vector_get_at(self->lines, i), j));
if (
(ch == ' ' && prev != ' ') ||
(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)
(i == self->length-1 && j == ((vector_t*)vector_get_at(self->lines, i))->size-1) ||
(j == ((vector_t*)vector_get_at(self->lines, i))->size-1 && j != 0)
) {
num_words++;
}