home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / gamesystem / source / gs_inifile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-06  |  3.2 KB  |  156 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    gsCIniFile
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    12/03/00
  8. //
  9. // Base:    CObject
  10. //
  11. // Derived:    None
  12. //
  13. //-------------------------------------------------------------
  14.  
  15. #include "gamesystem.h"
  16.  
  17. //-------------------------------------------------------------
  18.  
  19. gsCIniFile::gsCIniFile()
  20. {
  21.     m_is_open = false;
  22. }
  23.  
  24. //-------------------------------------------------------------
  25.  
  26. gsCIniFile::~gsCIniFile()
  27. {
  28.     if (isOpen())
  29.         close();
  30. }
  31.  
  32. //-------------------------------------------------------------
  33.  
  34. bool gsCIniFile::open(const char *filename)
  35. {
  36.     if (isOpen())
  37.         close();
  38.  
  39.     gsCFile::getFullName(filename,m_fullname);
  40.  
  41.     m_is_open = true;
  42.  
  43.     return true;
  44. }
  45.  
  46. //-------------------------------------------------------------
  47.  
  48. bool gsCIniFile::close()
  49. {
  50.     m_is_open = false;
  51.  
  52.     return true;
  53. }
  54.  
  55. //-------------------------------------------------------------
  56.  
  57. bool gsCIniFile::isOpen()
  58. {
  59.     return m_is_open;
  60. }
  61.  
  62. //-------------------------------------------------------------
  63.  
  64. int gsCIniFile::readInt(const char *section,const char *key,int default_value)
  65. {
  66.     if (!isOpen())
  67.         return default_value;
  68.  
  69.     char temp[50];
  70.  
  71.     sprintf(temp,"%i",default_value);
  72.  
  73.     GetPrivateProfileString(section,key,temp,m_buffer,gsINIFILE_BUFFER_SIZE - 2,m_fullname);
  74.  
  75.     int value;
  76.  
  77.     if (sscanf(m_buffer,"%i",&value) == 1)
  78.         return value;
  79.     else
  80.         return default_value;
  81. }
  82.  
  83. //-------------------------------------------------------------
  84.  
  85. float gsCIniFile::readFloat(const char *section,const char *key,float default_value)
  86. {
  87.     if (!isOpen())
  88.         return default_value;
  89.  
  90.     char temp[50];
  91.  
  92.     sprintf(temp,"%.2f",default_value);
  93.  
  94.     GetPrivateProfileString(section,key,temp,m_buffer,gsINIFILE_BUFFER_SIZE - 2,m_fullname);
  95.  
  96.     float value;
  97.  
  98.     if (sscanf(m_buffer,"%f",&value) == 1)
  99.         return value;
  100.     else
  101.         return default_value;
  102. }
  103.  
  104. //-------------------------------------------------------------
  105.  
  106. const char *gsCIniFile::readString(const char *section,const char *key,const char *default_value)
  107. {
  108.     if (!isOpen())
  109.         return default_value;
  110.  
  111. //    int size =
  112.     GetPrivateProfileString(section,key,default_value,m_buffer,gsINIFILE_BUFFER_SIZE - 2,m_fullname);
  113.  
  114.     return m_buffer;
  115. }
  116.  
  117. //-------------------------------------------------------------
  118.  
  119. bool gsCIniFile::writeInt(const char *section,const char *key,int value)
  120. {
  121.     if (!isOpen())
  122.         return false;
  123.  
  124.     char temp[50];
  125.  
  126.     sprintf(temp,"%i",value);
  127.  
  128.     return WritePrivateProfileString(section,key,temp,m_fullname) != 0;
  129. }
  130.  
  131. //-------------------------------------------------------------
  132.  
  133. bool gsCIniFile::writeFloat(const char *section,const char *key,float value)
  134. {
  135.     if (!isOpen())
  136.         return false;
  137.  
  138.     char temp[50];
  139.  
  140.     sprintf(temp,"%.2f",value);
  141.  
  142.     return WritePrivateProfileString(section,key,temp,m_fullname) != 0;
  143. }
  144. //-------------------------------------------------------------
  145.  
  146. bool gsCIniFile::writeString(const char *section,const char *key,const char *value)
  147. {
  148.     if (!isOpen())
  149.         return false;
  150.  
  151.     return WritePrivateProfileString(section,key,value,m_fullname) != 0;
  152. }
  153.  
  154. //-------------------------------------------------------------
  155.  
  156.