WinCE下读Ini文件函数

1 篇文章 0 订阅


对于WinCE下读取Ini配置文件的时候,没有类似于XP桌面系统上的API函数 GetPrivateProfileString 等函数。要想对于INI配置文件的读写就要自己来实现这个函数。其实这个函数在WinCE系统开放出来的源码中已经有实现。自己已经稍稍的进行了改动,可以直接在工程中导入下来2个文件就使用这2个函数了。

在配置文件中 =( 等号) 两侧都不能有空格。 

代码如下:

iniutil.h文件

//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//

#define lengthof(x) ( (sizeof((x))) / (sizeof(*(x))) )


BOOL ReadIni(LPCWSTR pwszSection, LPCWSTR pwszKey, LPWSTR pwszValue, size_t cchValue, LPCWSTR pwszIniFile);
BOOL WriteIni(LPCWSTR pwszSection, LPCWSTR pwszKey, LPWSTR pwszValue, size_t cchValue, LPCWSTR pwszIniFile);


 

 

 iniutil.cpp文件


 

//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "stdafx.h"
#include "iniutil.h"


BOOL ReadIni(LPCWSTR pwszSection, LPCWSTR pwszKey, LPWSTR pwszValue, size_t cchValue, LPCWSTR pwszIniFile)
{
   // GetPrivateProfileString is unavailable on CE

   BOOL bSuccess = FALSE;
   FILE * fin;
   WCHAR wszLine[256];
   LPWSTR pwsz = NULL;

   if (!pwszIniFile ||
      !pwszSection ||
      !pwszKey ||
      !pwszValue ||
      !cchValue)
   {
      goto leave;
   }

   fin = _wfopen(pwszIniFile, L"r");
   if (fin)
   {
      while (fgetws(wszLine, lengthof(wszLine), fin))
      {
         if (!wcsstr(wszLine, pwszSection)) // Section
         {
            continue;
         }

         while (fgetws(wszLine, lengthof(wszLine), fin)) // Key & Value
         {
            pwsz = wcschr(wszLine, L'=');
            if (!pwsz)
            {
               continue;
            }

            *pwsz = L'\0'; // Erase the '='

            if (0 != _wcsicmp(wszLine, pwszKey)) // Key
            {
               continue;
            }

            pwsz++; // Point to the value

            bSuccess = SUCCEEDED(StringCchCopyEx(
                                    pwszValue,
                                    cchValue,
                                    pwsz,
                                    &pwsz,
                                    NULL,
                                    0
                                    ));

            if (bSuccess)
            {
               if (*--pwsz == L'\n')
               {
                  *pwsz = L'\0';
               }
            }
         }
      }

      fclose(fin);
   }

leave:
   return bSuccess;
}


