Changeset 6503 for trunk/licq
- Timestamp:
- 09/06/08 03:25:28 (3 months ago)
- Location:
- trunk/licq
- Files:
-
- 2 modified
-
include/licq_file.h (modified) (2 diffs)
-
src/file.cpp (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/licq/include/licq_file.h
r4526 r6503 1 1 #ifndef INIFILE_H 2 2 #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 3 16 4 17 #define MAX_SECTIONxNAME_LEN 160 … … 46 59 bool SetSection(const char *_szSectionName); 47 60 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); 54 68 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); 61 76 62 77 int Error() { return (m_nError); } 63 78 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); 64 86 65 87 protected: -
trunk/licq/src/file.cpp
r6276 r6503 32 32 #include "licq_log.h" 33 33 34 using namespace std; 34 35 35 36 //=====Pre class helper functions============================================== … … 657 658 /*! \brief Finds a key and sets the data. Returns false if the key does not exist. 658 659 */ 659 bool CIniFile::ReadStr(const char *szKey, char *szData,660 bool CIniFile::ReadStr(const string& key, char *szData, 660 661 const char *szDefault, bool bTrim, int _nMax) 661 662 { … … 670 671 if (sz == NULL) 671 672 { 672 if (szLine == NULL) Warn(INI_ExNOKEY, szKey); 673 if (szLine == NULL) 674 Warn(INI_ExNOKEY, key.c_str()); 673 675 if (szDefault != NULL) strcpy(szData, szDefault); 674 676 return (false); 675 677 } 676 678 } 677 while (s trcmp(sz, szKey) != 0);679 while (sz != key); 678 680 679 681 if ((sz = GetDataFromLine(szData, szLine, bTrim, _nMax)) == NULL) … … 692 694 * exist. 693 695 */ 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 710 MAKE_READNUM(unsigned long, strtoul(strData, (char**)NULL, 10)) 711 MAKE_READNUM(unsigned int, strtoul(strData, (char**)NULL, 10)) 712 MAKE_READNUM(signed int, atoi(strData)) 713 MAKE_READNUM(unsigned short, (unsigned short)atoi(strData)) 714 MAKE_READNUM(signed short, (signed short)atoi(strData)) 715 MAKE_READNUM(char, (char)atoi(strData)) 751 716 752 717 //-----ReadBool---------------------------------------------------------------- … … 756 721 * mean true. Returns false if the key does not exist. 757 722 */ 758 bool CIniFile::ReadBool(const char *_szKey, bool &data, const bool _bDefault)723 bool CIniFile::ReadBool(const string& key, bool &data, const bool _bDefault) 759 724 { 760 725 char szData[MAX_LINE_LEN]; 761 if (!ReadStr( _szKey, szData, NULL))726 if (!ReadStr(key, szData, NULL)) 762 727 { 763 728 data = _bDefault; … … 835 800 * key at the end of the current section. Always returns true. 836 801 */ 837 bool CIniFile::WriteStr(const char *_szKey, const char *_szData)802 bool CIniFile::WriteStr(const string& key, const char *_szData) 838 803 { 839 804 char *sz, *szLine, szLineBuffer[MAX_LINE_LEN], szKeyBuffer[MAX_KEYxNAME_LEN]; … … 849 814 sz = GetKeyFromLine(szKeyBuffer, szLine); 850 815 } 851 while (sz != NULL && s trcmp(sz, _szKey) != 0);816 while (sz != NULL && sz != key); 852 817 int nCutEnd = m_nBufPos; 853 818 … … 858 823 { 859 824 gLog.Warn("%sInternal Error: CIniFile::WriteStr(%s, NULL).\n", 860 L_WARNxSTR, _szKey);825 L_WARNxSTR, key.c_str()); 861 826 strcpy(szDataNoNL, ""); 862 827 } 863 snprintf(szNewLine, MAX_LINE_LEN, "%s = %s\n", _szKey, szDataNoNL);828 snprintf(szNewLine, MAX_LINE_LEN, "%s = %s\n", key.c_str(), szDataNoNL); 864 829 szNewLine[MAX_LINE_LEN - 1] = '\0'; 865 830 // Don't lose the following key if szNewLine was truncated by snprintf … … 875 840 } 876 841 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 850 MAKE_WRITENUM(unsigned long, "%lu") 851 MAKE_WRITENUM(unsigned int, "%u") 852 MAKE_WRITENUM(signed int, "%i") 853 MAKE_WRITENUM(unsigned short, "%i") 854 MAKE_WRITENUM(signed short, "%i") 855 MAKE_WRITENUM(char, "%i") 856 857 bool CIniFile::WriteBool(const string& key, bool data) 858 { 859 return(WriteStr(key, data ? "1" : "0")); 860 } 861
