The method I mentioned, storing the PRC itself in a resource, then
creating a new database on the app from that resource is extremely
easy, and the PalmOS API provides a very handy function for doing
exactly that -- its how the standard PIM apps create their databases
when they're run the first time. Its the DmCreateDatabaseFromImage()
function, and you might want to take a look at the documentation for
it.

You just copy the PRC to your resource directory and use a line in
your resource script like so to link it in:

   DATA "resType" ID 1000 "application.prc"

and in your code, get a handle to the resource, lock it,  and pass the
pointer to DmCreateDatabaseFromImage() to make the magic happen.

The problem, however, with this *particular* PRC, is that it's 71Kb in
size, and Hotsync will choke on resources that size -- effectively
making it impossible to an app with a resource larger than 64k onto
the device.

So... here's what you'd have to do instead:

1) split SysZLib.prc into two parts. To do this, you'll need some sort
of binary-file-splitter that doesn't add headers or anything silly to
the output. I used the one found here: http://www.fourmilab.ch/splits

To actually split the resource using this particular utility, you
simply run it at the command line, thus:

splits SysZLib.prc 48Kb

And it creates two files, "SysZLib.prc.001" and "SysZLib.prc.002",
which I copied to my resource directory.

2) add entries to the resouce script to include the pieces of the PRC
as binary data. Essentially, I added these two lines to my RCP file:

DATA "resType" ID 1023 "SysZLib.prc.001"
DATA "resType" ID 1024 "SysZLib.prc.002"

I also have an alert that I display, should this fail somewhere along the way:

ALERT ID SysZLibMissingAlert 
ERROR
BEGIN
         MESSAGE "SysZLib.prc not found or failed to load. Unable to continue."
     TITLE "Missing Shared Library"
     BUTTONS "OK" 
END



3) add code to create the shared library on the handheld if needed.

Here, I'm trying to open the shared library from AppStart, and if it
fails, I create a Feature memory pointer to hold data from the
combined resources, passing that pointer to
DmCreateDatabaseFromImage() to write out the library; followed by a
little cleanup and another attempt to open the library, the error code
resulting from this attempt is returned by the function -- so if it
fails the second time for whatever reason, the calling function knows
it.

In case you're unaware, Feature memory is allocated from storage RAM,
rather than heap, so its better for this than using MemPtrNew. Since
we're passing it to a function that's accustomed to working with
pointers into storage RAM, everything's just fine. Normally, you have
to write to feature memory using the DataManager functions, rather
than directly (like I'm doing below), since it *is* on the storage
heap. I'm using the Memory Semaphore to bypass that as it seems
quicker. You could use DmWrite instead of MemMove, if you prefer.
There's certainly no *trick* to what I'm doing - it just seems faster.

This is using Feature Number 1024 -- that's a totally arbitrary
number, and you can use anything you want. Read up on features for
more info.

static Err AppStart(void)
{
        Err error;
        
        // open compression library
        error = SysZLib_OpenLibrary(&ZLibRef);
        if (error) {
                // open failed - try to create the library from included 
resources
                char *src;
                char *zlibFullP;
                MemHandle zlib1 = DmGetResource('resT', 1023); // 1st part of 
PRC
                MemHandle zlib2 = DmGetResource('resT', 1024); // 2nd part of 
PRC

                if (zlib1 && zlib2)
                {
                        // allocate a buffer in storage RAM to store the 
combined PRC
                        error = FtrPtrNew(appFileCreator, 1024, 
MemHandleSize(zlib1) +
MemHandleSize(zlib2), (void **)&zlibFullP);
                }

                // everything work?
                if (zlib1 && zlib2 && zlibFullP)
                {
                        // write out first part
                        char *dst = zlibFullP;
                        src = (char *)MemHandleLock(zlib1);
                        MemSemaphoreReserve(true);
                        MemMove(dst, src, MemHandleSize(zlib1));
                        MemSemaphoreRelease(true);
                        MemHandleUnlock(zlib1);

                        // get ready to write second part
                        dst += MemHandleSize(zlib1);
                        src = (char *)MemHandleLock(zlib2);

                        // write it out
                        MemSemaphoreReserve(true);
                        MemMove(dst, src, MemHandleSize(zlib2));
                        MemSemaphoreRelease(true);
                        MemHandleUnlock(zlib2);

                        // create shared library from image
                        error = DmCreateDatabaseFromImage(zlibFullP);

                        // try opening it again
                        if (!error)
                                error = SysZLib_OpenLibrary(&ZLibRef);
                }
                else
                {
                        // the resources weren't found and the library isn't on 
the
                        // device - - display an informative alert
                        FrmAlert(SysZLibMissingAlert);
                }

                // clean up
                if (zlib1)
                        DmReleaseResource(zlib1);
                if (zlib2)
                        DmReleaseResource(zlib2);
                if (zlibFullP)
                        FtrPtrFree(appFileCreator, 1024);

                // return the result from either FtrPtrNew, 
DmCreateDatabaseFromImage, or
                // SysZLib_OpenLibrary, so the caller knows what happened. If 
it all worked,
                // error will be Zero.
                return error;
        }

        // library loaded the first time, no assembly required. ;)
        return errNone;
}

Hope this helps.

Brandon

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please 
see http://www.palmos.com/dev/support/forums/

Reply via email to