📄 jxinifile.cpp
字号:
for(int i = begin; i <= end; i+=sizeof(WCHAR))
{
::memcpy(pHead + i/sizeof(WCHAR) - charDiff,
pHead + i/sizeof(WCHAR), sizeof(WCHAR));
}
}
pValue = pHead + uKey;
::memcpy(pValue, buffer, sizeof(WCHAR)*uCurLen);
m_dwUseSize += (uCurLen-uPrevLen)*sizeof(WCHAR);
delete buffer;
buffer = NULL;
bRet = TRUE;
}
m_bSectionMapDirty = TRUE;
if(!bRet)
{
UnloadIniFile();
return FALSE;
}
//If the File is large enough, flush the file
if((int)(m_dwUseSize - m_dwSize) > JXFlushIncrement - 5*JXPage)
{
//Flush Back to HardDisk
UnloadIniFile();
}
return TRUE;
}
//User Query All Section Name in INI File
DWORD CJXINIFile::GetSectionsW(
LPWSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCWSTR lpFileName // initialization file name
)
{
int posRet = 0;
LPWSTR pHead = (LPWSTR)m_pTrueData;
LPWSTR p1 = pHead;
LPWSTR p2 = pHead;
WORD wNULL = 0x0000;
while(p1- pHead < (int)(m_dwUseSize/sizeof(WCHAR)) - 1)
{
while(p1- pHead < (int)(m_dwUseSize/sizeof(WCHAR)) - 1 &&
*p1 != WCHAR('['))
p1++;
p2 = p1;
while(p2- pHead < (int)(m_dwUseSize/sizeof(WCHAR)) - 1 &&
*p2 != WCHAR(']'))
p2++;
if(p1- pHead < (int)(m_dwUseSize/sizeof(WCHAR)) &&
p2- pHead < (int)(m_dwUseSize/sizeof(WCHAR)))
{
//Found, Ready To Copy To lpReturnedString
int lenSection = p2 - p1 - 1;
if(lenSection >= 1)
{
p1++;
if(lenSection + 1 + posRet > (int)nSize) return 0; //nSize - 1;
::wcsncpy(lpReturnedString + posRet, p1, lenSection);
posRet += lenSection;
//NULL = 0x00 00, 2 Byte
::memcpy(lpReturnedString + posRet, &wNULL, 2);
posRet += 1;
}
}
p1 = p2 + 1;
}
//last NULL
if(1 + posRet > (int)nSize) return 0; //nSize - 1;
::memcpy(lpReturnedString + posRet, &wNULL, 2);
posRet += 1;
return posRet;
}
//User Query All Keys in a Given Section
DWORD CJXINIFile::GetKeysW(
LPCWSTR lpAppName, // section name
LPWSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCWSTR lpFileName // initialization file name
)
{
int posRet = 0;
int secLen = ::wcslen(lpAppName);
LPWSTR pHead = (LPWSTR)m_pTrueData;
LPWSTR p1 = pHead;
LPWSTR p2 = pHead;
LPWSTR pSecHead = NULL; //pointing to [
LPWSTR pSecTail = NULL; //point to next [
WORD wNULL = 0x0000;
while(p1- pHead < (int)(m_dwUseSize/sizeof(WCHAR)) - 1)
{
while(p1- pHead < (int)(m_dwUseSize/sizeof(WCHAR)) - 1 &&
*p1 != WCHAR('['))
p1++;
p2 = p1;
while(p2- pHead < (int)(m_dwUseSize/sizeof(WCHAR)) - 1 &&
*p2 != WCHAR(']'))
p2++;
//p2 points to ]
if(p1- pHead < (int)(m_dwUseSize/sizeof(WCHAR)) &&
p2- pHead < (int)(m_dwUseSize/sizeof(WCHAR)))
{
//Found A Section
if(pSecHead != NULL)
{
pSecTail = p1;
break;
}
else //pSecHead == NULL
{
if(secLen == p2 - p1 - 1) //same length
{
p1++;
LPWSTR p3 = p1; //p3 points to ["X"
for(int i = 0; i < secLen; i++)
{
if(*p1 != (WCHAR)(lpAppName[i])) break;
p1++;
}
if(i == secLen) //Found;
pSecHead = p3 - 1;
}
}
}
p1 = p2 + 1;
}
if(pSecHead == NULL) return -1;
if(pSecTail == NULL)
pSecTail = pHead + m_dwUseSize/sizeof(WCHAR);
pSecTail--;
//Enum Keys in Section
p1 = pSecHead;
int keyLen;
while(p1 < pSecTail - 1)
{
while(p1 < pSecTail - 1 &&
*p1 != WCHAR('\r') && *p1 != WCHAR('\n'))
p1++;
while(p1 < pSecTail - 1 &&
(*p1 == WCHAR('\r') || *p1 == WCHAR('\n')))
p1++;
p2 = p1;
while(p2 < pSecTail - 1 &&
*p2 != WCHAR('='))
p2++;
if(p1 < pSecTail - 1 && p2 < pSecTail - 1)
{
//Found A Key
keyLen = p2 - p1;
if(keyLen >= 1)
{
if(keyLen + 1 + posRet > (int)nSize) return 0; //nSize - 1;
::wcsncpy(lpReturnedString + posRet, p1, keyLen);
posRet += keyLen;
//NULL = 0x00 00, 2 Byte
::memcpy(lpReturnedString + posRet, &wNULL, 2);
posRet += 1;
}
}
p1 = p2 + 1;
}
//last NULL
if(1 + posRet > (int)nSize) return 0; //nSize - 1;
::memcpy(lpReturnedString + posRet, &wNULL, 2);
posRet += 1;
return posRet;
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//ANSI Version ----------------------------------------------
// return 0 -- no key, size too small to put value
//return -1 -- error
//return string length (of course > 1)
DWORD CJXINIFile::GetPrivateProfileStringA(
LPCSTR lpAppName, // section name
LPCSTR lpKeyName, // key name
LPCSTR lpDefault, // default string
LPSTR lpReturnedString, // destination buffer
DWORD nSize,
// size of destination buffer,Specifies the size, in TCHARs,
LPCSTR lpFileName // initialization file name
)
{
BOOL bRet = LoadIniFile(lpFileName, 1);
if(!bRet) return (UINT)-1;
if(lpDefault == NULL) return -1;
if(m_dwUseSize == 0) return -1;
if(lpAppName == NULL)
return GetSectionsA(lpReturnedString, nSize, lpFileName);
if(lpKeyName == NULL)
return GetKeysA(lpAppName, lpReturnedString, nSize, lpFileName);
LPSTR pHead = (LPSTR)m_pTrueData;
UINT uSec, uKey;
uSec = (UINT) -1;
uKey = (UINT)-1;
uKey = GetKeyA(pHead, (UINT)m_dwUseSize,
lpAppName, lpKeyName, uSec);
if(uKey == (UINT)-1) //not key
{
if(::strlen(lpDefault) > (int)nSize) return -1;
::strncpy(lpReturnedString, lpDefault, ::strlen(lpDefault));
//append a null
lpReturnedString[::strlen(lpDefault)+1] = char('\0');
return 0;
}
char buffer[MAX_PATH];
LPSTR pValue = pHead + uKey;
while(pValue - pHead < (int)((m_dwUseSize)/sizeof(char)) &&
*pValue != char('\r') && *pValue != char('\n'))
{
pValue++; //in unicode, +2 byte
}
int len = pValue - pHead - uKey;
pValue = pHead + uKey;
::strncpy(buffer, pValue, len);
if(nSize <= (UINT)len)
{
::strncpy(lpReturnedString, pValue, nSize);
lpReturnedString[nSize - 1] = char('\0');
return 0;
}
else
{
::strncpy(lpReturnedString, pValue, len);
//append null
lpReturnedString[len] = char('\0');
return len;
}
return (UINT) -1;
}
BOOL CJXINIFile::WritePrivateProfileStringA(
LPCSTR lpAppName, // section name
LPCSTR lpKeyName, // key name
LPCSTR lpString, // string to add
LPCSTR lpFileName // initialization file
)
{
BOOL bRet = this->LoadIniFile(lpFileName, 1);
if(!bRet) return FALSE;
m_bDirty = TRUE;
LPSTR pHead = (LPSTR)m_pTrueData;
//2001/10/19 if lpString.len > MAX_PATH error
//char buffer[MAX_PATH];
LPSTR buffer = NULL;
UINT uSec, uKey;
//Warning: uKey, uSec is measured in WCHARs!!!!!
//in UNICODE now
uKey = GetKeyA(pHead, (UINT)m_dwUseSize,
lpAppName, lpKeyName, uSec);
if(uKey == (UINT)-1 && uSec == (UINT)-1) //no section
{
int len = strlen(lpAppName) + strlen(lpKeyName) +strlen(lpString);
len += 7;
buffer = new char[len+1];
::sprintf(buffer, "[%s]\r\n%s=%s\r\n", lpAppName, lpKeyName, lpString);
buffer[len] = 0;
LPSTR pStr = pHead;
pStr += m_dwUseSize/sizeof(char);
::memcpy(pStr, buffer, len*sizeof(char));
m_dwUseSize += len*sizeof(char);
delete []buffer;
buffer = NULL;
bRet = TRUE;
}
else if(uKey == (UINT)-1 && uSec != (UINT)-1)
//section exist but value do not exist
{
// Following Write Just After [section],
//that new item become the first of the section
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
/*
LPSTR pSection = pHead + uSec;
while(pSection - pHead < (int)(m_dwUseSize/sizeof(char)) &&
*pSection != char('\r') && *pSection != char('\n'))
{
pSection++;
}
while(*pSection == char('\r') || *pSection == char('\n'))
pSection++;
UINT uMoveHead = (UINT)(pSection - pHead);
//move memory
::sprintf(buffer, L"%s=%s\r\n", lpKeyName, lpString);
int len = ::strlen(buffer);
//also ok
//::memmove(pHead + uMoveHead + len, pHead + uMoveHead, m_dwUseSize-uMoveHead*sizeof(char));
for(int i = (int)(m_dwUseSize - sizeof(char));
i >= (int)(uMoveHead*sizeof(char)); i -= sizeof(char))
{
::memcpy(pHead + i/sizeof(char) + len,
pHead + i/sizeof(char), sizeof(char));
}
*/
//Following Write new item the last item in the section
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
LPSTR pSection = pHead + uSec + 1;
while(pSection - pHead < (int)(m_dwUseSize/sizeof(char)) &&
*pSection != char('['))
{
pSection++;
}
// ::sprintf(buffer, L"%s=%s\r\n", lpKeyName, lpString);
// int len = ::strlen(buffer);
int len = strlen(lpKeyName) + strlen(lpString);
len += 3;
buffer = new char[len+1];
::sprintf(buffer, "%s=%s\r\n", lpKeyName, lpString);
buffer[len] = 0;
if(pSection - pHead == (int)(m_dwUseSize/sizeof(char)))
{
//no move needed, file end
}
else
{
UINT uMoveHead = (UINT)(pSection - pHead);
//move memory
//also ok
//::memmove(pHead + uMoveHead + len, pHead + uMoveHead, m_dwUseSize-uMoveHead*sizeof(char));
for(int i = (int)(m_dwUseSize - sizeof(char));
i >= (int)(uMoveHead*sizeof(char)); i -= sizeof(char))
{
::memcpy(pHead + i/sizeof(char) + len,
pHead + i/sizeof(char), sizeof(char));
}
}
::memcpy(pSection, buffer, sizeof(char)*len);
m_dwUseSize += len*sizeof(char);
delete buffer;
buffer = NULL;
bRet = TRUE;
}
else //key exist
{
ASSERT(uKey != (UINT)-1);
LPSTR pValue = pHead + uKey;
while(pValue - pHead < (int)(m_dwUseSize/sizeof(char)) &&
*pValue != char('\r') && *pValue != char('\n'))
{
pValue++;
}
while(*pValue == char('\r') || *pValue == char('\n'))
pValue++;
UINT uMoveHead = (UINT)(pValue - pHead);
UINT uPrevLen = uMoveHead - uKey;
int len = strlen(lpString);
len += 2;
buffer = new char[len+1];
::sprintf(buffer, "%s\r\n", lpString);
buffer[len] = 0;
UINT uCurLen = len;
//move have 2 direction
if(uCurLen > uPrevLen)
{
int charDiff = uCurLen - uPrevLen;
int begin = (int)(m_dwUseSize - sizeof(char));
int end = (int)uMoveHead*sizeof(char);
for(int i = begin; i >= end; i -= sizeof(char))
{
::memcpy(pHead + i/sizeof(char) + charDiff,
pHead + i/sizeof(char), sizeof(char));
}
}
else if(uCurLen < uPrevLen)
{
int charDiff = uPrevLen - uCurLen;
int begin = (int)uMoveHead*sizeof(char);
int end = (int)(m_dwUseSize - sizeof(char));
for(int i = begin; i <= end; i+=sizeof(char))
{
::memcpy(pHead + i/sizeof(char) - charDiff,
pHead + i/sizeof(char), sizeof(char));
}
}
pValue = pHead + uKey;
::memcpy(pValue, buffer, sizeof(char)*uCurLen);
m_dwUseSize += (uCurLen-uPrevLen)*sizeof(char);
delete buffer;
buffer = NULL;
bRet = TRUE;
}
m_bSectionMapDirty = TRUE;
if(!bRet)
{
UnloadIniFile();
return FALSE;
}
//If the File is large enough, flush the file
if((int)(m_dwUseSize - m_dwSize) > JXFlushIncrement - 5*JXPage)
{
//Flush Back to HardDisk
UnloadIniFile();
}
return TRUE;
}
//User Query All Section Name in INI File
DWORD CJXINIFile::GetSectionsA(
LPSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCSTR lpFileName // initialization file name
)
{
int posRet = 0;
LPSTR pHead = (LPSTR)m_pData;
LPSTR p1 = pHead;
LPSTR p2 = pHead;
BYTE wNULL = 0x0000;
while(p1- pHead < (int)(m_dwUseSize/sizeof(char)) - 1)
{
while(p1- pHead < (int)(m_dwUseSize/sizeof(char)) - 1 &&
*p1 != char('['))
p1++;
p2 = p1;
while(p2- pHead < (int)(m_dwUseSize/sizeof(char)) - 1 &&
*p2 != char(']'))
p2++;
if(p1- pHead < (int)(m_dwUseSize/sizeof(char)) &&
p2- pHead < (int)(m_dwUseSize/sizeof(char)))
{
//Found, Ready To Copy To lpReturnedString
int lenSection = p2 - p1 - 1;
if(lenSection >= 1)
{
p1++;
if(lenSection + 1 + posRet > (int)nSize) return 0; //nSize - 1;
::strncpy(lpReturnedString + posRet, p1, lenSection);
posRet += lenSection;
//NULL = 0x00, 1Byte
::memcpy(lpReturnedString + posRet, &wNULL, 1);
posRet += 1;
}
}
p1 = p2 + 1;
}
//last NULL
if(1 + posRet > (int)nSize) return 0; //nSize - 1;
::memcpy(lpReturnedString + posRet, &wNULL, 1);
posRet += 1;
return posRet;
}
//User Query All Keys in a Given Section
DWORD CJXINIFile::GetKeysA(
LPCSTR lpAppName, // section name
LPSTR lpReturnedString, // destination buffer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -