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

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