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 
00184 template <size_t LEN>
00185 class ststring
00186 {
00187     public:
00189         static size_t const max_len = LEN;
00190         
00192         ststring() : m_str() 
00193         { 
00194             memset(m_str, 0, max_len + 1);
00195         }
00197 
00202         explicit ststring(char const *str) : m_str() 
00203         {
00204             m_str[max_len] = 0;
00205             strncpy(m_str, str, max_len); 
00206         }    
00208         ststring(ststring const &sts) : m_str() { strcpy(m_str, sts.m_str); }
00210         ststring &operator=(ststring const &right)
00211         {
00212             if (this != &right)
00213                 strcpy(m_str, right.m_str);
00214             
00215             return *this;
00216         }
00217         
00219 
00223         ststring &operator=(char const *right) 
00224                 { strncpy(m_str, right, max_len); return *this; }
00225         
00227 
00235         bool operator==(char const *right) const
00236         {
00237             if (!str() && !right)
00238                 return true;
00239             
00240             if (!str() || !right)
00241                 return false;
00242             
00243             return (strncmp(m_str, right, max_len) == 0);
00244         }
00245             
00247 
00254         bool operator!=(char const *right) const
00255                 { return !(operator==(right)); }
00256         
00258         operator char *() { return str(); }
00260         operator char const *() const { return str(); }
00261         
00263         char *str() { return (*m_str ? m_str : 0); }
00265         char const *str() const { return (*m_str ? m_str : 0); }
00266     private:
00267         char m_str[LEN + 1];
00268 };
00269 
00270 /*************************************************************************/
00271 /*************************************************************************/
00272 
00274 class insensitive_less
00275 {
00276     public:
00277         bool operator()(std::string const &s1, std::string const &s2)
00278                 const;
00279 };
00280 
00281 /*************************************************************************/
00282 /*************************************************************************/
00283 
00285 
00286 static inline char *stpncpyzt(char *dest, const char *src, size_t n)
00287 {
00288     char *end = stpncpy(dest, src, n);
00289     dest[n-1] = 0;
00290     return end;
00291 }
00292 
00293 /*************************************************************************/
00294 
00296 
00297 static inline char *strncpyzt(char *dest, const char *src, size_t n)
00298 {
00299     strncpy(dest, src, n);
00300     dest[n-1] = 0;
00301     return dest;
00302 }
00303 
00304 /*************************************************************************/
00305 /*************************************************************************/
00306 
00308 
00314 extern char *nstrdup(char const *str);
00315 
00316 /*************************************************************************/
00317 
00319 
00334 extern bool strtol_range(long *num, char const *str, long min, long max,
00335         int base = 10);
00337 extern bool strtoul_range(unsigned long *num, char const *str,
00338         unsigned long min, unsigned long max, int base = 10);
00339 
00340 
00341 /*************************************************************************/
00342 
00343 } /* namespace misc */
00344 
00345 /*************************************************************************/
00346 
00347 #endif /* MISC_STRING_H */

Generated on Fri Apr 18 22:03:27 2008 for Epona API by  doxygen 1.5.3