From addbcaa09c3d10346fa37cb97712eef4bd813833 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 24 Nov 2010 13:12:41 +0900 Subject: [PATCH] Add PL_GetFromFile builtin. This is an extension "wrapper" (no such C function). This allows ruamoko programs to read property lists without worrying about the memory required to store the string for the property list being read. --- libs/ruamoko/rua_plist.c | 24 ++++++++++++++++++++++++ ruamoko/include/PropertyList.h | 1 + ruamoko/include/plist.h | 3 +++ ruamoko/lib/PropertyList.r | 5 +++++ ruamoko/lib/plist.r | 1 + 5 files changed, 34 insertions(+) diff --git a/libs/ruamoko/rua_plist.c b/libs/ruamoko/rua_plist.c index 3c59c4a9b..2c48d6583 100644 --- a/libs/ruamoko/rua_plist.c +++ b/libs/ruamoko/rua_plist.c @@ -175,6 +175,29 @@ plist_retain (plist_resources_t *res, plitem_t *plitem) return handle; } +static void +bi_PL_GetFromFile (progs_t *pr) +{ + plist_resources_t *res = PR_Resources_Find (pr, "plist"); + QFile *file = QFile_GetFile (pr, P_INT (pr, 0)); + plitem_t *plitem; + long offset; + long size; + long len; + char *buf; + + offset = Qtell (file); + size = Qfilesize (file); + len = size - offset; + buf = malloc (len + 1); + Qread (file, buf, len); + buf[len] = 0; + + plitem = PL_GetPropertyList (buf); + + R_INT (pr) = plist_retain (res, plitem); +} + static void bi_PL_GetPropertyList (progs_t *pr) { @@ -403,6 +426,7 @@ plist_compare (void *k1, void *k2, void *unused) } static builtin_t builtins[] = { + {"PL_GetFromFile", bi_PL_GetFromFile, -1}, {"PL_GetPropertyList", bi_PL_GetPropertyList, -1}, {"PL_WritePropertyList", bi_PL_WritePropertyList, -1}, {"PL_Type", bi_PL_Type, -1}, diff --git a/ruamoko/include/PropertyList.h b/ruamoko/include/PropertyList.h index 825249f44..c75d9b571 100644 --- a/ruamoko/include/PropertyList.h +++ b/ruamoko/include/PropertyList.h @@ -14,6 +14,7 @@ + (PLItem) newData:(void[]) data size:(integer) len; + (PLItem) newString:(string) str; + (PLItem) fromString:(string) str; ++ (PLItem) fromFile:(QFile) file; - initWithItem:(plitem_t) item; - initWithOwnItem:(plitem_t) item; diff --git a/ruamoko/include/plist.h b/ruamoko/include/plist.h index 5a78e529e..02d8cfd0e 100644 --- a/ruamoko/include/plist.h +++ b/ruamoko/include/plist.h @@ -1,11 +1,14 @@ #ifndef __ruamoko_plist_h #define __ruamoko_plist_h +#include "qfile.h" + struct plitem_s {integer dummy;}; #define PL_TEST(item) (item.dummy) typedef struct plitem_s plitem_t; typedef enum {QFDictionary, QFArray, QFBinary, QFString} pltype_t; // possible types +@extern plitem_t (QFile file) PL_GetFromFile; @extern plitem_t (string str) PL_GetPropertyList; @extern string (plitem_t pl) PL_WritePropertyList; @extern pltype_t (plitem_t str) PL_Type; diff --git a/ruamoko/lib/PropertyList.r b/ruamoko/lib/PropertyList.r index 237d07b50..a0a66e320 100644 --- a/ruamoko/lib/PropertyList.r +++ b/ruamoko/lib/PropertyList.r @@ -27,6 +27,11 @@ return [[PLItem itemClass: PL_GetPropertyList (str)] autorelease]; } ++ (PLItem) fromFile:(QFile) file +{ + return [[PLItem itemClass: PL_GetFromFile (file)] autorelease]; +} + + itemClass:(plitem_t) item { local string classname = NIL; diff --git a/ruamoko/lib/plist.r b/ruamoko/lib/plist.r index a0c91af96..5fdeada13 100644 --- a/ruamoko/lib/plist.r +++ b/ruamoko/lib/plist.r @@ -1,5 +1,6 @@ #include "plist.h" +plitem_t (QFile file) PL_GetFromFile = #0; plitem_t (string str) PL_GetPropertyList = #0; string (plitem_t pl) PL_WritePropertyList = #0; pltype_t (plitem_t str) PL_Type = #0;