1
/* -*- mode: C++; tab-width: 4 -*- */
2
/* ===================================================================== *\
3
Copyright (c) 1998-2001 Palm, Inc. or its subsidiaries.
6
This file is part of the Palm OS Emulator.
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
\* ===================================================================== */
15
#include "LoadApplication.h"
17
#include "EmErrCodes.h" // kError_OutOfMemory
18
#include "EmMemory.h" // EmMem_memcpy
19
#include "EmPalmStructs.h" // RecordEntryType, RsrcEntryType, etc.
20
#include "EmStreamFile.h" // EmStreamFile
21
#include "ErrorHandling.h" // Errors::ThrowIfPalmError
22
#include "Miscellaneous.h" // StMemory
23
#include "ROMStubs.h" // DmFindDatabase, DmGetLastErr, DmDatabaseInfo, DmOpenDatabase, ...
24
#include "Strings.r.h" // kStr_ values
27
static Bool PrvIsResources (UInt16 attributes)
29
return (attributes & dmHdrAttrResDB);
33
/************************************************************
34
* Export a resource or record database as a normal data file in
36
*************************************************************/
38
static void PrvMyShlExportAsPilotFile(EmStreamFile& fh, UInt16 cardNo, const char* nameP)
49
//------------------------------------------------------------
50
// Locate the Database and see what kind of database it is
51
//------------------------------------------------------------
54
dbID = ::DmFindDatabase (cardNo, (Char*) nameP);
56
Errors::ThrowIfPalmError (::DmGetLastErr ());
58
UInt16 attributes, version;
59
UInt32 crDate, modDate, bckUpDate;
62
LocalID appInfoID, sortInfoID;
63
const int kGapSize = 2;
65
err = ::DmDatabaseInfo (cardNo, dbID, 0,
66
&attributes, &version, &crDate,
71
Errors::ThrowIfPalmError(err);
73
dbP = ::DmOpenDatabase (cardNo, dbID, dmModeReadOnly);
75
Errors::ThrowIfPalmError (::DmGetLastErr ());
77
//------------------------------------------------------------
78
// Write out a resource database
79
//------------------------------------------------------------
80
if (::PrvIsResources (attributes))
86
numRecords = ::DmNumRecords(dbP);
88
size = EmAliasDatabaseHdrType<LAS>::GetSize () + numRecords * EmAliasRsrcEntryType<LAS>::GetSize () + kGapSize;
89
StMemory outP (size, true);
92
EmAliasDatabaseHdrType<LAS> hdr (outP.Get ());
94
strcpy ((char*) hdr.name.GetPtr (), nameP);
95
hdr.attributes = attributes;
96
hdr.version = version;
97
hdr.creationDate = crDate;
98
hdr.modificationDate = modDate;
99
hdr.lastBackupDate = bckUpDate;
100
hdr.modificationNumber = modNum;
104
hdr.creator = creator;
106
hdr.recordList.nextRecordListID = 0;
107
hdr.recordList.numRecords = numRecords;
109
// Get the size of the appInfo and sort Info if they exist
111
MemHandle appInfoH=0, sortInfoH=0;
112
UInt32 appInfoSize=0, sortInfoSize=0;
115
hdr.appInfoID = offset;
116
appInfoH = (MemHandle) ::MemLocalIDToGlobal (appInfoID, cardNo);
119
Errors::ThrowIfPalmError (-1);
121
appInfoSize = ::MemHandleSize (appInfoH);
122
offset += appInfoSize;
127
hdr.sortInfoID = offset;
128
sortInfoH = (MemHandle) ::MemLocalIDToGlobal (sortInfoID, cardNo);
131
Errors::ThrowIfPalmError (-1);
133
sortInfoSize = ::MemHandleSize (sortInfoH);
134
offset += sortInfoSize;
138
// Fill in the info on each resource into the header
140
for (i=0; i < numRecords; i++)
142
EmAliasRsrcEntryType<LAS> entry (hdr.recordList.resources[i]);
144
::DmResourceInfo (dbP, i, &resType, &resID, NULL);
145
MemHandle resH = ::DmGetResourceIndex (dbP, i);
148
resSize = ::MemHandleSize (resH);
149
::DmReleaseResource (resH);
156
entry.type = resType;
158
entry.localChunkID = offset;
165
memset (outP.Get() + (size - kGapSize), 0, kGapSize);
167
// Write out entry table
168
fh.PutBytes (outP.Get (), size);
170
// Write out the appInfo followed by sortInfo, if they exist
171
if (appInfoID && appInfoSize)
174
StMemory outP (appInfoSize);
175
srcP = (UInt32) ::MemHandleLock (appInfoH);
176
EmMem_memcpy ((void*) outP.Get(), srcP, appInfoSize);
177
::MemPtrUnlock ((MemPtr) srcP);
178
fh.PutBytes (outP.Get (), appInfoSize);
181
if (sortInfoID && sortInfoSize)
184
StMemory outP (sortInfoSize);
185
srcP = (UInt32) ::MemHandleLock (sortInfoH);
186
EmMem_memcpy ((void*) outP.Get(), srcP, sortInfoSize);
187
::MemPtrUnlock ((MemPtr) srcP);
188
fh.PutBytes (outP.Get (), sortInfoSize);
191
// Write out each resource
192
for (i=0; i < numRecords; i++)
197
::DmResourceInfo (dbP, i, &resType, &resID, &resChunkID);
202
srcResH = ::DmGetResourceIndex (dbP, i);
205
Errors::ThrowIfPalmError (-1);
207
resSize = ::MemHandleSize (srcResH);
209
StMemory outP (resSize);
210
srcP = (UInt32) ::MemHandleLock (srcResH);
211
EmMem_memcpy ((void*) outP.Get (), srcP, resSize);
212
::MemPtrUnlock ((MemPtr) srcP);
213
fh.PutBytes (outP.Get (), resSize);
215
::DmReleaseResource (srcResH);
218
} // Resource database
220
//------------------------------------------------------------
221
// Write out a records database
222
//------------------------------------------------------------
229
numRecords = ::DmNumRecords (dbP);
231
size = EmAliasDatabaseHdrType<LAS>::GetSize () + numRecords * EmAliasRecordEntryType<LAS>::GetSize () + kGapSize;
235
EmAliasDatabaseHdrType<LAS> hdr (outP.Get ());
237
strcpy ((char*) hdr.name.GetPtr (), nameP);
238
hdr.attributes = attributes;
239
hdr.version = version;
240
hdr.creationDate = crDate;
241
hdr.modificationDate = modDate;
242
hdr.lastBackupDate = bckUpDate;
243
hdr.modificationNumber = modNum;
247
hdr.creator = creator;
249
hdr.recordList.nextRecordListID = 0;
250
hdr.recordList.numRecords = numRecords;
253
// Get the size of the appInfo and sort Info if they exist
255
MemHandle appInfoH=0, sortInfoH=0;
256
UInt32 appInfoSize=0, sortInfoSize=0;
259
hdr.appInfoID = offset;
260
appInfoH = (MemHandle) ::MemLocalIDToGlobal (appInfoID, cardNo);
263
Errors::ThrowIfPalmError (-1);
265
appInfoSize = ::MemHandleSize (appInfoH);
266
offset += appInfoSize;
271
hdr.sortInfoID = offset;
272
sortInfoH = (MemHandle) ::MemLocalIDToGlobal (sortInfoID, cardNo);
275
Errors::ThrowIfPalmError (-1);
277
sortInfoSize = ::MemHandleSize (sortInfoH);
278
offset += sortInfoSize;
282
// Fill in the info on each resource into the header
283
for (i=0; i < numRecords; i++)
285
EmAliasRecordEntryType<LAS> entry (hdr.recordList.records[i]);
287
err = ::DmRecordInfo (dbP, i, &attr, &uniqueID, 0);
290
Errors::ThrowIfPalmError (-1);
293
srcH = ::DmQueryRecord (dbP, i);
295
entry.localChunkID = offset;
296
entry.attributes = attr;
297
entry.uniqueID[0] = (uniqueID >> 16) & 0x00FF;
298
entry.uniqueID[1] = (uniqueID >> 8) & 0x00FF;
299
entry.uniqueID[2] = uniqueID & 0x00FF;
302
offset += ::MemHandleSize (srcH);
307
memset (outP.Get() + (size - kGapSize), 0, kGapSize);
309
// Write out entry table
310
fh.PutBytes (outP.Get (), size);
312
// Write out the appInfo followed by sortInfo, if they exist
313
if (appInfoID && appInfoSize)
316
StMemory outP (appInfoSize);
317
srcP = (UInt32) ::MemHandleLock (appInfoH);
318
EmMem_memcpy ((void*) outP.Get(), srcP, appInfoSize);
319
::MemPtrUnlock ((MemPtr) srcP);
320
fh.PutBytes (outP.Get (), appInfoSize);
323
if (sortInfoID && sortInfoSize)
326
StMemory outP (sortInfoSize);
327
srcP = (UInt32) ::MemHandleLock (sortInfoH);
328
EmMem_memcpy ((void*) outP.Get(), srcP, sortInfoSize);
329
::MemPtrUnlock ((MemPtr) srcP);
330
fh.PutBytes (outP.Get (), sortInfoSize);
333
// Write out each record
334
for (i=0; i<numRecords; i++)
338
err = ::DmRecordInfo (dbP, i, &attr, &uniqueID, 0);
341
Errors::ThrowIfPalmError (-1);
344
srcH = ::DmQueryRecord (dbP, i);
350
recSize = ::MemHandleSize(srcH);
351
StMemory outP (recSize);
352
srcP = (UInt32) ::MemHandleLock (srcH);
353
EmMem_memcpy ((void*) outP.Get(), srcP, recSize);
354
::MemPtrUnlock ((MemPtr) srcP);
355
fh.PutBytes (outP.Get (), recSize);
361
::DmCloseDatabase (dbP);
366
::DmCloseDatabase (dbP);
373
// ---------------------------------------------------------------------------
375
// ---------------------------------------------------------------------------
376
// Saves a Palm OS program or database file to a file.
378
void SavePalmFile (EmStreamFile& appFile, UInt16 cardNo, const char* databaseName)
380
::PrvMyShlExportAsPilotFile (appFile, cardNo, databaseName);