man Palm::PDB () - Parse Palm database files.
NAME
Palm::PDB - Parse Palm database files.
SYNOPSIS
use Palm::PDB; use SomeHelperClass;
$pdb = new Palm::PDB; $pdb->Load("myfile.pdb");
# Manipulate records in $pdb
$pdb->Write("myotherfile.pdb");
(Note: yes, you do want to use CWPalm::PDB, even if you're dealing with some other type of database. CW$pdb will be reblessed to the appropriate type by CW$pdb->Load.)
DESCRIPTION
The Palm::PDB module provides a framework for reading and writing database files for use on PalmOS devices such as the PalmPilot. It can read and write both Palm Database (CW.pdb) and Palm Resource (CW.prc) files.
By itself, the PDB module is not terribly useful; it is intended to be used in conjunction with supplemental modules for specific types of databases, such as Palm::Raw or Palm::Memo.
The Palm::PDB module encapsulates the common work of parsing the structure of a Palm database. The Load() function reads the file, then passes the individual chunks (header, records, etc.) to application-specific functions for processing. Similarly, the Write() function calls application-specific functions to get the individual chunks, then writes them to a file.
METHODS
new
$new = new Palm::PDB();
Creates a new PDB. CW$new is a reference to an anonymous hash. Some of its elements have special significance. See Load().
RegisterPDBHandlers
&Palm::PDB::RegisterPDBHandlers("classname", typespec...);
Typically:
&Palm::PDB::RegisterPDBHandlers(__PACKAGE__, [ "FooB", "DATA" ], );
The CW$pdb->Load() method acts as a virtual constructor. When it reads the header of a CW.pdb file, it looks up the file's creator and type in a set of tables, and reblesses CW$pdb into a class capable of parsing the application-specific parts of the file (AppInfo block, records, etc.)
RegisterPDBHandlers() adds entries to these tables; it says that any file whose creator and/or type match any of the typespecs (there may be several) should be reblessed into the class classname.
Note that RegisterPDBHandlers() applies only to record databases (CW.pdb files). For resource databases, see RegisterPRCHandlers().
RegisterPDBHandlers() is typically called in the import() function of a helper class. In this case, the class is registering itself, and it is simplest just to use CW__PACKAGE__ for the package name:
package PalmFoo; use Palm::PDB;
sub import { &Palm::PDB::RegisterPDBHandlers(__PACKAGE__, [ "FooZ", "DATA" ] ); }
A typespec can be either a string, or an anonymous array with two elements. If it is an anonymous array, then the first element is the file's creator; the second element is its type. If a typespec is a string, it is equivalent to specifying that string as the database's creator, and a wildcard as its type.
The creator and type should be either four-character strings, or the empty string. An empty string represents a wildcard. Thus:
&Palm::PDB::RegisterPDBHandlers("MyClass", [ "fOOf", "DATA" ], [ "BarB", "" ], [ "", "BazQ" ], "Fred" );
Class MyClass will handle:
- Databases whose creator is CWfOOf and whose type is CWDATA.
- Databases whose creator is CWBarB, of any type.
- Databases with any creator whose type is CWBazQ.
- Databases whose creator is CWFred, of any type.
RegisterPRCHandlers
&Palm::PDB::RegisterPRCHandlers("classname", typespec...);
Typically:
&Palm::PDB::RegisterPRCHandlers(__PACKAGE__, [ "FooZ", "CODE" ], );
RegisterPRCHandlers() is similar to RegisterPDBHandlers(), but specifies a class to handle resource database (CW.prc) files.
A class for parsing applications should begin with:
package PalmApps; use Palm::PDB;
sub import { &Palm::PDB::RegisterPRCHandlers(__PACKAGE__, [ "", "appl" ] ); }
Load
$pdb->Load("filename");
Reads the file filename, parses it, reblesses CW$pdb to the appropriate class, and invokes appropriate methods to parse the application-specific parts of the database (see HELPER CLASSES).
Load() uses the typespecs given to RegisterPDBHandlers() and RegisterPRCHandlers() when deciding how to rebless CW$pdb. For record databases, it uses the typespecs passed to RegisterPDBHandlers(), and for resource databases, it uses the typespecs passed to RegisterPRCHandlers().
Load() looks for matching typespecs in the following order, from most to least specific:
- 1
- A typespec that specifies both the database's creator and its type exactly.
- 2
- A typespec that specifies the database's type and has a wildcard for the creator (this is rarely used).
- 3
- A typespec that specifies the database's creator and has a wildcard for the type.
- 4
- A typespec that has wildcards for both the creator and type.
Thus, if the database has creator FooZ and type DATA, Load() will first look for FooZ/DATA, then "/DATA, then FooZ/, and finally will fall back on /" (the universal default).
After Load() returns, CW$pdb may contain the following fields: The name of the database. These are the attribute flags from the database header. Each is true iff the corresponding flag is set. The LaunchableData attribute is set on PQAs. The database's version number. An integer. The database's creation time, last modification time, and time of last backup, in Unix CWtime_t format (seconds since Jan. 1, 1970). The database's modification number. An integer. The database's type. A four-character string. The database's creator. A four-character string. The database's unique ID seed. An integer. The two NUL bytes that appear after the record index and the AppInfo block. Included here because every once in a long while, they are not NULs, for some reason. The AppInfo block, as returned by the CW$pdb->ParseAppInfoBlock() helper method. The sort block, as returned by the CW$pdb->ParseSortBlock() helper method. The list of records in the database, as returned by the CW$pdb->ParseRecord() helper method. Resource databases do not have this. The list of resources in the database, as returned by the CW$pdb->ParseResource() helper method. Record databases do not have this.
All of these fields may be set by hand, but should conform to the format given above.
Write
$pdb->Write("filename");
Invokes methods in helper classes to get the application-specific parts of the database, then writes the database to the file filename.
Write() uses the following helper methods:
- PackAppInfoBlock()
- PackSortBlock()
- PackResource() or PackRecord()
See also HELPER CLASSES.
new_Record
$record = Palm::PDB->new_Record(); $record = new_Record Palm::PDB;
Creates a new record, with the bare minimum needed:
$record->{'category'} $record->{'attributes'}{'Dirty'} $record->{'id'}
The ``Dirty'' attribute is originally set, since this function will usually be called to create records to be added to a database.
CWnew_Record does not add the new record to a PDB. For that, you want CWappend_Record.
append_Record
$record = $pdb->append_Record; $record2 = $pdb->append_Record($record1);
If called without any arguments, creates a new record with new_Record(), and appends it to CW$pdb.
If given a reference to a record, appends that record to @{$pdb->{records}}.
Returns a reference to the newly-appended record.
This method updates CW$pdb's last modification time.
$resource = Palm::PDB->new_Resource(); $resource = new_Resource Palm::PDB;
Creates a new resource and initializes
$resource->{type} $resource->{id}
append_Resource
$resource = $pdb->append_Resource; $resource2 = $pdb->append_Resource($resource1);
If called without any arguments, creates a new resource with new_Resource(), and appends it to CW$pdb.
If given a reference to a resource, appends that resource to @{$pdb->{resources}}.
Returns a reference to the newly-appended resource.
This method updates CW$pdb's last modification time.
findRecordByID
$record = $pdb->findRecordByID($id);
Looks through the list of records in CW$pdb, and returns a reference to the record with ID CW$id, or the undefined value if no such record was found.
delete_Record
$pdb->delete_Record($record, $expunge);
Marks CW$record for deletion, so that it will be deleted from the database at the next sync.
If CW$expunge is false or omitted, the record will be marked for deletion with archival. If CW$expunge is true, the record will be marked for deletion without archival.
This method updates CW$pdb's last modification time.
HELPER CLASSES
$pdb->Load() reblesses CW$pdb into a new class. This helper class is expected to convert raw data from the database into parsed representations of it, and vice-versa.
A helper class must have all of the methods listed below. The Palm::Raw class is useful if you don't want to define all of the required methods.
ParseAppInfoBlock
$appinfo = $pdb->ParseAppInfoBlock($buf);
$buf is a string of raw data. ParseAppInfoBlock() should parse this data and return it, typically in the form of a reference to an object or to an anonymous hash.
This method will not be called if the database does not have an AppInfo block.
The return value from ParseAppInfoBlock() will be accessible as CW$pdb->{appinfo}.
PackAppInfoBlock
$buf = $pdb->PackAppInfoBlock();
This is the converse of ParseAppInfoBlock(). It takes CW$pdb's AppInfo block, CW$pdb->{appinfo}, and returns a string of binary data that can be written to the database file.
ParseSortBlock
$sort = $pdb->ParseSortBlock($buf);
$buf is a string of raw data. ParseSortBlock() should parse this data and return it, typically in the form of a reference to an object or to an anonymous hash.
This method will not be called if the database does not have a sort block.
The return value from ParseSortBlock() will be accessible as CW$pdb->{sort}.
PackSortBlock
$buf = $pdb->PackSortBlock();
This is the converse of ParseSortBlock(). It takes CW$pdb's sort block, CW$pdb->{sort}, and returns a string of raw data that can be written to the database file.
ParseRecord
$record = $pdb->ParseRecord( offset => $offset, # Record's offset in file attributes => # Record attributes { expunged => bool, # True iff expunged dirty => bool, # True iff dirty deleted => bool, # True iff deleted private => bool, # True iff private archive => bool, # True iff to be archived }, category => $category, # Record's category number id => $id, # Record's unique ID data => $buf, # Raw record data );
ParseRecord() takes the arguments listed above and returns a parsed representation of the record, typically as a reference to a record object or anonymous hash.
The output from ParseRecord() will be appended to @{$pdb->{records}}. The records appear in this list in the same order as they appear in the file.
$offset argument is not normally useful, but is included for completeness.
The fields in %$attributes are boolean values. They are true iff the record has the corresponding flag set.
$category is an integer in the range 0-15, which indicates which category the record belongs to. This is normally an index into a table given at the beginning of the AppInfo block.
A typical ParseRecord() method has this general form:
sub ParseRecord { my $self = shift my %record = @_;
# Parse $self->{data} and put the fields into new fields in # $self.
delete $record{data}; # No longer useful return \%record; }
PackRecord
$buf = $pdb->PackRecord($record);
The converse of ParseRecord(). PackRecord() takes a record as returned by ParseRecord() and returns a string of raw data that can be written to the database file.
PackRecord() is never called when writing a resource database.
ParseResource
$record = $pdb->ParseResource( type => $type, # Resource type id => $id, # Resource ID offset => $offset, # Resource's offset in file data => $buf, # Raw resource data );
ParseResource() takes the arguments listed above and returns a parsed representation of the resource, typically as a reference to a resource object or anonymous hash.
The output from ParseResource() will be appended to @{$pdb->{resources}}. The resources appear in this list in the same order as they appear in the file.
$type is a four-character string giving the resource's type.
$id is an integer that uniquely identifies the resource amongst others of its type.
$offset is not normally useful, but is included for completeness.
PackResource
$buf = $pdb->PackResource($resource);
The converse of ParseResource(). PackResource() takes a resource as returned by PackResource() and returns a string of raw data that can be written to the database file.
PackResource() is never called when writing a record database.
BUGS
These functions die too easily. They should return an error code.
Database manipulation is still an arcane art.
It may be possible to parse sort blocks further.
AUTHOR
Andrew Arensburger <arensb@ooblick.com>
SEE ALSO
Palm Database Files, in the ColdSync distribution.
The Virtual Constructor (aka Factory Method) pattern is described in Design Patterns, by Erich Gamma et al., Addison-Wesley.