-
Notifications
You must be signed in to change notification settings - Fork 1
/
birthdate.c
766 lines (635 loc) · 21.5 KB
/
birthdate.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
/*
HappyDays - A Birthday displayer for the Palm
Copyright (C) 1999-2006 JaeMok Jeong
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "birthdate.h"
#include "util.h"
#include "happydays.h"
#include "happydaysRsc.h"
#include "datebook.h"
#include "lunar.h"
#include "addresscommon.h"
#define INDICATE_TOP 110
#define INDICATE_HEIGHT 6
#define INDICATE_LEFT 20
#define INDICATE_WIDTH 15
#define INDICATE_NUM 8
void PackHappyDays(HappyDays *hd, void* recordP)
{
UInt16 offset = 0;
DmWrite(recordP, offset, (Char*)&hd->addrRecordNum, sizeof(hd->addrRecordNum));
offset += sizeof(hd->addrRecordNum);
DmWrite(recordP, offset, (Char*)&hd->date, sizeof(hd->date));
offset += sizeof(hd->date);
DmWrite(recordP, offset, (Char*)&hd->flag, sizeof(hd->flag));
offset += sizeof(hd->flag);
DmWrite(recordP, offset, (Char*)&hd->nth, sizeof(hd->nth));
offset += sizeof(hd->nth); // for corealign
if (hd->name1) {
DmStrCopy(recordP, offset, (Char*)hd->name1);
offset += StrLen(hd->name1) +1;
}
else {
DmStrCopy(recordP, offset, "\0");
offset += 1;
}
if (hd->name2) {
DmStrCopy(recordP, offset, (Char*)hd->name2);
offset += StrLen(hd->name2) +1;
}
else {
DmStrCopy(recordP, offset,"\0");
offset += 1;
}
if (hd->custom) {
DmStrCopy(recordP, offset, (Char*)hd->custom);
offset += StrLen(hd->custom) +1;
}
else {
DmStrCopy(recordP, offset,"\0");
offset += 1;
}
}
void UnpackHappyDays(HappyDays *hd, const PackedHappyDays *packedHappyDays)
{
char *s = (char*)packedHappyDays;
MemMove(&hd->addrRecordNum, s, sizeof(hd->addrRecordNum));
s += sizeof(hd->addrRecordNum);
MemMove(&hd->date, s, sizeof(hd->date));
s += sizeof(hd->date);
MemMove(&hd->flag, s, sizeof(hd->flag));
s += sizeof(hd->flag);
MemMove(&hd->nth, s, sizeof(hd->nth));
s += sizeof(hd->nth);
hd->name1 = s;
s += StrLen(s) + 1;
hd->name2 = s;
s += StrLen(s) + 1;
hd->custom = s;
s += StrLen(s) + 1;
}
static Int16 HDPackedSize(HappyDays* hd)
{
Int16
size = sizeof(PackedHappyDays) - sizeof(char); // corect
if (hd->name1) size += StrLen(hd->name1) +1;
else size += 1; // '\0'
if (hd->name2) size += StrLen(hd->name2) +1;
else size += 1; // '\0'
if (hd->custom) size+= StrLen(hd->custom) +1;
else size+= 1;
return size;
}
static Int16 CompareHappyDaysFunc(LineItemPtr p1, LineItemPtr p2, Int32 extra)
{
return DateCompare(p1->date, p2->date)*extra;
}
static Int16 CompareAgeFunc(LineItemPtr p1, LineItemPtr p2, Int32 extra)
{
if (p1->age > p2->age) return 1*extra;
else if (p1->age < p2->age) return -1*extra;
else return DateCompare(p1->date, p2->date)*extra;
}
static Int16 CalculateAge(DateType converted, DateType origin,
HappyDaysFlag flag)
{
int dummy = 0;
int ret;
int lyear, lmonth, lday;
if (flag.bits.lunar_leap || flag.bits.lunar) {
// lunar birthdate
// change to lunar date
//
// converted solar is again converted into lunar day for calculation
ret = lunarS2L(lunarRefNum, converted.year + 1904, converted.month, converted.day,
&lyear, &lmonth, &lday, &dummy);
if (ret != errNone) return -1; // error
converted.year = lyear - 1904;
converted.month = lmonth;
converted.day = lday;
}
return converted.year - origin.year;
}
/**
* @brief
* @param dbP Happydays DB pointer
* @param AddrCategory The category of Address to be displayed
* @param start Start position
* @return The number of total Item
* @remarks
* @endif
*/
UInt16 AddrGetHappyDays(DmOpenRef dbP, UInt16 AddrCategory, DateType start)
{
UInt16 totalItems;
UInt16 recordNum = 0;
MemHandle recordH = 0;
LineItemPtr ptr;
PackedHappyDays* rp;
HappyDays r;
DateType converted;
UInt16 currindex = 0;
Int16 age;
// if exist, free the memory
//
if (gTableRowHandle) {
MemHandleFree(gTableRowHandle);
gTableRowHandle = 0;
}
totalItems = DmNumRecordsInCategory(dbP, AddrCategory);
if (totalItems > 0) {
gTableRowHandle = MemHandleNew(sizeof(LineItemType)* totalItems);
ErrFatalDisplayIf(!gTableRowHandle, "Out of memory");
if ((ptr = MemHandleLock(gTableRowHandle))) {
for (recordNum = 0; recordNum < totalItems; recordNum++) {
if ((recordH = DmQueryNextInCategory(dbP, &currindex,
(UInt16)AddrCategory))) {
ptr[recordNum].birthRecordNum = currindex;
rp = (PackedHappyDays *) MemHandleLock(recordH);
/*
* Build the unpacked structure for an AddressDB
* record. It is just a bunch of pointers into the rp
* structure.
*/
UnpackHappyDays(&r, rp);
// original r(in MainDB) not changed
// local change for LineItemType
//
converted = r.date;
if (r.flag.bits.nthdays) {
int syear, smonth, sday;
if (r.flag.bits.lunar || r.flag.bits.lunar_leap) {
if (lunarL2S(lunarRefNum, converted.year + firstYear,
converted.month, converted.day,
r.flag.bits.lunar_leap, &syear, &smonth, &sday) != errNone) {
MemHandleUnlock(recordH);
currindex++;
recordNum--;
totalItems--;
continue;
}
converted.day = sday;
converted.month = smonth;
converted.year = syear - firstYear;
}
DateAdjust(&converted, r.nth);
if (DateToDays(converted) < DateToDays(start)) {
MemHandleUnlock(recordH);
currindex++;
recordNum--;
totalItems--;
continue;
}
}
else if (r.flag.bits.lunar || r.flag.bits.lunar_leap) {
if (!FindNearLunar(&converted, start,
r.flag.bits.lunar_leap)) {
// ignore the records
//
converted.year = INVALID_CONV_DATE;
// max year(for sorting)
// indicate for invalid date
}
}
else if (r.flag.bits.solar) {
int maxtry = 4;
DateType dt;
dt = start;
DateToInt(dt) =
(DateToInt(dt) > DateToInt(converted))
? DateToInt(dt) : DateToInt(converted);
if (converted.month < dt.month ||
( converted.month == dt.month
&& converted.day < dt.day)) {
// birthdate is in last year?
while (DaysInMonth(converted.month, ++dt.year) < converted.day
&& maxtry-- > 0)
;
}
else {
while (DaysInMonth(converted.month, dt.year) < converted.day
&& maxtry-- >0) {
dt.year++;
}
}
if (maxtry >0) converted.year = dt.year;
else {
converted.year = INVALID_CONV_DATE;
// max year
// indicate for invalid date
}
}
if (converted.year != INVALID_CONV_DATE
&& !r.flag.bits.nthdays
&& r.flag.bits.year
&& (age = CalculateAge(converted, r.date, r.flag)) >= 0) {
// calculate age if year exists
//
ptr[recordNum].age = age;
}
else {
ptr[recordNum].age = -1;
}
// save the converted data
//
ptr[recordNum].date = converted;
MemHandleUnlock(recordH);
currindex++;
}
}
// sort the order if sort order is converted date
//
if (gPrefsR.sort == 1) { // date sort
SysInsertionSort(ptr, totalItems, sizeof(LineItemType),
(_comparF *)CompareHappyDaysFunc, 1L);
}
else if (gPrefsR.sort == 3) { // age sort
SysInsertionSort(ptr, totalItems, sizeof(LineItemType),
(_comparF *)CompareAgeFunc, 1L);
}
else if (gPrefsR.sort == 2) { // age sort(re)
SysInsertionSort(ptr, totalItems, sizeof(LineItemType),
(_comparF *)CompareAgeFunc, -1L);
}
MemPtrUnlock(ptr);
} else return 0;
}
return totalItems;
}
Boolean AnalysisHappyDays(const char* field,
HappyDaysFlag *flag,
Int16* dYear, Int16* dMonth, Int16* dDay)
{
Char * s = (Char *)field;
Char * p;
Char numOfDelimeter = 0;
Int16 num[3], numIdx = 0;
Int16 year=0, month=0, day=0;
Char delimeter = '/';
Char priorityName1 = flag->bits.priority_name1;
while (*s == ' ' || *s == '\t') s++; // skip white space
StrNCopy(gAppErrStr, s, AppErrStrLen);
s = gAppErrStr;
// initalize flag except priority_name1;
flag->allBits = 0; flag->bits.priority_name1 = priorityName1;
if ((p = StrChr(s, ')'))) { // exist
if (*s == '-') flag->bits.lunar = 1;
else if (*s == '#') flag->bits.lunar_leap = 1;
else return false;
s = p+1;
}
else flag->bits.solar = 1; // default is solar
switch (gPrefdfmts) {
case dfDMYWithDots: // 31.12.95
case dfYMDWithDots: // 95.12.31
delimeter = '.';
break;
case dfDMYWithDashes: // 31-12-95
case dfYMDWithDashes: // 95-12-31
delimeter = '-';
break;
case dfYMDWithSlashes: // 95/12/31
case dfDMYWithSlashes: // 31/12/95
case dfMDYWithSlashes: // 12/31/95
default:
delimeter = '/';
break;
}
p = s;
// find the number of delimeter
while ((p = StrChr(p, delimeter))) {
numOfDelimeter++;
p++;
}
p = s;
if (numOfDelimeter < 1 || numOfDelimeter > 2) return false;
else if (numOfDelimeter == 2) {
p = StrChr(p, delimeter); *p = 0; p++;
num[numIdx++] = StrAToI(s);
s = p;
}
p = StrChr(s, delimeter); *p = 0; p++;
num[numIdx++] = StrAToI(s);
s = p;
if (*s == 0) flag->bits.year = 0;
else {
flag->bits.year = 1;
num[numIdx] = StrAToI(s);
}
if (numOfDelimeter == 1) {
flag->bits.year = 0;
}
switch (gPrefdfmts) {
case dfDMYWithSlashes: // 31/12/95
case dfDMYWithDots: // 31.12.95
case dfDMYWithDashes: // 31-12-95
if (numOfDelimeter == 2) {
year = num[numIdx--];
}
month = num[numIdx--];
day = num[numIdx];
break;
case dfYMDWithSlashes: // 95/12/31
case dfYMDWithDots: // 95.12.31
case dfYMDWithDashes: // 95-12-31
day = num[numIdx--];
month = num[numIdx--];
if (numOfDelimeter == 2) {
year = num[numIdx];
}
break;
case dfMDYWithSlashes: // 12/31/95
default:
if (numOfDelimeter == 2) {
year = num[numIdx--];
}
day = num[numIdx--];
month = num[numIdx];
break;
}
if (month < 1 || month > 12)
return false;
if (day < 1 || day > 31)
return false;
if (flag->bits.year) {
if (year >= 0 && year <= 31) *dYear = year + 2000; // 2000
else if (year > 31 && year <= 99) *dYear = year + 1900; // 1900
else if (year >= 1900) *dYear = year;
else return false;
if (flag->bits.lunar || flag->bits.lunar_leap) {
int syear, smonth, sday;
Err err;
err = lunarL2S(lunarRefNum, *dYear, month, day, flag->bits.lunar_leap, &syear, &smonth, &sday);
if (err != errNone) {
return false;
}
}
}
*dMonth = month; *dDay = day;
return true;
}
static void UnderscoreToSpace(Char *src)
{
// change _ into ' '
//
while (*src) {
if (*src == '_') *src = ' ';
src++;
}
}
static void HDFindKey(PackedHappyDays* r, char **key, Int16* whichKey)
{
if (*whichKey == 1) { // start
if (r->name[0] == 0) {
*whichKey = 3; // end
*key = &(r->name[1]);
return;
}
else {
*whichKey = 2; // second
*key = r->name;
return;
}
}
else if (*whichKey == 2) {
*whichKey = 3;
*key = r->name + StrLen(r->name) + 1;
return;
}
else {
*key = 0;
return;
}
}
// null fields are considered less than others
//
static Int16 HDComparePackedRecords(PackedHappyDays *rec1,
PackedHappyDays *rec2,
Int16 unusedInt,
SortRecordInfoPtr unused1,
SortRecordInfoPtr unused2,
MemHandle appInfoH)
{
Int16 result;
Int16 whichKey1 = 1, whichKey2 = 1;
char *key1, *key2;
do {
HDFindKey(rec1, &key1, &whichKey1);
HDFindKey(rec2, &key2, &whichKey2);
if (!key1 || *key1 == 0) {
if (!key2 || *key2 == 0) {
result = 0;
return result;
}
else result = -1;
}
else if (!key2 || *key2 == 0) result = 1;
else {
result = StrCaselessCompare(key1, key2);
if (result == 0)
result = StrCompare(key1, key2);
}
} while (!result);
return result;
}
static UInt16 HDFindSortPosition(DmOpenRef dbP, PackedHappyDays* newRecord)
{
return DmFindSortPosition(dbP, (void*)newRecord, 0,
(DmComparF *)HDComparePackedRecords, 0);
}
Int16 HDNewRecord(DmOpenRef dbP, HappyDays *r, UInt16 *index)
{
MemHandle recordH;
PackedHappyDays* recordP;
Int16 err;
UInt16 newIndex;
// 1) and 2) (make a new chunk with the correct size)
recordH = DmNewHandle(dbP, (Int16)HDPackedSize(r));
if (recordH == NULL)
return dmErrMemError;
// 3) Copy the data from the unpacked record to the packed one.
recordP = MemHandleLock(recordH);
PackHappyDays(r, recordP);
// Get the index
newIndex = HDFindSortPosition(dbP, recordP);
MemPtrUnlock(recordP);
// 4) attach in place
err = DmAttachRecord(dbP, &newIndex, recordH, 0);
if (err)
MemHandleFree(recordH);
else *index = newIndex;
return err;
}
Int16 AnalizeOneRecord(UInt16 addrattr, Char* src,
HappyDays* hd, Boolean *ignore)
{
Char *p, *q;
UInt16 index;
Int16 err;
Int16 year, month, day;
Int16 nth[20];
Int16 count = 0;
while (*src == ' ' || *src == '\t') src++; // skip white space
// ignore record with exclamation mark
if (*src == '!') {
if (gPrefsR.ignoreexclamation) return 0;
else src++;
}
if ((q = StrChr(src, '['))) {
// [ ÀÌ ÀÖ´Â °æ¿ì durationÀ¸·Î ÆÇ´Ü
p = q;
q = StrChr(src, ']');
if (q) *q = 0;
else return 0;
do {
p++;
nth[count++] = StrAToI(p);
if (count >= 20) break;
} while ((p = StrChr(p, ',')));
}
if (*src == '*') {
// this is multiple event
hd->flag.bits.multiple_event = true;
if ((p = StrChr(src, ' '))) *p = 0;
else goto ErrHandler;
if ( *(src+1) != 0 ) { // if src have 'custom' tag
hd->custom = src+1;
UnderscoreToSpace(src+1);
}
else hd->custom = 0;
src = p+1;
while (*src == ' ' || *src == '\t') src++; // skip white space
if ((p = StrChr(src, ' '))) *p = 0;
else goto ErrHandler;
if (*src == '.') {
hd->name1 = src+1;
hd->name2 = 0;
}
else {
hd->name2 = src;
}
UnderscoreToSpace(src);
src = p+1;
while (*src == ' ' || *src == '\t') src++; // skip white space
}
if (!AnalysisHappyDays(src, &hd->flag,
&year, &month, &day)) goto ErrHandler;
// convert into date format
//
if (hd->flag.bits.year) {
if ((year < 1904) || (year > 2031) ) goto ErrHandler;
else hd->date.year = year - 1904;
}
else hd->date.year = 0;
hd->date.month = month;
hd->date.day = day;
// maintain address book order(name order)
// list order is determined by sort
err = HDNewRecord(MainDB, hd, &index);
if (!err) {
UInt16 attr;
// set the category of the new record to the category
// it belongs in
DmRecordInfo(MainDB, index, &attr, NULL, NULL);
attr &= ~dmRecAttrCategoryMask;
attr |= addrattr;
DmSetRecordInfo(MainDB, index, &attr, NULL);
DmReleaseRecord(MainDB, index, true);
}
if (count > 0) {
Int16 i;
HappyDays hdr;
for (i = 0; i < count; i++) {
MemMove(&hdr, hd, sizeof(hdr));
hdr.flag.bits.nthdays = 1;
hdr.nth = nth[i];
if (!hdr.flag.bits.year) goto ErrHandler;
err = HDNewRecord(MainDB, &hdr, &index);
if (!err) {
UInt16 attr;
// set the category of the new record to the category
// it belongs in
DmRecordInfo(MainDB, index, &attr, NULL, NULL);
attr &= ~dmRecAttrCategoryMask;
attr |= addrattr;
DmSetRecordInfo(MainDB, index, &attr, NULL);
DmReleaseRecord(MainDB, index, true);
}
}
}
return 0;
ErrHandler:
if (*ignore) return 0;
switch (FrmCustomAlert(InvalidFormat,
((!hd->name2) ?
((!hd->name1) ? " " : hd->name1)
: hd->name2),
" ", " ")) {
case 0: // EDIT
if (!GotoAddress(hd->addrRecordNum)) return -1;
case 2: // Ignore all
*ignore = true;
case 1:
break; // Ignore
}
return 0;
}
Boolean IsChangedAddressDB()
{
return !((MemCmp((char*) &gAdcdate, gPrefsR.adrcdate, 4) == 0) &&
(MemCmp((char*) &gAdmdate, gPrefsR.adrmdate, 4) == 0));
}
void SetReadAddressDB()
{
MemMove(gPrefsR.adrcdate, (char *)&gAdcdate, 4);
MemMove(gPrefsR.adrmdate, (char *)&gAdmdate, 4);
}
void initIndicate()
{
RectangleType rect;
CustomPatternType pattern;
pattern[0] = pattern[2] = pattern[4] = pattern[6] = 0xaa;
pattern[1] = pattern[3] = pattern[5] = pattern[7] = 0x55;
WinSetPattern( (const CustomPatternType*)&pattern );
rect.topLeft.x = INDICATE_LEFT;
rect.topLeft.y = INDICATE_TOP;
rect.extent.x = INDICATE_WIDTH * (INDICATE_NUM);
rect.extent.y = INDICATE_HEIGHT;
if (gbVgaExists) {
rect.topLeft.x += rect.topLeft.x / 2;
rect.topLeft.y += rect.topLeft.y / 2;
rect.extent.x += rect.extent.x / 2 + 1;
rect.extent.y += rect.extent.y / 2;
}
WinFillRectangle( &rect, 0 );
return;
}
void displayNextIndicate( int index )
{
FormPtr form;
RectangleType rect;
form = FrmGetActiveForm();
rect.topLeft.x = INDICATE_LEFT + INDICATE_WIDTH * (index-1);
rect.topLeft.y = INDICATE_TOP;
rect.extent.x = INDICATE_WIDTH;
rect.extent.y = INDICATE_HEIGHT;
if (gbVgaExists) {
rect.topLeft.x += rect.topLeft.x / 2;
rect.topLeft.y += rect.topLeft.y / 2;
rect.extent.x += rect.extent.x / 2 + 1;
rect.extent.y += rect.extent.y / 2;
}
WinDrawRectangle( &rect, 0 );
return;
}