在Windows mobile 5.0下操作INI文件

   对于Windows mobile 5.0来说没有像window那样操作INI文件的API,所以只能自已来实现。其实操作INI文件就是操作普通的文本文件,只是要麻烦一些。以下是我实现的一些常用的操作的函数:
1。读INI
BOOL ReadINIFile(const wchar_t* pszSection,
                                     const wchar_t* pszKey,
                                       wchar_t* pszValue)
{
   ZeroMemory(chValue,ulValueLength);
  HANDLE hFile = ::CreateFile(L"ini文件所在路",
   GENERIC_READ,
   FILE_SHARE_READ,
   NULL,
   OPEN_EXISTING,
   FILE_ATTRIBUTE_NORMAL,
   NULL);
  if (hFile == INVALID_HANDLE_VALUE)
  {
   break;
  }
  if(hFile != NULL)
  {
   CloseHandle(hFile);
   return FLASE;
  }

 CFile iniFile;
 PBYTE pFileBuf;
 CString szBuf;
 DWORD dwLength;

  if(!iniFile.Open(lpFileName, CFile::modeRead))
  return FLASE;

 dwLength = (DWORD)iniFile.GetLength();
 if (dwLength == 0)
  return 0;
 pFileBuf = new BYTE[dwLength + 2];
 if (pFileBuf == NULL)
  return 0;
 memset(pFileBuf, 0x0, dwLength + 2);
 iniFile.Read((void *)pFileBuf, dwLength);
 iniFile.Close();

 if (pFileBuf[0] == 0xFF && pFileBuf[1] == 0xFE)
  szBuf = (LPCWSTR)(pFileBuf + 2);
 else
 {
  PTCHAR pszWideChar = new TCHAR[dwLength + 1];
  MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pFileBuf, dwLength, pszWideChar, dwLength);
  szBuf = pszWideChar;
  delete pszWideChar;
 }
 delete pFileBuf;

 while (1)
 {
  CString szTemp;
  int nPos;
  if (szBuf.IsEmpty()) 
   return FLASE;

  nPos = szBuf.FindOneOf(TEXT("\r\n"));
  if (nPos == -1)
  {
   szTemp = szBuf;
   szBuf.Empty();
  }
  else
  {
   szTemp = szBuf.Left(nPos);
   szBuf = szBuf.Right(szBuf.GetLength() - nPos);
   szBuf.TrimLeft(TEXT("\r\n"));
  }

  szTemp.TrimLeft(TEXT("\t "));
  szTemp.TrimRight(TEXT("\t "));
  if (szTemp.GetAt(0) == TEXT('[') &&
   szTemp.GetAt(szTemp.GetLength() - 1) == TEXT(']'))
  {
   szTemp = szTemp.Right(szTemp.GetLength() - 1);
   szTemp = szTemp.Left(szTemp.GetLength() - 1);
   if (lpAppName == NULL)
   {
    memcpy(lpReturnedString + nSize, (LPCTSTR)szTemp, (szTemp.GetLength() + 1)* sizeof(TCHAR));
    nSize += szTemp.GetLength() + 1;                   
   }
   else if (szTemp.Compare(lpAppName) == 0)
   {
    while (1)
    {
     if (szBuf.IsEmpty())
      goto _GetPrivateProfileString_EXIT;

     nPos = szBuf.FindOneOf(TEXT("\r\n"));
     if (nPos == -1)
     {
      szTemp = szBuf;
      szBuf.Empty();
     }
     else
     {
      szTemp = szBuf.Left(nPos);
      szBuf = szBuf.Right(szBuf.GetLength() - nPos);
      szBuf.TrimLeft(TEXT("\r\n"));
     }

     nPos = szTemp.Find(TEXT("="));
     if (nPos == -1) 
      return FALSE;
     CString szTemp1;
     szTemp1 = szTemp.Left(nPos);
     szTemp1.TrimLeft(TEXT("\t "));
     szTemp1.TrimRight(TEXT("\t "));
     if (lpKeyName == NULL)
     {
      memcpy(lpReturnedString + nSize, (LPCTSTR)szTemp1, (szTemp1.GetLength() + 1)* sizeof(TCHAR));
      nSize += szTemp1.GetLength() + 1;                   
     }
     if (szTemp1.Compare(lpKeyName) == 0)
     {
      szTemp1 = szTemp.Right(szTemp.GetLength() - nPos - 1);
      szTemp1.TrimLeft(TEXT("\t "));
      szTemp1.TrimRight(TEXT("\t "));
      memcpy(lpReturnedString + nSize, (LPCTSTR)szTemp1, (szTemp1.GetLength() + 1)* sizeof(TCHAR));
      nSize += szTemp1.GetLength() + 1;
      return FALSE;
     }

    }
  return TRUE;
}

2。写INI
BOOL WriteInIFile(CString strSection,CString strEntry,CString strValue)
{
  
 if(strSection == L"" || strEntry == L"" || strValue == L"")
 {
  return FLASE;
 }
 CFile    iniFile;
 CString  strCombine;

 if(!iniFile.Open(L"ini文件所在路", CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate))
 {
     return FLASE;
 }
 DWORD dwFilesize = iniFile.GetLength() / 2 + 1; 
 WCHAR  *pBuf;
 try
 {
     pBuf = new WCHAR[dwFilesize];
 }
 catch(...)
 {
     iniFile.Close();
     return FALSE;
 }
  
 if(iniFile.Read(pBuf, iniFile.GetLength()) != iniFile.GetLength())
 {
     delete[]  pBuf;
     iniFile.Close();
     return FALSE;
 }
 
 pBuf[iniFile.GetLength() / 2] = NULL;
 strCombine.GetBuffer(MAX_LEN);
 strCombine = pBuf;
 delete[]   pBuf;
 int iIndex1, iIndex2, iIndex3, iIndexT;   
 iIndex1 = strCombine.Find(L"[" + strSection + L"]\r\n");
 if(iIndex1 == -1) 
 {
  strCombine += L"[" + strSection + L"]" + L"\r\n"
            + strEntry + L"=" + strValue + L"\r\n";
  LPTSTR  lpCombine = strCombine.GetBuffer(0);
  iniFile.SetLength(0);
  iniFile.SeekToBegin();
  iniFile.Write(lpCombine, strCombine.GetLength() * 2);
  iniFile.Close();
  return FLASE;
 }
 iIndexT = iIndex1 + 4 + strSection.GetLength();
 iIndex2 = strCombine.Find(strEntry + L"=", iIndexT);
 if(iIndex2 == -1)
 {
  strCombine.Insert(iIndexT, strEntry + L"=" + strValue + L"\r\n");
  LPTSTR  lpCombine = strCombine.GetBuffer(0);
  iniFile.SetLength(0);
  iniFile.SeekToBegin();
  iniFile.Write(lpCombine, strCombine.GetLength() * 2);
  iniFile.Close();
  return TRUE;
 }
 else
 {
  iIndex3 = strCombine.Find(L"\r\n", iIndex2 + 1);
  if(iIndex3 == -1)
  {
   iniFile.Close();
   return FLASE;
  }
  iIndexT = iIndex2 + 1 + strEntry.GetLength();
  strCombine.Delete(iIndexT, iIndex3 - iIndexT);
  strCombine.Insert(iIndexT, strValue);
  LPTSTR  lpCombine = strCombine.GetBuffer(0);
  iniFile.SetLength(0); 
  iniFile.SeekToBegin();
  iniFile.Write(lpCombine, strCombine.GetLength() * 2);
  iniFile.Close();
  return TRUE;
 }

 iniFile.Close(); 
 return FLASE;

}

3。替换INI值
BOOL ReplaceInIVal(LPCTSTR lpFolderName,LPCTSTR lpKey,LPCTSTR lpValue)
{

 if(lpFolderName==NULL||lpFolderName == NULL || lpFolderName == NULL)
 {
  return HT_ERROR;
 }
 CFile    iniFile;
 CString  strCombine;

 if(!iniFile.Open(L"ini文件所在路", CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate))
 {
  return FALSE;
 }
 DWORD dwFilesize = iniFile.GetLength() / 2 + 1; 
 WCHAR  *pBuf;
 try
 {
  pBuf = new WCHAR[dwFilesize];
 }
 catch(...)
 {
  iniFile.Close();
  return FALSE;
 }

 if(iniFile.Read(pBuf, iniFile.GetLength()) != iniFile.GetLength())
 {
  delete[]  pBuf;
  iniFile.Close();
  return FALSE;
 }

 pBuf[iniFile.GetLength() / 2] = NULL;
 strCombine.GetBuffer(MAX_LEN);
 strCombine = pBuf;
 delete[]   pBuf;
 int iIndex1, iIndex2, iIndex3,iIndex4, iIndexT;   
 iIndex1 = strCombine.Find(CString(lpFolderName));
 if(iIndex1 == -1) 
 {
  iniFile.Close();
  return FALSE;
 }
 
 iIndex2 = strCombine.Find(CString(lpKey), iIndex1);
 if(iIndex2 == -1)
 {
  iniFile.Close();
  return FALSE;
 }
 iIndex3=strCombine.Find(L"=", iIndex2);
 if(iIndex3 == -1)
 {
  iniFile.Close();
  return FALSE;
 }
 else
 {
  iIndex4 = strCombine.Find(L"\r\n", iIndex3);
  if(iIndex4 == -1)
  {
   iniFile.Close();
   return FALSE;
  }
  strCombine.Delete(iIndex3+1, iIndex4- iIndex3-1);
  strCombine.Insert(iIndex3+1, CString(lpValue));
  LPTSTR  lpCombine = strCombine.GetBuffer(0);
  iniFile.SetLength(0); 
  iniFile.SeekToBegin();
  iniFile.Write(lpCombine, strCombine.GetLength() * 2);
  iniFile.Close();
  return TRUE;
 }

 iniFile.Close(); 
 return FALSE;

}

4.获取section数量
int GetINISectionNum(LPCTSTR lpFileName)
{
 CFile iniFile;
 PBYTE pFileBuf;
 CString szBuf;
 DWORD dwLength;
 int iIniNum=0;
 if (lpFileName == NULL)
  return 0;

 if(!iniFile.Open(lpFileName, CFile::modeRead))
  return 0;

 dwLength = (DWORD)iniFile.GetLength();
 if (dwLength == 0)
  return 0;
 pFileBuf = new BYTE[dwLength + 2];
 if (pFileBuf == NULL)
  return 0;
 memset(pFileBuf, 0x0, dwLength + 2);
 iniFile.Read((void *)pFileBuf, dwLength);
 iniFile.Close();

 if (pFileBuf[0] == 0xFF && pFileBuf[1] == 0xFE)
  szBuf = (LPCWSTR)(pFileBuf + 2);
 else
 {
  PTCHAR pszWideChar = new TCHAR[dwLength + 1];
  MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pFileBuf, dwLength, pszWideChar, dwLength);
  szBuf = pszWideChar;
  delete pszWideChar;
 }
 delete pFileBuf;

 CString szTemp;
 int nPos;
 if (szBuf.IsEmpty()) 
  retrun -1;
 nPos = szBuf.FindOneOf(TEXT("\r\n"));
 if (nPos == -1)
 {
  return -1;
 }

 while (1)
 {
  szTemp = szBuf.Left(nPos);
  szBuf = szBuf.Right(szBuf.GetLength() - nPos);
  szBuf.TrimLeft(TEXT("\r\n"));

  szTemp.TrimLeft(TEXT("\t "));
  szTemp.TrimRight(TEXT("\t "));
  if(szTemp.IsEmpty())
   break;
  if (szTemp.GetAt(0) == TEXT('[') &&
   szTemp.GetAt(szTemp.GetLength() - 1) == TEXT(']'))
  {
   iIniNum++;
  }
  nPos = szBuf.FindOneOf(TEXT("\r\n"));
 }
 return iIniNum;
 
 return -1;
}

你可能感兴趣的:(Windows Mobile)