Skip to content

Commit 4ae1497

Browse files
committed
Added binary read/write functions
1 parent 564fd4b commit 4ae1497

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

runtime/FileUtilities.cake

+24
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,27 @@
109109
(var-cast-to out-buffer (* char) (malloc file-size))
110110
(fread out-buffer file-size 1 in-file)
111111
(return out-buffer))
112+
113+
(defun write-string (out-file (* FILE) out-string (* (const char)))
114+
(var string-length size_t (strlen out-string))
115+
;; (fprintf stderr "%s has length %d\n" out-string (type-cast string-length int))
116+
(fwrite (addr string-length) (sizeof string-length) 1 out-file)
117+
(fwrite out-string string-length 1 out-file))
118+
119+
(defun read-string (in-file (* FILE) out-string-buffer (* char) out-string-buffer-length size_t)
120+
(var string-length size_t 0)
121+
(fread (addr string-length) (sizeof string-length) 1 in-file)
122+
;; (fprintf stderr "Next string has length %d\n" (type-cast string-length int))
123+
(assert (<= string-length out-string-buffer-length))
124+
(fread out-string-buffer string-length 1 in-file))
125+
126+
;; Plain old data with a known size
127+
(defmacro write-pod (item any out-file any)
128+
(tokenize-push output
129+
(fwrite (addr (token-splice item)) (sizeof (token-splice item)) 1 (token-splice out-file)))
130+
(return true))
131+
132+
(defmacro read-pod (item any in-file any)
133+
(tokenize-push output
134+
(fread (addr (token-splice item)) (sizeof (token-splice item)) 1 (token-splice in-file)))
135+
(return true))

0 commit comments

Comments
 (0)