diff options
| author | nasr <nsrddyn@gmail.com> | 2026-03-03 20:05:11 +0000 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-03-03 20:05:11 +0000 |
| commit | 7165bb7ae9e34e69d0c8802d6e356851cffe0f07 (patch) | |
| tree | c2af6059cb3bcf38277188583af7d41207cf7cc0 /source | |
| parent | 4e77bc7164070d7ffafdee1ba6ce3bb1aaf10746 (diff) | |
feature(main): loading and printing csv file
Diffstat (limited to 'source')
| -rw-r--r-- | source/base/base_os.h | 41 | ||||
| -rw-r--r-- | source/base/base_string.h | 4 | ||||
| -rw-r--r-- | source/engine/engine.c | 33 | ||||
| -rw-r--r-- | source/storage/csv_reader.c | 9 | ||||
| -rw-r--r-- | source/storage/csv_reader.h | 6 |
5 files changed, 52 insertions, 41 deletions
diff --git a/source/base/base_os.h b/source/base/base_os.h index ec0a3de..23587c6 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h | |||
| @@ -14,39 +14,24 @@ load_file(const char *path) | |||
| 14 | { | 14 | { |
| 15 | string8 result = {0}; | 15 | string8 result = {0}; |
| 16 | struct stat sbuf = {0}; | 16 | struct stat sbuf = {0}; |
| 17 | int err = 0; | 17 | |
| 18 | i32 file = open(path, O_RDONLY); | 18 | i32 file = open(path, O_RDONLY); |
| 19 | if(file == -1) return result; | ||
| 19 | 20 | ||
| 20 | if(file) | 21 | if(fstat(file, &sbuf) == -1) |
| 21 | { | 22 | { |
| 23 | print("error: fstat failed"); | ||
| 24 | close(file); | ||
| 25 | return result; | ||
| 26 | } | ||
| 22 | 27 | ||
| 23 | if(file != -1) | 28 | result.size = (u64)sbuf.st_size; |
| 24 | { | 29 | if(result.size != 0) |
| 25 | err = fstat(file, &sbuf); | 30 | { |
| 26 | check(err != -1); | 31 | result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0); |
| 27 | |||
| 28 | result.size = (u64)sbuf.st_size; | ||
| 29 | |||
| 30 | if(result.size != 0) | ||
| 31 | { | ||
| 32 | result.data = (u8 *)mmap(0, | ||
| 33 | result.size, | ||
| 34 | PROT_READ, | ||
| 35 | MAP_PRIVATE, | ||
| 36 | file, | ||
| 37 | 0); | ||
| 38 | |||
| 39 | check(result.data != MAP_FAILED); | ||
| 40 | } | ||
| 41 | |||
| 42 | close(file); | ||
| 43 | } | ||
| 44 | else | ||
| 45 | { | ||
| 46 | // TODO(nasr): logging | ||
| 47 | } | ||
| 48 | |||
| 49 | } | 32 | } |
| 33 | |||
| 34 | close(file); | ||
| 50 | return result; | 35 | return result; |
| 51 | } | 36 | } |
| 52 | 37 | ||
diff --git a/source/base/base_string.h b/source/base/base_string.h index 941dc53..64a3162 100644 --- a/source/base/base_string.h +++ b/source/base/base_string.h | |||
| @@ -9,6 +9,10 @@ | |||
| 9 | #define PushString(arena, size) \ | 9 | #define PushString(arena, size) \ |
| 10 | (string8){ (u8 *)PushArray((arena), u8, (size)), (u64)(size) } | 10 | (string8){ (u8 *)PushArray((arena), u8, (size)), (u64)(size) } |
| 11 | 11 | ||
| 12 | #define StringFmt "%.*s" | ||
| 13 | #define ULongFmt "%lu" | ||
| 14 | #define ULLongFmt "%llu" | ||
| 15 | |||
| 12 | typedef struct string8 string8; | 16 | typedef struct string8 string8; |
| 13 | struct string8 | 17 | struct string8 |
| 14 | { | 18 | { |
diff --git a/source/engine/engine.c b/source/engine/engine.c index 609101e..ada4ecb 100644 --- a/source/engine/engine.c +++ b/source/engine/engine.c | |||
| @@ -10,25 +10,32 @@ | |||
| 10 | #include "../repl/repl.h" | 10 | #include "../repl/repl.h" |
| 11 | #include "../repl/repl.c" | 11 | #include "../repl/repl.c" |
| 12 | 12 | ||
| 13 | int main(int c, char **v) | 13 | #include "../storage/csv_reader.h" |
| 14 | { | 14 | #include "../storage/csv_reader.c" |
| 15 | mem_arena *global_arena = arena_create(MiB(1)); | 15 | |
| 16 | #if 1 | ||
| 17 | #include <stdio.h> | ||
| 18 | #endif | ||
| 16 | 19 | ||
| 17 | unused(c); | ||
| 18 | unused(v); | ||
| 19 | 20 | ||
| 20 | string8 buffer = PushString(global_arena, 5); | 21 | int main(int c, char **v) |
| 21 | unused(buffer); | 22 | { |
| 23 | if(c < 2) return -999; | ||
| 22 | 24 | ||
| 25 | string8 buffer = load_file(v[1]); | ||
| 26 | read_csv(buffer); | ||
| 23 | 27 | ||
| 24 | for (;;) | ||
| 25 | { | ||
| 26 | print("reading user input..."); | ||
| 27 | // TODO(nasr): design a repl system | ||
| 28 | 28 | ||
| 29 | sleep(1); | 29 | // for(;;) |
| 30 | } | 30 | // { |
| 31 | // print("reading user input..."); | ||
| 32 | // // TODO(nasr): design a repl system | ||
| 33 | // | ||
| 34 | // sleep(1); | ||
| 35 | // } | ||
| 36 | // | ||
| 31 | 37 | ||
| 38 | return 0; | ||
| 32 | } | 39 | } |
| 33 | 40 | ||
| 34 | 41 | ||
diff --git a/source/storage/csv_reader.c b/source/storage/csv_reader.c index e69de29..6023a3a 100644 --- a/source/storage/csv_reader.c +++ b/source/storage/csv_reader.c | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | #if 1 | ||
| 2 | #include <stdio.h> | ||
| 3 | #endif | ||
| 4 | |||
| 5 | internal void | ||
| 6 | read_csv(string8 buffer) | ||
| 7 | { | ||
| 8 | printf("\nsize:%lu\ndata %s\n", buffer.size, buffer.data); | ||
| 9 | } | ||
diff --git a/source/storage/csv_reader.h b/source/storage/csv_reader.h index e69de29..29ac8ab 100644 --- a/source/storage/csv_reader.h +++ b/source/storage/csv_reader.h | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #ifndef CSV_READER_H | ||
| 2 | #define CSV_READER_H | ||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | #endif /* CSV_READER_H */ | ||
