//--------------------------------------------------------------------------- // 函数: GetKeyValue // 功能: 取得Key的Value // 参数: pSection 节名 // pKey 建名 // pValue 建值 // 返回: TRUE-成功 FALSE-失败 //--------------------------------------------------------------------------- BOOL KIniFile::GetKeyValue( LPCSTR pSection, LPCSTR pKey, LPSTR pValue, DWORD dwSize) { DWORD dwID; // setup section name char szSection[32] = "["; if (pSection[0] != '[') { g_StrCat(szSection, pSection); g_StrCat(szSection, "]"); } else { g_StrCpy(szSection, pSection); } // search for the matched section SECNODE* pSecNode = m_Header.pNextNode; dwID = String2Id(szSection); while (pSecNode != NULL) { if (dwID == pSecNode->dwID) { break; } pSecNode = pSecNode->pNextNode; } // if no such section founded if (pSecNode == NULL) { return FALSE; } // search for the same key KEYNODE* pKeyNode = pSecNode->pKeyNode.pNextNode; dwID = String2Id(pKey); while (pKeyNode != NULL) { if (dwID == pKeyNode->dwID) { break; } pKeyNode = pKeyNode->pNextNode; } // if no such key found if (pKeyNode == NULL) { return FALSE; } // copy the value of the key g_StrCpyLen(pValue, pKeyNode->pValue, dwSize); return TRUE; }
//--------------------------------------------------------------------------- // 函数: GetKeyValue // 功能: 取得Key的Value // 参数: pSection 节名 // pKey 建名 // pValue 建值 // 返回: true-成功 false-失败 //--------------------------------------------------------------------------- int QIniFileImpl::GetKeyValue(const char* pSection,const char* pKey,char* pValue,unsigned int dwSize) { unsigned int dwID; // setup section name char szSection[64]; COPY_SECTION_AND_ADD_BOUND(szSection, pSection); // search for the matched section SECNODE* pSecNode = m_Header.pNextNode; dwID = String2Id(szSection); while (pSecNode != NULL) { if (dwID == pSecNode->dwID) { break; } pSecNode = pSecNode->pNextNode; } // if no such section founded if (pSecNode == NULL) { return false; } // search for the same key KEYNODE* pKeyNode = pSecNode->RootNode.pNextNode; dwID = String2Id(pKey); while (pKeyNode != NULL) { if (dwID == pKeyNode->dwID) { break; } pKeyNode = pKeyNode->pNextNode; } // if no such key found if (pKeyNode == NULL) { return false; } // copy the value of the key QStrCpyLen(pValue, pKeyNode->pValue, dwSize); return true; }
BOOL KIniFile::GetNextSection(LPCSTR pSection, LPSTR pNextSection) { if (!pSection[0]) // 传入参数为""时就是取第一个Section { if (!m_Header.pNextNode) return FALSE; else { g_StrCpy(pNextSection, m_Header.pNextNode->pSection); return TRUE; } } else { char szSection[32] = "["; if (pSection[0] != '[') { g_StrCat(szSection, pSection); g_StrCat(szSection, "]"); } else { g_StrCpy(szSection, pSection); } // 查找参数给定的Section SECNODE* pThisSecNode = &m_Header; SECNODE* pNextSecNode = pThisSecNode->pNextNode; DWORD dwID = String2Id(szSection); while (pNextSecNode != NULL) { if (dwID == pNextSecNode->dwID) { break; } pThisSecNode = pNextSecNode; pNextSecNode = pThisSecNode->pNextNode; } // 没有这个Section,无所谓下一个 if (!pNextSecNode) { return FALSE; } else { // 参数给定的Section已经是最后一个了 if (!pNextSecNode->pNextNode) { return FALSE; } else { g_StrCpy(pNextSection, pNextSecNode->pNextNode->pSection); return TRUE; } } } }
//--------------------------------------------------------------------------- // 函数: EraseKey // 功能: 清除Section的下一个Key的内容 // 参数: pSection 节的名字 //--------------------------------------------------------------------------- void QIniFileImpl::EraseKey(const char* lpSection, const char* lpKey) { // setup section name char szSection[64]; COPY_SECTION_AND_ADD_BOUND(szSection, lpSection); // search for the matched section SECNODE* pSecNode = m_Header.pNextNode; unsigned int dwID = String2Id(szSection); while (pSecNode != NULL) { if (dwID == pSecNode->dwID) { break; } pSecNode = pSecNode->pNextNode; } // if no such section found if (pSecNode == NULL) { return; } KEYNODE* pThisKey = &pSecNode->RootNode; KEYNODE* pNextKey = pThisKey->pNextNode; dwID = String2Id(lpKey); while (pNextKey != NULL) { if (pNextKey->dwID == dwID) { pThisKey->pNextNode = pNextKey->pNextNode; SAFE_DELETE_ARRAY(pNextKey->pKey); SAFE_DELETE_ARRAY(pNextKey->pValue); SAFE_DELETE(pNextKey); if (pSecNode->RootNode.pNextNode == NULL) EraseSection(szSection); return; } pThisKey = pNextKey; pNextKey = pNextKey->pNextNode; } }
//--------------------------------------------------------------------------- //--------------------------------------------------------------------------- int QIniFileImpl::GetNextSection(const char* pSection, char* pNextSection) { if (!pSection[0]) // 传入参数为""时就是取第一个Section { if (!m_Header.pNextNode) return false; else { COPY_SECTION_AND_REMOVE_BOUND(pNextSection, m_Header.pNextNode->pSection); return true; } } else { char szSection[64]; COPY_SECTION_AND_ADD_BOUND(szSection, pSection); // 查找参数给定的Section SECNODE* pThisSecNode = &m_Header; SECNODE* pNextSecNode = pThisSecNode->pNextNode; unsigned int dwID = String2Id(szSection); while (pNextSecNode != NULL) { if (dwID == pNextSecNode->dwID) { break; } pThisSecNode = pNextSecNode; pNextSecNode = pThisSecNode->pNextNode; } // 没有这个Section,无所谓下一个 if (!pNextSecNode) { return false; } else { // 参数给定的Section已经是最后一个了 if (!pNextSecNode->pNextNode) { return false; } else { COPY_SECTION_AND_REMOVE_BOUND(pNextSection, pNextSecNode->pNextNode->pSection); return true; } } } }
//--------------------------------------------------------------------------- // 函数: IsSectionExist // 功能: Section是否存在 // 参数: pSection 节的名字 // 返回: int //--------------------------------------------------------------------------- int QIniFileImpl::IsSectionExist(const char* pSection) { // setup section name char szSection[64]; COPY_SECTION_AND_ADD_BOUND(szSection, pSection); // search for the matched section SECNODE* pSecNode = m_Header.pNextNode; unsigned int dwID = String2Id(szSection); while (pSecNode != NULL) { if (dwID == pSecNode->dwID) return true; pSecNode = pSecNode->pNextNode; } return false; }
//--------------------------------------------------------------------------- // 函数: EraseSection // 功能: 清除一个Section的内容 // 参数: pSection 节的名字 //--------------------------------------------------------------------------- void QIniFileImpl::EraseSection(const char* pSection) { // setup section name char szSection[64]; COPY_SECTION_AND_ADD_BOUND(szSection, pSection); // search for the matched section unsigned int dwID = String2Id(szSection); SECNODE* pPreSec = &m_Header; SECNODE* pSecNode; while((pSecNode = pPreSec->pNextNode) != NULL) { if (dwID == pSecNode->dwID) break; pPreSec = pSecNode; } // if no such section found if (pSecNode == NULL) return; pPreSec->pNextNode = pSecNode->pNextNode; // erase all key in the section KEYNODE* pThisKey = &pSecNode->RootNode; KEYNODE* pNextKey = pThisKey->pNextNode; while (pNextKey != NULL) { pThisKey = pNextKey->pNextNode; SAFE_DELETE_ARRAY(pNextKey->pKey); SAFE_DELETE_ARRAY(pNextKey->pValue); SAFE_DELETE(pNextKey); pNextKey = pThisKey; } pSecNode->RootNode.pNextNode = NULL; SAFE_DELETE_ARRAY(pSecNode->pSection); SAFE_DELETE(pSecNode); }
BOOL KIniFile::GetNextKey(LPCSTR pSection, LPCSTR pKey, LPSTR pNextKey) { char szSection[32] = "["; if (pSection[0] != '[') { g_StrCat(szSection, pSection); g_StrCat(szSection, "]"); } else { g_StrCpy(szSection, pSection); } // 查找参数给定的Section SECNODE* pThisSecNode = &m_Header; SECNODE* pNextSecNode = pThisSecNode->pNextNode; DWORD dwID = String2Id(szSection); while (pNextSecNode != NULL) { if (dwID == pNextSecNode->dwID) { break; } pThisSecNode = pNextSecNode; pNextSecNode = pThisSecNode->pNextNode; } // 没有这个Section,无所谓取Key if (!pNextSecNode) { return FALSE; } KEYNODE* pThisKeyNode = &pNextSecNode->pKeyNode; KEYNODE* pNextKeyNode = pThisKeyNode->pNextNode; // Key给的参数为""的话,取第一个Key if (!pKey[0]) { // 没有Key,返回失败 if (!pNextKeyNode->pKey) { return FALSE; } else { g_StrCpy(pNextKey, pNextKeyNode->pKey); return TRUE; } } dwID = String2Id(pKey); while(pNextKeyNode != NULL) { if (dwID == pNextKeyNode->dwID) { break; } pThisKeyNode = pNextKeyNode; pNextKeyNode = pThisKeyNode->pNextNode; } // 找不到所给的Key,无所谓下一个 if (!pNextKeyNode) { return FALSE; } // 参数给定的Key已经是最后一个了 if (!pNextKeyNode->pNextNode) { return FALSE; } else { g_StrCpy(pNextKey, pNextKeyNode->pNextNode->pKey); return TRUE; } }
//--------------------------------------------------------------------------- // 函数: SetKeyValue // 功能: 设置Key的Value // 参数: pSection 节名 // pKey 建名 // pValue 建值 // 返回: TRUE-成功 FALSE-失败 //--------------------------------------------------------------------------- BOOL KIniFile::SetKeyValue( LPCSTR pSection, LPCSTR pKey, LPCSTR pValue) { int nLen; DWORD dwID; // setup section name char szSection[32] = "["; if (pSection[0] != '[') { g_StrCat(szSection, pSection); g_StrCat(szSection, "]"); } else { g_StrCpy(szSection, pSection); } // search for the matched section SECNODE* pThisSecNode = &m_Header; SECNODE* pNextSecNode = pThisSecNode->pNextNode; dwID = String2Id(szSection); while (pNextSecNode != NULL) { if (dwID == pNextSecNode->dwID) { break; } pThisSecNode = pNextSecNode; pNextSecNode = pThisSecNode->pNextNode; } // if no such section found create a new section if (pNextSecNode == NULL) { nLen = g_StrLen(szSection) + 1; pNextSecNode = (SECNODE *)m_MemStack.Push(sizeof(SECNODE)); pNextSecNode->pSection = (char *)m_MemStack.Push(nLen); g_MemCopy(pNextSecNode->pSection, szSection, nLen); pNextSecNode->dwID = dwID; pNextSecNode->pKeyNode.pNextNode = NULL; pNextSecNode->pNextNode = NULL; pThisSecNode->pNextNode = pNextSecNode; } // search for the same key KEYNODE* pThisKeyNode = &pNextSecNode->pKeyNode; KEYNODE* pNextKeyNode = pThisKeyNode->pNextNode; dwID = String2Id(pKey); while (pNextKeyNode != NULL) { if (dwID == pNextKeyNode->dwID) { break; } pThisKeyNode = pNextKeyNode; pNextKeyNode = pThisKeyNode->pNextNode; } // if no such key found create a new key if (pNextKeyNode == NULL) { pNextKeyNode = (KEYNODE *)m_MemStack.Push(sizeof(KEYNODE)); nLen = g_StrLen(pKey) + 1; pNextKeyNode->pKey = (char *)m_MemStack.Push(nLen); g_MemCopy(pNextKeyNode->pKey, (void*)pKey, nLen); nLen = g_StrLen(pValue) + 1; pNextKeyNode->pValue = (char *)m_MemStack.Push(nLen); g_MemCopy(pNextKeyNode->pValue, (void*)pValue, nLen); pNextKeyNode->dwID = dwID; pNextKeyNode->pNextNode = NULL; pThisKeyNode->pNextNode = pNextKeyNode; } // replace the old value with new else { m_MemStack.Free(pNextKeyNode->pValue); nLen = g_StrLen(pValue) + 1; pNextKeyNode->pValue = (char *)m_MemStack.Push(nLen); g_MemCopy(pNextKeyNode->pValue, (void*)pValue, nLen); } return TRUE; }
//--------------------------------------------------------------------------- // 函数: SetKeyValue // 功能: 设置Key的Value // 参数: pSection 节名 // pKey 建名 // pValue 建值 // 返回: true-成功 false-失败 //--------------------------------------------------------------------------- int QIniFileImpl::SetKeyValue( const char* pSection, const char* pKey, const char* pValue) { int nLen; unsigned int dwID; // setup section name char szSection[64]; COPY_SECTION_AND_ADD_BOUND(szSection, pSection); // search for the matched section SECNODE* pThisSecNode = &m_Header; SECNODE* pNextSecNode = pThisSecNode->pNextNode; dwID = String2Id(szSection); while (pNextSecNode != NULL) { if (dwID == pNextSecNode->dwID) { break; } pThisSecNode = pNextSecNode; pNextSecNode = pThisSecNode->pNextNode; } // if no such section found create a new section if (pNextSecNode == NULL) { nLen = strlen(szSection) + 1; pNextSecNode = new SECNODE; pNextSecNode->pSection = new char[nLen]; memcpy(pNextSecNode->pSection, szSection, nLen); pNextSecNode->dwID = dwID; pNextSecNode->RootNode.pNextNode = NULL; pNextSecNode->pNextNode = NULL; pThisSecNode->pNextNode = pNextSecNode; } // search for the same key KEYNODE* pThisKeyNode = &pNextSecNode->RootNode; KEYNODE* pNextKeyNode = pThisKeyNode->pNextNode; dwID = String2Id(pKey); while (pNextKeyNode != NULL) { if (dwID == pNextKeyNode->dwID) { break; } pThisKeyNode = pNextKeyNode; pNextKeyNode = pThisKeyNode->pNextNode; } // if no such key found create a new key if (pNextKeyNode == NULL) { pNextKeyNode = new KEYNODE; nLen = strlen(pKey) + 1; pNextKeyNode->pKey = new char[nLen]; memcpy(pNextKeyNode->pKey, (void*)pKey, nLen); nLen = strlen(pValue) + 1; pNextKeyNode->pValue = new char[nLen]; memcpy(pNextKeyNode->pValue, (void*)pValue, nLen); pNextKeyNode->dwID = dwID; pNextKeyNode->pNextNode = NULL; pThisKeyNode->pNextNode = pNextKeyNode; } // replace the old value with new else { SAFE_DELETE_ARRAY(pNextKeyNode->pValue); nLen = strlen(pValue) + 1; pNextKeyNode->pValue = new char[nLen]; memcpy(pNextKeyNode->pValue, (void*)pValue, nLen); } return true; }
//--------------------------------------------------------------------------- //--------------------------------------------------------------------------- int QIniFileImpl::GetNextKey(const char* pSection, const char* pKey, char* pNextKey) { char szSection[64]; COPY_SECTION_AND_ADD_BOUND(szSection, pSection); // 查找参数给定的Section SECNODE* pThisSecNode = &m_Header; SECNODE* pNextSecNode = pThisSecNode->pNextNode; unsigned int dwID = String2Id(szSection); while (pNextSecNode != NULL) { if (dwID == pNextSecNode->dwID) { break; } pThisSecNode = pNextSecNode; pNextSecNode = pThisSecNode->pNextNode; } // 没有这个Section,无所谓取Key if (!pNextSecNode) { return false; } KEYNODE* pThisKeyNode = &pNextSecNode->RootNode; KEYNODE* pNextKeyNode = pThisKeyNode->pNextNode; // Key给的参数为""的话,取第一个Key if (!pKey[0]) { // 没有Key,返回失败 if (!pNextKeyNode->pKey) { return false; } else { strcpy(pNextKey, pNextKeyNode->pKey); return true; } } dwID = String2Id(pKey); while(pNextKeyNode != NULL) { if (dwID == pNextKeyNode->dwID) { break; } pThisKeyNode = pNextKeyNode; pNextKeyNode = pThisKeyNode->pNextNode; } // 找不到所给的Key,无所谓下一个 if (!pNextKeyNode) { return false; } // 参数给定的Key已经是最后一个了 if (!pNextKeyNode->pNextNode) { return false; } else { strcpy(pNextKey, pNextKeyNode->pNextNode->pKey); return true; } }