📄 .#progectdb.c.1.21
字号:
/* -*-Mode:C; tab-width:4; indent-tabs-mode:t; c-file-style:"stroustrup";-*- */#include "task.h"#include "progect.h"#include "progectdb.h"#include "progectRsc.h"#include "xb.h"/**************************************************************************** * Name : AddRecord * Desc : add a record to the database * In : database, record to add * Out : new index * Auth : lb, 27.07.2000 * Mod : lb, 03.08.2000 * support to insert as a brother of selected task * Mod : lb, 22.08.2000 * added field for note * Mod : rw, 30.12.2000 * fixed bug in father inheritance, added category inheritance * Rem : doesn't handle err codes ***************************************************************************/Err AddRecord(DmOpenRef dbP, TaskTypePtr task, UInt16 *index){ MemHandle h; TaskRecordType *p; UInt32 size = TaskRecordTypeSize; Err Err = errNone; UInt16 realIndex; // could be the clipboard UInt16 numRec = DmNumRecords(dbP);// - calcNumDeleted(dbP); // pack this in a newly allocated record // calc the needed size // the two \0 are already in TaskRecordTypeSize size += (StrLen(task->description) + StrLen(task->note)); // get a handle on a newly created record // new one always comes after actual if (numRec == 0) { realIndex = 0; } else if (numRec > 1 && TaskGetHasChild(dbP, gParentTask)) { realIndex = TaskGetNextRelativeIndex(dbP, *index); // place of new one } else { realIndex = gParentTask + 1; } if (!realIndex) { realIndex = numRec; } h = DmNewRecord(dbP, &realIndex, size); task->attr.allBits = 0; task->attr.bits.opened = 1; task->dueDate.year = 0; task->dueDate.month = 0; task->dueDate.day = 0; // if index = 0, this is the first one // it's not a real task !!! if (realIndex == 0) { task->attr.bits.hasChild = 0; } else { // retrieve info about user of previous index // this sets : level, next if (*index == 0 || !TaskGetHasChild(dbP, gParentTask)) { // this is a first child task->attr.bits.level = gRefLevel + 1; TaskSetHasChild(dbP, gParentTask, true); } else { task->attr.bits.level = TaskGetLevel(dbP, *index); task->attr.bits.hasPrev = 1; task->attr.bits.hasNext = TaskGetHasNext(dbP, *index); // update hasNext field in previous TaskSetHasNext(dbP, *index, true); if (gProjectPrefs.useFatherStatus) { if ((task->attr.bits.level) != FIRST_LEVEL) { UInt16 father = TaskGetFatherIndex(dbP, *index); task->dueDate = TaskGetDueDate(dbP, father); task->priority = TaskGetPriority(dbP, father); } } // inherit the default category if (gProjectPrefs.useFatherStatus) { if ((task->attr.bits.level) != FIRST_LEVEL) { UInt16 father = TaskGetFatherIndex(dbP, *index); UInt8 category; // get the father's category category = TaskGetCategory(gdbP, father); // set the default for the child TaskSetCategory(gdbP, realIndex, category); } } } } // get a pointer on the record p = MemHandleLock(h); // write the data DmWrite(p, 0, task, OffsetOf(TaskRecordType, description)); DmStrCopy(p, OffsetOf(TaskRecordType, description), task->description); DmStrCopy(p, OffsetOf(TaskRecordType, description) + StrLen(task->description) + 1, task->note); // unlock the pointer MemHandleUnlock(h); // unlock the record DmReleaseRecord(dbP, realIndex, true); *index = realIndex;// DEBUG1("Added desc : ");// DEBUG1(task->description);// DEBUG1("Added note : ");// DEBUG1(task->note);// DEBUGVAL("len desc : ", StrLen(task->description));// DEBUGVAL("len note : ", StrLen(task->note)); return Err;} // static Err AddRecord(DmOpenRef dbP, TaskTypePtr task, UInt16 *index)/**************************************************************************** * Name : AddRecordSub * Desc : add a record to the database, as subtask of index * In : database, record to add, index * Out : new index * Auth : lb, 04.08.2000 * Rem : doesn't handle err codes * Mod : lb, 13.08.2000 * fix bug, can now create more subtasks. Subtasks always created * as last child * Mod : lb, 22.08.2000 * added field for note * Mod : rw, 30.12.2000 * fixed bug in father inheritance, added category inheritance ***************************************************************************/Err AddRecordSub(DmOpenRef dbP, TaskTypePtr task, UInt16 *index){ MemHandle h; TaskRecordType *p; UInt32 size = TaskRecordTypeSize; Err Err = errNone; UInt16 realIndex; // pack this in a newly allocated record // calc the needed size size += (StrLen(task->description) + StrLen(task->note)); // get a handle on a newly created record // new one always comes after actual realIndex = TaskGetNextRelativeIndex(dbP, *index); // place of new one if (!realIndex) { realIndex = PgNumRecords(dbP); } h = DmNewRecord(dbP, &realIndex, size); // retrieve info about user of previous index // this sets : level, next task->attr.allBits = 0; task->attr.bits.level = TaskGetLevel(dbP, *index) + 1; task->attr.bits.opened = 1; task->attr.bits.hasPrev = TaskGetHasChild(dbP, *index); //task->priority = 6; task->dueDate.year = 0; task->dueDate.month = 0; task->dueDate.day = 0; if (gProjectPrefs.useFatherStatus) { if ((task->attr.bits.level) != FIRST_LEVEL) { task->dueDate = TaskGetDueDate(dbP, *index); task->priority = TaskGetPriority(dbP, *index); } } // inherit the default category if (gProjectPrefs.useFatherStatus) { if ((task->attr.bits.level) != FIRST_LEVEL) { UInt8 category; // get the father's category category = TaskGetCategory(gdbP, *index); // set the default for the child TaskSetCategory(gdbP, realIndex, category); } } // if father had children already if (TaskGetHasChild(dbP, *index)) { // search prev task UInt16 n = realIndex - 1; while (TaskGetLevel(dbP, n) > task->attr.bits.level) n--; // old last child has now a next TaskSetHasNext(dbP, n, true); } // update hasChild field in father TaskSetHasChild(dbP, *index, true); // get a pointer on the record p = MemHandleLock(h); // write the data DmWrite(p, 0, task, OffsetOf(TaskRecordType, description)); DmStrCopy(p, OffsetOf(TaskRecordType, description), task->description); DmStrCopy(p, OffsetOf(TaskRecordType, description) + StrLen(task->description) + 1, task->note); // unlock the pointer MemHandleUnlock(h); // unlock the record DmReleaseRecord(dbP, realIndex, true); *index = realIndex; return Err;} // static Err AddRecordSub(DmOpenRef dbP, TaskTypePtr task, UInt16 *index)/**************************************************************************** * Name : CreateDB * Desc : create a database * In : * -> name without prepanding ! * Out : - * Auth : lb, 26.07.2000 * Rem : doesn't handle err codes * Mod : lb, 17.08.2000 * - prepand "lbPG-" to the dbname ***************************************************************************/Err CreateDB(const char *nameP){ UInt16 cardNo = 0; Char dbname[dmDBNameLength]; Err Err = errNone; LocalID dbID; TaskType task0; UInt16 index = 0; UInt16 dbversion = DBVERSION; SafeCopy(dbname,nameP,dmDBNameLength); Prepend(dbname); dbID = DmFindDatabase(cardNo, dbname); // if database doesn't exist, create it if (!dbID) { Err = DmCreateDatabase(cardNo, dbname, CREATOR, 'DATA', false); dbID = DmFindDatabase(cardNo, dbname); if (!dbID) { return Err; } // set database format version DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, &dbversion, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); // debug code to initialise a test DB // OpenDB takes a name without prepend OpenDB(nameP, &gdbP); task0 = gEmptyTask; AddRecord(gdbP, &task0, &index); CloseDB(gdbP); gdbP = NULL; } else { Err = dmErrAlreadyExists; } return Err;} // static Err CreateDB(const char *nameP)/**************************************************************************** * Name : RemoveDB * Desc : remove a database * In : name of database to remove * Out : Err code * Auth : lb, 27.07.2000 * Mod : lb, 17.08.2000 * prepending support * control of CREATOR ID ***************************************************************************/Err RemoveDB(const char *nameP){ UInt16 cardNo = 0; Err Err = errNone; LocalID dbID; Char dbname[dmDBNameLength]; UInt32 creatorID; SafeCopy(dbname,nameP,dmDBNameLength); Prepend(dbname); dbID = DmFindDatabase(cardNo, dbname); // if database exists, delete it if (dbID) { DmDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &creatorID); if (creatorID == CREATOR) { Err = DmDeleteDatabase(cardNo, dbID); } } else { dbID = DmFindDatabase(cardNo, nameP); // if database exists, delete it if (dbID) { DmDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &creatorID); if (creatorID == CREATOR) { Err = DmDeleteDatabase(cardNo, dbID); } } } return Err;} // static Err RemoveDB(const char *nameP)/**************************************************************************** * Name : PgNumRecords * Desc : Return the number of non-deleted records in the database * Prm : * -> dbP, database reference * Out : num of non-deleted records * Rem : gNumDeleted MUST be uptodate * Auth : lb, 2001-09-05 * Mod : lb, 2002-01-21 * gNumDeleted not used anymore ***************************************************************************/UInt16 PgNumRecords(DmOpenRef dbP){ return DmNumRecords(dbP); //return DmNumRecords(dbP) - gNumDeleted;} // UInt16 PgNumRecords(DmOpenRef dbP)/**************************************************************************** * Name : calcNumDeleted * Desc : calc and init gNumDeleted, the number of deleted records in dbP * Prm : * -> dbP, database reference * Out : number of deleted records * Auth : lb, 2001-09-04 * Mod : lb, 2002-01-21 * Not used anymore ***************************************************************************/UInt16 calcNumDeleted(DmOpenRef dbP){// Int32 i = (Int32)DmNumRecords(dbP) - 1;// UInt16 attr;// Int32 num = -1;// DBGMSG((DBB, "In calcNumDeleted"));// attr = dmRecAttrDelete;// while ((attr & dmRecAttrDelete) && (i > 0))// {// num++;// DmRecordInfo(dbP, i--, &attr, NULL, NULL);// }// if (num == -1)// num = 0;// DBGMSG((DBB, "Num deleted : %lu", num));// return (UInt16)num; return 0;} // UInt16 calcNumDeleted(DmOpenRef dbP)/**************************************************************************** * Name : OpenDB * Desc : open a database by name * Prm : * -> name of the db to open without prepending
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -