bool KTaskDataFile::SaveData() { KFile File; char szFileName[128]; GetFileName(szFileName, sizeof(szFileName)); bool bOk = false; while (ms_pPersonalRecord || ms_nSystemRecordCount > 0) { if (!File.Create(szFileName)) break; //==写文件头== TASK_FILE_HEADER Header = { 0 }; *(int*)(&(Header.cFlag[0])) = TASK_FILE_FLAG; if (ms_pPersonalRecord && ms_pPersonalRecord->nLen > 0) { Header.nPersonalRecordBytes = sizeof(KPersonalRecord) + ms_pPersonalRecord->nLen - sizeof(ms_pPersonalRecord->cBuffer); } Header.nSystemRecordCount = ms_nSystemRecordCount; if (File.Write(&Header, sizeof(Header)) != sizeof(Header)) break; //==写个人纪录== if (ms_pPersonalRecord) { if (File.Write(ms_pPersonalRecord, Header.nPersonalRecordBytes) != Header.nPersonalRecordBytes) break; } //==写系统纪录== KTaskSystemRecordNode* pCurrent = ms_pSystemRecordList; int i; for (i = 0; i < ms_nSystemRecordCount; i++) { int nSize = sizeof(TASK_SYSTEM_RECORD) + pCurrent->Record.nContentLen - sizeof(pCurrent->Record.cBuffer); if (File.Write(&pCurrent->Record, nSize) != nSize) break; pCurrent = pCurrent->pNext; } if (i >= ms_nSystemRecordCount) { bOk = true; } break; }; File.Close(); if (bOk == false) { char szFullName[MAX_PATH]; g_GetFullPath(szFullName, szFileName); DeleteFile(szFullName); } return bOk; }
void KTaskDataFile::LoadData() { ClearAll(); KFile File; char szFileName[128]; GetFileName(szFileName, sizeof(szFileName)); bool bOk = false; while(File.Open(szFileName)) { //==读文件头== TASK_FILE_HEADER Header; if (File.Read(&Header, sizeof(Header)) != sizeof(Header)) break; if (*(int*)(&(Header.cFlag[0])) != TASK_FILE_FLAG) break; //==读个人纪录== if (Header.nPersonalRecordBytes) { ms_pPersonalRecord = (KPersonalRecord*)malloc(Header.nPersonalRecordBytes); if (ms_pPersonalRecord) { if (!File.Read(ms_pPersonalRecord, Header.nPersonalRecordBytes)) break; } else { break; } } //==读系统纪录== TASK_SYSTEM_RECORD record; int nPreSize = sizeof(TASK_SYSTEM_RECORD) - sizeof(record.cBuffer); for (int i = 0; i < Header.nSystemRecordCount; i++) { if (File.Read(&record, nPreSize) != nPreSize) break; KTaskSystemRecordNode* pNode = (KTaskSystemRecordNode*)malloc( sizeof(KTaskSystemRecordNode) + record.nContentLen); if (pNode) { if (File.Read(pNode->Record.cBuffer, record.nContentLen) != record.nContentLen) { free (pNode); break; } pNode->pNext = NULL; pNode->Record.nContentLen = record.nContentLen; pNode->Record.tTime = record.tTime; pNode->Record.uReserved = record.uReserved; AppendSystemRecord(pNode); } } bOk = true; break; }; File.Close(); if (bOk == false) ClearAll(); }