diff options
author | Ash Wolf <ninji@wuffs.org> | 2022-10-19 21:16:13 +0100 |
---|---|---|
committer | Ash Wolf <ninji@wuffs.org> | 2022-10-19 21:16:13 +0100 |
commit | d1f153d34b023d81768f6087f67dbfff714bafc9 (patch) | |
tree | a694d470a60655d0cda15a70791fbdb90a2398cf /command_line/CmdLine/Src/CLLoadAndCache.c | |
parent | 775b6861666af36d317fb577cf489e2c6377f878 (diff) | |
download | MWCC-d1f153d34b023d81768f6087f67dbfff714bafc9.tar.gz MWCC-d1f153d34b023d81768f6087f67dbfff714bafc9.zip |
let's commit all this before my VM blows up and nukes my work
Diffstat (limited to '')
-rw-r--r-- | command_line/CmdLine/Src/CLLoadAndCache.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/command_line/CmdLine/Src/CLLoadAndCache.c b/command_line/CmdLine/Src/CLLoadAndCache.c index e69de29..efc3f63 100644 --- a/command_line/CmdLine/Src/CLLoadAndCache.c +++ b/command_line/CmdLine/Src/CLLoadAndCache.c @@ -0,0 +1,85 @@ +#include "cmdline.h" + +short FixTextHandle(Handle txt) { + char *tptr; + UInt32 tlen; + char *scan; + + HLock(txt); + tptr = *txt; + tlen = GetHandleSize(txt); + + scan = tptr; + while (scan < (tptr + tlen)) { + if (scan[0] == '\r') { + if (scan[1] == '\n') + scan += 2; + else + scan += 1; + } else if (scan[0] == '\n') { + scan[0] = '\r'; + scan++; + } else { + scan++; + } + } + + HUnlock(txt); + return 0; +} + +int LoadAndCacheFile(OSSpec *spec, Handle *texthandle, Boolean *precomp) { + Handle h; + int err; + OSType mactype; + int refnum; + UInt32 ltxtlen; + + if ((*texthandle = CachedIncludeFile(spec, precomp))) + return 0; + + *precomp = !OS_GetMacFileType(spec, &mactype) && mactype != CWFOURCHAR('T','E','X','T'); + *texthandle = NULL; + + err = OS_Open(spec, OSReadOnly, &refnum); + if (err) + return err; + err = OS_GetSize(refnum, <xtlen); + if (err) + return err; + + h = NewHandle(ltxtlen + 1); + if (!h) { + fprintf(stderr, "\n*** Out of memory\n"); + exit(-23); + } + + HLock(h); + err = OS_Read(refnum, *h, <xtlen); + if (err) { + DisposeHandle(h); + OS_Close(refnum); + return err; + } + + OS_Close(refnum); + (*h)[ltxtlen] = 0; + HUnlock(h); + + if (!*precomp) + FixTextHandle(h); + + CacheIncludeFile(spec, h, *precomp); + *texthandle = h; + return 0; +} + +void CopyFileText(Handle src, char **text, SInt32 *textsize) { + *textsize = GetHandleSize(src); + *text = xmalloc(NULL, *textsize); + + HLock(src); + memcpy(*text, *src, *textsize); + *textsize -= 1; + HUnlock(src); +} |