string.h

Go to the documentation of this file.
00001 /* String-related treasures.
00002  *
00003  * PegSoft miscellaneous library (c) 2004 PegSoft
00004  * Contact us at pegsoft@pegsoft.net
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License version 2 as
00008  * published by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this software (the COPYING file); if not, write to the
00017  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
00018  * USA.
00019  *
00020  */
00021 
00027 #ifndef MISC_STRING_H
00028 #define MISC_STRING_H
00029 
00030 /*************************************************************************/
00031 
00032 #include <string>
00033 
00034 /*************************************************************************/
00035 
00036 namespace misc
00037 {
00038 
00039 /*************************************************************************/
00040 
00042 
00049 class cstring
00050 {
00051     public:
00053         cstring() : m_str(0) { }
00055 
00059         explicit cstring(char const *str) : m_str(0) { copy(str); }
00061 
00067         cstring(char const *str, size_t len) : m_str(0) { copy(str, len); }
00069         cstring(cstring const &cs) : m_str(0) { copy(cs.m_str); }
00071         cstring &operator=(cstring const &right);
00072         
00074 
00079         cstring &operator=(char const *right) { copy(right); return *this; }
00080         
00082 
00088         bool operator==(char const *right) const;
00090 
00096         bool operator!=(char const *right) const
00097                 { return !(operator==(right)); }
00098         
00100         operator char *() { return m_str; }
00102         operator char const *() const { return m_str; }
00103         
00105 
00110         cstring &assign(char const *str) { copy(str); return *this; }
00111         
00113 
00120         cstring &assign(char const *str, size_t len) 
00121                 { copy(str, len); return *this; }
00122         
00124 
00132         int compare(char const *str) const;
00133         
00135 
00144         int compare(char const *str, size_t len) const;
00145         
00147 
00155         char *reserve(size_t size);
00156         
00158 
00163         void swap(misc::cstring &str);
00164         
00166         char *str() { return m_str; }
00168         char const *str() const { return m_str; }
00169     private:
00170         char *m_str;
00171         
00172         void copy(char const *s);
00173         void copy(char const *s, size_t len);
00174         void free();
00175 };
00176 
00177 /*************************************************************************/
00178 /*************************************************************************/
00179 
00181 class insensitive_less
00182 {
00183     public:
00184         bool operator()(std::string const &s1, std::string const &s2)
00185                 const;
00186 };
00187 
00188 /*************************************************************************/
00189 /*************************************************************************/
00190 
00192 
00193 static inline char *stpncpyzt(char *dest, const char *src, size_t n)
00194 {
00195     char *end = stpncpy(dest, src, n);
00196     dest[n-1] = 0;
00197     return end;
00198 }
00199 
00200 /*************************************************************************/
00201 
00203 
00204 static inline char *strncpyzt(char *dest, const char *src, size_t n)
00205 {
00206     strncpy(dest, src, n);
00207     dest[n-1] = 0;
00208     return dest;
00209 }
00210 
00211 /*************************************************************************/
00212 /*************************************************************************/
00213 
00215 
00221 extern char *nstrdup(char const *str);
00222 
00223 /*************************************************************************/
00224 
00226 
00241 extern bool strtol_range(long *num, char const *str, long min, long max,
00242         int base = 10);
00244 extern bool strtoul_range(unsigned long *num, char const *str,
00245         unsigned long min, unsigned long max, int base = 10);
00246 
00247 
00248 /*************************************************************************/
00249 
00250 } /* namespace misc */
00251 
00252 /*************************************************************************/
00253 
00254 #endif /* MISC_STRING_H */

Generated on Sun May 20 21:32:15 2007 for Epona API by  doxygen 1.4.6