Ver código fonte

Reuse nob in tools more

Also get rid of sv.h
rexim 7 meses atrás
pai
commit
45849f28e8
5 arquivos alterados com 44 adições e 433 exclusões
  1. 17 1
      dev-deps/nob.h
  2. 0 318
      dev-deps/sv.h
  3. 1 1
      nob.c
  4. 19 99
      tools/obj2c.c
  5. 7 14
      tools/png2c.c

+ 17 - 1
nob.h → dev-deps/nob.h

@@ -1,4 +1,4 @@
-/* nob - v1.14.1 - Public Domain - https://github.com/tsoding/nob.h
+/* nob - v1.14.1+ - Public Domain - https://github.com/tsoding/nob.h
 
    This library is the next generation of the [NoBuild](https://github.com/tsoding/nobuild) idea.
 
@@ -518,6 +518,7 @@ typedef struct {
 const char *nob_temp_sv_to_cstr(Nob_String_View sv);
 
 Nob_String_View nob_sv_chop_by_delim(Nob_String_View *sv, char delim);
+Nob_String_View nob_sv_chop_left(Nob_String_View *sv, size_t n);
 Nob_String_View nob_sv_trim(Nob_String_View sv);
 Nob_String_View nob_sv_trim_left(Nob_String_View sv);
 Nob_String_View nob_sv_trim_right(Nob_String_View sv);
@@ -1531,6 +1532,20 @@ Nob_String_View nob_sv_chop_by_delim(Nob_String_View *sv, char delim)
     return result;
 }
 
+Nob_String_View nob_sv_chop_left(Nob_String_View *sv, size_t n)
+{
+    if (n > sv->count) {
+        n = sv->count;
+    }
+
+    Nob_String_View result = nob_sv_from_parts(sv->data, n);
+
+    sv->data  += n;
+    sv->count -= n;
+
+    return result;
+}
+
 Nob_String_View nob_sv_from_parts(const char *data, size_t count)
 {
     Nob_String_View sv;
@@ -1846,6 +1861,7 @@ int closedir(DIR *dirp)
         #define String_View Nob_String_View
         #define temp_sv_to_cstr nob_temp_sv_to_cstr
         #define sv_chop_by_delim nob_sv_chop_by_delim
+        #define sv_chop_left nob_sv_chop_left
         #define sv_trim nob_sv_trim
         #define sv_trim_left nob_sv_trim_left
         #define sv_trim_right nob_sv_trim_right

+ 0 - 318
dev-deps/sv.h

@@ -1,318 +0,0 @@
-// Copyright 2021 Alexey Kutepov <[email protected]>
-
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-#ifndef SV_H_
-#define SV_H_
-
-#include <stdint.h>
-#include <stdlib.h>
-#include <stdbool.h>
-#include <string.h>
-#include <ctype.h>
-
-#ifndef SVDEF
-#define SVDEF
-#endif // SVDEF
-
-typedef struct {
-    size_t count;
-    const char *data;
-} String_View;
-
-#define SV(cstr_lit) sv_from_parts(cstr_lit, sizeof(cstr_lit) - 1)
-#define SV_STATIC(cstr_lit)   \
-    {                         \
-        sizeof(cstr_lit) - 1, \
-        (cstr_lit)            \
-    }
-
-#define SV_NULL sv_from_parts(NULL, 0)
-
-// printf macros for String_View
-#define SV_Fmt "%.*s"
-#define SV_Arg(sv) (int) (sv).count, (sv).data
-// USAGE:
-//   String_View name = ...;
-//   printf("Name: "SV_Fmt"\n", SV_Arg(name));
-
-SVDEF String_View sv_from_parts(const char *data, size_t count);
-SVDEF String_View sv_from_cstr(const char *cstr);
-SVDEF String_View sv_trim_left(String_View sv);
-SVDEF String_View sv_trim_right(String_View sv);
-SVDEF String_View sv_trim(String_View sv);
-SVDEF String_View sv_take_left_while(String_View sv, bool (*predicate)(char x));
-SVDEF String_View sv_chop_by_delim(String_View *sv, char delim);
-SVDEF String_View sv_chop_by_sv(String_View *sv, String_View thicc_delim);
-SVDEF bool sv_try_chop_by_delim(String_View *sv, char delim, String_View *chunk);
-SVDEF String_View sv_chop_left(String_View *sv, size_t n);
-SVDEF String_View sv_chop_right(String_View *sv, size_t n);
-SVDEF String_View sv_chop_left_while(String_View *sv, bool (*predicate)(char x));
-SVDEF bool sv_index_of(String_View sv, char c, size_t *index);
-SVDEF bool sv_eq(String_View a, String_View b);
-SVDEF bool sv_eq_ignorecase(String_View a, String_View b);
-SVDEF bool sv_starts_with(String_View sv, String_View prefix);
-SVDEF bool sv_ends_with(String_View sv, String_View suffix);
-SVDEF uint64_t sv_to_u64(String_View sv);
-
-#endif  // SV_H_
-
-#ifdef SV_IMPLEMENTATION
-
-SVDEF String_View sv_from_parts(const char *data, size_t count)
-{
-    String_View sv;
-    sv.count = count;
-    sv.data = data;
-    return sv;
-}
-
-SVDEF String_View sv_from_cstr(const char *cstr)
-{
-    return sv_from_parts(cstr, strlen(cstr));
-}
-
-SVDEF String_View sv_trim_left(String_View sv)
-{
-    size_t i = 0;
-    while (i < sv.count && isspace(sv.data[i])) {
-        i += 1;
-    }
-
-    return sv_from_parts(sv.data + i, sv.count - i);
-}
-
-SVDEF String_View sv_trim_right(String_View sv)
-{
-    size_t i = 0;
-    while (i < sv.count && isspace(sv.data[sv.count - 1 - i])) {
-        i += 1;
-    }
-
-    return sv_from_parts(sv.data, sv.count - i);
-}
-
-SVDEF String_View sv_trim(String_View sv)
-{
-    return sv_trim_right(sv_trim_left(sv));
-}
-
-SVDEF String_View sv_chop_left(String_View *sv, size_t n)
-{
-    if (n > sv->count) {
-        n = sv->count;
-    }
-
-    String_View result = sv_from_parts(sv->data, n);
-
-    sv->data  += n;
-    sv->count -= n;
-
-    return result;
-}
-
-SVDEF String_View sv_chop_right(String_View *sv, size_t n)
-{
-    if (n > sv->count) {
-        n = sv->count;
-    }
-
-    String_View result = sv_from_parts(sv->data + sv->count - n, n);
-
-    sv->count -= n;
-
-    return result;
-}
-
-SVDEF bool sv_index_of(String_View sv, char c, size_t *index)
-{
-    size_t i = 0;
-    while (i < sv.count && sv.data[i] != c) {
-        i += 1;
-    }
-
-    if (i < sv.count) {
-        if (index) {
-            *index = i;
-        }
-        return true;
-    } else {
-        return false;
-    }
-}
-
-SVDEF bool sv_try_chop_by_delim(String_View *sv, char delim, String_View *chunk)
-{
-    size_t i = 0;
-    while (i < sv->count && sv->data[i] != delim) {
-        i += 1;
-    }
-
-    String_View result = sv_from_parts(sv->data, i);
-
-    if (i < sv->count) {
-        sv->count -= i + 1;
-        sv->data  += i + 1;
-        if (chunk) {
-            *chunk = result;
-        }
-        return true;
-    }
-
-    return false;
-}
-
-SVDEF String_View sv_chop_by_delim(String_View *sv, char delim)
-{
-    size_t i = 0;
-    while (i < sv->count && sv->data[i] != delim) {
-        i += 1;
-    }
-
-    String_View result = sv_from_parts(sv->data, i);
-
-    if (i < sv->count) {
-        sv->count -= i + 1;
-        sv->data  += i + 1;
-    } else {
-        sv->count -= i;
-        sv->data  += i;
-    }
-
-    return result;
-}
-
-SVDEF String_View sv_chop_by_sv(String_View *sv, String_View thicc_delim)
-{
-    String_View window = sv_from_parts(sv->data, thicc_delim.count);
-    size_t i = 0;
-    while (i + thicc_delim.count < sv->count 
-        && !(sv_eq(window, thicc_delim))) 
-    {
-        i++;
-        window.data++;
-    }
-
-    String_View result = sv_from_parts(sv->data, i);
-
-    if (i + thicc_delim.count == sv->count) {
-        // include last <thicc_delim.count> characters if they aren't 
-        //  equal to thicc_delim
-        result.count += thicc_delim.count; 
-    }
-    
-    // Chop!
-    sv->data  += i + thicc_delim.count;
-    sv->count -= i + thicc_delim.count;
-
-    return result;
-}
-
-SVDEF bool sv_starts_with(String_View sv, String_View expected_prefix)
-{
-    if (expected_prefix.count <= sv.count) {
-        String_View actual_prefix = sv_from_parts(sv.data, expected_prefix.count);
-        return sv_eq(expected_prefix, actual_prefix);
-    }
-
-    return false;
-}
-
-SVDEF bool sv_ends_with(String_View sv, String_View expected_suffix)
-{
-    if (expected_suffix.count <= sv.count) {
-        String_View actual_suffix = sv_from_parts(sv.data + sv.count - expected_suffix.count, expected_suffix.count);
-        return sv_eq(expected_suffix, actual_suffix);
-    }
-
-    return false;
-}
-
-SVDEF bool sv_eq(String_View a, String_View b)
-{
-    if (a.count != b.count) {
-        return false;
-    } else {
-        return memcmp(a.data, b.data, a.count) == 0;
-    }
-}
-
-SVDEF bool sv_eq_ignorecase(String_View a, String_View b)
-{
-    if (a.count != b.count) {
-        return false;
-    }
-    
-    char x, y;
-    for (size_t i = 0; i < a.count; i++) {
-        x = 'A' <= a.data[i] && a.data[i] <= 'Z'
-              ? a.data[i] + 32
-              : a.data[i];
-        
-        y = 'A' <= b.data[i] && b.data[i] <= 'Z'
-              ? b.data[i] + 32
-              : b.data[i];
-
-        if (x != y) return false;
-    } 
-    return true;
-}
-
-SVDEF uint64_t sv_to_u64(String_View sv)
-{
-    uint64_t result = 0;
-
-    for (size_t i = 0; i < sv.count && isdigit(sv.data[i]); ++i) {
-        result = result * 10 + (uint64_t) sv.data[i] - '0';
-    }
-
-    return result;
-}
-
-uint64_t sv_chop_u64(String_View *sv)
-{
-    uint64_t result = 0;
-    while (sv->count > 0 && isdigit(*sv->data)) {
-        result = result*10 + *sv->data - '0';
-        sv->count -= 1;
-        sv->data += 1;
-    }
-    return result;
-}
-
-SVDEF String_View sv_chop_left_while(String_View *sv, bool (*predicate)(char x))
-{
-    size_t i = 0;
-    while (i < sv->count && predicate(sv->data[i])) {
-        i += 1;
-    }
-    return sv_chop_left(sv, i);
-}
-
-SVDEF String_View sv_take_left_while(String_View sv, bool (*predicate)(char x))
-{
-    size_t i = 0;
-    while (i < sv.count && predicate(sv.data[i])) {
-        i += 1;
-    }
-    return sv_from_parts(sv.data, i);
-}
-
-#endif // SV_IMPLEMENTATION

+ 1 - 1
nob.c

@@ -1,7 +1,7 @@
 #define NOB_IMPLEMENTATION
 #define NOB_STRIP_PREFIX
 #define NOB_EXPERIMENTAL_DELETE_OLD
-#include "./nob.h"
+#include "./dev-deps/nob.h"
 
 #define COMMON_CFLAGS "-Wall", "-Wextra", "-pedantic", "-ggdb", "-I.", "-I./build/", "-I./dev-deps/"
 

+ 19 - 99
tools/obj2c.c

@@ -5,64 +5,9 @@
 #include <limits.h>
 #include <string.h>
 
-#define SV_IMPLEMENTATION
-#include "sv.h"
-
-#define ARENA_IMPLEMENTATION
-#include "arena.h"
-
-#define return_defer(value) do { result = (value); goto defer; } while (0)
-typedef int Errno;
-#define UNUSED(x) (void)(x)
-
-const char *shift(int *argc, char ***argv)
-{
-    assert(*argc > 0);
-    const char *result = *argv[0];
-    *argc -= 1;
-    *argv += 1;
-    return result;
-}
-
-static Arena default_arena = {0};
-static Arena *context_arena = &default_arena;
-
-static void *context_alloc(size_t size)
-{
-    assert(context_arena);
-    return arena_alloc(context_arena, size);
-}
-
-static void *context_realloc(void *oldp, size_t oldsz, size_t newsz)
-{
-    if (newsz <= oldsz) return oldp;
-    return memcpy(context_alloc(newsz), oldp, oldsz);
-}
-
-Errno read_entire_file(const char *file_path, char **buffer, size_t *buffer_size)
-{
-    Errno result = 0;
-    FILE *f = NULL;
-
-    f = fopen(file_path, "rb");
-    if (f == NULL) return_defer(errno);
-
-    if (fseek(f, 0, SEEK_END) < 0) return_defer(errno);
-    long m = ftell(f);
-    if (m < 0) return_defer(errno);
-    if (fseek(f, 0, SEEK_SET) < 0) return_defer(errno);
-
-    *buffer_size = m;
-    *buffer = context_alloc(*buffer_size + 1);
-    (*buffer)[*buffer_size] = '\0';
-
-    fread(*buffer, *buffer_size, 1, f);
-    if (ferror(f)) return_defer(errno);
-
-defer:
-    if (f) fclose(f);
-    return result;
-}
+#define NOB_IMPLEMENTATION
+#define NOB_STRIP_PREFIX
+#include "nob.h"
 
 typedef struct {
     float x, y;
@@ -76,7 +21,6 @@ Vector2 make_vector2(float x, float y)
     return v2;
 }
 
-
 typedef struct {
     float x, y, z;
 } Vector3;
@@ -158,25 +102,6 @@ typedef struct {
     size_t count;
 } TexCoords;
 
-#define DA_INIT_CAPACITY 8192
-#define DA_REALLOC context_realloc
-#define da_append(da, item)                                                 \
-    do {                                                                    \
-        if ((da)->count >= (da)->capacity) {                                \
-            size_t new_capacity = (da)->capacity*2;                         \
-            if (new_capacity == 0) {                                        \
-                new_capacity = DA_INIT_CAPACITY;                            \
-            }                                                               \
-                                                                            \
-            (da)->items = DA_REALLOC((da)->items,                           \
-                                     (da)->capacity*sizeof((da)->items[0]), \
-                                     new_capacity*sizeof((da)->items[0]));  \
-            (da)->capacity = new_capacity;                                  \
-        }                                                                   \
-                                                                            \
-        (da)->items[(da)->count++] = (item);                                \
-    } while (0)
-
 typedef struct {
     int *items;
     size_t count;
@@ -317,7 +242,7 @@ int main(int argc, char **argv)
     int result = 0;
 
     assert(argc > 0);
-    const char *program_name = shift(&argc, &argv);
+    const char *program_name = shift(argv, argc);
     const char *output_file_path = NULL;
     const char *input_file_path = NULL;
     float scale = 0.75;
@@ -325,7 +250,7 @@ int main(int argc, char **argv)
 
     // TODO: consider using https://github.com/tsoding/flag.h in here
     while (argc > 0) {
-        const char *flag = shift(&argc, &argv);
+        const char *flag = shift(argv, argc);
         if (strcmp(flag, "-o") == 0) {
             if (argc <= 0) {
                 usage(program_name);
@@ -339,7 +264,7 @@ int main(int argc, char **argv)
                 return_defer(1);
             }
 
-            output_file_path = shift(&argc, &argv);
+            output_file_path = shift(argv, argc);
         } else if (strcmp(flag, "-s") == 0) {
             if (argc <= 0) {
                 usage(program_name);
@@ -347,7 +272,7 @@ int main(int argc, char **argv)
                 return_defer(1);
             }
 
-            const char *value = shift(&argc, &argv);
+            const char *value = shift(argv, argc);
             scale = strtof(value, NULL);
         } else if (strcmp(flag, "-d") == 0) {
             if (argc <= 0) {
@@ -356,7 +281,7 @@ int main(int argc, char **argv)
                 return_defer(1);
             }
 
-            const char *value = shift(&argc, &argv);
+            const char *value = shift(argv, argc);
             da_append(&delete_components, atoi(value));
         } else {
             if (input_file_path != NULL) {
@@ -380,15 +305,10 @@ int main(int argc, char **argv)
         return_defer(1);
     }
 
-    char *buffer;
-    size_t buffer_size;
-    Errno err = read_entire_file(input_file_path, &buffer, &buffer_size);
-    if (err != 0) {
-        fprintf(stderr, "ERROR: could not read file %s: %s\n", input_file_path, strerror(errno));
-        return_defer(1);
-    }
+    String_Builder buffer = {0};
+    if (!read_entire_file(input_file_path, &buffer)) return_defer(1);
 
-    String_View content = sv_from_parts(buffer, buffer_size);
+    String_View content = sb_to_sv(buffer);
     Vertices vertices = {0};
     TexCoords texcoords = {0};
     Normals normals = {0};
@@ -403,7 +323,7 @@ int main(int argc, char **argv)
         String_View line = sv_trim_left(sv_chop_by_delim(&content, '\n'));
         if (line.count > 0 && *line.data != '#') {
             String_View kind = sv_chop_by_delim(&line, ' ');
-            if (sv_eq(kind, SV("v"))) {
+            if (sv_eq(kind, sv_from_cstr("v"))) {
                 char *endptr;
 
                 line = sv_trim_left(line);
@@ -425,7 +345,7 @@ int main(int argc, char **argv)
                 sv_chop_left(&line, endptr - line.data);
 
                 da_append(&vertices, make_vertex(x, y, z));
-            } else if (sv_eq(kind, SV("f"))) {
+            } else if (sv_eq(kind, sv_from_cstr("f"))) {
                 // TODO: This code assumes that we already parsed all of the vertices.
                 // Since we don't have any OBJ files in the assets that have faces before
                 // vertices, it does not really matter that much. If we ever have any
@@ -445,11 +365,11 @@ int main(int argc, char **argv)
                 da_append(&vertices.items[v3].faces, face_index);
 
                 da_append(&faces, make_face(v1, v2, v3, vt1, vt2, vt3, vn1, vn2, vn3));
-            } else if (sv_eq(kind, SV("mtllib"))) {
+            } else if (sv_eq(kind, sv_from_cstr("mtllib"))) {
                 fprintf(stderr, "%s:%zu: WARNING: mtllib is not supported yet. Ignoring it...\n", input_file_path, line_number);
-            } else if (sv_eq(kind, SV("usemtl"))) {
+            } else if (sv_eq(kind, sv_from_cstr("usemtl"))) {
                 fprintf(stderr, "%s:%zu: WARNING: usemtl is not supported yet. Ignoring it...\n", input_file_path, line_number);
-            } else if (sv_eq(kind, SV("o"))) {
+            } else if (sv_eq(kind, sv_from_cstr("o"))) {
                 if (one_object_encountered) {
                     fprintf(stderr, "%s:%zu: ERROR: %s supports only one object as of right now.\n", input_file_path, line_number, program_name);
                     fprintf(stderr, "%s:%zu: NOTE: we already processing this object\n", input_file_path, one_object_line_number);
@@ -460,9 +380,9 @@ int main(int argc, char **argv)
                 fprintf(stderr, "%s:%zu: INFO: processing object `"SV_Fmt"`\n", input_file_path, line_number, SV_Arg(name));
                 one_object_encountered = true;
                 one_object_line_number = line_number;
-            } else if (sv_eq(kind, SV("s"))) {
+            } else if (sv_eq(kind, sv_from_cstr("s"))) {
                 fprintf(stderr, "%s:%zu: WARNING: smooth groups are not supported right now. Ignoring them...\n", input_file_path, line_number);
-            } else if (sv_eq(kind, SV("vn"))) {
+            } else if (sv_eq(kind, sv_from_cstr("vn"))) {
                 char *endptr;
 
                 line = sv_trim_left(line);
@@ -478,7 +398,7 @@ int main(int argc, char **argv)
                 sv_chop_left(&line, endptr - line.data);
 
                 da_append(&normals, make_vector3(x, y, z));
-            } else if (sv_eq(kind, SV("vt"))) {
+            } else if (sv_eq(kind, sv_from_cstr("vt"))) {
                 char *endptr;
 
                 line = sv_trim_left(line);

+ 7 - 14
tools/png2c.c

@@ -10,16 +10,9 @@
 #define STB_IMAGE_IMPLEMENTATION
 #include "./stb_image.h"
 
-#define return_defer(value) do { result = (value); goto defer; } while (0)
-
-const char *shift(int *argc, char ***argv)
-{
-    assert(*argc > 0);
-    const char *result = *argv[0];
-    *argc -= 1;
-    *argv += 1;
-    return result;
-}
+#define NOB_IMPLEMENTATION
+#define NOB_STRIP_PREFIX
+#include "nob.h"
 
 void usage(FILE *out, const char *program_name)
 {
@@ -97,13 +90,13 @@ defer:
 int main(int argc, char *argv[])
 {
     assert(argc > 0);
-    const char *program_name = shift(&argc, &argv);
+    const char *program_name = shift(argv, argc);
     const char *output_file_path = NULL;
     const char *input_file_path = NULL;
     const char *name = NULL;
 
     while (argc > 0) {
-        const char *flag = shift(&argc, &argv);
+        const char *flag = shift(argv, argc);
         if (strcmp(flag, "-o") == 0) {
             if (argc <= 0) {
                 usage(stderr, program_name);
@@ -117,7 +110,7 @@ int main(int argc, char *argv[])
                 return 1;
             }
 
-            output_file_path = shift(&argc, &argv);
+            output_file_path = shift(argv, argc);
         } else if (strcmp(flag, "-n") == 0) {
             if (argc <= 0) {
                 usage(stderr, program_name);
@@ -131,7 +124,7 @@ int main(int argc, char *argv[])
                 return 1;
             }
 
-            name = shift(&argc, &argv);
+            name = shift(argv, argc);
         } else {
             if (input_file_path != NULL) {
                 usage(stderr, program_name);