initial commit

This commit is contained in:
Odweta
2023-12-07 20:03:18 +01:00
commit 7fb1267224
8 changed files with 722 additions and 0 deletions
+105
View File
@@ -0,0 +1,105 @@
#include <odweta/file.h>
#include <odweta/vector.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int
fileCountLines(file_t* f) {
char ch;
int lines = 0;
while ((ch = fgetc(f->descriptor)) != EOF) {
if (ch == '\n') {
lines++;
}
}
rewind(f->descriptor);
return lines;
}
void
_fileLoadLines(file_t* f) {
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) {
vectorPush(line, &ch);
}
// Skip the newline character if it exists
if (ch == '\n') {
char* empty = "";
vectorPush(line, empty);
}
vectorPush(f->lines, line);
line = vectorInit(CHAR);
}
rewind(f->descriptor);
}
char*
fileToString(file_t* f) {
return (char *)vectorToString(f->lines);
}
file_t*
fileInit(char* path, char* mode) {
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->descriptor = fopen(path, mode);
if (f->descriptor == NULL) {
fprintf(stderr, "unable to open file %s with mode %s\n", path, mode);
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);
return f;
}
void
fileCleanup(file_t* f) {
vectorCleanup(f->lines);
fclose(f->descriptor);
free(f);
}
void
filePrint(file_t* f) {
vector_t* line = vectorInit(CHAR);
for (int i = 0; i < f->length; i++) {
line = (vector_t *)vectorGetAt(f->lines, i);
vectorPrintAsString(line);
}
}
+385
View File
@@ -0,0 +1,385 @@
#include <odweta/vector.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
vectorCleanup(vector_t* v) {
free(v->data);
free(v);
}
int printNewline = 0;
void
vectorReset(vector_t* v) {
while (v->size > 0) {
vectorPop(v);
}
}
void
_printVectorRecursive(vector_t* v, int depth) {
int i;
fprintf(stdout, "[");
for (i = 0; i < v->size; i++) {
switch (v->type) {
case INT:
fprintf(stdout, "%d", *((int *)(v->data) + i));
break;
case FLOAT:
fprintf(stdout, "%f", *((float *)(v->data) + i));
break;
case CHAR:
fprintf(stdout, "%c", *((char *)(v->data) + i));
break;
case VECTOR:
// Recursively print each level of the vector
_printVectorRecursive((vector_t *)(v->data + i * v->elementSize), depth + 1);
break;
default:
perror("tried to print unsupported data type\n");
exit(EXIT_FAILURE);
}
if (i < v->size - 2 && i != v->size - 1) {
fprintf(stdout, ", ");
}
}
fprintf(stdout, "]");
if (depth != 0) {
fprintf(stdout, "\n");
}
}
void
vectorPrint(vector_t* v) {
fprintf(stdout, "[");
int i;
for (i = 0; i < v->size; i++) {
switch (v->type) {
case INT:
fprintf(stdout, "%d", *((int *)(v->data) + i));
break;
case FLOAT:
fprintf(stdout, "%f", *((float *)(v->data) + i));
break;
case CHAR:
fprintf(stdout, "%c", *((char *)(v->data) + i));
break;
case VECTOR:
// Print an nD vector
_printVectorRecursive((vector_t *)(v->data + i * v->elementSize), 0);
break;
default:
perror("tried to print unsupported data type\n");
exit(EXIT_FAILURE);
}
if (i < v->size - 1) {
fprintf(stdout, ", ");
}
}
fprintf(stdout, "]");
if (printNewline == 0) {
fprintf(stdout, "\n");
} else {
fprintf(stdout, "]");
}
}
void
_printVectorAsStringRecursive(vector_t* v, int depth) {
int i;
for (i = 0; i < v->size; i++) {
switch (v->type) {
case CHAR:
fprintf(stdout, "%c", *((char *)(v->data) + i));
break;
case VECTOR:
// Recursively print each level of the vector
_printVectorAsStringRecursive((vector_t *)(v->data + i * v->elementSize), depth + 1);
break;
default:
perror("tried to print a non-char type vector as string\n");
exit(EXIT_FAILURE);
}
}
if (depth != 0) fprintf(stdout, "\n"); // do not print the last additional new line
}
void
vectorPrintAsString(vector_t* v) {
switch (v->type) {
case CHAR:
for (int i = 0; i < v->size; i++) {
fprintf(stdout, "%c", *((char *)(v->data) + i));
}
fprintf(stdout, "\n");
break;
case VECTOR:
_printVectorAsStringRecursive(v, 0);
break;
default:
perror("tried to print a non-char type vector as string\n");
exit(EXIT_FAILURE);
}
}
vector_t*
vectorInit(dataType_e type) {
vector_t *v = malloc(sizeof(vector_t));
if (v == NULL) {
perror("failed to allocate memory for the vector\n");
exit(EXIT_FAILURE);
}
v->type = type;
v->size = 0;
v->capacity = 1;
switch (v->type) {
case INT:
v->elementSize = sizeof(int);
break;
case FLOAT:
v->elementSize = sizeof(float);
break;
case CHAR:
v->elementSize = sizeof(char);
break;
case VECTOR:
v->elementSize = sizeof(vector_t);
break;
default:
perror("specify a valid vector type\n");
exit(EXIT_FAILURE);
}
v->data = malloc(v->elementSize * v->capacity);
if (v->data == NULL) {
perror("failed to allocate memory for vector data\n");
exit(EXIT_FAILURE);
}
return v;
}
void
vectorPush(vector_t* v, void* element) {
switch (v->type) {
case INT:
*((int *)(v->data) + v->size) = *((int *)element);
break;
case FLOAT:
*((float *)(v->data) + v->size) = *((float *)element);
break;
case CHAR:
*((char *)(v->data) + v->size) = *((char *)element);
break;
case VECTOR:
// Instead of reallocating memory for vector_t pointers, allocate memory for the vector data
if (v->size == 0) {
// Allocate memory for the vector data for the first time
v->data = malloc(v->elementSize * v->capacity);
} else {
// Reallocate memory for the vector data
v->data = realloc(v->data, v->elementSize * v->capacity);
}
if (v->data == NULL) {
perror("failed to allocate memory for vector data\n");
exit(EXIT_FAILURE);
}
// Copy the vector data from the element to the vector
memcpy((vector_t *)(v->data) + v->size, (vector_t *)element, sizeof(vector_t));
break;
default:
perror("specify a valid vector type\n");
exit(EXIT_FAILURE);
}
v->size += 1;
if (v->size == v->capacity) {
v->capacity *= 2;
}
}
void*
vectorPop(vector_t* v) {
// capacity is always a power of 2, so a rounding error cannot happen i guess _/-(shi)-\_
switch (v->type) {
case INT:
int *iretval = (int *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
v->data = realloc(v->data, v->elementSize * v->capacity);
if (v->capacity == 0) v->capacity = 1;
return iretval;
case FLOAT:
float *fretval = (float *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
if (v->capacity == 0) v->capacity = 1;
v->data = realloc(v->data, v->elementSize * v->capacity);
return fretval;
case CHAR:
char *cretval = (char *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
if (v->capacity == 0) v->capacity = 1;
v->data = realloc(v->data, v->elementSize * v->capacity);
return cretval;
case VECTOR:
vector_t *vretval = (vector_t *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
if (v->capacity == 0) v->capacity = 1;
v->data = realloc(v->data, v->elementSize * v->capacity);
return vretval;
default:
perror("specify a valid vector type\n");
exit(EXIT_FAILURE);
}
}
char*
vectorToString(vector_t* v) {
char* retString;
int totalSize = 0;
// Calculate the total size
for (int i = 0; i < v->size; i++) {
if (v->type == CHAR) {
totalSize += 1; // For a single character
} else if (v->type == VECTOR) {
vector_t* innerVector = (vector_t *)vectorGetAt(v, i);
char* innerString = vectorToString(innerVector);
totalSize += strlen(innerString);
// Add newline between vectors (except the last one)
if (i < v->size - 1) {
totalSize += 1;
}
free(innerString); // Free the memory allocated for innerString
}
}
// Allocate memory for the string
retString = (char *)malloc((totalSize + 1 + 1) * sizeof(char)); // +1 for null terminator; +1 for ending '\n'
// Copy the elements to the string
int index = 0;
for (int i = 0; i < v->size; i++) {
if (v->type == CHAR) {
retString[index++] = *((char *)vectorGetAt(v, i));
} else if (v->type == VECTOR) {
vector_t* innerVector = (vector_t *)vectorGetAt(v, i);
char* innerString = vectorToString(innerVector);
strcpy(retString + index, innerString);
index += strlen(innerString);
// Add newline between vectors (including the last one)
if (i < v->size) {
retString[index++] = '\n';
}
free(innerString); // Free the memory allocated for innerString
}
}
retString[index] = '\0'; // Null-terminate the string
return retString;
}
void*
vectorGetAt(vector_t* v, int index) {
switch (v->type) {
case INT:
return (int *)(v->data) + index;
case FLOAT:
return (float *)(v->data) + index;
case CHAR:
return (char *)(v->data) + index;
case VECTOR:
return (vector_t *)(v->data) + index;
default:
perror("specify a valid vector type\n");
exit(EXIT_FAILURE);
}
}
void
vectorSetAt(vector_t* v, int index, void* element) {
if (index >= v->size) {
perror("index out of range\n");
exit(EXIT_FAILURE);
}
switch (v->type) {
case INT:
*((int *)(v->data) + index) = *((int *)element);
break;
case FLOAT:
*((float *)(v->data) + index) = *((float *)element);
break;
case CHAR:
*((char *)(v->data) + index) = *((char *)element);
break;
case VECTOR:
*((vector_t *)(v->data) + index) = *((vector_t *)element);
break;
default:
perror("specify a valid vector type\n");
exit(EXIT_FAILURE);
}
}
void
vectorInsertAt(vector_t* v, int index, void* element) {
if (index >= v->size) {
perror("index out of range\n");
exit(EXIT_FAILURE);
}
v->size += 1;
if (v->size >= v->capacity) {
v->capacity *= 2;
v->data = realloc(v->data, v->elementSize * v->capacity);
}
switch (v->type) {
case INT:
// move all elements to the right by one
for (int i = v->size-1; i > index; i--) {
*((int *)(v->data) + i) = *((int *)(v->data) + i-1);
}
// set the element at the given index
*((int *)(v->data) + index) = *((int *)element);
break;
case FLOAT:
for (int i = v->size-1; i > index; i--) {
*((float *)(v->data) + i) = *((float *)(v->data) + i-1);
}
*((float *)(v->data) + index) = *((float *)element);
break;
case CHAR:
for (int i = v->size-1; i > index; i--) {
*((char *)(v->data) + i) = *((char *)(v->data) + i-1);
}
*((char *)(v->data) + index) = *((char *)element);
break;
case VECTOR:
for (int i = v->size-1; i > index; i--) {
*((vector_t *)(v->data) + i) = *((vector_t *)(v->data) + i-1);
}
*((vector_t *)(v->data) + index) = *((vector_t *)element);
break;
default:
perror("specify a valid vector type\n");
exit(EXIT_FAILURE);
}
}