summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/CLLoadAndCache.c
diff options
context:
space:
mode:
authorAsh Wolf <ninji@wuffs.org>2022-10-19 21:16:13 +0100
committerAsh Wolf <ninji@wuffs.org>2022-10-19 21:16:13 +0100
commitd1f153d34b023d81768f6087f67dbfff714bafc9 (patch)
treea694d470a60655d0cda15a70791fbdb90a2398cf /command_line/CmdLine/Src/CLLoadAndCache.c
parent775b6861666af36d317fb577cf489e2c6377f878 (diff)
downloadMWCC-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.c85
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, &ltxtlen);
+ 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, &ltxtlen);
+ 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);
+}