diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/doc/._.DS_Store b/doc/._.DS_Store new file mode 100755 index 0000000..8e82ed9 Binary files /dev/null and b/doc/._.DS_Store differ diff --git a/doc/._guf_dict-diagram.png b/doc/._guf_dict-diagram.png new file mode 100755 index 0000000..3028d20 Binary files /dev/null and b/doc/._guf_dict-diagram.png differ diff --git a/doc/guf_dict-diagram.png b/doc/guf_dict-diagram.png old mode 100644 new mode 100755 diff --git a/src/guf_alloc.h b/src/guf_alloc.h old mode 100644 new mode 100755 diff --git a/src/guf_alloc_libc.h b/src/guf_alloc_libc.h old mode 100644 new mode 100755 diff --git a/src/guf_alloc_tracker.h b/src/guf_alloc_tracker.h old mode 100644 new mode 100755 diff --git a/src/guf_assert.h b/src/guf_assert.h old mode 100644 new mode 100755 index 21a1023..1077185 --- a/src/guf_assert.h +++ b/src/guf_assert.h @@ -26,6 +26,7 @@ typedef enum guf_err { GUF_ERR_NOT_FOUND, GUF_ERR_ALREADY_EXISTS, GUF_ERR_ASSERT_FAIL, + GUF_ERR_IO, GUF_ERR_TYPES_NUM } guf_err; diff --git a/src/guf_common.h b/src/guf_common.h old mode 100644 new mode 100755 diff --git a/src/guf_cstr.h b/src/guf_cstr.h old mode 100644 new mode 100755 diff --git a/src/guf_dbuf.h b/src/guf_dbuf.h old mode 100644 new mode 100755 diff --git a/src/guf_hash.h b/src/guf_hash.h old mode 100644 new mode 100755 diff --git a/src/guf_id_pool.h b/src/guf_id_pool.h old mode 100644 new mode 100755 diff --git a/src/guf_init.h b/src/guf_init.h old mode 100644 new mode 100755 diff --git a/src/guf_linalg.h b/src/guf_linalg.h old mode 100644 new mode 100755 diff --git a/src/guf_math.h b/src/guf_math.h old mode 100644 new mode 100755 diff --git a/src/guf_math_ckdint.h b/src/guf_math_ckdint.h old mode 100644 new mode 100755 diff --git a/src/guf_rand.h b/src/guf_rand.h old mode 100644 new mode 100755 diff --git a/src/guf_sort.h b/src/guf_sort.h old mode 100644 new mode 100755 diff --git a/src/guf_str.h b/src/guf_str.h old mode 100644 new mode 100755 index 50e699e..7de9ee0 --- a/src/guf_str.h +++ b/src/guf_str.h @@ -128,6 +128,10 @@ GUF_STR_KWRDS guf_str_tok_state guf_str_tok_state_new(guf_str_view str, guf_str_ */ GUF_STR_KWRDS bool guf_str_tok_next(guf_str_tok_state *state, bool preserve_delims); +GUF_STR_KWRDS unsigned guf_str_view_read_uint(guf_str_view *sv); // TODO: Handle overflow, signs etc. +GUF_STR_KWRDS uint_least64_t guf_str_view_read_u64(guf_str_view *sv); + + // 2.) guf_str: @@ -208,6 +212,14 @@ GUF_STR_KWRDS guf_str *guf_str_append_one_char(guf_str *str, char c); GUF_STR_KWRDS guf_str *guf_str_try_append(guf_str *str, guf_str_view sv, guf_err *err); GUF_STR_KWRDS guf_str *guf_str_append(guf_str *str, guf_str_view sv); +// Append from file: +#define GUF_READ_FILE_BUFSIZE 128 +GUF_STR_KWRDS ptrdiff_t guf_str_try_append_file(guf_str *to_append, const char *fname, guf_err *err); +GUF_STR_KWRDS ptrdiff_t guf_str_append_file(guf_str *to_append, const char *fname); + +// Append ints: +GUF_STR_KWRDS guf_str *guf_str_append_u64(guf_str *str, uint_least64_t n); + // Return a pointer to the null-terminated char array representing the string (works like std::string::c_str in C++) GUF_STR_KWRDS const char *guf_str_const_cstr(const guf_str *str); GUF_STR_KWRDS char *guf_str_try_get_cstr(guf_str *str, guf_err *err); // Error if str is readonly. @@ -1142,6 +1154,66 @@ GUF_STR_KWRDS guf_str *guf_str_append(guf_str *str, guf_str_view sv) return guf_str_try_append(str, sv, NULL); } +GUF_STR_KWRDS ptrdiff_t guf_str_try_append_file(guf_str *to_append, const char *fname, guf_err *err) +{ + if (!fname) { + guf_err_set_or_panic(err, GUF_ERR_NULL_PTR, GUF_ERR_MSG("guf_str_read_file: fname is NULL")); + return 0; + } + + FILE *f = fopen(fname, "r"); + if (!f) { + guf_err_set_or_panic(err, GUF_ERR_IO, GUF_ERR_MSG("guf_str_read_file:: Failed to open file (fopen returned NULL)")); + return 0; + } + + size_t chars_read = 0, total_chars_read = 0; + char buf[GUF_READ_FILE_BUFSIZE] = {'\0'}; + while ((chars_read = fread(buf, sizeof buf[0], GUF_ARR_SIZE(buf), f)) && !ferror(f)) { + guf_str_append(to_append, (guf_str_view){.len = chars_read, .str = buf}); + total_chars_read += chars_read; + if (feof(f)) { + break; + } + } + + if (ferror(f)) { + fclose(f); + guf_err_set_or_panic(err, GUF_ERR_IO, GUF_ERR_MSG("read_file: Unexpected error while reading file")); + } else { + GUF_ASSERT(feof(f)); + fclose(f); + } + + return total_chars_read; +} + +GUF_STR_KWRDS ptrdiff_t guf_str_append_file(guf_str *to_append, const char *fname) { + return guf_str_try_append_file(to_append, fname, NULL); +} + + +GUF_STR_KWRDS guf_str *guf_str_append_u64(guf_str *str, uint_least64_t n) { + + char buf[20] = {'\0'}; + int start_idx = GUF_ARR_SIZE(buf); + int num_digits = 0; + do { + GUF_ASSERT(start_idx > 0); + char c = (n % 10) + '0'; + buf[--start_idx] = c; + ++num_digits; + } while ((n = n / 10)); + + GUF_ASSERT(num_digits == (int)GUF_ARR_SIZE(buf) - start_idx); + + const guf_str_view num_sv = (guf_str_view) {.str = buf + start_idx, .len = num_digits}; + + return guf_str_append(str, num_sv); +} + + + GUF_STR_KWRDS guf_str *guf_str_try_substr(guf_str *str, ptrdiff_t pos, ptrdiff_t count, guf_err *err) { GUF_ASSERT(guf_str_is_valid(str)); @@ -1526,6 +1598,30 @@ GUF_STR_KWRDS bool guf_str_view_equal_val_arg(guf_str_view a_val, guf_str_view b return guf_str_view_equal(&a_val, &b_val); } +GUF_STR_KWRDS unsigned guf_str_view_read_uint(guf_str_view *sv) +{ // TODO: Handle overflow etc. + unsigned res = 0; + while (sv->len && sv->str[0] >= '0' && sv->str[0] <= '9') { + res *= 10; + res += (sv->str[0] - '0'); + sv->len -= 1; + sv->str = sv->len > 0 ? sv->str + 1 : NULL; + } + return res; +} + +GUF_STR_KWRDS uint_least64_t guf_str_view_read_u64(guf_str_view *sv) +{ // TODO: Handle overflow etc. + uint_least64_t res = 0; + while (sv->len && sv->str[0] >= '0' && sv->str[0] <= '9') { + res *= 10; + res += (sv->str[0] - '0'); + sv->len -= 1; + sv->str = sv->len > 0 ? sv->str + 1 : NULL; + } + return res; +} + #undef GUF_STR_IMPL #undef GUF_STR_IMPL_STATIC #endif /* end impl */ diff --git a/src/guf_str_view_type.h b/src/guf_str_view_type.h old mode 100644 new mode 100755 diff --git a/src/guf_utf8.h b/src/guf_utf8.h old mode 100644 new mode 100755 diff --git a/src/guf_utils.h b/src/guf_utils.h old mode 100644 new mode 100755 diff --git a/src/test/data/bartleby.txt b/src/test/data/bartleby.txt old mode 100644 new mode 100755 diff --git a/src/test/data/utf8-test.txt b/src/test/data/utf8-test.txt old mode 100644 new mode 100755 diff --git a/src/test/example.c b/src/test/example.c old mode 100644 new mode 100755 diff --git a/src/test/impls/alloc_libc_impl.c b/src/test/impls/alloc_libc_impl.c old mode 100644 new mode 100755 diff --git a/src/test/impls/alloc_tracker_impl.c b/src/test/impls/alloc_tracker_impl.c old mode 100644 new mode 100755 diff --git a/src/test/impls/ckdint_impl.c b/src/test/impls/ckdint_impl.c old mode 100644 new mode 100755 diff --git a/src/test/impls/dbuf_impl.c b/src/test/impls/dbuf_impl.c old mode 100644 new mode 100755 diff --git a/src/test/impls/dbuf_impl.h b/src/test/impls/dbuf_impl.h old mode 100644 new mode 100755 diff --git a/src/test/impls/dict_impl.c b/src/test/impls/dict_impl.c old mode 100644 new mode 100755 diff --git a/src/test/impls/dict_impl.h b/src/test/impls/dict_impl.h old mode 100644 new mode 100755 diff --git a/src/test/impls/init_impl.c b/src/test/impls/init_impl.c old mode 100644 new mode 100755 diff --git a/src/test/impls/linalg_impl.c b/src/test/impls/linalg_impl.c old mode 100644 new mode 100755 diff --git a/src/test/impls/rand_impl.c b/src/test/impls/rand_impl.c old mode 100644 new mode 100755 diff --git a/src/test/impls/sort_impl.c b/src/test/impls/sort_impl.c old mode 100644 new mode 100755 diff --git a/src/test/impls/sort_impl.h b/src/test/impls/sort_impl.h old mode 100644 new mode 100755 diff --git a/src/test/impls/str_impl.c b/src/test/impls/str_impl.c old mode 100644 new mode 100755 diff --git a/src/test/test.cpp b/src/test/test.cpp old mode 100644 new mode 100755 diff --git a/src/test/test.hpp b/src/test/test.hpp old mode 100644 new mode 100755 diff --git a/src/test/test_ckdint.cpp b/src/test/test_ckdint.cpp old mode 100644 new mode 100755 diff --git a/src/test/test_ckdint.hpp b/src/test/test_ckdint.hpp old mode 100644 new mode 100755 diff --git a/src/test/test_dbuf.cpp b/src/test/test_dbuf.cpp old mode 100644 new mode 100755 diff --git a/src/test/test_dbuf.hpp b/src/test/test_dbuf.hpp old mode 100644 new mode 100755 diff --git a/src/test/test_dict.cpp b/src/test/test_dict.cpp old mode 100644 new mode 100755 diff --git a/src/test/test_dict.hpp b/src/test/test_dict.hpp old mode 100644 new mode 100755 diff --git a/src/test/test_str.cpp b/src/test/test_str.cpp old mode 100644 new mode 100755 diff --git a/src/test/test_str.hpp b/src/test/test_str.hpp old mode 100644 new mode 100755 diff --git a/src/test/test_utf8.cpp b/src/test/test_utf8.cpp old mode 100644 new mode 100755 diff --git a/src/test/test_utf8.hpp b/src/test/test_utf8.hpp old mode 100644 new mode 100755 diff --git a/todo.txt b/todo.txt old mode 100644 new mode 100755 index 8070cb8..3fb13a1 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,7 @@ - fix readonly str/uninit ? +- dict: allow set + - guf_stack, guf_queue, guf_dqueue, guf_prio_queue (using a heap), guf_ringbuf - sort: add cpp #ifdef to remove restrict from declaration diff --git a/tools/ckdint-gen.py b/tools/ckdint-gen.py old mode 100644 new mode 100755 diff --git a/tools/intwrap-gen.py b/tools/intwrap-gen.py old mode 100644 new mode 100755 diff --git a/tools/min_max_clamp-gen.py b/tools/min_max_clamp-gen.py old mode 100644 new mode 100755