-
Notifications
You must be signed in to change notification settings - Fork 0
/
soarDB.c
1681 lines (1490 loc) · 52.9 KB
/
soarDB.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <PalmOS.h>
#include "soaring.h"
#include "soarDB.h"
#include "soarUtil.h"
#include "soarIO.h"
#include "soarForm.h"
#include "soarPLst.h"
#include "soarTask.h"
#include "soarSUA.h"
#include "soarMem.h"
#include "soarCAI.h"
#include "soarWay.h"
#include "soarWind.h"
#include "soarComp.h"
#include "soarEW.h"
#include "soarMap.h"
#include "soarRECO.h"
DmOpenRef config_db;
DmOpenRef logger_db;
DmOpenRef flight_db;
DmOpenRef waypoint_db;
DmOpenRef polar_db;
DmOpenRef task_db;
DmOpenRef suaidx_db;
DmOpenRef suadata_db;
DmOpenRef terrain_db;
DmOpenRef memo_db;
DmOpenRef doc_db;
DmOpenRef sim_db;
extern PolarData *inusePolar;
extern Int16 selectedPolarIndex;
extern PolarData *selectedPolar;
extern RECOData *recodata;
extern EWMRData *ewmrdata;
extern Char *filelist;
extern UInt16 numfilesfound;
Char NameString[26];
/*****************************************************************************
* OpenCreateDB - open a database. If it doesn't exist, create it.
*****************************************************************************/
Int16 OpenCreateDB(DmOpenRef *dbP,
UInt16 cardNo,
Char* name,
UInt32 dbcreator,
UInt32 type,
UInt16 mode)
{
if ((*dbP=DmOpenDatabaseByTypeCreator(type,dbcreator,mode)) == 0) {
DmCreateDatabase(cardNo,name,dbcreator,type,false);
if ((*dbP=DmOpenDatabaseByTypeCreator(type,dbcreator,mode)) == 0) {
return (DB_OPEN_FAIL);
} else {
return (DB_OPEN_CREATE);
}
}
return (DB_OPEN_SUCCESS);
}
/*****************************************************************************
* OpenCreateDB2 - open a database and get DB version number.
*****************************************************************************/
Int16 OpenCreateDB2(DmOpenRef *dbP,
UInt16 cardNo,
Char *name,
UInt32 dbcreator,
UInt32 type,
UInt16 mode,
UInt16 curver)
{
LocalID dbID;
UInt16 dbver;
MemHandle aih;
LocalID ai;
// HostTraceOutputTL(appErrorClass, "soarDB:About to call DmFindDatabase-name=|%s|", name);
if ((dbID = DmFindDatabase(cardNo, name)) == 0) {
// If the database isn't found - Create it.
DmCreateDatabase(cardNo, name, dbcreator, type, false);
// Then find the new database to get the dbID
if ((dbID = DmFindDatabase(cardNo, name)) == 0) {
return (DB_OPEN_FAIL);
} else {
// Then open the new database with the dbID
if ((*dbP=DmOpenDatabase(cardNo, dbID, mode)) == 0) {
return (DB_OPEN_FAIL);
} else {
// If the newly created database is opened - Set the DB Info
DmOpenDatabaseInfo(*dbP, &dbID, NULL, NULL, NULL, NULL);
DmDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &ai, NULL, NULL, NULL);
if (ai == 0) {
aih = DmNewHandle(*dbP, 64);
ai = MemHandleToLocalID(aih);
DmSetDatabaseInfo(cardNo,dbID,NULL,NULL,&curver,NULL,NULL,NULL,NULL,&ai,NULL,NULL,NULL);
// HostTraceOutputTL(appErrorClass, "soarDB:%s-curver create |%hu|", name, curver);
}
return (DB_OPEN_CREATE);
}
}
} else {
// If the database is found - Open it.
*dbP = DmOpenDatabase(cardNo, dbID, mode);
// Get the DB Info
DmDatabaseInfo(cardNo,dbID,NULL,NULL,&dbver,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
// HostTraceOutputTL(appErrorClass, "soarDB:%s-curver|%hu| dbver|%hu|", name, curver, dbver);
if (curver != dbver) {
// If it is a different version of the database, clear it and start over
FrmCustomAlert(WarningAlert, name, "Database Reset", " ");
// Close the current database
DmCloseDatabase(*dbP);
// Delete the database
DmDeleteDatabase(cardNo, dbID);
// Create a new version of it
DmCreateDatabase(cardNo, name, dbcreator, type, false);
// Then find the new database to get the dbID
if ((dbID = DmFindDatabase(cardNo, name)) == 0) {
return (DB_OPEN_FAIL);
} else {
// Then open the new database with the dbID
if ((*dbP=DmOpenDatabase(cardNo, dbID, mode)) == 0) {
return (DB_OPEN_FAIL);
} else {
// If the newly created database is opened - Set the DB Info
DmOpenDatabaseInfo(*dbP, &dbID, NULL, NULL, NULL, NULL);
DmDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &ai, NULL, NULL, NULL);
if (ai == 0) {
aih = DmNewHandle(*dbP, 64);
ai = MemHandleToLocalID(aih);
DmSetDatabaseInfo(cardNo,dbID,NULL,NULL,&curver,NULL,NULL,NULL,NULL,&ai,NULL,NULL,NULL);
// HostTraceOutputTL(appErrorClass, "soarDB:%s-curver create |%hu|", name, curver);
}
return (DB_OPEN_CREATE);
}
}
}
return (DB_OPEN_SUCCESS);
}
}
/*****************************************************************************
* OpenDeleteDB - delete an open.
*****************************************************************************/
Boolean OpenDeleteDB(DmOpenRef *dbP, UInt16 cardNo, Char *name)
{
LocalID dbID;
if ((dbID = DmFindDatabase(cardNo, name)) != 0) {
// Close the current database
DmCloseDatabase(*dbP);
// Delete the database
DmDeleteDatabase(cardNo, dbID);
return (true);
} else {
return (false);
}
}
/*****************************************************************************
* OpenDBCountRecords
*****************************************************************************/
UInt16 OpenDBCountRecords(DmOpenRef openDBDB)
{
if (openDBDB == 0)
return(NO_RECORD);
else
return(DmNumRecords(openDBDB));
}
/*****************************************************************************
* OpenDBAddRecord - add a record in the open db at the start or
* end of the db (according to atStart)
*****************************************************************************/
UInt16 OpenDBAddRecord(DmOpenRef openDBDB,
Int32 size,
MemPtr openDBP,
Boolean atStart)
{
MemHandle recH;
UInt8 *recP;
Err err;
UInt16 dbIndex = 0;
if ((dbIndex = OpenDBNewRecord(openDBDB, size, &recH, (MemPtr)&recP, atStart))
== NO_RECORD)
return NO_RECORD;
// write the data
err = DmWrite(recP, 0, openDBP, size);
// release record
OpenDBReleaseRecord(openDBDB, dbIndex, &recH, (MemPtr)&recP, !err);
// delete on failure
if (err) {
OpenDBDeleteRecord(openDBDB, dbIndex);
return(NO_RECORD);
} else {
// HostTraceOutputTL(appErrorClass, "soarDB:dbIndex:|%hu|", dbIndex);
return(dbIndex);
}
}
/*****************************************************************************
* OpenDBNewRecord - create a new record in the open db at the start or
* end of the db (according to atStart)
*****************************************************************************/
UInt16 OpenDBNewRecord(DmOpenRef openDBDB,
Int32 size,
MemHandle *openDBH,
MemPtr *openDBP,
Boolean atStart)
{
UInt16 dbIndex = 0;
// check for an open db
if (openDBDB == 0)
return false;
if (atStart)
dbIndex = 0;
else
dbIndex = DmNumRecords(openDBDB);
*openDBP = NULL;
// create and write a new record
if ((*openDBH = DmNewRecord(openDBDB, &dbIndex, size)) == NULL ||
(*openDBP = MemHandleLock(*openDBH)) == NULL) {
// couldn't create properly, back out
OpenDBReleaseRecord(openDBDB, dbIndex, openDBH, openDBP, false);
return NO_RECORD;
}
return dbIndex;
}
/*****************************************************************************
* OpenDBGetRecord - get a record from the OpenDB
*****************************************************************************/
Boolean OpenDBGetRecord(DmOpenRef openDBDB,
Int16 index,
MemHandle *openDBH,
MemPtr *openDBP)
{
// init the ptr up front
*openDBP = NULL;
// check for an open db
if (openDBDB == 0)
return false;
// check for bad index
if (index >= DmNumRecords(openDBDB))
return false;
// get the record
if ((*openDBH = DmGetRecord(openDBDB, index)) == NULL ||
(*openDBP = MemHandleLock(*openDBH)) == NULL) {
// couldn't get properly, back out
OpenDBReleaseRecord(openDBDB, index, openDBH, openDBP, false);
return false;
}
return true;
}
/*****************************************************************************
* OpenDBQueryRecord - get a read-only record from the OpenDB
*****************************************************************************/
Boolean OpenDBQueryRecord(DmOpenRef openDBDB,
Int16 index,
MemHandle *openDBH,
MemPtr *openDBP)
{
// init the ptr up front
*openDBP = NULL;
// check for an open db
if (openDBDB == 0)
return(false);
// check for bad index
if (index >= DmNumRecords(openDBDB))
return(false);
// get the record
if ((*openDBH = DmQueryRecord(openDBDB, index)) == NULL ||
(*openDBP = MemHandleLock(*openDBH)) == NULL) {
// couldn't get properly, back out
OpenDBReleaseRecord(openDBDB, index, openDBH, openDBP, false);
return(false);
}
return(true);
}
/*****************************************************************************
* OpenDBReleaseRecord - release a record from the openDB db.
*****************************************************************************/
Boolean OpenDBReleaseRecord(DmOpenRef openDBDB,
Int16 index,
MemHandle *openDBH,
MemPtr *openDBP,
Boolean save)
{
// check for an open db
if (openDBDB == 0)
return false;
// check for bad index
if (index >= DmNumRecords(openDBDB))
return false;
// unlock first
if (*openDBP)
MemHandleUnlock(*openDBH);
// release the record and close the database
DmReleaseRecord(openDBDB, index, save);
// clean up
*openDBP = NULL;
*openDBH = NULL;
return true;
}
/*****************************************************************************
* OpenDBDeleteRecord - delete a record from the openDB db.
*****************************************************************************/
Boolean OpenDBDeleteRecord(DmOpenRef openDBDB, Int16 index)
{
// check for an open db
if (openDBDB == 0)
return false;
// check for bad index and delete
if (index >= DmNumRecords(openDBDB) ||
DmRemoveRecord(openDBDB, index) != 0)
return false;
return true;
}
/*****************************************************************************
* OpenDBEmpty - delete the records from the openDB db.
*****************************************************************************/
Boolean OpenDBEmpty(DmOpenRef openDBDB)
{
Int16 index;
Boolean result = true;
// check for an open db
if (openDBDB == 0)
return false;
// delete backwards
for (index = DmNumRecords(openDBDB) - 1; index > -1 && result; index--)
if (DmRemoveRecord(openDBDB, index) != 0)
result = false;
return result;
}
/*****************************************************************************
* OpenDBSetBackup
* set the bit in a database so the database will or will not be backed up
* by the default backup conduit
* Parameters:
* DmOpenRef dbRef - reference to the OPEN database
* Boolean setForBackup - set for backup or not
*****************************************************************************/
void OpenDBSetBackup(DmOpenRef openDBRef,
Boolean setForBackup)
{
UInt16 cardNo, dbAttrs;
LocalID dbID;
// get the dbID then get the attributes
DmOpenDatabaseInfo(openDBRef, &dbID, NULL, NULL, &cardNo, NULL);
DmDatabaseInfo(cardNo, dbID, NULL, &dbAttrs,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
// set the attribute
if (setForBackup)
// back up the database
dbAttrs |= dmHdrAttrBackup;
else
// DON'T backup the database
dbAttrs &= ~dmHdrAttrBackup;
// save the attributes
DmSetDatabaseInfo(cardNo, dbID, NULL, &dbAttrs,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}
/*****************************************************************************
* OpenDBUpdateRecord - update a record. NULL records are written out
* as empty
* Return: false if dbIndex out of range or write fails, true otherwise
*****************************************************************************/
Boolean OpenDBUpdateRecord(DmOpenRef openDBDB,
Int32 size,
MemPtr openDBP,
UInt16 dbIndex)
{
MemHandle dbH = NULL;
MemPtr dbP = NULL;
Boolean result = true;
// fetch and write the record
if (dbIndex < DmNumRecords(openDBDB) &&
(dbH = DmGetRecord(openDBDB, dbIndex)) != NULL &&
(dbP = MemHandleLock(dbH)) != NULL) {
if (openDBP) {
// DebugWriteCheck(dbP, 0, size);
DmWrite(dbP, 0, openDBP, size);
} else {
// NULL data, so write 0s
DmSet(dbP, 0, size, 0);
}
} else {
result = false;
}
// clean up
if (dbP)
MemHandleUnlock(dbH);
if (dbH)
DmReleaseRecord(openDBDB, dbIndex, result);
return(result);
}
/*****************************************************************************
* OpenDBUpdateAppInfo - update the App Info data for a database
* Return: false - If AppInfo Chuck not found otherwise true
*****************************************************************************/
Boolean OpenDBUpdateAppInfo(DmOpenRef openDBDB,
UInt16 cardNo,
double appInfoData)
{
LocalID ai = DmGetAppInfoID(openDBDB);
double *dblptr;
Boolean result=true;
if (ai) {
dblptr = MemLocalIDToLockedPtr(ai, CARD_NO);
DmSet(dblptr, 0, 64, 0);
DmWrite(dblptr, 0, &appInfoData, sizeof(double));
MemPtrUnlock(dblptr);
} else {
result = false;
}
return(result);
}
/*****************************************************************************
* OpenDBGetAppInfo - update the App Info data for a database
* Return: false - If AppInfo Chuck not found otherwise true
*****************************************************************************/
Boolean OpenDBGetAppInfo(DmOpenRef openDBDB,
UInt16 cardNo,
double *appInfoData)
{
LocalID ai = DmGetAppInfoID(openDBDB);
double *dblptr;
Boolean result=true;;
if (ai) {
dblptr = MemLocalIDToLockedPtr(ai, CARD_NO);
*appInfoData = *dblptr;
MemPtrUnlock(dblptr);
} else {
result = false;
}
return(result);
}
/************************************************************
*
* FUNCTION: MemoAppInfoInit
*
* DESCRIPTION: Create an app info chunk if missing. Set
* the strings to a default.
*
* PARAMETERS: database pointer
*
* RETURNS: 0 if successful, errorcode if not
*
*************************************************************/
Err MemoAppInfoInit(DmOpenRef dbP)
{
UInt16 cardNo;
MemHandle h;
LocalID dbID;
LocalID appInfoID;
MemoAppInfoPtr nilP = 0;
MemoAppInfoPtr appInfoP;
if (DmOpenDatabaseInfo(dbP, &dbID, NULL, NULL, &cardNo, NULL))
return dmErrInvalidParam;
if (DmDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, &appInfoID, NULL, NULL, NULL))
return dmErrInvalidParam;
if (appInfoID == 0)
{
h = DmNewHandle (dbP, sizeof (MemoAppInfoType));
if (! h) return dmErrMemError;
appInfoID = MemHandleToLocalID (h);
DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &appInfoID, NULL, NULL, NULL);
}
appInfoP = MemLocalIDToLockedPtr(appInfoID, cardNo);
// Clear the app info block.
DmSet (appInfoP, 0, sizeof(MemoAppInfoType), 0);
// Initialize the categories.
CategoryInitialize ((AppInfoPtr) appInfoP, LocalizedAppInfoStr);
// Initialize the sort order.
DmSet (appInfoP, (UInt32)&nilP->sortOrder, sizeof(appInfoP->sortOrder),
soAlphabetic);
MemPtrUnlock(appInfoP);
return 0;
}
/***********************************************************************
*
* FUNCTION: MemoGetDatabase
*
* DESCRIPTION: Get the application's database. Open the database if it
* exists, create it if neccessary.
*
* PARAMETERS: *dbPP - pointer to a database ref (DmOpenRef) to be set
* mode - how to open the database (dmModeReadWrite)
*
* RETURNED: Err - zero if no error, else the error
*
***********************************************************************/
Err MemoGetDatabase (DmOpenRef *dbPP, UInt16 mode)
{
Err error = 0;
DmOpenRef dbP;
UInt16 cardNo;
LocalID dbID;
*dbPP = NULL;
// Find the application's data file. If it doesn't exist create it.
dbP = DmOpenDatabaseByTypeCreator (memoDBType, sysFileCMemo, mode);
if (!dbP) {
error = DmCreateDatabase (0, memoDBName, sysFileCMemo, memoDBType, false);
if (error)
return error;
dbP = DmOpenDatabaseByTypeCreator(memoDBType, sysFileCMemo, mode);
if (!dbP)
return (1);
// Set the backup bit. This is to aid syncs with non Palm software.
OpenDBSetBackup(dbP, true);
error = MemoAppInfoInit (dbP);
if (error) {
DmOpenDatabaseInfo(dbP, &dbID, NULL, NULL, &cardNo, NULL);
DmCloseDatabase(dbP);
DmDeleteDatabase(cardNo, dbID);
return error;
}
}
*dbPP = dbP;
return 0;
}
Boolean HandleMemoData(Int8 action, Char *data, UInt32 *numBytes)
{
static Char *memodata=NULL;
static MemHandle memohand=NULL;
MemHandle output_hand;
MemPtr output_ptr;
Int16 x = 0;
static Int16 nrecs;
Boolean found=false;
static UInt16 recidx=0;
Boolean retval = true;
static UInt32 curReadChar=0;
static UInt16 datalength=0;
UInt32 endReadChar=0;
static Char memofilename[26];
static UInt16 numfiles=0;
Boolean exitloop=false;
switch (action) {
case IOINIT:
curReadChar = 0;
datalength = 0;
memohand = MemHandleNew(memoMaxLength);
memodata = MemHandleLock(memohand);
MemSet(memodata,sizeof(memodata),0);
// Opens the Memo Database
// HostTraceOutputTL(appErrorClass, "soarDB:Opening MemoDB");
if (MemoGetDatabase(&memo_db, DB_RW_MODE) == 0) {
retval = true;
} else {
retval = false;
}
nrecs = DmNumRecords(memo_db);
// HostTraceOutputTL(appErrorClass, "soarDB:nrecs|%hd", nrecs);
while((x < nrecs) && (exitloop == false)) {
// HostTraceOutputTL(appErrorClass, "soarDB:Getting Existing Record-x=|%hd|", x);
if (OpenDBQueryRecord(memo_db, x, &output_hand, &output_ptr)) {
// HostTraceOutputTL(appErrorClass, "soarDB:Calling MemMove");
MemMove(memodata, output_ptr, memoMaxLength);
// HostTraceOutputTL(appErrorClass, "soarDB:Calling MemHandleUnlock");
MemHandleUnlock(output_hand);
// HostTraceOutputTL(appErrorClass, "soarDB:Calling StrLen");
if (StrLen(memodata) > 0) {
StrNCopy(NameString, memodata, StrLen(data)+1);
NameString[StrLen(data)+1] = '\0';
// check filename for match
// HostTraceOutputTL(appErrorClass, "soarDB:Filename Required %s",NameString);
// HostTraceOutputTL(appErrorClass, "soarDB:Filename to Check %s",data);
if (StrNCompare(trim(NameString, '*', true), data, StrLen(data)) == 0) {
// HostTraceOutputTL(appErrorClass, "soarDB:Found it %s",NameString);
recidx = x;
if (*numBytes == IOOPENTRUNC) {
// delete a record if writing new data
OpenDBDeleteRecord(memo_db, recidx);
x--;
nrecs--;
} else {
// stop with first file found if reading data
exitloop = true;
}
found = true;
}
}
} else {
// HostTraceOutputTL(appErrorClass, "soarDB:Didn't find it %s",NameString);
found = false;
}
x++;
}
if (*numBytes == IOOPENTRUNC) {
//make new file using data as the base filename
StrCopy(memodata, "*");
StrCat(memodata, data);
// save file name and zero files counter
StrCopy(memofilename, memodata);
StrCat(memodata, "\r\n");
numfiles = 0;
// HostTraceOutputTL(appErrorClass, "soarDB:Adding New Record w/filename:|%s|", memodata);
recidx = OpenDBAddRecord(memo_db, memoMaxLength, memodata, ATEND);
// HostTraceOutputTL(appErrorClass, "soarDB:Added recidx:|%hu|", recidx);
retval = true;
} else if (!found && *numBytes == IOOPEN) {
retval = false;
}
datalength = StrLen(memodata);
break;
case IOWRITE:
// Acumulates text to copy into a MemoPad file
if ((StrLen(memodata)+StrLen(data)) < memoMaxLength) {
if (StrLen(memodata) == 0) {
StrCopy(memodata, data);
} else {
StrCat(memodata, data);
}
} else {
// If more data has been acumulated than will fit in a Notepad
// add the MemoPad entry and then start on a new one
// HostTraceOutputTL(appErrorClass, "soarDB:Adding New record-2");
recidx = OpenDBAddRecord(memo_db, memoMaxLength, memodata, ATEND);
MemSet(memodata,sizeof(memodata),0);
// put filename and sequence number
numfiles++;
StrCopy(memodata, memofilename);
StrCat(memodata, leftpad(DblToStr(pround(numfiles,0),0), '0', 2));
StrCat(memodata, "\r\n");
// Copy leftover data into memodata for the next MemoPad file
StrCat(memodata, data);
}
// HostTraceOutputTL(appErrorClass, "soarDB:Updating memo data-memodata|%s|", memodata);
OpenDBUpdateRecord(memo_db, memoMaxLength, memodata, recidx);
retval = true;
break;
case IOREAD:
// Check to see if at end of memo
if (curReadChar == datalength) {
// At end of memo
// HostTraceOutputTL(appErrorClass, "At EOF");
// look for more files in the memo database with the same name
exitloop = false;
x = recidx + 1;
while((x < nrecs) && (exitloop == false)) {
// HostTraceOutputTL(appErrorClass, "soarDB:Getting Existing Record-x=|%hd|", x);
if (OpenDBQueryRecord(memo_db, x, &output_hand, &output_ptr)) {
// HostTraceOutputTL(appErrorClass, "soarDB:Calling MemMove");
MemMove(memodata, output_ptr, memoMaxLength);
// HostTraceOutputTL(appErrorClass, "soarDB:Calling MemHandleUnlock");
MemHandleUnlock(output_hand);
// HostTraceOutputTL(appErrorClass, "soarDB:Filename Required %s",NameString);
if (StrLen(memodata) > 0) {
//StrNCopy(NameString, memodata, StrLen(data)+1);
//NameString[StrLen(data)+1] = '\0';
StrNCopy(data, memodata, StrLen(NameString)+1);
trim(data, '*', true);
// HostTraceOutputTL(appErrorClass, "soarDB:Filename to Check %s",data);
// check filename for match
if (StrNCompare(trim(NameString, '*', true), data, StrLen(data)) == 0) {
// HostTraceOutputTL(appErrorClass, "soarDB:Found it %s",NameString);
recidx = x;
// stop with first file found because reading data
exitloop = true;
found = true;
}
}
} else {
// HostTraceOutputTL(appErrorClass, "soarDB:Didn't find it %s",NameString);
found = false;
}
x++;
}
if (found == 0 ) {
// exit if no more files found
return(false);
} else {
// set up to read next file
curReadChar = 0;
datalength = StrLen(memodata);
}
}
// Calculate new stopping point based on the number of bytes requested
endReadChar = curReadChar + *numBytes;
// Check to see if there are fewer characters left to read
// than have been requested.
if (endReadChar > datalength) {
// If there are fewer, change end point and number of bytes read
endReadChar = datalength;
*numBytes = datalength - curReadChar;
}
// Copy the correct number of characters into the data pointer
// passed in
StrNCopy(data, &memodata[curReadChar], *numBytes);
// HostTraceOutputTL(appErrorClass, "data: |%s|", data);
// Move the current read point to the new location
// HostTraceOutputTL(appErrorClass, "Moving current read pointer");
curReadChar += *numBytes;
if (curReadChar == datalength && data[*numBytes-1] != '\n') {
data[*numBytes] = '\n';
(*numBytes)++;
// curReadChar++;
}
data[*numBytes] = '\0';
// HostTraceOutputTL(appErrorClass, "Returning true");
retval = true;
break;
case IOCLOSE:
// HostTraceOutputTL(appErrorClass, "soarDB:Closing memo database");
DmCloseDatabase(memo_db);
MemHandleUnlock(memohand);
MemHandleFree(memohand);
retval = true;
break;
}
return(retval);
}
Boolean HandleDOCData(Int8 action, Char *data, UInt32 *numBytes)
{
static Char *docdata=NULL;
static MemHandle dochand=NULL;
MemHandle output_hand;
MemPtr output_ptr;
UInt16 nrecs=0;
Boolean found=false;
static UInt16 recidx=0;
Boolean retval = true;
static UInt32 curReadChar=0;
static UInt16 datalength=0;
UInt32 endReadChar=0;
Int16 dberr;
static palmdoc_record0 pdocrec0;
static Boolean firsttime=true;
static UInt16 curDocRec=0;
UInt32 leninc, lenover;
DmSearchStateType state;
Boolean latestVer = false;
UInt16 card;
LocalID currentDB;
Char nameP[FILENAMESIZE];
Char *prev_filelist = NULL;
Int16 prev_numfilesfound = 0;
Char extension[5];
switch (action) {
case IODELETE:
// delete a file
if ((currentDB = DmFindDatabase(CARD_NO, data)) != 0) {
DmDeleteDatabase(CARD_NO, currentDB);
return(true);
} else {
return(false);
}
case IOEXIST:
// test if a filename exists
if ((currentDB = DmFindDatabase(CARD_NO, data)) == 0) {
return(true);
} else {
return(false);
}
case IODIR:
// list the DOC databases on the Palm that have the required extension (data)
numfilesfound = 0;
dberr = DmGetNextDatabaseByTypeCreator(true, &state, docDBType, docCreator, latestVer, &card, ¤tDB);
while (!dberr && currentDB) {
// do something with currentDB
dberr = DmDatabaseInfo(card, currentDB, nameP, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
// HostTraceOutputTL(appErrorClass, "File =|%s|", nameP);
// check file extension (ignore case)
StrToLower(extension , Right(nameP, 4));
if (StrCompare(extension, data) == 0) {
// HostTraceOutputTL(appErrorClass, "Add File to List");
// increment file count
numfilesfound++;
// add item to the list
if (prev_numfilesfound > 0) {
AllocMem((void *)&prev_filelist, prev_numfilesfound * FILENAMESIZE);
MemMove(prev_filelist, filelist, prev_numfilesfound * FILENAMESIZE);
}
if (filelist) FreeMem((void *)&filelist);
AllocMem((void *)&filelist, numfilesfound * FILENAMESIZE);
if ((prev_numfilesfound > 0) && prev_filelist) MemMove(filelist, prev_filelist, prev_numfilesfound * FILENAMESIZE);
// filename without extension
StrCopy(&filelist[(numfilesfound-1)*FILENAMESIZE], Left(nameP, StrLen(nameP)-4));
if ((prev_numfilesfound > 0) && prev_filelist) FreeMem((void *)&prev_filelist);
prev_numfilesfound = numfilesfound;
}
// now get the next DB
dberr = DmGetNextDatabaseByTypeCreator(false, &state, docDBType, docCreator, latestVer, &card, ¤tDB);
}
quicksort(filelist, numfilesfound, FILENAMESIZE, 1);
*numBytes = numfilesfound;
break;
case IOINIT:
if (StrLen(data) < 5) {
// exit if filename too short
retval = false;
break;
}
dochand = MemHandleNew(docrecMaxLength+1);
docdata = MemHandleLock(dochand);
MemSet(docdata, docrecMaxLength+1, '\0');
doc_db = 0;
// Opens the DOC Database
// HostTraceOutputTL(appErrorClass, "soarDB:Opening DOC Database:|%s|", data);
dberr=OpenCreateDB2(&doc_db, CARD_NO, data, docCreator, docDBType, DB_RW_MODE, DOCDBVER);
retval = true;
found = true;
if (dberr == DB_OPEN_SUCCESS) {
// HostTraceOutputTL(appErrorClass, "soarDB:Found and Opened DOC File");
}
if (dberr == DB_OPEN_FAIL) {
ErrDisplay("Could Not Create DOC File");
retval = false;
found = false;
}
if (dberr==DB_OPEN_CREATE) {
OpenDBSetBackup(doc_db, true);
// HostTraceOutputTL(appErrorClass, "soarDB:New DOC Database-Adding Record0");
MemSet(&pdocrec0, sizeof(pdocrec0), 0);
pdocrec0.version = 1;
pdocrec0.doc_size = 0;
pdocrec0.num_recs = 0;
pdocrec0.rec_size = docrecMaxLength;
OpenDBAddRecord(doc_db, sizeof(pdocrec0), &pdocrec0, ATSTART);
found = true;
// Even though it was created, it wasn't initially found
if (*numBytes==IOOPEN) {
found = false;
retval = false;
// Delete the newly created database because it wasn't needed for an
// IOOPEN call
OpenDeleteDB(&doc_db, CARD_NO, data);
doc_db = NULL;
}
}
//Poplulate pdocrec0 structure
// HostTraceOutputTL(appErrorClass, "soarDB:Getting Record0 Data");
MemSet(&pdocrec0, sizeof(pdocrec0), 0);
if (found && OpenDBQueryRecord(doc_db, 0, &output_hand, &output_ptr)) {
// HostTraceOutputTL(appErrorClass, "soarDB:Found Record0 Moving the Data");
MemMove(&pdocrec0, output_ptr, sizeof(pdocrec0));
MemHandleUnlock(output_hand);
if (pdocrec0.version != 1) {
FrmCustomAlert(WarningAlert, "Compressed DOC Files Not Supported"," "," ");
found = false;
retval = false;
}
} else {
// HostTraceOutputTL(appErrorClass, "soarDB:Didn't find record 0");
found = false;
retval = false;
}
if (found && *numBytes == IOOPENTRUNC) {
nrecs = pdocrec0.num_recs;
// HostTraceOutputTL(appErrorClass, "soarDB:nrecs|%hu|", nrecs);
if (nrecs > 0) {
// HostTraceOutputTL(appErrorClass, "soarDB:Deleting Existing Records");
// Clear the current doc file
OpenDBEmpty(doc_db);
// Put a new record0 into the doc file
MemSet(&pdocrec0,sizeof(pdocrec0),0);
pdocrec0.version = 1;
pdocrec0.doc_size = 0;
pdocrec0.num_recs = 0;
pdocrec0.rec_size = docrecMaxLength;
OpenDBAddRecord(doc_db, sizeof(pdocrec0), &pdocrec0, ATSTART);
}
// Add first record empty
recidx = OpenDBAddRecord(doc_db, StrLen(docdata), docdata, false);
// HostTraceOutputTL(appErrorClass, "soarDB:Added new empty record at recidx:|%hu|", recidx);
// Update the record0 info to account for the new data
pdocrec0.num_recs = 1;
// HostTraceOutputTL(appErrorClass, "soarDB:Updating Record0");
OpenDBUpdateRecord(doc_db, sizeof(pdocrec0), &pdocrec0, 0);
retval = true;
} else if (!found && *numBytes == IOOPEN) {
retval = false;
}
break;
case IOWRITE: