在Windows mobile 5.0下操作INI檔案

來源:互聯網
上載者:User

   對於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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.