Skip to content

Instantly share code, notes, and snippets.

@paniq
Created August 18, 2019 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paniq/453ea55a0c56b3d3247cc81d79ab1e3f to your computer and use it in GitHub Desktop.
Save paniq/453ea55a0c56b3d3247cc81d79ab1e3f to your computer and use it in GitHub Desktop.
using import struct
typedef FILE
let
FILE* = (mutable pointer FILE)
SEEK_SET = 0
SEEK_CUR = 1
SEEK_END = 2
let
fopen = (extern 'fopen (function FILE* rawstring rawstring))
fclose = (extern 'fclose (function i32 FILE*))
fseek = (extern 'fseek (function i32 FILE* i64 i32))
fread = (extern 'fread (function u64 voidstar u64 u64 FILE*))
fwrite = (extern 'fwrite (function u64 voidstar u64 u64 FILE*))
ftell = (extern 'ftell (function i64 FILE*))
ferror = (extern 'ferror (function i32 FILE*))
fflush = (extern 'fflush (function i32 FILE*))
errno = (extern 'errno i32)
struct File
_handle : FILE*
inline __typecall (cls)
static-error "use File.open to create files"
inline open (filename mode)
let handle = (fopen filename mode)
if (handle == null)
raise (deref (@ errno))
super-type.__typecall this-type
_handle = handle
inline error? (self)
ferror self._handle
inline write (self ptr size)
(fwrite ptr size 1 self._handle) == 1
inline read (self ptr size)
(fread ptr size 1 self._handle) == 1
inline flush (self)
fflush self._handle
inline __drop (self)
fclose self._handle
super-type.__drop self
do
let File
locals;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment