Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

string.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2001 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 to the GNU General Public License, permission is 
00018 // granted for additional uses of the text contained in its release 
00019 // of Common C++.
00020 // 
00021 // The exception is that, if you link the Common C++ library with other
00022 // files to produce an executable, this does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public License.
00024 // Your use of that executable is in no way restricted on account of
00025 // linking the Common C++ library code into it.
00026 //
00027 // This exception does not however invalidate any other reasons why
00028 // the executable file might be covered by the GNU General Public License.
00029 // 
00030 // This exception applies only to the code released under the 
00031 // name Common C++.  If you copy code from other releases into a copy of
00032 // Common C++, as the General Public License permits, the exception does
00033 // not apply to the code that you add in this way.  To avoid misleading
00034 // anyone as to the status of such modified files, you must delete
00035 // this exception notice from them.
00036 // 
00037 // If you write modifications of your own for Common C++, it is your choice
00038 // whether to permit this exception to apply to your modifications.
00039 // If you do not wish that, delete this exception notice.
00040 
00046 #ifndef CCXX_STRING_H_
00047 #define CCXX_STRING_H_
00048 
00049 #ifndef CCXX_MISSING_H_
00050 #include <cc++/missing.h>
00051 #endif
00052 
00053 #ifndef CCXX_STRCHAR_H_
00054 #include <cc++/strchar.h>
00055 #endif
00056 
00057 #ifdef  CCXX_NAMESPACES
00058 namespace ost {
00059 #endif
00060 
00077 class __EXPORT String
00078 {
00079 protected:
00080         static const unsigned minsize;
00081         static const unsigned slotsize;
00082         static const unsigned pagesize;
00083         static const unsigned slotlimit;
00084         static const unsigned slots;
00085 
00086 private:
00087         friend class MemPager;
00088 
00089         static MemPager *pager;
00090         static char **idx;
00091 
00092 #pragma pack(1)
00093         union
00094         {
00095                 struct
00096                 {
00097                         char *text;
00098                         size_t size;
00099                         size_t length;
00100                 }       bigstring;
00101                 struct
00102                 {
00103                         char text[(sizeof(char *) + (sizeof(size_t) * 2) + 1)];
00104                         char length : 6;
00105                         bool big : 1;
00106                 }       ministring;
00107         }       content;
00108 #pragma pack()
00109 
00110 protected:
00117         inline bool isBig(void) const
00118                 {return content.ministring.big;};
00119 
00128         const char *set(const char *str, size_t len = 0);
00129 
00136         void set(const String &str);
00137 
00138 #ifdef  HAVE_SNPRINTF
00139 
00145         const char *set(size_t size, const char *format, ...);
00146 #endif
00147 
00154         void copy(const String &str);
00155 
00159         void init(void);
00160 
00168         char *getSpace(size_t size);
00169 
00177         size_t setSize(size_t size);
00178 
00184         void setLength(size_t len);
00185 
00196         virtual int compare(const char *text, size_t len = 0, size_t index = 0) const;
00197 
00207         size_t search(const char *text, size_t clen = 0, size_t offset = 0) const;
00208 
00209 public:
00210         static const size_t npos;
00211 
00212         typedef size_t size_type;
00213 
00217         String();
00218 
00224         String(const String &original);
00225 
00231         String(const char *str);
00232 
00238         inline String(std::string str)
00239                 {String(str.c_str());};
00240 
00248         String(const String &str, size_t offset, size_t len = npos);
00249 
00250 #ifdef  HAVE_SNPRINTF
00251 
00258         String(size_t count, const char *fmt, ...);
00259 #else
00260 
00267         String(size_t count, const char *str);
00268 #endif
00269 
00276         String(size_t count, char fill = ' ');
00277 
00281         virtual ~String();
00282 
00290         const char *getIndex(size_t index) const;
00291 
00297         char *getText(void) const;
00298 
00304         long getValue(long defvalue = 0l) const;
00305 
00311         bool getBool(bool defbool = false) const;
00312 
00318         const size_t getLength(void) const;
00319 
00325         const size_t getSize(void) const;
00326 
00332         bool isEmpty(void) const;
00333 
00339         void resize(size_t size);
00340 
00344         void clear(void);
00345 
00351         char at(ssize_t offset) const;
00352 
00361         unsigned count(const String &s, size_t offset = 0) const;
00362 
00372         unsigned count(const char *s, size_t offset = 0, size_t len = 0) const;
00373 
00381         String token(const char *delim = " \t\n\r", size_t offset = 0);
00382 
00391         size_t find(const String &s, size_t offset = 0, unsigned instance = 1) const;
00392 
00400         size_t rfind(const String &s, size_t offset = 0) const;
00401 
00411         size_t find(const char *s, size_t offset = 0, size_t len = 0, unsigned count = 1) const;
00412 
00421         size_t rfind(const char *s, size_t offset = 0, size_t len = 0) const;
00422 
00428         inline void trim(const char *cs)
00429                 {setLength(strtrim(cs, getText(), getLength()));};
00430 
00436         inline void chop(const char *cs)
00437                 {setLength(strchop(cs, getText(), getLength()));};
00438 
00444         void strip(const char *cs);
00445 
00451         inline void chop(size_t chars)
00452                 {erase(0, chars);};
00453 
00459         void trim(size_t count);
00460 
00467         void erase(size_t start, size_t len = npos);
00468 
00476         void insert(size_t start, const char *text, size_t len = 0);
00477 
00484         void insert(size_t start, const String &str);
00485 
00495         void replace(size_t start, size_t len, const char *text, size_t count = 0);
00496 
00505         void replace(size_t start, size_t len, const String &s);
00506 
00516         inline size_t find(unsigned instance, const char *cp, size_t offset = 0, size_t len = 0) const
00517                 {return find(cp, offset, len, instance);};
00518 
00527         inline size_t find(unsigned instance, const String &s, size_t offset = 0) const
00528                 {return find(s, offset, instance);};
00529 
00538         inline String substr(size_t start, size_t len) const
00539                 {return String(*this, start, len);};
00540 
00548         inline const char *(index)(size_t ind) const
00549                 {return getIndex(ind);};
00550 
00555         inline void compact(void)
00556                 {resize(getLength() + 1);};
00557 
00563         inline char *c_str(void) const
00564                 {return getText();};
00565 
00571         inline operator char *() const
00572                 {return getText();};
00573 
00579         inline bool operator!(void) const
00580                 {return isEmpty();};
00581 
00587         inline char *text(void) const
00588                 {return getText();};
00589 
00595         inline char *data(void) const
00596                 {return getText();};
00597 
00603         inline size_t length(void) const
00604                 {return strlen(getText());};
00605 
00611         inline size_t size(void) const
00612                 {return getLength();};
00613 
00619         inline size_t capacity(void) const
00620                 {return getSize();};
00621 
00625         bool empty(void) const
00626                 {return isEmpty();};
00627 
00634         void append(const char *str, size_t count = 0);
00635 
00636 #ifdef  HAVE_SNPRINTF
00637 
00643         void append(size_t size, const char *format, ...);
00644 #endif
00645 
00653         void append(const char *str, size_t offset, size_t count);
00654 
00660         void add(char c);
00661 
00667         void append(const String &str);
00668 
00674         inline const char operator[](unsigned ind) const
00675                 {return at(ind);};
00676 
00680         inline const char *operator =(const char *str)
00681                 {return set(str);};
00682 
00686         friend __EXPORT String operator+(const String &s1, const String &s2);
00687 
00688         friend __EXPORT String operator+(const String &s1, const char *s2);
00689 
00690         friend __EXPORT String operator+(const char *s1, const String &s2);
00691 
00692         friend __EXPORT String operator+(const String &s1, const char c2);
00693         
00694         friend __EXPORT String operator+(const char c1, const String &s2);
00695 
00699         inline String &operator+=(const String &str)
00700                 {append(str); return *this;};
00701 
00705         inline String &operator+=(char c)
00706                 {add(c); return *this;};
00707 
00711         inline String &operator+=(const char *str)
00712                 {append(str); return *this;};
00713 
00717         inline String &operator+=(const std::string &str)
00718                 {append(str.c_str()); return *this;};
00719 
00730         friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0);
00731 
00736         friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str);
00737 
00741         inline friend std::istream &operator>>(std::istream &is, String &str)
00742                 {return getline(is, str);};
00743 
00744 #ifdef  HAVE_SNPRINTF
00745 
00753         friend __EXPORT int strprintf(String &str, size_t size, const char *fmt, ...);
00754 #endif
00755 
00756         bool operator<(const String &str) const;
00757         bool operator<(const char *str) const;
00758         bool operator>(const String &str) const;
00759         bool operator>(const char *str) const;
00760         bool operator<=(const String &str) const;
00761         bool operator<=(const char *str) const;
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 
00769 #ifdef  HAVE_SNPRINTF
00770 
00774         inline String &operator+=(int i)
00775                 {append(16, "%d", i); return *this;};
00776 
00777         inline String &operator+=(unsigned int i)
00778                 {append(16, "%u", i); return *this;};
00779 
00780         inline String &operator+=(long l)
00781                 {append(16, "%l", l); return *this;};
00782 
00783         inline String &operator+=(unsigned long l)
00784                 {append(16, "%ul", l); return *this;};
00785 
00786         inline String &operator+=(float f)
00787                 {append(32, "%f", f); return *this;};
00788 
00789         inline String &operator+=(double d)
00790                 {append(32, "%f", d); return *this;};
00791 
00792         inline String &operator+=(short s)
00793                 {append(8, "%hd", s); return *this;};
00794 
00795         inline String &operator+=(unsigned short s)
00796                 {append(8, "%hu", s); return *this;};
00797 
00798 
00802         inline String &operator=(int i)
00803                 {set(16, "%d", i); return *this;};
00804 
00805         inline String &operator=(unsigned int i)
00806                 {set(16, "%u", i); return *this;};
00807 
00808         inline String &operator=(long l)
00809                 {set(16, "%l", l); return *this;};
00810 
00811         inline String &operator=(unsigned long l)
00812                 {set(16, "%ul", l); return *this;};
00813 
00814         inline String &operator=(float f)
00815                 {set(32, "%f", f); return *this;};
00816 
00817         inline String &operator=(double d)
00818                 {set(32, "%f", d); return *this;};
00819 
00820         inline String &operator=(short s)
00821                 {set(8, "%hd", s); return *this;};
00822 
00823         inline String &operator=(unsigned short s)
00824                 {set(8, "%hu", s); return *this;};
00825 #endif
00826 
00827         inline String &operator=(const String &original)
00828                 {copy(original); return *this;};
00829 
00833         bool operator*=(const String &str) const;
00834 
00838         bool operator*=(const char *str) const;
00839 };
00840 
00841 class __EXPORT SString : public String, protected std::streambuf, public std::ostream
00842 {
00843 protected:
00849         int overflow(int c);
00850 
00851 public:
00855         SString();
00856 
00860         SString(const SString &from);
00861 
00865         ~SString();
00866 };
00867 
00868 #ifdef  CCXX_NAMESPACES
00869 }
00870 #endif
00871 
00872 #endif

Generated on Tue Jan 18 14:32:37 2005 for GNU CommonC++ by  doxygen 1.3.9.1