BOOL WriteIni(LPCWSTR pwszSection, LPCWSTR pwszKey, LPWSTR pwszValue, size_t cchValue, LPCWSTR pwszIniFile)
{
   // WritePrivateProfileString is unavailable on CE

   BOOL bSuccess = FALSE;
   BOOL bReplace;
   FILE * fin;
   fpos_t posSrc;
   fpos_t posDest;
   size_t cchLine;
   size_t cchNewLine;
   WCHAR wszLine[256];
   WCHAR wszNewLine[256];
   LPWSTR pwsz = NULL;
   DWORD dwAttrib;

   if (!pwszIniFile ||
      !pwszSection ||
      !pwszKey ||
      (!pwszValue && cchValue) ||
      (cchValue >= lengthof(wszLine)))
   {
      goto leave;
   }

   dwAttrib = GetFileAttributes(pwszIniFile);
   if (dwAttrib == -1)
   {
      goto leave;
   }

   SetFileAttributes(pwszIniFile, dwAttrib & ~FILE_ATTRIBUTE_READONLY);

   fin = _wfopen(pwszIniFile, L"r+");
   if (fin)
   {
      bReplace = FALSE;
      cchNewLine = 0;

      while (fgetws(wszLine, lengthof(wszLine), fin))
      {
         if (!wcsstr(wszLine, pwszSection)) // Section
            continue;

         if (fgetpos(fin, &posSrc) || fgetpos(fin, &posDest))
         {
            goto closeFile;
         }

         while (fgetws(wszLine, lengthof(wszLine), fin)) // Key & Value
         {
            cchLine = wcslen(wszLine);

            pwsz = wcschr(wszLine, L'=');
            if (!pwsz)
            {
               goto nextLine;
            }

            *pwsz = L'\0'; // Erase the '='

            if (0 != _wcsicmp(wszLine, pwszKey)) // Key
            {
               *pwsz = L'='; // Add the '=' back
               goto nextLine;
            }

            *pwsz = L'='; // Add the '=' back
            pwsz++; // Point to the value

            // Clear Key & Value on NULL or empty string 
            if (!pwszValue || !cchValue)
            {
               bReplace = TRUE;
               cchLine = 0;
               goto nextLine;
            }

            // Do not overwritte more than the number of read characters.
            size_t cchKey = (pwsz - wszLine);
            if (SUCCEEDED(StringCchCopyNEx(
                                    wszNewLine,
                                    lengthof(wszNewLine),
                                    wszLine,
                                    cchKey,
                                    &pwsz,
                                    &cchNewLine,
                                    0
                                    )) &&
               SUCCEEDED(StringCchCopyNEx(
                                    pwsz,
                                    cchNewLine,
                                    pwszValue,
                                    min((cchLine - cchKey), cchValue),
                                    &pwsz,
                                    &cchNewLine,
                                    STRSAFE_IGNORE_NULLS
                                    )))
            {
               bReplace = TRUE;

               BOOL bAppendNewLine = FALSE;
               if (wszLine[cchLine-1] == L'\n')
               {
                  cchLine--;
                  bAppendNewLine = TRUE;
               }

               // See if there are remaining characters to copy
               if (cchValue > (cchLine - cchKey))
               {
                  if (bAppendNewLine)
                  {
                     cchLine++;
                  }

                  pwszValue += (cchLine - cchKey);
                  if (FAILED(StringCchCopyNEx(
                                       wszLine,
                                       lengthof(wszLine),
                                       pwszValue,
                                       (cchValue - (cchLine - cchKey)),
                                       &pwsz,
                                       &cchLine,
                                       0
                                       )) ||
                     (bAppendNewLine &&
                     FAILED(StringCchCopy(pwsz, cchLine, L"\n"))) ||
                     FAILED(StringCchLength(wszLine, lengthof(wszLine), &cchLine)))
                  {
                     bReplace = FALSE;
                  }
               }
               else
               {
                  if (bAppendNewLine &&
                     FAILED(StringCchCopy(pwsz, cchNewLine, L"\n")))
                  {
                    bReplace = FALSE;
                  }
                  cchLine = 0;
               }

               if (FAILED(StringCchLength(wszNewLine, lengthof(wszNewLine), &cchNewLine)))
               {
                  bReplace = FALSE;
               }
            }

nextLine:
            if (bReplace)
            {
               BOOL bEOF = (BOOL)feof(fin);

               // Save read offset, then restore write offset
               if (fgetpos(fin, &posSrc) || fsetpos(fin, &posDest))
               {
                  goto closeFile;
               }

               if (cchNewLine)
               {
                  bSuccess = (WEOF != fputws(wszNewLine, fin));
                  if (cchLine &&
                     SUCCEEDED(StringCchCopyN(
                                          wszNewLine,
                                          lengthof(wszLine),
                                          wszLine,
                                          cchLine
                                          )))
                  {
                     cchNewLine = cchLine;
                  }
                  else
                  {
                     cchNewLine = 0;
                  }
               }
               else if (cchLine)
               {
                  bSuccess = (WEOF != fputws(wszLine, fin));
                  cchLine = 0;
               }

               // Save write offset, then restore read offset
               if (fgetpos(fin, &posDest) ||
                  bEOF ||
                  fsetpos(fin, &posSrc))
               {
                  goto closeFile;
               }
            }
            else
            {
               // Just update read and write offsets
               if (fgetpos(fin, &posSrc) || fgetpos(fin, &posDest))
               {
                  goto closeFile;
               }
            }
         }
      }

closeFile:
      if (bReplace && !fsetpos(fin, &posDest))
      {
         if (cchNewLine)
         {
            bSuccess = (WEOF != fputws(wszNewLine, fin));
         }
         SetEndOfFile(_fileno(fin));
      }

      fclose(fin);
   }

   SetFileAttributes(pwszIniFile, dwAttrib);

leave:
   return bSuccess;
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值