00001 #ifndef DV_NTOSTR_H 00002 #define DV_NTOSTR_H 00003 // $Id: ntostr.h,v 1.19 2003/08/18 13:56:52 dvermeir Exp $ 00004 00005 #include <string> 00006 #include <stdexcept> 00007 00008 namespace Dv { 00009 namespace Util { 00010 /** Produces a string representation of its int argument. 00011 * The representation is determined by a sprintf format string. 00012 * @param i integer to represent 00013 * @param fmt format C-string, the default (if 0) is <code>"%d"</code>. 00014 * @return string representation of @a i, according to @a fmt. 00015 * @warning The buffer size is 20. 00016 */ 00017 std::string ntostr(int i, const char* fmt=0); 00018 00019 /** Produces a string representation of its unsigned int argument. 00020 * The representation is determined by a sprintf format string. 00021 * @param u unsigned integer to represent 00022 * @param fmt format string, the default (if 0) is <code>"%u"</code>. 00023 * @return string representation of @a u, according to @a fmt. 00024 * @warning The buffer size is 20. 00025 */ 00026 std::string ntostr(unsigned int u, const char* fmt=0); 00027 00028 /** Produces a string representation of its long argument. 00029 * The representation is determined by a sprintf format string. 00030 * @param l long integer to represent 00031 * @param fmt format string, the default (if 0) is <code>"%ld"</code>. 00032 * @return string representation of @a l, according to @a fmt. 00033 * @warning The buffer size is 20. 00034 */ 00035 std::string ntostr(long l, const char* fmt=0); 00036 00037 /** Produces a string representation of its unsigned long argument. 00038 * The representation is determined by a sprintf format string. 00039 * @param ul unsigned long integer to represent 00040 * @param fmt format string, the default (if 0) is <code>"%lu"</code>. 00041 * @return string representation of @a ul, according to @a fmt. 00042 * @warning The buffer size is 20. 00043 */ 00044 std::string ntostr(unsigned long ul, const char* fmt=0); 00045 00046 /** Produces a string representation of its double argument. 00047 * The representation is determined by a sprintf format string. 00048 * @param d unsigned long integer to represent 00049 * @param precision number of digits after decimal point in representation 00050 * @param fmt format string, the default (if 0) is <code>"%.Pf"</code>. 00051 * @return string representation of @a d, according to @a fmt and precision. 00052 * @warning The buffer size is 20. 00053 */ 00054 std::string ntostr(double d, unsigned int precision = 7, const char* fmt=0); 00055 00056 /** Try to convert the initial part of a C string to a long. 00057 * @param p C-string to convert 00058 * @param l long receiving the value 00059 * @param base to be used in the conversion. The default base (0) reads 00060 * decimal or octal (if the string starts with 0) numbers. 00061 * @return true iff an initial part of the C string p can be converted to a long value. 00062 * @warning If decimal is necessary, specify base 10 explicitely! 00063 */ 00064 bool cstr2long(const char* p,long& l,int base=0); 00065 00066 /** Try to convert the initial part of a C string to an unsigned long. 00067 * @param p C-string to convert 00068 * @param ul unsigned long receiving the value 00069 * @param base to be used in the conversion. The default base (0) reads 00070 * decimal or octal (if the string starts with 0) numbers. 00071 * @return true iff an initial part of the C string p can be converted to an unsigned long value. 00072 * @warning If decimal is necessary, specify base 10 explicitely! 00073 */ 00074 bool cstr2ulong(const char* p, unsigned long& ul, int base=0); 00075 00076 /** Try to convert the initial part of a C string to a double. 00077 * @param p C-string to convert 00078 * @param d double receiving the value 00079 * @return true iff an initial part of the C string p can be converted 00080 * to a double value. 00081 */ 00082 bool cstr2double(const char* p, double& d); 00083 00084 /** Change (in-place) all chars in string to lower case. 00085 * @param s string to convert. 00086 * @return reference to first argument. 00087 */ 00088 std::string& s2lower(std::string& s); 00089 00090 /** Change (in-place) all chars in string to upper case. 00091 * @param s string to convert. 00092 * @return reference to first argument. 00093 */ 00094 std::string& s2upper(std::string& s); 00095 00096 /** Remove leading and trailing white noise (' ', '\\n', '\\t') from string. 00097 * The argument is changed and returned 00098 * @param s string to trim 00099 * @return reference to the argument 00100 */ 00101 std::string& strim(std::string& s); 00102 00103 /** Remove leading and trailing white noise (' ','\\n','\\t') from C string. 00104 * @param pc C-string to trim. 00105 * @return pointer to first non-white-space character in the argument. 00106 * @warning The argument is changed. Since the return value points to the 00107 * first non-white-space char in the argument, the following is 00108 * a recepy for disaster: 00109 * @code 00110 * char* pc = new char[10]; 00111 * .. 00112 * pc = strim(pc); 00113 * delete[] pc; 00114 * @endcode 00115 */ 00116 char* strim(char* pc); 00117 00118 /** Class wrapper for int. 00119 * This class is useful for automatic conversion of a string to an int. 00120 * @code 00121 * int i = Int("14"); 00122 * @endcode 00123 */ 00124 class Int { 00125 public: 00126 /** Constructor. 00127 * @param s containing string representation of integer. 00128 * @param base used when converting s. The default is 0, in which case the function 00129 * reads decimal or octal (if the string starts with 0) numbers. 00130 * @warning If decimal is necessary, specify base 10 explicitely! 00131 */ 00132 Int(const std::string& s,int base=0) throw (std::domain_error); 00133 /** Conversion to int. */ 00134 operator int() const { return int_; } 00135 /** Return associated numeric value. */ 00136 int value() const { return int_; } 00137 private: 00138 int int_; 00139 }; 00140 00141 /** Class wrapper for long. 00142 * This class is useful for automatic conversion of a string to an long. 00143 * @code 00144 * long l = Long("14909090"); 00145 * @endcode 00146 */ 00147 class Long { 00148 public: 00149 /** Constructor. 00150 * @param s containing string representation of integer. 00151 * @param base used to convert @a s. The default is 0, in which case the function 00152 * reads decimal or octal (if the string starts with 0) numbers. 00153 * @warning If decimal is necessary, specify base 10 explicitely! 00154 */ 00155 Long(const std::string& s,int base=0) throw (std::domain_error); 00156 /** Conversion to int. */ 00157 operator long() const { return long_; } 00158 /** Return associated numeric value. */ 00159 long value() const { return long_; } 00160 private: 00161 long long_; 00162 }; 00163 00164 /** Class wrapper for double. 00165 * This class is useful for automatic conversion of a string to an 00166 * double. 00167 * @code 00168 * double d = Double("3.14"); 00169 * @endcode 00170 */ 00171 class Double { 00172 public: 00173 /** Constructor. 00174 * @param s containing string representation of integer. 00175 */ 00176 Double(const std::string& s) throw (std::domain_error); 00177 /** Conversion to double. */ 00178 operator double() const { return double_; } 00179 /** Return associated numeric value. */ 00180 double value() const { return double_; } 00181 private: 00182 double double_; 00183 }; 00184 00185 }} 00186 #endif
dvutil-0.13.15 | [30 December, 2004] |