Changeset 6503 for trunk/licq

Show
Ignore:
Timestamp:
09/06/08 03:25:28 (3 months ago)
Author:
flynd
Message:

Added ReadNum/WriteNum? functions for int and unsigned int since long is 64 bits on 64 bit systems. Since time_t is declared as int on some systems (at least in BSD) this caused crashes when calling ReadNum?.
ReadNum/WriteNum? for unsigned long are left to keep existing code working for now but since we probably don't need to save any 64 bit values they are marked as deprecated.

Location:
trunk/licq
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/licq/include/licq_file.h

    r4526 r6503  
    11#ifndef INIFILE_H 
    22#define INIFILE_H 
     3 
     4#include <string> 
     5 
     6// Define for marking functions as deprecated 
     7#ifndef LICQ_DEPRECATED 
     8# if defined(__GNUC__) && !defined(__INTEL_COMPILER) && (__GNUC__ - 0 > 3 || (__GNUC__ - 0 == 3 && __GNUC_MINOR__ - 0 >= 2)) 
     9#  define LICQ_DEPRECATED __attribute__ ((__deprecated__)) 
     10# elif defined(_MSC_VER) && (_MSC_VER >= 1300) 
     11#  define LICQ_DEPRECATED __declspec(deprecated) 
     12# else 
     13#  define LICQ_DEPRECATED 
     14# endif 
     15#endif 
    316 
    417#define MAX_SECTIONxNAME_LEN 160 
     
    4659  bool SetSection(const char *_szSectionName); 
    4760  bool CreateSection(const char *_szSectionName); 
    48   bool ReadStr(const char *_szKey, char *_szData, const char *_szDefault = NULL, bool bTrim = true, int _nMax = 0); 
    49   bool ReadNum(const char *_szKey, unsigned long &data, const unsigned long _nDefault = 0); 
    50   bool ReadNum(const char *_szKey, unsigned short &data, const unsigned short _nDefault = 0); 
    51   bool ReadNum(const char *_szKey, signed short &data, const signed short _nDefault = 0); 
    52   bool ReadNum(const char *_szKey, char &data, const char _nDefault = 0); 
    53   bool ReadBool(const char *_szKey, bool &data, const bool _bDefault = false); 
     61  bool ReadStr(const std::string& Key, char* data, const char* defValue = NULL, bool trim = true, int maxLength = 0); 
     62  bool ReadNum(const std::string& key, unsigned int &data, unsigned int defValue = 0); 
     63  bool ReadNum(const std::string& key, signed int &data, signed int defValue = 0); 
     64  bool ReadNum(const std::string& key, unsigned short &data, unsigned short defValue = 0); 
     65  bool ReadNum(const std::string& key, signed short &data, signed short defValue = 0); 
     66  bool ReadNum(const std::string& key, char &data, char defValue = 0); 
     67  bool ReadBool(const std::string& key, bool &data, const bool defValue = false); 
    5468 
    55   bool WriteStr(const char *_szKey, const char *_szData); 
    56   bool WriteNum(const char *_szKey, const unsigned long _szData); 
    57   bool WriteNum(const char *_szKey, const unsigned short _szData); 
    58   bool WriteNum(const char *_szKey, const signed short _szData); 
    59   bool WriteNum(const char *_szKey, const char _szData); 
    60   bool WriteBool(const char *_szKey, const bool _szData); 
     69  bool WriteStr(const std::string& key, const char* data); 
     70  bool WriteNum(const std::string& key, unsigned int data); 
     71  bool WriteNum(const std::string& key, signed int data); 
     72  bool WriteNum(const std::string& key, unsigned short data); 
     73  bool WriteNum(const std::string& key, signed short data); 
     74  bool WriteNum(const std::string& key, char data); 
     75  bool WriteBool(const std::string& key, bool data); 
    6176 
    6277  int Error()  { return (m_nError); } 
    6378  const char *FileName()  { return m_szFilename; } 
     79 
     80  // Long differs in size between 32 bit systems and 64 bit systems so int 
     81  // should be used instead. This function is kept to keep old code working but 
     82  // since it is unlikely that we need to save any 64 bit integers these 
     83  // function is declared as deprecated. 
     84  LICQ_DEPRECATED bool ReadNum(const std::string& key, unsigned long &data, unsigned long defValue = 0); 
     85  LICQ_DEPRECATED bool WriteNum(const std::string& key, unsigned long data); 
    6486 
    6587protected: 
  • trunk/licq/src/file.cpp

    r6276 r6503  
    3232#include "licq_log.h" 
    3333 
     34using namespace std; 
    3435 
    3536//=====Pre class helper functions============================================== 
     
    657658/*! \brief Finds a key and sets the data.  Returns false if the key does not exist. 
    658659 */ 
    659 bool CIniFile::ReadStr(const char *szKey, char *szData, 
     660bool CIniFile::ReadStr(const string& key, char *szData, 
    660661                       const char *szDefault, bool bTrim, int _nMax) 
    661662{ 
     
    670671    if (sz == NULL) 
    671672    { 
    672        if (szLine == NULL) Warn(INI_ExNOKEY, szKey); 
     673      if (szLine == NULL) 
     674        Warn(INI_ExNOKEY, key.c_str()); 
    673675       if (szDefault != NULL) strcpy(szData, szDefault); 
    674676       return (false); 
    675677    } 
    676678  } 
    677   while (strcmp(sz, szKey) != 0); 
     679  while (sz != key); 
    678680 
    679681  if ((sz = GetDataFromLine(szData, szLine, bTrim, _nMax)) == NULL) 
     
    692694 * exist. 
    693695 */ 
    694 bool CIniFile::ReadNum(const char *_szKey, unsigned long &data, 
    695                        const unsigned long _nDefault) 
    696 { 
    697   char szData[MAX_LINE_LEN]; 
    698   if (!ReadStr(_szKey, szData, NULL)) 
    699   { 
    700     data = _nDefault; 
    701     return (false); 
    702   } 
    703  
    704   //can't use atol because it truncates numbers over 0x7FFFFFFF 
    705   data = strtoul(szData, (char **)NULL, 10); 
    706   return(true); 
    707 } 
    708  
    709 bool CIniFile::ReadNum(const char *_szKey, unsigned short &data, 
    710                        const unsigned short _nDefault) 
    711 { 
    712   char szData[MAX_LINE_LEN]; 
    713   if (!ReadStr(_szKey, szData, NULL)) 
    714   { 
    715     data = _nDefault; 
    716     return (false); 
    717   } 
    718  
    719   data = (unsigned short)atoi(szData); 
    720   return(true); 
    721 } 
    722  
    723  
    724 bool CIniFile::ReadNum(const char *_szKey, char &data, const char _nDefault) 
    725 { 
    726   char szData[MAX_LINE_LEN]; 
    727   if (!ReadStr(_szKey, szData, NULL)) 
    728   { 
    729     data = _nDefault; 
    730     return (false); 
    731   } 
    732  
    733   data = (char)atoi(szData); 
    734   return(true); 
    735 } 
    736  
    737 bool CIniFile::ReadNum(const char *_szKey, signed short &data, 
    738                        const signed short _nDefault) 
    739 { 
    740   char szData[MAX_LINE_LEN]; 
    741   if (!ReadStr(_szKey, szData, NULL)) 
    742   { 
    743     data = _nDefault; 
    744     return (false); 
    745   } 
    746  
    747   data = (signed short)atoi(szData); 
    748   return(true); 
    749 } 
    750  
     696 
     697#define MAKE_READNUM(valueType, convertion) \ 
     698    bool CIniFile::ReadNum(const string& key, valueType &data, valueType defValue) \ 
     699    { \ 
     700      char strData[MAX_LINE_LEN]; \ 
     701      if (!ReadStr(key, strData, NULL)) \ 
     702      { \ 
     703        data = defValue; \ 
     704        return false; \ 
     705      } \ 
     706      data = convertion; \ 
     707      return true; \ 
     708    } 
     709 
     710MAKE_READNUM(unsigned long, strtoul(strData, (char**)NULL, 10)) 
     711MAKE_READNUM(unsigned int, strtoul(strData, (char**)NULL, 10)) 
     712MAKE_READNUM(signed int, atoi(strData)) 
     713MAKE_READNUM(unsigned short, (unsigned short)atoi(strData)) 
     714MAKE_READNUM(signed short, (signed short)atoi(strData)) 
     715MAKE_READNUM(char, (char)atoi(strData)) 
    751716 
    752717//-----ReadBool---------------------------------------------------------------- 
     
    756721 * mean true.  Returns false if the key does not exist. 
    757722 */ 
    758 bool CIniFile::ReadBool(const char *_szKey, bool &data, const bool _bDefault) 
     723bool CIniFile::ReadBool(const string& key, bool &data, const bool _bDefault) 
    759724{ 
    760725  char szData[MAX_LINE_LEN]; 
    761   if (!ReadStr(_szKey, szData, NULL)) 
     726  if (!ReadStr(key, szData, NULL)) 
    762727  { 
    763728    data = _bDefault; 
     
    835800 * key at the end of the current section.  Always returns true. 
    836801 */ 
    837 bool CIniFile::WriteStr(const char *_szKey, const char *_szData) 
     802bool CIniFile::WriteStr(const string& key, const char *_szData) 
    838803{ 
    839804  char *sz, *szLine, szLineBuffer[MAX_LINE_LEN], szKeyBuffer[MAX_KEYxNAME_LEN]; 
     
    849814    sz = GetKeyFromLine(szKeyBuffer, szLine); 
    850815  } 
    851   while (sz != NULL && strcmp(sz, _szKey) != 0); 
     816  while (sz != NULL && sz != key); 
    852817  int nCutEnd = m_nBufPos; 
    853818 
     
    858823  { 
    859824    gLog.Warn("%sInternal Error: CIniFile::WriteStr(%s, NULL).\n", 
    860               L_WARNxSTR, _szKey); 
     825        L_WARNxSTR, key.c_str()); 
    861826    strcpy(szDataNoNL, ""); 
    862827  } 
    863   snprintf(szNewLine, MAX_LINE_LEN, "%s = %s\n", _szKey, szDataNoNL); 
     828  snprintf(szNewLine, MAX_LINE_LEN, "%s = %s\n", key.c_str(), szDataNoNL); 
    864829  szNewLine[MAX_LINE_LEN - 1] = '\0'; 
    865830  // Don't lose the following key if szNewLine was truncated by snprintf 
     
    875840} 
    876841 
    877  
    878 bool CIniFile::WriteNum(const char *_szKey, const unsigned short _nData) 
    879 { 
    880    char szN[32]; 
    881    snprintf(szN, 32, "%u", _nData); 
    882    szN[31] = '\0'; 
    883    return(WriteStr(_szKey, szN)); 
    884 } 
    885  
    886 bool CIniFile::WriteNum(const char *_szKey, const unsigned long _nData) 
    887 { 
    888    char szN[32]; 
    889    snprintf(szN, 32, "%lu", _nData); 
    890    szN[31] = '\0'; 
    891    return(WriteStr(_szKey, szN)); 
    892 } 
    893  
    894 bool CIniFile::WriteNum(const char *_szKey, const signed short _nData) 
    895 { 
    896   char szN[32]; 
    897   snprintf(szN, 32, "%d", _nData); 
    898   szN[31] = '\0'; 
    899   return(WriteStr(_szKey, szN)); 
    900 } 
    901  
    902 bool CIniFile::WriteNum(const char *_szKey, const char _nData) 
    903 { 
    904   char szN[32]; 
    905   snprintf(szN, 32, "%d", _nData); 
    906   szN[31] = '\0'; 
    907   return(WriteStr(_szKey, szN)); 
    908 } 
    909  
    910 bool CIniFile::WriteBool(const char *_szKey, const bool _nData) 
    911 { 
    912    return(WriteStr(_szKey, _nData ? "1" : "0")); 
    913 } 
    914  
     842#define MAKE_WRITENUM(valueType, convertion) \ 
     843    bool CIniFile::WriteNum(const string& key, valueType data) \ 
     844    { \ 
     845      char strData[32]; \ 
     846      snprintf(strData, sizeof(strData), convertion, data); \ 
     847      return WriteStr(key, strData); \ 
     848    } 
     849 
     850MAKE_WRITENUM(unsigned long, "%lu") 
     851MAKE_WRITENUM(unsigned int, "%u") 
     852MAKE_WRITENUM(signed int, "%i") 
     853MAKE_WRITENUM(unsigned short, "%i") 
     854MAKE_WRITENUM(signed short, "%i") 
     855MAKE_WRITENUM(char, "%i") 
     856 
     857bool CIniFile::WriteBool(const string& key, bool data) 
     858{ 
     859   return(WriteStr(key, data ? "1" : "0")); 
     860} 
     861