/** * @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; }
void OpenTransliterations( void ) { DmSearchStateType stateInfo; Err err; UInt16 i; UInt16 cardNo; LocalID dbID; Char temp[ 100 ]; XlitEntry* entry; if ( alreadyOpened ) return; alreadyOpened = true; numXlits = 0; xlitDataList = ListCreate(); err = DmGetNextDatabaseByTypeCreator( true, &stateInfo, (UInt32) XlitDBType, (UInt32) ViewerAppID, false, &cardNo, &dbID ); while ( err == errNone ) { ProcessXlitDB( cardNo, dbID ); err = DmGetNextDatabaseByTypeCreator( false, &stateInfo, (UInt32) SkinResourceType, (UInt32) ViewerAppID, false, &cardNo, &dbID ); } if ( 0 < numXlits ) { xlitData = SafeMemPtrNew( sizeof( XlitEntry ) * numXlits ); i = 0; entry = ListFirst( xlitDataList ); while ( entry != NULL ) { xlitData[ i++ ] = *entry; SafeMemPtrFree( entry ); entry = ListNext( xlitDataList, entry ); } SysInsertionSort( xlitData, numXlits, sizeof( XlitEntry ), XlitCompare, 0 ); } ListDelete( xlitDataList ); xlitNames = SafeMemPtrNew( ( numXlits + 1 ) * sizeof( Char* ) ); SysCopyStringResource( temp, strNoTransliteration ); xlitNames[ 0 ] = SafeMemPtrNew( 1 + StrLen( temp ) ); StrCopy( xlitNames[ 0 ], temp ); for ( i = 1 ; i < numXlits + 1 ; i++ ) { Char* name; name = xlitData[ i - 1 ].header->name; xlitNames[ i ] = SafeMemPtrNew( 1 + StrLen( name ) ); StrCopy( xlitNames[ i ], name ); } }