From f126b7952b543722bb33ec667227fb016d2b6511 Mon Sep 17 00:00:00 2001 From: Macoy Madson Date: Tue, 4 May 2021 18:36:52 -0700 Subject: [PATCH] More dynstring functions, share stb code Added macro for using STB in order to reduce copy-paste. --- src/DynamicArray.cake | 67 +++++++++++++++++++++++++++---------------- src/STB.cake | 29 +++++++++++++++++++ 2 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 src/STB.cake diff --git a/src/DynamicArray.cake b/src/DynamicArray.cake index 568b0cb..1e1ebd8 100644 --- a/src/DynamicArray.cake +++ b/src/DynamicArray.cake @@ -9,30 +9,13 @@ (set-cakelisp-option cakelisp-src-dir "Dependencies/cakelisp/src") (add-cakelisp-search-directory "Dependencies/cakelisp/runtime") -(import &comptime-only "CHelpers.cake" "Dependencies.cake") +(import &comptime-only "CHelpers.cake") -;; TODO: Make this an "infect" to the importer rather than having to be global -(add-c-search-directory-global "Dependencies/stb") -;; TODO: Remove once importing this module no longer changes all compile commands -(add-build-config-label "STB") +(import &comptime-only "STB.cake") +(use-stb-ds clone-stb-headers-dynamic-array) -;; (add-c-search-directory-module "Dependencies/stb") - -(add-dependency-git-submodule clone-stb-headers-dynamic-array - "https://github.com/nothings/stb" - "Dependencies/stb") - -;; Enforce only defining the implemention once -(comptime-cond - ('stb-ds-defined) - (true - (c-preprocessor-define STB_DS_IMPLEMENTATION) - (comptime-define-symbol 'stb-ds-defined))) - -(c-import &with-decls "stb_ds.h" - &with-defs "stb_ds.h" - ;; For dynstring - "" "") +;; For dynstring +(c-import "" "" "") (def-c-function-alias dynarray-free arrfree) ;; (array (* T)) @@ -95,10 +78,37 @@ (dynarray-set-length (deref str) (+ 1 num-chars-required)) (return num-chars-required)) +;; Returns the length of the string, not including the null terminator +(defun dynstring-concat (str (* dynstring) append-str (* (const char)) &return int) + ;; Things will go bad if these aren't equal + (assert (!= (deref str) append-str)) + + ;; Remove the null terminator + (when (> (dynstring-strlen (deref str)) 0) + (dynarray-pop (deref str))) + + ;; using strcat may be faster if it is optimized. I won't worry about it for now + (each-char-in-string-const append-str current-char + ;; Will resize if necessary + (dynarray-append (deref str) (deref current-char))) + + ;; Add the null terminator + (dynarray-append (deref str) 0) + (return (dynstring-strlen (deref str)))) + ;; dynarray-length includes the null terminator. This function removes it (defun dynstring-strlen (str dynstring &return size_t) (return (- (dynarray-length str) 1))) +(defmacro make-dynstring-f (var-name symbol format-string string + &rest format-arguments any) + (tokenize-push output + (var (token-splice var-name) dynstring null) + (dynstring-printf (addr (token-splice var-name)) + (token-splice format-string) + (token-splice-rest format-arguments tokens))) + (return true)) + (comptime-cond ('auto-test (c-import "stdio.h") @@ -135,7 +145,7 @@ (dynarray-free my-dynarray) (return 0)) - (defun print-dynstring-details (str dynstring) + (defun-local print-dynstring-details (str dynstring) (fprintf stderr "dynstring: '%s'\n capacity: %d length: %d strlen: %d\n" str (type-cast (dynarray-capacity str) int) @@ -148,5 +158,14 @@ (print-dynstring-details my-string) (dynstring-printf (addr my-string) "Short %d" 42) (print-dynstring-details my-string) - (dynarray-free my-string) + + (dynstring-concat (addr my-string) "/") + (dynstring-concat (addr my-string) "Test") + (print-dynstring-details my-string) + + (make-dynstring-f my-quick-string "The answer is %d" 42) + (print-dynstring-details my-quick-string) + + (dynarray-free my-string) + (dynarray-free my-quick-string) (return 0)))) diff --git a/src/STB.cake b/src/STB.cake new file mode 100644 index 0000000..e5b7c04 --- /dev/null +++ b/src/STB.cake @@ -0,0 +1,29 @@ +(skip-build) +(import &comptime-only "Dependencies.cake") + +;; Add all the necessary includes, defines, download the repo, etc. +(defmacro use-stb-ds (clone-func-name symbol) + (tokenize-push output + ;; TODO: Make this an "infect" to the importer rather than having to be global + (add-c-search-directory-global "Dependencies/stb") + ;; TODO: Remove once importing this module no longer changes all compile commands + (add-build-config-label "STB") + + ;; (add-c-search-directory-module "Dependencies/stb") + + ;; TODO: Remove parameter, replace with gensym/check for redefine? + (add-dependency-git-submodule (token-splice clone-func-name) + "https://github.com/nothings/stb" + "Dependencies/stb") + + + ;; Enforce only defining the implemention once + (comptime-cond + ('stb-ds-defined) + (true + (c-preprocessor-define STB_DS_IMPLEMENTATION) + (comptime-define-symbol 'stb-ds-defined))) + + (c-import &with-decls "stb_ds.h" + &with-defs "stb_ds.h")) + (return true))