Starting to work on files
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 5 May 2020 21:41:12 +0000 (16:41 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 5 May 2020 21:41:12 +0000 (16:41 -0500)
Makefile
bh.h
demo.onyx [deleted file]
onyx
onyx.c
progs/demo.onyx [new file with mode: 0644]

index 1df1e52ecaf423f657bc68b0374dea8b131f2a9b..2f544b466121dc719beb581160546b0d1aeaa277 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,10 +6,13 @@ INCLUDES=
 LIBS=
 FLAGS=-g
 
-%.o: %.c
+%.o: %.c bh.h
        $(CC) $(FLAGS) -c $< -o $@ $(INCLUDES)
 
 onyx: $(OBJ_FILES)
        $(CC) $(FLAGS) $< -o $@ $(LIBS)
 
+clean:
+       rm $(OBJ_FILES) 2>&1 >/dev/null
+
 all: onyx clean
diff --git a/bh.h b/bh.h
index a08aad4f3d52edf23c9c314881e56e1463c98bdd..6e4f2d057c70854b33a44ab3aa71f6cfddb6061f 100644 (file)
--- a/bh.h
+++ b/bh.h
@@ -62,7 +62,7 @@ void bh_string_append_cstr(bh_string* str1, const char* str2);
 void bh_string_replace_at_bh_string(bh_string* dest, bh_string* src, u64 offset);
 void bh_string_replace_at_cstr(bh_string* dest, const char* src, u64 offset);
 void bh_string_insert_at_bh_string(bh_string* dest, bh_string* src, u64 offset);
-void bh_string_insert_at_cstr(bh_string* dest, bh_string* src, u64 offset);
+void bh_string_insert_at_cstr(bh_string* dest, const char* src, u64 offset);
 void bh_string_trim_end(bh_string* str, const char* charset);
 void bh_string_trim_begin(bh_string* str, const char* charset);
 void bh_string_trim_end_space(bh_string* str);
@@ -70,6 +70,49 @@ void bh_string_trim_end_space(bh_string* str);
 void bh_string_print(bh_string* str);
 
 
+//-------------------------------------------------------------------------------------
+// Better files
+//-------------------------------------------------------------------------------------
+
+typedef enum bh_file_error {
+       BH_FILE_ERROR_NONE
+} bh_file_error;
+
+typedef enum bh_file_mode {
+       BH_FILE_MODE_READ = 1 << 0,
+       BH_FILE_MODE_WRITE = 1 << 1,
+       BH_FILE_MODE_APPEND = 1 << 2,
+       BH_FILE_MODE_RW = 1 << 3,
+
+       BH_FILE_MODE_MODES = BH_FILE_MODE_READ | BH_FILE_MODE_WRITE | BH_FILE_MODE_APPEND | BH_FILE_MODE_RW
+} bh_file_mode;
+
+typedef enum bh_file_whence {
+       bh_file_whence_begin = 0,
+       bh_file_whence_current = 1,
+       bh_file_whence_end = 2,
+} bh_file_whence;
+
+typedef int bh_file_descriptor;
+
+typedef struct bh_file {
+       bh_file_descriptor fd;
+       char const* filename;
+} bh_file;
+
+typedef enum bh_file_standard {
+       BH_FILE_STANDARD_INPUT,
+       BH_FILE_STANDARD_OUTPUT,
+       BH_FILE_STANDARD_ERROR
+} bh_file_standard;
+
+bh_file_error bh_file_get_standard(bh_file* file, bh_file_standard stand);
+
+bh_file_error bh_file_create(bh_file* file, char const* filename);
+bh_file_error bh_file_open(bh_file* file, char const* filename);
+bh_file_error bh_file_open_mode(bh_file* file, bh_file_mode mode, char const* filename);
+bh_file_error bh_file_new(bh_file* file, bh_file_descriptor fd, char const* filename);
+
 //-------------------------------------------------------------------------------------
 // IMPLEMENTATIONS
 //-------------------------------------------------------------------------------------
@@ -151,62 +194,56 @@ void bh_string_replace_at_cstr(bh_string* dest, const char* src, u64 offset) {
 }
 
 void bh_string_insert_at_bh_string(bh_string* dest, bh_string* src, u64 offset) {
-}
+       if (!bh_string_ensure_capacity(dest, dest->length + src->length)) return;
 
-void bh_string_insert_at_cstr(bh_string* dest, bh_string* src, u64 offset) {
+       memmove(dest->data + offset + src->length, dest->data + offset, dest->length + src->length - offset);
+       memcpy(dest->data + offset, src->data, src->length);
+       dest->length += src->length;
 }
 
-void bh_string_trim_end(bh_string* str, const char* charset) {
-}
+void bh_string_insert_at_cstr(bh_string* dest, const char* src, u64 offset) {
+       const int srclen = strlen(src);
+       if (!bh_string_ensure_capacity(dest, dest->length + srclen)) return;
 
-void bh_string_trim_begin(bh_string* str, const char* charset) {
+       // TODO: Use something better. This copies to a seperate buffer first
+       memmove(dest->data + offset + srclen, dest->data + offset, dest->length + srclen - offset);
+       memcpy(dest->data + offset, src, srclen);
+       dest->length += srclen;
 }
 
-void bh_string_trim_end_space(bh_string* str) {
-}
+static inline u8 charset_contains(const char* charset, char ch) {
+       while (*charset) {
+               if (*charset == ch) return *charset;
+               charset++;
+       }
 
-// TEMP
-void bh_string_print(bh_string* str) {
-       write(STDOUT_FILENO, str->data, str->capacity);
+       return 0;
 }
 
+void bh_string_trim_end(bh_string* str, const char* charset) {
+       while (charset_contains(charset, str->data[str->length - 1]))
+               str->length--;
+}
 
+void bh_string_trim_begin(bh_string* str, const char* charset) {
+       u32 off = 0, i;
+       while (charset_contains(charset, str->data[off])) off++;
 
-//-------------------------------------------------------------------------------------
-// Better files
-//-------------------------------------------------------------------------------------
-typedef struct File {
-       i32 fd;
-       u8 open : 1;
-       const char* path;
-} File;
-
-int file_open(File *f, const char* path, int flags) {
-       f->fd = open(path, flags,
-               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH       // +rw-rw-rw
-       );
-
-       if (f->fd < 0)
-               return 1;
-
-       f->open = 1;
-       f->path = path;
-       return 0;
-}
+       if (off == 0) return;
 
-int file_close(File* f) {
-       if (close(f->fd) < 0) {
-               return errno;
+       for (i = 0; i < str->length - off; i++) {
+               str->data[i] = str->data[i + off];
        }
 
-       f->open = 0;
-       return 0;
+       str->length -= off;
 }
 
-unsigned char file_read_byte(File* f) {
-       if (!f->open) return 0;
+void bh_string_trim_end_space(bh_string* str) {
+       bh_string_trim_end(str, " \t\n\r");
+}
 
-       unsigned char byte;
-       read(f->fd, &byte, 1);
-       return byte;
+// TEMP
+void bh_string_print(bh_string* str) {
+       write(STDOUT_FILENO, str->data, str->length);
 }
+
diff --git a/demo.onyx b/demo.onyx
deleted file mode 100644 (file)
index ee02d76..0000000
--- a/demo.onyx
+++ /dev/null
@@ -1,9 +0,0 @@
-use "core";
-
-Foo :: struct {
-       x, y i32;
-}
-
-main :: (argc i32, argv []*u8) int {
-       print("Hello World!");
-}
diff --git a/onyx b/onyx
index f41434829deb74d21d276f499cbd68a11df9be1f..7702a58d16b89279edab5528bb390035dcc44b2a 100755 (executable)
Binary files a/onyx and b/onyx differ
diff --git a/onyx.c b/onyx.c
index 615c50cade9756de0c4c4e7d1feef4814874f8de..2391e03b1ac40f71013a6b0e68969d4303bb386b 100644 (file)
--- a/onyx.c
+++ b/onyx.c
@@ -1,38 +1,49 @@
 #include <stdio.h> // TODO: Replace with custom lib
 #include <stdlib.h> // TODO: Replace with custom lib
 #include "bh.h"
-// 
+//
 // int main(int argc, char *argv[]) {
 //     if (argc < 2) {
 //             fprintf(stderr, "Expected file to compile\n");
 //             return -1;
 //     }
-// 
+//
 //     File file;
-// 
+//
 //     if (file_open(&file, argv[1], O_RDONLY)) {
 //             fprintf(stderr, "Failed to open file: %s\n", argv[1]);
 //             return -1;
 //     }
-// 
+//
 //     file_close(&file);
-// 
+//
 //     return 0;
 // }
-// 
+//
 
 int main(int argc, char *argv[]) {
        bh_string test_str = bh_string_new(256);
-       bh_string world_str = bh_string_new("World");
+       bh_string world_str = bh_string_new("World FOO Bar test\n");
 
-       bh_string_append(&test_str, "Hello Frank!");
+       bh_string_append(&test_str, "Hello Frank!\n");
        bh_string_replace_at(&test_str, &world_str, 6);
        bh_string_replace_at(&test_str, "Hola ", 0);
+       bh_string_insert_at(&test_str, "World", 3);
        bh_string_print(&test_str);
 
+       bh_string trim_str = bh_string_new("abcdeTesting words herezzzz\n   \t");
+       bh_string_print(&trim_str);
+       bh_string_trim_begin(&trim_str, "abcde");
+       bh_string_print(&trim_str);
+       bh_string_trim_end_space(&trim_str);
+       bh_string_print(&trim_str);
+
        bh_string_delete(&test_str);
        bh_string_delete(&world_str);
-       
+       bh_string_delete(&trim_str);
+
+       // bh_string file_contents = bh_file_read_contents("path");
+
        return 0;
 }
 
@@ -42,11 +53,11 @@ int main(int argc, char *argv[]) {
 //     fseek(file, 0, SEEK_END);
 //     long end = ftell(file);
 //     fseek(file, 0, SEEK_SET);
-// 
-// 
+//
+//
 //     char* data = (char *) malloc(sizeof(u8) * (end - start + 1));
 //     read(file->, data, end - start);
 //     fclose(file);
 //     printf("%ld - %ld = %ld\n", end, start, end - start);
 //     printf("%s", data);
-// }
\ No newline at end of file
+// }
diff --git a/progs/demo.onyx b/progs/demo.onyx
new file mode 100644 (file)
index 0000000..ee02d76
--- /dev/null
@@ -0,0 +1,9 @@
+use "core";
+
+Foo :: struct {
+       x, y i32;
+}
+
+main :: (argc i32, argv []*u8) int {
+       print("Hello World!");
+}