Skip to content

Instantly share code, notes, and snippets.

@euhmeuh
Last active December 20, 2018 12:48
Show Gist options
  • Save euhmeuh/6989533e18e92ca3ac7b64837007f41e to your computer and use it in GitHub Desktop.
Save euhmeuh/6989533e18e92ca3ac7b64837007f41e to your computer and use it in GitHub Desktop.
Create a Forth word containing the bytes of a given file
$FFFF constant FILE-MAX-SIZE
: >rcopy ( a -- a ) ( R: -- a )
compile dup
compile >r
; immediate
: rfetch> ( -- a ) ( R: a -- a )
compile r>
compile dup
compile >r
; immediate
: slurp-file ( str len -- str len )
r/o bin open-file throw
>rcopy file-size throw
if ." File too large (> 4 GB)" cr abort then
dup FILE-MAX-SIZE >
if ." File too large (> " FILE-MAX-SIZE . ." B)" cr abort then
dup allocate throw
swap 2dup rfetch> read-file throw
over <>
if ." Could not read whole file" cr abort
then
r> close-file throw
;
: create-string-word ( str len "name" -- )
create
here 2dup ! over cells cell+ allot
cell+ swap cmove>
;
: cell-count ( addr -- addr len )
dup cell+ swap @
;
: include-raw ( "file" "name" -- )
bl lword count 2dup
>newline ." Include raw file " type cr
slurp-file
create-string-word
;
\ include-raw icon.bmp ICON
\ ICON cell-count dump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment