~akheon23/pose/origin

« back to all changes in this revision

Viewing changes to SrcShared/LoadApplication.cpp

  • Committer: Christopher Werkshage
  • Date: 2014-12-27 12:51:57 UTC
  • Revision ID: git-v1:cc9410eb6ec5b4e23248dc175d142202f86be8e8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++; tab-width: 4 -*- */
 
2
/* ===================================================================== *\
 
3
        Copyright (c) 1998-2001 Palm, Inc. or its subsidiaries.
 
4
        All rights reserved.
 
5
 
 
6
        This file is part of the Palm OS Emulator.
 
7
 
 
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
\* ===================================================================== */
 
13
 
 
14
#include "EmCommon.h"
 
15
#include "LoadApplication.h"
 
16
 
 
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
 
25
 
 
26
 
 
27
static Bool PrvIsResources (UInt16 attributes)
 
28
{
 
29
        return (attributes & dmHdrAttrResDB);
 
30
}
 
31
 
 
32
 
 
33
/************************************************************
 
34
 * Export a resource or record database as a normal data file in 
 
35
 *      Pilot format.
 
36
 *************************************************************/
 
37
 
 
38
static void PrvMyShlExportAsPilotFile(EmStreamFile& fh, UInt16 cardNo, const char* nameP)
 
39
{
 
40
        DmOpenRef       dbP = 0;
 
41
        Err             err;
 
42
        UInt16          numRecords;
 
43
        int             size;
 
44
        long            offset;
 
45
        int             i;
 
46
 
 
47
        try
 
48
        {
 
49
                //------------------------------------------------------------
 
50
                // Locate the Database and see what kind of database it is
 
51
                //------------------------------------------------------------
 
52
 
 
53
                LocalID dbID;
 
54
                dbID = ::DmFindDatabase (cardNo, (Char*) nameP);
 
55
                if (!dbID)
 
56
                        Errors::ThrowIfPalmError (::DmGetLastErr ());
 
57
 
 
58
                UInt16          attributes, version;
 
59
                UInt32          crDate, modDate, bckUpDate;
 
60
                UInt32          type, creator;
 
61
                UInt32          modNum;
 
62
                LocalID         appInfoID, sortInfoID;
 
63
                const int       kGapSize = 2;
 
64
 
 
65
                err = ::DmDatabaseInfo (cardNo, dbID, 0,
 
66
                                        &attributes, &version, &crDate,
 
67
                                        &modDate, &bckUpDate,
 
68
                                        &modNum, &appInfoID,
 
69
                                        &sortInfoID, &type,
 
70
                                        &creator);
 
71
                Errors::ThrowIfPalmError(err);
 
72
 
 
73
                dbP = ::DmOpenDatabase (cardNo, dbID, dmModeReadOnly);
 
74
                if (!dbP)
 
75
                        Errors::ThrowIfPalmError (::DmGetLastErr ());
 
76
                
 
77
                //------------------------------------------------------------
 
78
                // Write out a resource database
 
79
                //------------------------------------------------------------
 
80
                if (::PrvIsResources (attributes))
 
81
                {
 
82
                        DmResType       resType;
 
83
                        DmResID         resID;
 
84
                        UInt32          resSize;
 
85
 
 
86
                        numRecords = ::DmNumRecords(dbP);
 
87
 
 
88
                        size = EmAliasDatabaseHdrType<LAS>::GetSize () + numRecords * EmAliasRsrcEntryType<LAS>::GetSize () + kGapSize;
 
89
                        StMemory        outP (size, true);
 
90
 
 
91
                        // Fill in header
 
92
                        EmAliasDatabaseHdrType<LAS>     hdr (outP.Get ());
 
93
 
 
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;
 
101
                        hdr.appInfoID                   = 0;
 
102
                        hdr.sortInfoID                  = 0;
 
103
                        hdr.type                                = type;
 
104
                        hdr.creator                             = creator;
 
105
 
 
106
                        hdr.recordList.nextRecordListID = 0;
 
107
                        hdr.recordList.numRecords               = numRecords;
 
108
 
 
109
                        // Get the size of the appInfo and sort Info if they exist
 
110
                        offset = size;
 
111
                        MemHandle       appInfoH=0, sortInfoH=0;
 
112
                        UInt32          appInfoSize=0, sortInfoSize=0;
 
113
                        if (appInfoID)
 
114
                        {
 
115
                                hdr.appInfoID = offset;
 
116
                                appInfoH = (MemHandle) ::MemLocalIDToGlobal (appInfoID, cardNo);
 
117
                                if (!appInfoH)
 
118
                                {
 
119
                                        Errors::ThrowIfPalmError (-1);
 
120
                                }
 
121
                                appInfoSize = ::MemHandleSize (appInfoH);
 
122
                                offset += appInfoSize;
 
123
                        }
 
124
 
 
125
                        if (sortInfoID)
 
126
                        {
 
127
                                hdr.sortInfoID = offset;
 
128
                                sortInfoH = (MemHandle) ::MemLocalIDToGlobal (sortInfoID, cardNo);
 
129
                                if (!sortInfoH)
 
130
                                {
 
131
                                        Errors::ThrowIfPalmError (-1);
 
132
                                }
 
133
                                sortInfoSize = ::MemHandleSize (sortInfoH);
 
134
                                offset += sortInfoSize;
 
135
                        }
 
136
 
 
137
 
 
138
                        // Fill in the info on each resource into the header
 
139
                        offset = size;
 
140
                        for (i=0; i < numRecords; i++)
 
141
                        {
 
142
                                EmAliasRsrcEntryType<LAS>       entry (hdr.recordList.resources[i]);
 
143
 
 
144
                                ::DmResourceInfo (dbP, i, &resType, &resID, NULL);
 
145
                                MemHandle       resH = ::DmGetResourceIndex (dbP, i);
 
146
                                if (resH)
 
147
                                {
 
148
                                        resSize = ::MemHandleSize (resH);
 
149
                                        ::DmReleaseResource (resH);
 
150
                                }
 
151
                                else
 
152
                                {
 
153
                                        resSize = 0;
 
154
                                }
 
155
 
 
156
                                entry.type                      = resType;
 
157
                                entry.id                        = resID;
 
158
                                entry.localChunkID      = offset;
 
159
                                
 
160
                                offset += resSize;
 
161
                        }
 
162
                        
 
163
                        // Clear out gap
 
164
                        if (kGapSize > 0)
 
165
                                memset (outP.Get() + (size - kGapSize), 0, kGapSize);
 
166
                        
 
167
                        // Write out  entry table
 
168
                        fh.PutBytes (outP.Get (), size);
 
169
                        
 
170
                        // Write out the appInfo followed by sortInfo, if they exist
 
171
                        if (appInfoID && appInfoSize)
 
172
                        {
 
173
                                UInt32          srcP;
 
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);
 
179
                        }
 
180
 
 
181
                        if (sortInfoID && sortInfoSize)
 
182
                        {
 
183
                                UInt32          srcP;
 
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);
 
189
                        }
 
190
 
 
191
                        // Write out each resource
 
192
                        for (i=0; i < numRecords; i++)
 
193
                        {
 
194
                                MemHandle       srcResH;
 
195
                                LocalID         resChunkID;
 
196
                                
 
197
                                ::DmResourceInfo (dbP, i, &resType, &resID, &resChunkID);
 
198
 
 
199
                                if (resChunkID)
 
200
                                {
 
201
                                        UInt32  srcP;
 
202
                                        srcResH = ::DmGetResourceIndex (dbP, i);
 
203
                                        if (!srcResH)
 
204
                                        {
 
205
                                                Errors::ThrowIfPalmError (-1);
 
206
                                        }
 
207
                                        resSize = ::MemHandleSize (srcResH);
 
208
 
 
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);
 
214
 
 
215
                                        ::DmReleaseResource (srcResH);
 
216
                                }
 
217
                        }
 
218
                } // Resource database
 
219
 
 
220
                //------------------------------------------------------------
 
221
                // Write out a records database
 
222
                //------------------------------------------------------------
 
223
                else
 
