Browse Source

Add writer for writing to fixed-size buffer

master
Macoy Madson 3 months ago
parent
commit
f179e623ae
  1. 34
      src/Introspection.cake

34
src/Introspection.cake

@ -837,6 +837,40 @@
(fprintf output-file "%s" write-string))
(return true))
(defstruct write-introspect-struct-buffer-data
buffer (* char)
write-head (* char)
size size_t)
(defun write-introspect-struct-buffer-writer (write-string (* (const char))
optional-length (unsigned int)
userdata (* void)
&return bool)
(var-cast-to buffer-data (* write-introspect-struct-buffer-data) userdata)
(var remaining-space size_t (- (path buffer-data > size)
(- (path buffer-data > write-head)
(path buffer-data > buffer))))
(when (>= optional-length remaining-space)
(fprintf stderr "Not enough space in buffer remaining\n")
(return false))
(if optional-length
(scope
(strncpy (path buffer-data > write-head) write-string optional-length)
(set (path buffer-data > write-head) (+ (path buffer-data > write-head)
optional-length)))
(scope
(var write-length size_t
(snprintf (path buffer-data > write-head)
remaining-space
"%s" write-string))
(unless (< write-length remaining-space)
(fprintf stderr "write-introspect-struct-s-expr buffer was not large enough to fit\n")
(return false))
(set (path buffer-data > write-head) (+ (path buffer-data > write-head)
write-length))))
(return true))
(defmacro snprintf-or-return-false (output-buffer any format any &optional &rest arguments any)
(tokenize-push output
(unless (< (snprintf (token-splice output-buffer)

Loading…
Cancel
Save