string.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 // 
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 // 
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 // 
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however    
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.    
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // Common C++.  If you copy code from other releases into a copy of GNU
00028 // Common C++, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU Common C++, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00043 #ifndef CCXX_STRING_H_
00044 #define CCXX_STRING_H_
00045 
00046 #ifndef CCXX_MISSING_H_
00047 #include <cc++/missing.h>
00048 #endif
00049 
00050 #ifndef CCXX_STRCHAR_H_
00051 #include <cc++/strchar.h>
00052 #endif
00053 
00054 #ifdef  CCXX_NAMESPACES
00055 namespace ost {
00056 #endif
00057 
00058 class MemPager;
00059 
00076 class __EXPORT String
00077 {
00078 protected:
00079         static const unsigned minsize;
00080         static const unsigned slotsize;
00081         static const unsigned pagesize;
00082         static const unsigned slotlimit;
00083         static const unsigned slotcount;
00084 
00085         friend class StringObject;
00086 
00087 private:
00088         friend class MemPager;
00089 
00090         static MemPager *pager;
00091         static char **idx;
00092 
00093 #ifdef  CCXX_PACKED
00094 #pragma pack(1)
00095 #endif
00096 
00097         union
00098         {
00099                 struct
00100                 {
00101                         char *text;
00102                         size_t size;
00103                         size_t length;
00104                 }       bigstring;
00105                 struct
00106                 {
00107                         char text[(sizeof(char *) + (sizeof(size_t) * 2) + 1)];
00108                         char length : 6;
00109                         bool big : 1;
00110                 }       ministring;
00111         }       content;
00112 
00113 #ifdef  CCXX_PACKED
00114 #pragma pack()
00115 #endif
00116 
00117 protected:
00124         inline bool isBig(void) const
00125                 {return content.ministring.big;};
00126 
00135         const char *set(const char *str, size_t len = 0);
00136 
00143         void set(const String &str);
00144 
00145 #ifdef  HAVE_SNPRINTF
00146 
00153         const char *set(size_t size, const char *format, ...);
00154 #endif
00155 
00162         void copy(const String &str);
00163 
00167         void init(void);
00168 
00176         static char *getSpace(size_t size);
00177 
00185         size_t setSize(size_t size);
00186 
00192         void setLength(size_t len);
00193 
00204         virtual int compare(const char *text, size_t len = 0, size_t index = 0) const;
00205 
00215         size_t search(const char *text, size_t clen = 0, size_t offset = 0) const;
00216 
00217 public:
00218         static const size_t npos;
00219 
00220         typedef size_t size_type;
00221 
00225         String();
00226 
00232         String(const String &original);
00233 
00239         String(const char *str);
00240 
00246         String(std::string string);
00247 
00255         String(const String &str, size_t offset, size_t len = npos);
00256 
00257 #ifdef  HAVE_SNPRINTF
00258 
00264         String(size_t size, const char *format, ...);
00265 #else
00266 
00273         String(size_t count, const char *str);
00274 #endif
00275 
00282         String(size_t count, const char fill = ' ');
00283 
00287         virtual ~String();
00288 
00296         const char *getIndex(size_t index) const;
00297 
00303         char *getText(void) const;
00304 
00310         long getValue(long defvalue = 0l) const;
00311 
00317         bool getBool(bool defbool = false) const;
00318 
00324         const size_t getLength(void) const;
00325 
00331         const size_t getSize(void) const;
00332 
00338         bool isEmpty(void) const;
00339 
00345         void resize(size_t size);
00346 
00350         void clear(void);
00351 
00357         char at(ssize_t offset) const;
00358 
00367         unsigned count(const String &s, size_t offset = 0) const;
00368 
00378         unsigned count(const char *s, size_t offset = 0, size_t len = 0) const;
00379 
00387         String token(const char *delim = " \t\n\r", size_t offset = 0);
00388 
00397         size_t find(const String &s, size_t offset = 0, unsigned instance = 1) const;
00398 
00406         size_t rfind(const String &s, size_t offset = 0) const;
00407 
00417         size_t find(const char *s, size_t offset = 0, size_t len = 0, unsigned count = 1) const;
00418 
00427         size_t rfind(const char *s, size_t offset = 0, size_t len = 0) const;
00428 
00434         inline void trim(const char *cs)
00435                 {setLength(strtrim(cs, getText(), getLength()));};
00436 
00442         inline void chop(const char *cs)
00443                 {setLength(strchop(cs, getText(), getLength()));};
00444 
00450         void strip(const char *cs);
00451 
00457         inline void chop(size_t chars)
00458                 {erase(0, chars);};
00459 
00465         void trim(size_t count);
00466 
00473         void erase(size_t start, size_t len = npos);
00474 
00482         void insert(size_t start, const char *text, size_t len = 0);
00483 
00490         void insert(size_t start, const String &str);
00491 
00501         void replace(size_t start, size_t len, const char *text, size_t count = 0);
00502 
00511         void replace(size_t start, size_t len, const String &string);
00512 
00522         inline size_t find(unsigned instance, const char *text, size_t offset = 0, size_t len = 0) const
00523                 {return find(text, offset, len, instance);};
00524 
00533         inline size_t find(unsigned instance, const String &string, size_t offset = 0) const
00534                 {return find(string, offset, instance);};
00535 
00544         inline String substr(size_t start, size_t len) const
00545                 {return String(*this, start, len);};
00546 
00554         inline const char *(index)(size_t ind) const
00555                 {return getIndex(ind);};
00556 
00561         inline void compact(void)
00562                 {resize(getLength() + 1);};
00563 
00569         inline char *c_str(void) const
00570                 {return getText();};
00571 
00577         inline operator char *() const
00578                 {return getText();};
00579 
00585         inline bool operator!(void) const
00586                 {return isEmpty();};
00587 
00593         inline char *text(void) const
00594                 {return getText();};
00595 
00601         inline char *data(void) const
00602                 {return getText();};
00603 
00609         inline size_t length(void) const
00610                 {return strlen(getText());};
00611 
00617         inline size_t size(void) const
00618                 {return getLength();};
00619 
00625         inline size_t capacity(void) const
00626                 {return getSize();};
00627 
00631         bool empty(void) const
00632                 {return isEmpty();};
00633 
00640         void append(const char *str, size_t count = 0);
00641 
00642 #ifdef  HAVE_SNPRINTF
00643 
00649         void append(size_t size, const char *format, ...);
00650 #endif
00651 
00659         void append(const char *str, size_t offset, size_t count);
00660 
00666         void add(char c);
00667 
00673         void append(const String &str);
00674 
00680         inline const char operator[](unsigned ind) const
00681                 {return at(ind);};
00682 
00686         inline const char *operator =(const char *str)
00687                 {return set(str);};
00688 
00692         friend __EXPORT String operator+(const String &s1, const String &s2);
00693 
00694         friend __EXPORT String operator+(const String &s1, const char *s2);
00695 
00696         friend __EXPORT String operator+(const char *s1, const String &s2);
00697 
00698         friend __EXPORT String operator+(const String &s1, const char c2);
00699         
00700         friend __EXPORT String operator+(const char c1, const String &s2);
00701 
00705         inline String &operator+=(const String &str)
00706                 {append(str); return *this;};
00707 
00711         inline String &operator+=(char c)
00712                 {add(c); return *this;};
00713 
00717         inline String &operator+=(const char *str)
00718                 {append(str); return *this;};
00719 
00723         inline String &operator+=(const std::string &str)
00724                 {append(str.c_str()); return *this;};
00725 
00736         friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0);
00737 
00742         friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str);
00743 
00747         inline friend std::istream &operator>>(std::istream &is, String &str)
00748                 {return getline(is, str);};
00749 
00750 #ifdef  HAVE_SNPRINTF
00751 
00759         friend __EXPORT int strprintf(String &str, size_t size, const char *format, ...);
00760 #endif
00761 
00762         bool operator<(const String &str) const;
00763         bool operator<(const char *str) const;
00764         bool operator>(const String &str) const;
00765         bool operator>(const char *str) const;
00766         bool operator<=(const String &str) const;
00767         bool operator<=(const char *str) const;
00768         bool operator>=(const String &str) const;
00769         bool operator>=(const char *str) const;
00770         bool operator==(const String &str) const;
00771         bool operator==(const char *str) const;
00772         bool operator!=(const String &str) const;
00773         bool operator!=(const char *str) const;
00774 
00775 #ifdef  HAVE_SNPRINTF
00776 
00780         inline String &operator+=(int i)
00781                 {append(16, "%d", i); return *this;};
00782 
00783         inline String &operator+=(unsigned int i)
00784                 {append(16, "%u", i); return *this;};
00785 
00786         inline String &operator+=(long l)
00787                 {append(16, "%l", l); return *this;};
00788 
00789         inline String &operator+=(unsigned long l)
00790                 {append(16, "%ul", l); return *this;};
00791 
00792         inline String &operator+=(float f)
00793                 {append(32, "%f", f); return *this;};
00794 
00795         inline String &operator+=(double d)
00796                 {append(32, "%f", d); return *this;};
00797 
00798         inline String &operator+=(short s)
00799                 {append(8, "%hd", s); return *this;};
00800 
00801         inline String &operator+=(unsigned short s)
00802                 {append(8, "%hu", s); return *this;};
00803 
00804 
00808         inline String &operator=(int i)
00809                 {set(16, "%d", i); return *this;};
00810 
00811         inline String &operator=(unsigned int i)
00812                 {set(16, "%u", i); return *this;};
00813 
00814         inline String &operator=(long l)
00815                 {set(16, "%l", l); return *this;};
00816 
00817         inline String &operator=(unsigned long l)
00818                 {set(16, "%ul", l); return *this;};
00819 
00820         inline String &operator=(float f)
00821                 {set(32, "%f", f); return *this;};
00822 
00823         inline String &operator=(double d)
00824                 {set(32, "%f", d); return *this;};
00825 
00826         inline String &operator=(short s)
00827                 {set(8, "%hd", s); return *this;};
00828 
00829         inline String &operator=(unsigned short s)
00830                 {set(8, "%hu", s); return *this;};
00831 #endif
00832 
00833         inline String &operator=(const String &original)
00834                 {copy(original); return *this;};
00835 
00839         bool operator*=(const String &str) const;
00840 
00844         bool operator*=(const char *str) const;
00845 };
00846 
00847 class __EXPORT SString : public String, protected std::streambuf, public std::ostream
00848 {
00849 protected:
00855         int overflow(int c);
00856 
00857 public:
00861         SString();
00862 
00866         SString(const SString &from);
00867 
00871         ~SString();
00872 };
00873 
00883 class __EXPORT StringObject
00884 {
00885 public:
00889         void *operator new(size_t size) NEW_THROWS;
00890 
00894         void operator delete(void *obj);
00895 };
00896 
00897 #ifdef  CCXX_NAMESPACES
00898 }
00899 #endif
00900 
00901 #endif

Generated on Thu Nov 9 19:38:30 2006 for GNU CommonC++ by  doxygen 1.4.6