224
                {
 
225
                        UInt16          attr;
 
226
                        UInt32          uniqueID;
 
227
                        MemHandle       srcH;
 
228
 
 
229
                        numRecords = ::DmNumRecords (dbP);
 
230
 
 
231
                        size = EmAliasDatabaseHdrType<LAS>::GetSize () + numRecords * EmAliasRecordEntryType<LAS>::GetSize () + kGapSize;
 
232
                        StMemory        outP(size);
 
233
 
 
234
                        // Fill in header
 
235
                        EmAliasDatabaseHdrType<LAS>     hdr (outP.Get ());
 
236
 
 
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;
 
244
                        hdr.appInfoID                   = 0;
 
245
                        hdr.sortInfoID                  = 0;
 
246
                        hdr.type                                = type;
 
247
                        hdr.creator                             = creator;
 
248
 
 
249
                        hdr.recordList.nextRecordListID = 0;
 
250
                        hdr.recordList.numRecords               = numRecords;
 
251
 
 
252
 
 
253
                        // Get the size of the appInfo and sort Info if they exist
 
254
                        offset = size;
 
255
                        MemHandle       appInfoH=0, sortInfoH=0;
 
256
                        UInt32          appInfoSize=0, sortInfoSize=0;
 
257
                        if (appInfoID)
 
258
                        {
 
259
                                hdr.appInfoID = offset;
 
260
                                appInfoH = (MemHandle) ::MemLocalIDToGlobal (appInfoID, cardNo);
 
261
                                if (!appInfoH)
 
262
                                {
 
263
                                        Errors::ThrowIfPalmError (-1);
 
264
                                }
 
265
                                appInfoSize = ::MemHandleSize (appInfoH);
 
266
                                offset += appInfoSize;
 
267
                        }
 
268
 
 
269
                        if (sortInfoID)
 
270
                        {
 
271
                                hdr.sortInfoID = offset;
 
272
                                sortInfoH = (MemHandle) ::MemLocalIDToGlobal (sortInfoID, cardNo);
 
273
                                if (!sortInfoH)
 
274
                                {
 
275
                                        Errors::ThrowIfPalmError (-1);
 
276
                                }
 
277
                                sortInfoSize = ::MemHandleSize (sortInfoH);
 
278
                                offset += sortInfoSize;
 
279
                        }
 
280
 
 
281
 
 
282
                        // Fill in the info on each resource into the header
 
283
                        for (i=0; i < numRecords; i++)
 
284
                        {
 
285
                                EmAliasRecordEntryType<LAS>     entry (hdr.recordList.records[i]);
 
286
 
 
287
                                err = ::DmRecordInfo (dbP, i, &attr, &uniqueID, 0);
 
288
                                if (err)
 
289
                                {
 
290
                                        Errors::ThrowIfPalmError (-1);
 
291
                                }
 
292
 
 
293
                                srcH = ::DmQueryRecord (dbP, i);
 
294
 
 
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;
 
300
 
 
301
                                if (srcH)
 
302
                                        offset += ::MemHandleSize (srcH);
 
303
                        }
 
304
 
 
305
                        // Clear out gap
 
306
                        if (kGapSize > 0)
 
307
                                memset (outP.Get() + (size - kGapSize), 0, kGapSize);
 
308
 
 
309
                        // Write out  entry table
 
310
                        fh.PutBytes (outP.Get (), size);
 
311
 
 
312
                        // Write out the appInfo followed by sortInfo, if they exist
 
313
                        if (appInfoID && appInfoSize)
 
314
                        {
 
315
                                UInt32          srcP;
 
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);
 
321
                        }
 
322
 
 
323
                        if (sortInfoID && sortInfoSize)
 
324
                        {
 
325
                                UInt32          srcP;
 
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);
 
331
                        }
 
332
 
 
333
                        // Write out each record
 
334
                        for (i=0; i<numRecords; i++)
 
335
                        {
 
336
                                UInt32          recSize;
 
337
 
 
338
                                err = ::DmRecordInfo (dbP, i, &attr, &uniqueID, 0);
 
339
                                if (err)
 
340
                                {
 
341
                                        Errors::ThrowIfPalmError (-1);
 
342
                                }
 
343
 
 
344
                                srcH = ::DmQueryRecord (dbP, i);
 
345
 
 
346
                                if (srcH)
 
347
                                {
 
348
                                        UInt32  srcP;
 
349
 
 
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);
 
356
                                }
 
357
                        }
 
358
                }
 
359
 
 
360
                // Clean up
 
361
                ::DmCloseDatabase (dbP);
 
362
        }
 
363
        catch (...)
 
364
        {
 
365
                if (dbP)
 
366
                        ::DmCloseDatabase (dbP);
 
367
 
 
368
                throw;
 
369
        }
 
370
}
 
371
 
 
372
 
 
373
// ---------------------------------------------------------------------------
 
374
//              � SavePalmFile
 
375
// ---------------------------------------------------------------------------
 
376
// Saves a Palm OS program or database file to a file.
 
377
 
 
378
void SavePalmFile (EmStreamFile& appFile, UInt16 cardNo, const char* databaseName)
 
379
{
 
380
        ::PrvMyShlExportAsPilotFile (appFile, cardNo, databaseName);
 
381
}