aboutsummaryrefslogtreecommitdiffstats
path: root/src/badlib.h
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2024-06-15 13:00:59 +0200
committerBad Diode <bd@badd10de.dev>2024-06-15 13:00:59 +0200
commit805efd71e0f5b10a6e78da08565407ec0a3649fe (patch)
tree895d44056643337aafad451749790ba438732428 /src/badlib.h
parent4c92dafaea614d50903d5adc61c069b21d42a9cf (diff)
downloadbdl-805efd71e0f5b10a6e78da08565407ec0a3649fe.tar.gz
bdl-805efd71e0f5b10a6e78da08565407ec0a3649fe.zip
Add initial scanner functions
Diffstat (limited to 'src/badlib.h')
-rw-r--r--src/badlib.h20
1 files changed, 6 insertions, 14 deletions
diff --git a/src/badlib.h b/src/badlib.h
index 9bab563..a91fcf8 100644
--- a/src/badlib.h
+++ b/src/badlib.h
@@ -571,6 +571,9 @@ str_next(Str *s) {
571char 571char
572str_peek(Str s) { 572str_peek(Str s) {
573 assert(s.mem); 573 assert(s.mem);
574 if (s.size == 0) {
575 return EOF;
576 }
574 return *s.mem; 577 return *s.mem;
575} 578}
576 579
@@ -936,7 +939,6 @@ typedef enum {
936 FILE_ERR_OK = 0, 939 FILE_ERR_OK = 0,
937 FILE_ERR_CANT_OPEN, 940 FILE_ERR_CANT_OPEN,
938 FILE_ERR_READ_ERR, 941 FILE_ERR_READ_ERR,
939 FILE_ERR_PATH_TOO_BIG,
940 FILE_ERR_EMPTY, 942 FILE_ERR_EMPTY,
941 FILE_ERR_NUM, 943 FILE_ERR_NUM,
942} FileErr; 944} FileErr;
@@ -945,8 +947,7 @@ Str file_err_str[FILE_ERR_NUM] = {
945 cstr(""), 947 cstr(""),
946 cstr("couldn't open file"), 948 cstr("couldn't open file"),
947 cstr("couldn't read file"), 949 cstr("couldn't read file"),
948 cstr("file path too big"), 950 cstr("empty file"),
949 cstr("file is empty"),
950}; 951};
951 952
952typedef struct FileContents { 953typedef struct FileContents {
@@ -958,19 +959,11 @@ typedef struct FileContents {
958FileContents 959FileContents
959platform_read_file(Str path, Arena *a) { 960platform_read_file(Str path, Arena *a) {
960 // Transform Str to cstr. 961 // Transform Str to cstr.
961 char path_str[KB(1)]; 962 Str path_str = str_concat(path, cstr("\0"), a);
962 if (path.size >= KB(1) - 1) {
963 return (FileContents){
964 .path = path,
965 .err = FILE_ERR_PATH_TOO_BIG,
966 };
967 }
968 memcpy(path_str, path.mem, path.size);
969 path_str[path.size] = 0;
970 963
971 // Read the entire file into memory. 964 // Read the entire file into memory.
972 sz file_size = 0; 965 sz file_size = 0;
973 FILE *fp = fopen(path_str, "rb+"); 966 FILE *fp = fopen((char*)path_str.mem, "rb+");
974 if (!fp) { 967 if (!fp) {
975 return (FileContents){ 968 return (FileContents){
976 .path = path, 969 .path = path,
@@ -989,7 +982,6 @@ platform_read_file(Str path, Arena *a) {
989 .err = FILE_ERR_EMPTY, 982 .err = FILE_ERR_EMPTY,
990 }; 983 };
991 } 984 }
992
993 return (FileContents){ 985 return (FileContents){
994 .path = path, 986 .path = path,
995 .data = (Array){.mem = memory, .size = file_size}, 987 .data = (Array){.mem = memory, .size = file_size},