📄 ndb.c
字号:
// First, get the open database in question. if ( theDatabase != NULL ) {// Find the number of records on this card. *errP = DmOpenDatabaseInfo ( theDatabase, &dbID, NULL, NULL, NULL, NULL ); if ( *errP != noErr ) break; *errP = DmDatabaseSize ( theCard, dbID, &recCountOnCard, NULL, NULL ); if ( *errP != noErr ) break; // That database is valid. Try to get the index'th element if ( theIndex >= recCountOnCard ) {// Nope. Decrement the virtual index and hop to the next card. theIndex -= (UInt16)recCountOnCard; continue; } else {// Aha! We've found the record. found = true; dataH = DmQueryRecord ( theDatabase, theIndex ); break; } // is this index on the card? } // is this a valid open database? } // loop over cards// If it's not found and there's no error, the search is over. if ( !found && *errP == noErr ) { *errP = ndbErrorSearchOver; } return dataH;}/******************************************************************** * FUNCTION: NDBStartSearchByString * * DESCRIPTION: This routine starts searching the * specified database for the substring * and returns a result. * * RETURNED: Nothing * error code at the location specified * by errP.**********************************************************************/static void NDBStartSearchByString // (out) Nothing( NDBDatabaseEnum db, // (in) database to search CharPtr keyP, // (in) key to find MemHandle *resultH, // (in) where to stash results // (out) results of search Err *errP // (in) where to place error) // (out) error code for error{ NDBRecordType *resultP; *errP = noErr; // validate input if ( resultH == NULL ) *errP = dmErrInvalidParam; if ( *errP == noErr ) { resultP = MemHandleLock( *resultH ); } if ( *errP == noErr ) { resultP->recordIdx = 0;// optimization: if KEY is null or an empty string, the first record matches. if ( keyP == NULL ) { resultP->recordH = NDBGetRecordWithIndex( db, resultP->recordIdx, errP ); } else if ( StrCompare( keyP, "" ) == 0 ) { resultP->recordH = NDBGetRecordWithIndex( db, resultP->recordIdx, errP ); } else {// Find the next entry matching the desired substring. NDBNextSearchByString( resultP, errP, ndbSearchNext ); }// If it's not found, say so. if ( resultP->recordH == NULL ) { *errP = ndbErrorSearchOver; } if (resultP) MemHandleUnlock( *resultH ); } return;}// Application-level interfaces/******************************************************************** * FUNCTION: NDBFindRecordWith * * DESCRIPTION: This routine finds a record with the * specified value in any field. * SIDE EFFECT(S): This routine opens a database using * NDBOpenDatabase. * * RETURNED: Handle to NDBRecord. Caller must free. * error code at the location specified * by errP.**********************************************************************/MemHandle NDBFindRecordWith // (out) pointer to result( NDBDatabaseEnum db, // (in) database to search CharPtr keyP, // (in) search target Err *errP // (in) where to place error) // (out) error code for error{ MemHandle resultH; *errP = noErr;// Validate arguments if ( db <= ndbInvalid || db >= ndbLastDatabase ) *errP = dmErrIndexOutOfRange;// Optimization: if the key is empty, make it null. if ( keyP ) if ( StrCompare( keyP, "" ) == 0 ) keyP = NULL;// Create a result structure resultH = NDBRecordNew( db, keyP, errP ); if ( *errP == noErr ) {// Find the first record containing the key// First, open the database to search. It'll be closed when we free the result object. NDBOpenDatabase( db, errP ); }// Search the database by key. if ( *errP == noErr ) { NDBStartSearchByString( db, keyP, &resultH, errP ); }// and return return resultH;}/******************************************************************** * FUNCTION: NDBRecordGetNext * * DESCRIPTION: This routine gets the next record in * a search. * * RETURNED: Handle to record or NULL if no elements * remaining in enumeration * error code at the location specified * by errP.**********************************************************************/MemHandle NDBRecordGetNext // (out) Handle to next record( MemHandle recordH, // (in) handle to current record Err *errP // (in) where to place error) // (out) error code for error{ NDBRecordType *recordP = NULL; *errP = noErr; // Validate arguments if ( !recordH ) *errP = dmErrInvalidParam;// Find the next occurrence if ( *errP == noErr ) { recordP = ( NDBRecordType * )MemHandleLock( recordH ); }// Optimization: If there's nothing to search for, go ahead and // return the next record if ( *errP == noErr && recordP->targetP == NULL ) { recordP->recordIdx++; recordP->recordH = NDBGetRecordWithIndex( recordP->database, recordP->recordIdx, errP ); } else if ( *errP == noErr && StrLen( recordP->targetP ) == 0 ) { recordP->recordIdx++; recordP->recordH = NDBGetRecordWithIndex( recordP->database, recordP->recordIdx, errP ); } else {// Do the search recordP->recordIdx++; NDBNextSearchByString( recordP, errP, ndbSearchNext ); }// and return if ( recordP ) MemHandleUnlock( recordH ); return recordH;}/******************************************************************** * FUNCTION: NDBRecordGetPrev * * DESCRIPTION: This routine gets the previous record in * a search. * * RETURNED: Handle to record or NULL if no elements * remaining in enumeration * error code at the location specified * by errP.**********************************************************************/MemHandle NDBRecordGetPrev // (out) Handle to next record( MemHandle recordH, // (in) handle to current record Err *errP // (in) where to place error) // (out) error code for error{ NDBRecordType *recordP = NULL; *errP = noErr; // Validate arguments if ( !recordH ) *errP = dmErrInvalidParam;// Find the next occurrence if ( *errP == noErr ) { recordP = ( NDBRecordType * )MemHandleLock( recordH ); }// If we're at the start, tell the user and give an error. if ( recordP->recordIdx == 0 ) { *errP = ndbErrorNoPreviousRecord; recordP->recordH = NULL; }// Optimization: If there's nothing to search for, go ahead and // return the next record else if ( *errP == noErr && recordP->targetP == NULL ) { recordP->recordIdx--; recordP->recordH = NDBGetRecordWithIndex( recordP->database, recordP->recordIdx, errP ); } else if ( *errP == noErr && StrLen( recordP->targetP ) == 0 ) { recordP->recordIdx--; recordP->recordH = NDBGetRecordWithIndex( recordP->database, recordP->recordIdx, errP ); } else {// Do the search recordP->recordIdx--; NDBNextSearchByString( recordP, errP, ndbSearchPrev ); }// and return if ( recordP ) MemHandleUnlock( recordH ); return recordH;}/******************************************************************** * FUNCTION: NDBRecordGetHandle * * DESCRIPTION: This routine returns a handle to the * native database record. * * RETURNED: Handle to the database record in native * format. * error code at the location specified * by errP.**********************************************************************/MemHandle NDBRecordGetHandle // (out) Handle( MemHandle recordH, // (in) record handle tor return Err *errP // (in) where to place error) // (out) error code for error{ MemHandle resultH = NULL; NDBRecordType *recordP = NULL; *errP = noErr; // validate arguments if ( recordH == NULL ) *errP = dmErrInvalidParam; // Fetch the result handle recordP = ( NDBRecordType * )MemHandleLock( recordH ); resultH = recordP->recordH; if ( *errP == noErr ) MemHandleUnlock( recordH );// And return return resultH;}/******************************************************************** * FUNCTION: NDBRecordGetText * * DESCRIPTION: This routine creates a handle to a text * string containing a text representation * of the current record. * * RETURNED: A handle to the result or NULL * error code at the location specified * by errP.**********************************************************************/MemHandle NDBRecordGetText // (out) result string( MemHandle recordH, // (in) Handle to record Err * errP // (in) where to place error) // (out) error code for error{ NDBRecordType *recordP = NULL; MemHandle resultH = NULL; *errP = noErr; // validate arguments if ( recordH == NULL ) *errP = dmErrInvalidParam; if ( *errP == noErr ) {// Fetch the record handle obtain its contents recordP = ( NDBRecordType * )MemHandleLock ( recordH ); if ( recordP->recordH == NULL )// Huh? There's nothing here! *errP = ndbErrorRecordInvalid; } // switch on the kind of database and call the appropriate property routine if ( *errP == noErr ) { switch ( recordP->database ) { case ndbAddress: resultH = NDBAddrGetText( recordP, errP ); break; case ndbMemo: resultH = NDBMemoGetText( recordP, errP ); break; case ndbDatebook: resultH = NDBDateGetText( recordP, errP ); break; case ndbToDo: resultH = NDBTodoGetText( recordP, errP ); break; default: *errP = dmErrInvalidParam; break; } } if ( recordP ) MemHandleUnlock( recordH ); return resultH;}/******************************************************************** * FUNCTION: NDBRecordFree * * DESCRIPTION: This routine frees the current record. * SIDE EFFECT(S): This routine closes the database ref * for the record's database using * NDBCloseDatabase. * * RETURNED: nothing * error code at the location specified * by errP.**********************************************************************/void NDBRecordFree // (out) nothing( MemHandle recordH, Err *errP // (in) where to place error) // (out) error code for error{ NDBRecordType *recordP = NULL; *errP = noErr; // validate arguments if ( recordH == NULL ) *errP = dmErrInvalidParam; if ( *errP == noErr ) {// Fetch the record handle to free its contents recordP = ( NDBRecordType * )MemHandleLock ( recordH ); } if ( recordP ) { // Free the search string if any if ( recordP->targetP ) MemPtrFree ( recordP->targetP );// Close the database NDBCloseDatabase( recordP->database, errP ); } if ( recordP ) MemHandleUnlock( recordH ); if ( recordH ) MemHandleFree( recordH ); return;}/******************************************************************** * FUNCTION: NDBRecordGetProp * * DESCRIPTION: This routine returns a handle with the * value of the specified field as a string. * * RETURNED: A memory handle or NULL * error code at the location specified * by errP.**********************************************************************/MemHandle NDBRecordGetProp // (out) handle to string( MemHandle recordH, // (in) Record of interest NDBPropertyEnum prop, // (in) desired property Err *errP // (in) where to place error) // (out) error code for error{ NDBRecordType *recordP = NULL; MemHandle resultH = NULL; *errP = noErr; // validate arguments if ( recordH == NULL ) *errP = dmErrInvalidParam; if ( *errP == noErr ) {// Fetch the record handle obtain its contents recordP = ( NDBRecordType * )MemHandleLock ( recordH ); if ( recordP->recordH == NULL )// Huh? There's nothing here! *errP = ndbErrorRecordInvalid; } // switch on the kind of database and call the appropriate property routine if ( *errP == noErr ) { switch ( recordP->database ) { case ndbAddress: resultH = NDBAddrGetProp( recordP, prop, errP ); break; case ndbMemo: resultH = NDBMemoGetProp( recordP, prop, errP ); break; case ndbDatebook: resultH = NDBDateGetProp( recordP, prop, errP ); break; case ndbToDo: resultH = NDBTodoGetProp( recordP, prop, errP ); break; default: *errP = dmErrInvalidParam; break; } } if ( recordP ) MemHandleUnlock( recordH ); return resultH;}/******************************************************************** * FUNCTION: NDBRecordIsValid * * DESCRIPTION: This routine inspects a record to see if * it points to a real database item. * * RETURNED: true if this record is valid. * error code at the location specified * by errP.**********************************************************************/Boolean NDBRecordIsValid // (out) true if valid( MemHandle recordH, // (in) record to inspect Err *errP // (in) where to place error) // (out) error code for error{ Boolean valid = false; NDBRecordType *recordP = NULL; *errP = noErr;// validate arguments if ( recordH == NULL ) *errP = dmErrInvalidParam; recordP = ( NDBRecordType * )MemHandleLock ( recordH ); if ( *errP == noErr ) {// Make sure it points to a legal database if ( ( recordP->database == ndbAddress ) || ( recordP->database == ndbMemo ) || ( recordP->database == ndbDatebook ) || ( recordP->database == ndbToDo ) ) valid = true;// and that it points to a valid record. if ( recordP->recordH == NULL ) valid = false; } if ( recordP ) MemHandleUnlock( recordH ); return valid;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -