refactoring done, small bugfixes included, vectors and lists of non-primitive types are not working though

This commit is contained in:
Odweta
2024-02-05 00:16:25 +01:00
parent 8832a2a4aa
commit e40b61a21b
9 changed files with 631 additions and 797 deletions
+17 -16
View File
@@ -2,38 +2,39 @@
#define FILE_H
#include <odweta/vector.h>
#include <odweta/util.h>
typedef struct {
char* path; // the file descriptor
char *path; // the file descriptor
int length; // line count
vector_t* lines; // 2d array of chars
vector_t *lines; // 2d array of chars
int not_newline_terminated; // bool: 1 = true, 0 = false
} file_t;
file_t* fileInit(char* path); // initialization function
file_t *file_new(char *path); // initialization function
void fileCleanup(file_t* f); // just to be memory safe...
void file_cleanup(file_t *this); // just to be memory safe...
void filePrint(file_t* f); // prints the file as a string
void file_print(file_t *this); // prints the file as a string
char* fileToString(file_t* f); // returns a string with the file contents
char *file_to_string(file_t *this); // returns a string with the file contents
void fileWriteln(file_t* f, char* payload); // appends a given string to the file on a newline
void file_writeln(file_t *this, char *payload); // appends a given string to the file on a newline
void fileWrite(file_t* f, char* payload); // appends a given string to the file (not on a newline!)
void file_write(file_t *this, char *payload); // appends a given string to the file (not on a newline!)
void fileCopy(file_t* src, char* dst); // copy file src into file dst
void file_copy(file_t* src, char *dst); // copy file src into file dst
void fileDelete(file_t* f); // deletes a file
void file_delete(file_t *this); // deletes a file
void fileMove(file_t* src, char* dst); // moves/renames a file
void file_move(file_t* src, char *dst); // moves/renames a file
int fileExists(file_t* f); // 1 = yes, 0 = no
bool file_exists(file_t *this); // 1 = yes, 0 = no
void fileCreateEmpty(char* path); // create an empty file with the given name
void file_create(char *path); // create an empty file with the given name
void fileNumberLines(file_t* f); // prepend line numbers in front of each line in a file
void file_number_lines(file_t *this); // prepend line numbers in front of each line in a file
int fileCountWords(file_t* f); // counts the number of words in a file
int file_count_words(file_t *this); // counts the number of words in a file
#endif
#endif // FILE_H
+12 -12
View File
@@ -16,26 +16,26 @@
typedef struct {
size_t size; // number of elements that are inside the list
size_t capacity; // how many elements can be put inside
vector_t* data; // vector of vectors
vector_t *data; // vector of vectors
} list_t;
list_t* listInit(); // returns a new list
list_t *list_new(); // returns a new list
void listPush(list_t* l, dataType_e type, void* element); // appends an element at the end of a list
void list_push(list_t *this, type_e type, void *element); // appends an element at the end of a list
void listPrint(list_t* l); // prints a list
void _listPrintRecursive(list_t* l, int depth);
void list_show(list_t *this); // prints a list
void _list_print_recursive(list_t *this, int depth);
void listCleanup(list_t* l); // freeing pointers
void list_cleanup(list_t *this); // freeing pointers
void* listPop(list_t* l); // popping the last value of the list and returning it
void *list_pop(list_t *this); // popping the last value of the list and returning it
void listInsertAt(list_t* l, int index, dataType_e type, void* element); // inserts an element at a given index
void list_insert_at(list_t *this, int index, type_e type, void *element); // inserts an element at a given index
void listRemoveAt(list_t* l, int index);
void list_remove_at(list_t *this, int index);
void listSetAt(list_t* l, int index, dataType_e type, void* element);
void list_set_at(list_t *this, int index, type_e type, void *element);
void* listGetAt(list_t* l, int index);
void *list_get_at(list_t *this, int index);
#endif
#endif // LIST_H
+15 -12
View File
@@ -1,7 +1,9 @@
#ifndef UTIL_H
#define UTIL_H
#include <stdbool.h>
#undef bool
typedef enum { false, true } bool;
typedef enum {
LESS,
@@ -15,18 +17,19 @@ typedef enum {
typedef long unsigned int size_t;
typedef enum {
INT = 0,
FLOAT = 1,
CHAR = 2,
VECTOR = 3, // allows for multi-dimensional arrays
LIST = 4,
BOOL = 5
} dataType_e;
INT,
FLOAT,
CHAR,
VECTOR, // allows for multi-dimensional arrays
LIST,
BOOL,
STRING
} type_e;
char* intToString(int x); // convert an int into a string
char* int_to_string(int x); // convert an int into a string
long pow(int x, int n); // x^n
long pow(int x, int n); // x^n
int maxLowerPowerOf10(int x); // biggest power of 10 that is < x
int max_lower_power_of_10(int x); // biggest power of 10 that is < x
#endif
#endif // UTIL_H
+16 -28
View File
@@ -4,51 +4,39 @@
#include <odweta/util.h>
typedef struct {
dataType_e type;
type_e type;
size_t size;
size_t capacity;
size_t elementSize;
void* data;
size_t element_size;
void *data;
} vector_t;
vector_t*
vectorInit(dataType_e dataType); // size gets determined by the dataType and the capacity starts at 1 and x1.5 every time the vector gets filled up
vector_t *vector_new(type_e type); // size gets determined by the dataType and the capacity starts at 1 and x1.5 every time the vector gets filled up
void
vectorReset(vector_t* v); // sets the vector to its original state
void vector_reset(vector_t *this); // sets the vector to its original state
void*
vectorPop(vector_t* v); // remove one value from the end of the vector and return it
void *vector_pop(vector_t *this); // remove one value from the end of the vector and return it
void
vectorPush(vector_t* v, void* element); // push one value at the end of the vector
void vector_push(vector_t *this, void *element); // push one value at the end of the vector
void
vectorCleanup(vector_t* v); // freeing pointers
void vector_cleanup(vector_t *this); // freeing pointers
void
vectorPrint(vector_t* v); // shows the vector
void vector_print(vector_t *this); // shows the vector
void
vectorPrintAsString(vector_t* v); // show the vector as a string if the v->type is CHAR
void vector_print_as_string(vector_t *this); // show the vector as a string if the v->type is CHAR
void
vectorInsertAt(vector_t* v, int index, void* element); // inserts an element 'element' at index 'index'
void vector_insert_at(vector_t *this, int index, void *element); // inserts an element 'element' at index 'index'
void
vectorSetAt(vector_t* v, int index, void* element); // set an element's value at a given index
void vector_set_at(vector_t *this, int index, void *element); // set an element's value at a given index
char*
vectorToString(vector_t* v); // convert the vector into a regular string
char *vector_to_string(vector_t *this); // convert the vector into a regular string
void*
vectorGetAt(vector_t* v, int index); // get an element at the given index
void *vector_get_at(vector_t *this, int index); // get an element at the given index
void
vectorRemoveAt(vector_t* v, int index);
void vector_remove_at(vector_t *this, int index);
void _printVectorRecursive(vector_t* v, int depth);
void _vector_print_recursive(vector_t *this, int depth);
#endif