/************************************************************ * * FUNCTION: ApptNewRecord * * DESCRIPTION: Create a new packed record in sorted position * * PARAMETERS: database pointer * database record * * RETURNS: ##0 if successful, error code if not * * CREATED: 1/25/95 * * BY: Roger Flores * *************************************************************/ Err ApptNewRecord(DmOpenRef dbP, ApptDBRecordPtr r, UInt16 *index) { MemHandle recordH; ApptPackedDBRecordPtr recordP; UInt16 newIndex; Err err; // Make a new chunk with the correct size. recordH = DmNewHandle (dbP, (UInt32) ApptPackedSize(r)); if (recordH == NULL) return dmErrMemError; recordP = MemHandleLock (recordH); // Copy the data from the unpacked record to the packed one. ApptPack (r, recordP); newIndex = ApptFindSortPosition(dbP, recordP); MemPtrUnlock (recordP); // 4) attach in place err = DmAttachRecord(dbP, &newIndex, recordH, 0); if (err) MemHandleFree(recordH); else *index = newIndex; return err; }
/* Create an app info chunk if missing, return result from the database call */ static void InitPlkrAppInfo ( DmOpenRef docRef /* reference to document */ ) /* THROWS */ { UInt16 cardNo; MemHandle handle; LocalID dbID; LocalID appInfoID; PlkrAppInfoType* appInfoP; Err err; err = DmOpenDatabaseInfo( docRef, &dbID, NULL, NULL, &cardNo, NULL ); THROW_IF( err != errNone, err ); err = DmDatabaseInfo( cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &appInfoID, NULL, NULL, NULL ); THROW_IF( err != errNone, err ); if ( appInfoID == 0 ) { handle = DmNewHandle( docRef, sizeof *appInfoP ); THROW_IF( handle == NULL, dmErrMemError ); appInfoID = MemHandleToLocalID( handle ); DmSetDatabaseInfo( cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &appInfoID, NULL, NULL, NULL ); } appInfoP = MemLocalIDToLockedPtr( appInfoID, cardNo ); DmSet( appInfoP, 0, sizeof *appInfoP, 0 ); CategoryInitialize( ( AppInfoPtr ) appInfoP, strCatDefault ); MemPtrUnlock( appInfoP ); }
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; }
/************************************************************ * * FUNCTION: ApptChangeRecord * * DESCRIPTION: Change a record in the Appointment Database * * PARAMETERS: database pointer * database index * database record * changed fields * * RETURNS: ##0 if successful, errorcode if not * * CREATED: 1/25/95 * * BY: Roger Flores * * COMMENTS: Records are not stored with extra padding - they * are always resized to their exact storage space. This avoids * a database compression issue. The code works as follows: * * 1) get the size of the new record * 2) make the new record * 3) pack the packed record plus the changes into the new record * 4) if the sort position is changes move to the new position * 5) attach in position * *************************************************************/ Err ApptChangeRecord(DmOpenRef dbP, UInt16 *index, ApptDBRecordPtr r, ApptDBRecordFlags changedFields) { Err result; Int16 newIndex; UInt16 attributes; Boolean dontMove; MemHandle oldH; MemHandle srcH; MemHandle dstH; ApptDBRecordType src; ApptPackedDBRecordPtr dst = 0; ApptPackedDBRecordPtr cmp; // We do not assume that r is completely valid so we get a valid // ApptDBRecordPtr... if ((result = ApptGetRecord(dbP, *index, &src, &srcH)) != 0) return result; // and we apply the changes to it. if (changedFields.when) src.when = r->when; if (changedFields.alarm) src.alarm = r->alarm; if (changedFields.repeat) src.repeat = r->repeat; if (changedFields.exceptions) src.exceptions = r->exceptions; if (changedFields.description) src.description = r->description; if (changedFields.note) src.note = r->note; // Allocate a new chunk with the correct size and pack the data from // the unpacked record into it. dstH = DmNewHandle(dbP, (UInt32) ApptPackedSize(&src)); if (dstH) { dst = MemHandleLock (dstH); ApptPack (&src, dst); } MemHandleUnlock (srcH); if (dstH == NULL) return dmErrMemError; // If the sort position is changed move to the new position. // Check if any of the key fields have changed. if ((!changedFields.when) && (! changedFields.repeat)) goto attachRecord; // repeating events aren't in sorted order // Make sure *index-1 < *index < *index+1, if so it's in sorted // order. Leave it there. if (*index > 0) { // This record wasn't deleted and deleted records are at the end of the // database so the prior record may not be deleted! cmp = MemHandleLock (DmQueryRecord(dbP, *index-1)); dontMove = (ApptComparePackedRecords (cmp, dst, 0, NULL, NULL, 0) <= 0); MemPtrUnlock (cmp); } else dontMove = true; if (dontMove && (*index+1 < DmNumRecords (dbP))) { DmRecordInfo(dbP, *index+1, &attributes, NULL, NULL); if ( ! (attributes & dmRecAttrDelete) ) { cmp = MemHandleLock (DmQueryRecord(dbP, *index+1)); dontMove &= (ApptComparePackedRecords (dst, cmp, 0, NULL, NULL, 0) <= 0); MemPtrUnlock (cmp); } } if (dontMove) goto attachRecord; // The record isn't in the right position. Move it. newIndex = ApptFindSortPosition (dbP, dst); DmMoveRecord (dbP, *index, newIndex); if (newIndex > *index) newIndex--; *index = newIndex; // return new position attachRecord: // Attach the new record to the old index, the preserves the // category and record id. result = DmAttachRecord (dbP, index, dstH, &oldH); MemPtrUnlock(dst); if (result) return result; MemHandleFree(oldH); return 0; }