[Klone-users] Open file since internal KLone system
thomas fossati
tho at koanlogic.com
Fri Dec 19 08:56:46 EST 2008
Hi Mickael,
On Dec 19, 2008, at 12:21 PM, Mickael Auger wrote:
> Hi KLone team,
>
> I wish to open a file in my "webapp" folder. It's embedded with
> KLone binary.
> I need to read a file into "webapp" (ex : webapp/myconf.ini") with
> INI parser but my INI parser use standard "open()", "read()" ...
> functions.
>
> Can I open an embedded file in binary, please ?
>
> ex :
> My KL1 page could be :
> ===================================
> <%
> FILE fic;
> fic = open("%klone%/webapp/myconf.ini");
> ===================================
> which "%klone% would be replace by the good fonction (for example :
> fs_open("/webapp/myconf.ini") where "fs_open" could the function to
> open a file in embedded KLone system) after the post-compilation (in
> "pg_xxxx.c" file in "target/klone/site")
If I understand correctly you want klone to recreate the FILE
abstraction in a way that would be consistent with klone's embfs.
It's a feature quite hard to meat since it means replacing (or
hooking) the whole C stdio library with klone-friendly fopen/fclose/
fread & co. which is a looooooooooooot of work :)
I think that if you have at hand the INI parser sources you could
operate in one of the following ways:
1) abstract the I/O driver and set the read/write handlers at runtime:
e.g.:
drv->cb_open = my_fopen (io_open if you are working on embfs);
drv->cb_read = my_fread (io_read);
drv->cb_close = my_fclose (io_close);
much like the libu::config and libu::pwd modules do;
2) if your parser can work with memory buffers, load the content of
the INI file into memory via emb_open and io_read:
char buf[...];
ssize_t sz;
emb_open("/webapp/myconf.ini", &io);
sz = io_read(io, buf, sizeof buf);
ini_parse_mem(buf, sz, &ini);
In case you don't have the sources you could still handle the
situation like this:
3) read the ini file from embfs and put its content in a temp file,
then call your parser on the temp file:
char fname[] = "/tmp/iniXXXXXX";
FILE *fp = fopen(mktemp(fname), "w+");
emb_open("/webapp/myconf.ini", &io);
for (;;)
{
sz = io_read(io, buf, sizeof buf);
if (sz == 0) break;
fwrite(buf, sz, 1, fp);
}
rewind(fp);
ini_parse_fp(fp, &ini);
ciao, t.
More information about the klone-users
mailing list