Exemple #1
0
/* Save a given word in bookmarks database. */
Err AddBookmark(AppContext* appContext, char * word)
{
    Err                 err;
    UInt16              pos;
    Boolean             fWordAlreadyBookmarked;
    MemHandle           recHandle;
    char *              wordInRec;
    BookmarkSortType    currDbOpen;

    currDbOpen = appContext->currBookmarkDbType;

    // 1. See if we already have the record in bookmarks.
    //    We assume that if the record wasn't found in sorted-by-name database, it doesn't exist in the other.
    //    The sorted-by-name database is quicker to scan, because we can use binary search.

    err = OpenBookmarksDB(appContext, bkmSortByName);
    if (errNone != err)
        goto Exit;

    pos = DmFindSortPosition(appContext->bookmarksDb, word, NULL, (DmComparF *) BookmarksByNameCompare, 0);
    
    fWordAlreadyBookmarked = false;
    // DmFindSortPosition returns the position there the new record should be placed
    // so if the record exist its position is (pos - 1)
    recHandle = DmQueryRecord(appContext->bookmarksDb, pos>0 ? pos-1 : pos);
    // it's ok if we don't get handle - this might be an empty database
    if (recHandle)
    {
        wordInRec = (char*)MemHandleLock(recHandle);
        if (0 == StrCompare(word, wordInRec))
            fWordAlreadyBookmarked = true;
        MemHandleUnlock(recHandle);
    }
    
    if (fWordAlreadyBookmarked)
    {
        err = errNone;
        goto Exit;
    }

    // 2. If we haven't found the record, we add it to databases

    WriteWordInRecord(appContext->bookmarksDb, pos, word);
    CloseBookmarksDB(appContext);

    err = OpenBookmarksDB(appContext, bkmSortByTime);
    if (errNone != err)
        goto Exit;
    // the record must be put chronogically, so we add it at the beginning of the database
    WriteWordInRecord(appContext->bookmarksDb, 0, word);
    CloseBookmarksDB(appContext);

Exit:
    if (bkmInvalid != currDbOpen)
        OpenBookmarksDB(appContext, currDbOpen);
    return err;
}
Exemple #2
0
/************************************************************
 *
 *  FUNCTION: ApptFindSortPosition
 *
 *  DESCRIPTION: Return where a record is or should be
 *      Useful to find or find where to insert a record.
 *
 *  PARAMETERS: database record
 *
 *  RETURNS: position where a record should be
 *
 *  CREATED: 1/25/95 
 *
 *  BY: Roger Flores
 *
 *************************************************************/
static UInt16 ApptFindSortPosition(DmOpenRef dbP, ApptPackedDBRecordPtr
                                 newRecord)
{
    return (DmFindSortPosition (dbP, newRecord, NULL,
                                (DmComparF *)ApptComparePackedRecords, 0));
}
Exemple #3
0
static UInt16 HDFindSortPosition(DmOpenRef dbP, PackedHappyDays* newRecord)
{
    return DmFindSortPosition(dbP, (void*)newRecord, 0,
                               (DmComparF *)HDComparePackedRecords, 0);
}