Malcolm's github site Baby X Resource compiler    Baby X RC banner

Importing Directories into C programs with the Baby X Resource Compiler

You often have data in a directory or folder somewhere on your machine, and want to import the entire folder into a C program. And it would be nice to achieve this with the resource compiler with a "folder" tag. But this cannot be done because the resource compiler is written in portable ANSI C, and there is no way of traversing a computer's directory structure. For that you must go to Posix. However there is now a solution, but it involves a second program written in Posix. And so we have a separate project, BabyXFS, which is supplementary to the resource compiler, babyxrc.

babyxfs_dirtoxml

The solution is to use a program called babyxfs_dirtoxml, which crawls a C source directory for files and writes them as xml. You then have a big xml file which you embed in the C program as a string. You then call the XML parser on the string to get a document tree, which is your file structure. You then use a function provided to load files a FILE *, which you can pass to functions in your C program which operate on streams.

     
     babyxfs_dirtoxml <targetfolder>
    
     The program accepts a folder as an argument, traverses it,
     and spits out the results as XML to stdout.
     
     Example:
        babysfxs_dirtoxml  data/myfolder > myfolder.xml
        
 

babyxfs_dirtoxml is simple but very powerful, and produces clean XML with text files represented a plain text and binary files uuencoded. It produces XML in the <FileSystem> format.

Embedding the directory in a C program

Once we've got the XML, we need to get it into our C program and mount it as a directory. And so of course to embed a string, we use the Baby X resource compiler with a simple script.

     
 <BabyXRC>
    <string src="myfolder.xml"></string>
 </BabyXRC>

 

Now we need to mount the string as filesystem. So first we need to load it as an XML document using the function xmldocfromstring() from the fie xmlparser2.h. Then we use the function xml_fopen() from the file directory.c to get a FILE * with the desired file.


 #include "xmlparser2.h"
 #include "xml_fload.h" /* you need to steal this yourself from directory.c */
 
 XMLDOC *doc;
 char error[1024];
 FILE * fp;
 
 /* load the documents. babyxrc will have created the string "myfolder" */
 doc = xmldocfromstring(myfolder, error, 1024);
 if (!doc)
    fprintf(stderr, "%s\n", error); /* whoops */
 
 fp = xml_fload(doc, "/myfolder/books/MalcolmMcLean/Adam_and_Abagail_Go_to_St_Toms.pdf", "r");
 

Very simple.

It's rather a greedy solution in terms of memory, but that's seldom much of a consderation with machines with many gigabyres installed. But the power and simplicity win out, and it is all completely open source and no dependencies.

C source files

These are old files which are retained here for refererence, and so that if you come to the page you have something. The programs are being developed actively in the Baby X resource compiler project