home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / RunModule / src / Inifile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-31  |  4.2 KB  |  110 lines

  1. // IniFile.cpp: implementation of the CIniFile class.
  2. //
  3. /*
  4. Copyright 2001 Anish Mistry. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8.  
  9.    1. Redistributions of source code must retain the above copyright notice, 
  10.    this list of conditions and the following disclaimer.
  11.    2. Redistributions in binary form must reproduce the above copyright notice,
  12.    this list of conditions and the following disclaimer in the documentation 
  13.    and/or other materials provided with the distribution.
  14.  
  15. THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  16. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS 
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  20. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  22. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  
  25. The views and conclusions contained in the software and documentation are those
  26. of the authors and should not be interpreted as representing official policies,
  27. either expressed or implied, of Anish Mistry or AM Productions.
  28.  
  29. * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
  30. */
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. #include "stdafx.h"
  34. #include "IniFile.h"
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. CIniFile::CIniFile()
  41. {
  42.     m_pFilePath = NULL;
  43. }
  44.  
  45. CIniFile::~CIniFile()
  46. {// begin ~CIniFile
  47.     if(m_pFilePath)
  48.     {// begin free previous file path
  49.         HeapFree(GetProcessHeap(),NULL,m_pFilePath);
  50.     }// end free previous file path
  51. }// end ~CIniFile
  52.  
  53. bool CIniFile::WriteKey(const char *pSection, const char *pKey, const char *pData)
  54. {// begin WriteKey
  55.     if(!m_pFilePath)
  56.         return false;
  57.     return (bool)WritePrivateProfileString(pSection,pKey,pData,m_pFilePath); 
  58. }// end WriteKey
  59.  
  60. int CIniFile::WriteKey(const char *pSection, const char *pKey, int nData)
  61. {// begin WriteKey
  62.     if(!m_pFilePath)
  63.         return false;
  64.     char pData[12] = {NULL};
  65.     wsprintf(pData,"%i",nData);
  66.     return (bool)WritePrivateProfileString(pSection,pKey,pData,m_pFilePath); 
  67. }// end WriteKey
  68.  
  69. void CIniFile::SetFile(const char *pFileName)
  70. {// begin SetFile
  71.     HANDLE hProcessHeap = GetProcessHeap();
  72.     if(m_pFilePath)
  73.     {// begin free previous file path
  74.         HeapFree(hProcessHeap,NULL,m_pFilePath);
  75.     }// end free previous file path
  76.     // get the current directory to store
  77.     int nAllocAmount = 2;    // initilize to 2 so that the NULL terminatior 
  78.     // and '\' at the end of the CurrentDirectory are included
  79.     char pDirectory[MAX_PATH_LENGTH] = {NULL};
  80.     GetCurrentDirectory(MAX_PATH_LENGTH-1,pDirectory);
  81.     // calcluate the amount of memory to allocate
  82.     nAllocAmount += lstrlen(pDirectory)+lstrlen(pFileName);
  83.     // allocate the required amount of memory and assign it to m_pFileName
  84.     m_pFilePath = (char *)HeapAlloc(hProcessHeap,HEAP_ZERO_MEMORY,nAllocAmount);
  85.     // copy the path into the new buffer
  86.     lstrcpy(m_pFilePath,pDirectory);
  87.     lstrcat(m_pFilePath,"\\");
  88.     lstrcat(m_pFilePath,pFileName);
  89. }// end SetFile
  90.  
  91. unsigned long int CIniFile::ReadKey(const char *pSection, const char *pKey, const char *pDefault,char *pOutputBuffer, unsigned long nOutputBufferLength)
  92. {// begin ReadKey
  93.     if(!m_pFilePath)
  94.         return false;
  95.     return GetPrivateProfileString(pSection,pKey,pDefault,pOutputBuffer,nOutputBufferLength,m_pFilePath);
  96. }// end ReadKey
  97.  
  98. int CIniFile::ReadKey(const char *pSection, const char *pKey, int nDefault)
  99. {// begin ReadKey
  100.     if(!m_pFilePath)
  101.         return false;
  102.     return (long int)GetPrivateProfileInt(pSection,pKey,nDefault,m_pFilePath);
  103. }// end ReadKey
  104.  
  105. const char *CIniFile::GetFilePath()
  106. {// begin GetFilePath
  107.     return m_pFilePath;
  108. }// end GetFilePath
  109.  
  110.