attempt to write a C file API, also fixed array list
This commit is contained in:
56
src/files/gryphn_file.c
Normal file
56
src/files/gryphn_file.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "gryphn_file.h"
|
||||
#include <stdio.h>
|
||||
// #include "fstream"
|
||||
// #include "iostream"
|
||||
|
||||
gnFile gnLoadFile(const gnString path, gnFileType type) {
|
||||
gnFile new_file = {
|
||||
.path = path,
|
||||
.type = type
|
||||
};
|
||||
|
||||
if (type == GN_FILE_TEXT) {
|
||||
new_file.data = malloc(sizeof(gnString));
|
||||
*((gnString*)new_file.data) = gnCreateEmptyString();
|
||||
}
|
||||
|
||||
if (type == GN_FILE_TEXT) {
|
||||
FILE *fptr;
|
||||
fptr = fopen(path.value, "r");
|
||||
|
||||
int ch;
|
||||
if (fptr != NULL) {
|
||||
while ((ch = fgetc(fptr)) != EOF) {
|
||||
putchar(ch); // or process the char
|
||||
}
|
||||
fclose(fptr);
|
||||
}
|
||||
}
|
||||
else if (type == GN_FILE_BINARY) {
|
||||
FILE *file = fopen(path.value, "rb");
|
||||
if (file) {
|
||||
fseek(file, 0, SEEK_END);
|
||||
long size = ftell(file);
|
||||
rewind(file);
|
||||
char *buffer = malloc(size);
|
||||
if (buffer) {
|
||||
fread(buffer, 1, size, file);
|
||||
// use buffer here
|
||||
free(buffer);
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
return new_file;
|
||||
}
|
||||
|
||||
gnString gnGetFileData(const gnFile file) { return *(gnString*)file.data;}
|
||||
// why I dont just return the file as a list of lines, cuz thats the easy way this way revealed so many errors in my string class,
|
||||
// dont you love it when you dont actually copy a string and then the data gets lost and you dont know why because your an idiot
|
||||
// thats never happened to me...... yea so im an idiot shouve actually copied the bytes over not just haphazardly asigned them to a string.
|
||||
|
||||
gnString gnGetFilePath(const gnFile file) {
|
||||
return file.path;
|
||||
}
|
||||
|
||||
gnChar* gnGetFileBytes(const gnFile file) { return (gnChar*)file.data; }
|
@@ -1,49 +0,0 @@
|
||||
#include "gryphn_file.h"
|
||||
#include "fstream"
|
||||
#include "iostream"
|
||||
|
||||
gnFile gnLoadFile(const gnString& path, gnFileType type) {
|
||||
gnFile new_file = gnFile();
|
||||
|
||||
if (type == gnFileType::Text) {
|
||||
std::ifstream file(gnToCString(path));
|
||||
if (!file.is_open()) throw std::runtime_error("failed to open text file!"); // thats right I wont use std::vector but il use std::runtime_error my priorities are straight
|
||||
// not as straight as me around ethan mooney, il see myself out now. (PS i actually did go to bed these will stop now)
|
||||
|
||||
std::string line;
|
||||
while(std::getline(file, line)) {
|
||||
gnListAdd(new_file.lines, gnCreateString(line.c_str()));
|
||||
}
|
||||
} else if (type == gnFileType::Binary) {
|
||||
std::ifstream file(gnToCString(path), std::ios::ate | std::ios::binary);
|
||||
|
||||
if (!file.is_open()) throw std::runtime_error("failed to open file!");
|
||||
|
||||
size_t file_size = (size_t)file.tellg();
|
||||
new_file.bytes = gnCreateList<gnChar>(file_size);
|
||||
|
||||
file.seekg(0);
|
||||
file.read(gnListData(new_file.bytes), file_size);
|
||||
|
||||
file.close(); // straight from vulkan-tutorial.com, not as strai, imma stop myself now
|
||||
}
|
||||
new_file.path = path;
|
||||
|
||||
return new_file;
|
||||
}
|
||||
|
||||
gnString gnGetFileData(const gnFile& file) {
|
||||
gnString file_data;
|
||||
for (int i = 0; i < gnListLength(file.lines); i++) {
|
||||
file_data += gnListGet(file.lines, i) + '\n';
|
||||
}
|
||||
return file_data;
|
||||
} // why I dont just return the file as a list of lines, cuz thats the easy way this way revealed so many errors in my string class,
|
||||
// dont you love it when you dont actually copy a string and then the data gets lost and you dont know why because your an idiot
|
||||
// thats never happened to me...... yea so im an idiot shouve actually copied the bytes over not just haphazardly asigned them to a string.
|
||||
|
||||
gnString gnGetFilePath(const gnFile& file) {
|
||||
return file.path;
|
||||
}
|
||||
|
||||
gnList<gnChar> gnGetFileBytes(const gnFile& file) { return file.bytes; }
|
@@ -1,44 +1,29 @@
|
||||
#pragma once
|
||||
#include <utils/gryphn_access_level.h>
|
||||
|
||||
// TODO: this file API is shit
|
||||
// have you ever wanted to write a file, well too bad cuz that shits a lot of work and im not doing allllll that
|
||||
// also like im pretty sure something is fucked up in the reading of binary files, what, I dont know but something is
|
||||
// other than that im pretty happy with it cuz like I still have zero fuckcking clue how it works
|
||||
// the binary part that is
|
||||
|
||||
#include "../strings/gryphn_string.h"
|
||||
#include "../lists/gryphn_list.h"
|
||||
#include "../lists/gryphn_array_list.h"
|
||||
|
||||
enum class gnFileType {
|
||||
Text, Binary
|
||||
};
|
||||
typedef enum gnFileType {
|
||||
GN_FILE_TEXT, GN_FILE_BINARY
|
||||
} gnFileType;
|
||||
|
||||
struct gnFile {
|
||||
ACCESS_LEVEL:
|
||||
gnList<gnString> lines = gnCreateList<gnString>(); // if file type is string
|
||||
gnList<gnChar> bytes = gnCreateList<gnChar>(); // if file type is binary
|
||||
// ofc dumbass they should know what file type there loading
|
||||
// wellllllll actually this is horrible cuz im storing the like 4 bytes it take for an empty list in every loaded file
|
||||
// regardless of if there is actually any data in the list
|
||||
// buttttt as code astetic once said "premature optimization is like totally the root of all evil bro *procedes to hit bong*"
|
||||
// i think i remember that right, i need to fix this but templates are hard and im not
|
||||
typedef struct gnFile {
|
||||
void* data;
|
||||
// GN_FILE_TEXT = gnString
|
||||
// GN_FILE_BINARY = gnChar*
|
||||
gnString path;
|
||||
gnFileType type;
|
||||
} gnFile;
|
||||
|
||||
gnString path = gnCreateString();
|
||||
gnFileType type = gnFileType::Text; // who loads binary files anyway *silence*, i do I guess thats why I wrote this
|
||||
public:
|
||||
gnFile() {}
|
||||
};
|
||||
#ifdef GN_UITLS_CPP
|
||||
gnFile gnLoadFile(const gnString& path, gnFileType type = GN_FILE_TEXT);
|
||||
#else
|
||||
gnFile gnLoadFile(const gnString path, gnFileType type);
|
||||
#endif
|
||||
// gnFile gnCreateFile(const gnString path);
|
||||
// gnFile gnWriteFile(const gnFile file);
|
||||
|
||||
gnFile gnLoadFile(const gnString& path, gnFileType type = gnFileType::Text);
|
||||
// gnFile gnCreateFile(const gnString& path);
|
||||
// gnFile gnCreateFile();
|
||||
// gnFile gnWriteFile(const gnFile& file);
|
||||
// which fucking loser thought they were writing a file writing API
|
||||
// ...... that was me, im lazy, its 10:30, 2.5 hours after I go to sleep, imma go to sleep.
|
||||
|
||||
gnString gnGetFilePath(const gnFile& file);
|
||||
gnString gnGetFileData(const gnFile& file); // i should rename this
|
||||
gnList<gnChar> gnGetFileBytes(const gnFile& file); // fuck object oriented code
|
||||
gnString gnGetFilePath(const gnFile file);
|
||||
gnString gnGetFileData(const gnFile file); // i should rename this
|
||||
gnChar* gnGetFileBytes(const gnFile file); // fuck object oriented code
|
||||
// and fuck error detection the user can get the bytes of a text file if they want to cuz its slow to do error checking, its also smart but im slow
|
||||
// so me and this project are one in the same
|
||||
|
@@ -5,12 +5,12 @@ typedef struct gnArrayList {
|
||||
int count;
|
||||
int maxCount;
|
||||
void* data;
|
||||
} gnCList;
|
||||
} gnArrayList;
|
||||
|
||||
const int GROWTH_RATE = 2; // i heard somewhere that 1.5 is better but imma use 2 because I also heard that its better somewhere else
|
||||
|
||||
inline gnArrayList gnCreateArrayList(int count) {
|
||||
gnCList newList;
|
||||
gnArrayList newList;
|
||||
|
||||
if (count == 0) {
|
||||
|
||||
|
Reference in New Issue
Block a user