//--------------------------------------------------------------------------- // 函数: Save // 功能: 保存当前的INI文件 // 返回: 1 成功, 0失败 //--------------------------------------------------------------------------- int QIniFileImpl::Save(const char* FileName) { int nResult = false; IFile* piFile = NULL; SECNODE* SecNode = m_Header.pNextNode; KEYNODE* KeyNode = NULL; DWORD dwStringLen = 0; DWORD dwWriteSize = 0; const char cszNewLine[3] = "\r\n"; piFile = g_CreateFile(FileName); PROCESS_ERROR(piFile); while (SecNode != NULL) { dwStringLen = (DWORD)strlen(SecNode->pSection); dwWriteSize = piFile->Write(SecNode->pSection, dwStringLen); PROCESS_ERROR(dwWriteSize == dwStringLen); dwWriteSize = piFile->Write(cszNewLine, 2); PROCESS_ERROR(dwWriteSize == 2); KeyNode = SecNode->RootNode.pNextNode; while (KeyNode != NULL) { dwStringLen = (DWORD)strlen(KeyNode->pKey); dwWriteSize = piFile->Write(KeyNode->pKey, dwStringLen); PROCESS_ERROR(dwWriteSize == dwStringLen); dwWriteSize = piFile->Write("=", 1); PROCESS_ERROR(dwWriteSize == 1); dwStringLen = (DWORD)strlen(KeyNode->pValue); dwWriteSize = piFile->Write(KeyNode->pValue, dwStringLen); PROCESS_ERROR(dwWriteSize == dwStringLen); dwWriteSize = piFile->Write(cszNewLine, 2); PROCESS_ERROR(dwWriteSize == 2); KeyNode = KeyNode->pNextNode; } dwWriteSize = piFile->Write(cszNewLine, 2); PROCESS_ERROR(dwWriteSize == 2); SecNode = SecNode->pNextNode; } nResult = true; EXIT0: SAFE_RELEASE(piFile); return nResult; }
//从包中解出某个文件 bool KPackFileManager::UnpackElemByIndex(int nPakIndex, unsigned int uElemIndex, const char* pDestName) { if (nPakIndex < 0 || nPakIndex >= PACK_FILE_SHELL_MAX_SUPPORT_PAK_NUM || !m_PackItemList[nPakIndex].pIOFile) return false; KSmartFile dest; dest = g_CreateFile(pDestName); if (dest) { unsigned uElemSize = 0; unsigned char* pBuffer = AllocBufferAndReadElemFile(nPakIndex, uElemIndex, uElemSize); if (pBuffer) { dest->Write(pBuffer, uElemSize); SAFE_FREE(pBuffer); return true; } } return false; }
//创建/打开的一个打包文件,返回打包文件索引,返回0值表示操作失败。 int KPackFileManager::CreatePack(const char* pszFile, int bOpenExist, int bExcludeOfCheckId) { int nPakIndex; for (nPakIndex = 0; nPakIndex < PACK_FILE_SHELL_MAX_SUPPORT_PAK_NUM; nPakIndex++) { if (m_PackItemList[nPakIndex].pIOFile == NULL) break; } if (nPakIndex == PACK_FILE_SHELL_MAX_SUPPORT_PAK_NUM) return -1; PACK_ITEM& item = m_PackItemList[nPakIndex]; bool bOk = false; g_GetFullPath(item.PackFileName, pszFile); while(true) { item.pIndexList = (XPackIndexInfo*)malloc(sizeof(XPackIndexInfo) * PACK_FILE_SHELL_MAX_SUPPORT_ELEM_FILE_NUM); if (item.pIndexList == NULL) break; memset(item.pIndexList, 0, sizeof(XPackIndexInfo) * PACK_FILE_SHELL_MAX_SUPPORT_ELEM_FILE_NUM); if (bOpenExist) { if (!g_IsFileExist(pszFile)) //FilePath.cpp:426 --> pszFile = 路径名+文件名 break; item.pIOFile = g_OpenFile(pszFile, true, true); if (item.pIOFile == NULL) break; if (item.pIOFile->Read(&item.Header, sizeof(item.Header)) != sizeof(item.Header) || (*(int*)(&(item.Header.cSignature)) != IPACK_FILE_SIGNATURE_FLAG)) { break; } item.pIOFile->Seek(item.Header.uIndexTableOffset, SEEK_SET); if (item.pIOFile->Read(item.pIndexList, sizeof(XPackIndexInfo) * item.Header.uCount) != sizeof(XPackIndexInfo) * item.Header.uCount)//读入文件尾部,插入新的文件数据之后再插回来 break; item.nDataEndOffset = item.Header.uIndexTableOffset; item.pIOFile->Seek(item.nDataEndOffset, SEEK_SET); LoadPackPartner(nPakIndex); } else { item.pIOFile = g_CreateFile(pszFile); if (item.pIOFile == NULL) break; memset(&item.Header, 0, sizeof(item.Header)); *(int*)(&(item.Header.cSignature)) = IPACK_FILE_SIGNATURE_FLAG; /* 前提条件为:Little-Endian */ if (item.pIOFile->Write(&item.Header, sizeof(item.Header)) != sizeof(item.Header)) break; item.Header.uDataOffset = sizeof(item.Header); item.nDataEndOffset = sizeof(item.Header); item.bModified = true; CreatePackPartner(nPakIndex); } item.bExcludeOfCheckId = (bExcludeOfCheckId != false); bOk = true; break; } if (!bOk) { SAFE_FREE(item.pIndexList); SAFE_RELEASE(item.pIOFile); item.PackFileName[0] = 0; return -1; } return nPakIndex; }
int KSystemScriptTable::LuaSaveDataToFile(Lua_State* L) { int nRetCode = false; IFile *piFile = NULL; const char *pcszFile = NULL; const char *pcsBuff = NULL; unsigned uFilePathLength = 0; size_t uBuffSize = 0; int i = 0; char szFile[MAX_PATH]; char szPath[MAX_PATH]; KGLOG_PROCESS_ERROR(L); nRetCode = lua_gettop(L); KGLOG_PROCESS_ERROR(nRetCode == 2); pcsBuff = lua_tolstring(L, 1, &uBuffSize); KGLOG_PROCESS_ERROR(pcsBuff); KGLOG_PROCESS_ERROR(uBuffSize > 0); pcszFile = lua_tostring(L, 2); KGLOG_PROCESS_ERROR(pcszFile); KGLOG_PROCESS_ERROR(pcszFile[0]); uFilePathLength = snprintf( szFile, sizeof(szFile), "%s\\%s", F_UI_USER_DATA_FOLDER, pcszFile ); KGLOG_PROCESS_ERROR(uFilePathLength > 0); szFile[sizeof(szFile) - 1] = '\0'; FormatFilePath(szFile); szPath[0] = '\0'; for (i = uFilePathLength; i >= 0; i--) { if (szFile[i] != '/' && szFile[i] != '\\') continue; ASSERT(i < sizeof(szPath)); strncpy(szPath, szFile, i); szPath[i] = '\0'; break; } nRetCode = KUiConfig::IsFilePathExist(szPath); if (!nRetCode) { nRetCode = KUiConfig::CreatePath(szPath); KGLOG_PROCESS_ERROR(nRetCode); } piFile = g_CreateFile(szFile); KGLOG_PROCESS_ERROR(piFile); nRetCode = piFile->Write(pcsBuff, (unsigned long)uBuffSize); KGLOG_PROCESS_ERROR(nRetCode == uBuffSize); Exit0: SAFE_RELEASE(piFile); return 0; }
//保存打包文件信息 bool KPackFilePartner::Save(const char* pFileName, unsigned int uPackTime, unsigned int uPackCRC) { int nResult = false; m_uPackTime = uPackTime; m_uCRC = uPackCRC; KSmartFile file; file = g_CreateFile(pFileName); if (!file) return false; #define MAX_BUFF_SIZE 10240 int nElemIndex; struct tm* pFormatTime = NULL; struct tm t = { 0 }; char line[MAX_BUFF_SIZE + 512]; int nPos = 0; pFormatTime = localtime((time_t *)&uPackTime); if (pFormatTime == NULL) pFormatTime = &t; nPos += sprintf((line + nPos), LINE_FORMAT_FIRST, m_nElemCount, pFormatTime->tm_year + 1900, pFormatTime->tm_mon + 1, pFormatTime->tm_mday, pFormatTime->tm_hour, pFormatTime->tm_min, pFormatTime->tm_sec, m_uPackTime, m_uCRC); nPos += sprintf((line + nPos), LINE_FORMAT_SECOND); bool bResult = true; for (nElemIndex = 0; nElemIndex < m_nElemCount; ++nElemIndex) { PACKPARTNER_ELEM_INFO& info = m_pElemInfoList[nElemIndex]; pFormatTime = localtime((time_t *)(&info.uTime)); if (pFormatTime == NULL) pFormatTime = &t; nPos += sprintf((line + nPos), LINE_FORMAT_OTHERS, info.nElemIndex, info.uId, pFormatTime->tm_year + 1900, pFormatTime->tm_mon + 1, pFormatTime->tm_mday, pFormatTime->tm_hour, pFormatTime->tm_min, pFormatTime->tm_sec, info.szFileName, info.uSize, (info.uStoreSizeAndCompressFlag & XPACK_COMPRESS_SIZE_FILTER), (info.uStoreSizeAndCompressFlag >> XPACK_COMPRESS_SIZE_BIT), info.uCRC); if (nPos >= MAX_BUFF_SIZE) { if (file->Write(line, nPos) != nPos) { nPos = 0; bResult = false; break; } nPos = 0; } } if (nPos > 0) { if (file->Write(line, nPos) != nPos) bResult = false; } file->Close(); return bResult; }