include/Strings/BString.hpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of the bstring string library.  This code was
00003  * written by Paul Hsieh in 2002 - 2006, and is covered by the BSD open source 
00004  * license. Refer to the accompanying documentation for details on usage and 
00005  * license.
00006  */
00007 
00008 /*
00009  * bstrwrap.h
00010  *
00011  * This file is the C++ wrapper for the bstring functions.
00012  */
00013 
00014 #ifndef BSTRWRAP_INCLUDE
00015 #define BSTRWRAP_INCLUDE
00016 
00017 
00019 
00020 // By default it is assumed that your compiler can deal with and has enabled
00021 // exception handlling.  If this is not the case then you will need to 
00022 // #define BSTRLIB_DOESNT_THROW_EXCEPTIONS
00023 #if !defined(BSTRLIB_THROWS_EXCEPTIONS) && !defined(BSTRLIB_DOESNT_THROW_EXCEPTIONS)
00024 #define BSTRLIB_THROWS_EXCEPTIONS
00025 #endif
00026 
00028 
00029 #include "BString/bstrlib.h"
00030 
00031 #ifdef __cplusplus
00032 
00033 
00034 // This is required for disabling exception handling 
00035 #define bstringThrow(er) do{} while(0)
00036 // Assignment operator not obvious 
00037 #ifdef _MSC_VER
00038 #pragma warning(disable:4512)
00039 #endif
00040 
00041 
00042 namespace Bstrlib 
00043 {
00045     struct  String;
00046     
00048     class  CharWriteProtected 
00049     {
00050         // Members
00051     private:
00053         friend struct String;
00055         const struct tagbstring& s;
00057         const unsigned int idx;
00058 
00060         CharWriteProtected(const struct tagbstring& c, int i) : s(c), idx((unsigned int)i) 
00061             {   if (idx >=(unsigned) s.slen) bstringThrow("character index out of bounds"); }
00062         
00063         // Operators
00064     public:
00066         inline char operator=(char c) 
00067         {
00068             if (s.mlen <= 0)
00069                 bstringThrow("Write protection error");
00070             else 
00071             {
00072 #ifndef BSTRLIB_THROWS_EXCEPTIONS
00073                 if (idx >=(unsigned) s.slen)
00074                     return '\0';
00075 #endif
00076                 s.data[idx] = (unsigned char) c;
00077             }
00078             return (char) s.data[idx];
00079         }
00080         
00082         inline unsigned char operator=(unsigned char c) 
00083         {
00084             if (s.mlen <= 0)
00085                 bstringThrow("Write protection error");
00086             else 
00087             {
00088 #ifndef BSTRLIB_THROWS_EXCEPTIONS
00089                 if (idx >=(unsigned) s.slen)
00090                     return '\0';
00091 #endif
00092                 s.data[idx] = c;
00093             }
00094             return s.data[idx];
00095         }
00097         inline operator unsigned char() const 
00098         {
00099 #ifndef BSTRLIB_THROWS_EXCEPTIONS
00100             if (idx >=(unsigned) s.slen)
00101                 return (unsigned char) '\0';
00102 #endif
00103             return s.data[idx];
00104         }
00105     };
00106     
00108     struct String : public tagbstring 
00109     {
00110     // Construction and destruction
00111     //public:
00113         String();
00115         String(char c);
00116         String(unsigned char c);
00117         String(char c, int len);
00119         String(const char *s);
00122         String(int len, const char *s);
00123 
00125         String(const String& b);
00126         String(const tagbstring& x);
00128         String(const void * blk, int len);
00129     
00131         ~String();
00132 
00133     // Operators
00134     //public:       
00135 
00137         const String& operator=(char c);
00138         const String& operator=(unsigned char c);
00139         const String& operator=(const char *s);
00140         const String& operator=(const String& b);
00141         const String& operator=(const tagbstring& x);
00142         
00144         const String& operator +=(char c);
00145         const String& operator +=(unsigned char c);
00146         const String& operator +=(const char *s);
00147         const String& operator +=(const String& b);
00148         const String& operator +=(const tagbstring& x);
00149         
00151         inline const String& operator *=(int count) 
00152         {
00153             this->Repeat(count);
00154             return *this;
00155         }
00156         
00158         const String operator+(char c) const;
00159         const String operator+(unsigned char c) const;
00160         const String operator+(const unsigned char *s) const;
00161         const String operator+(const char *s) const;
00162         const String operator+(const String& b) const;
00163         const String operator+(const tagbstring& x) const;
00164         
00166         inline const String operator * (int count) const 
00167         {
00168             String retval(*this);
00169             retval.Repeat(count);
00170             return retval;
00171         }
00172         
00174         bool operator ==(const String& b) const;
00175         bool operator ==(const char * s) const;
00176         bool operator ==(const unsigned char * s) const;
00177         bool operator !=(const String& b) const;
00178         bool operator !=(const char * s) const;
00179         bool operator !=(const unsigned char * s) const;
00180         bool operator <(const String& b) const;
00181         bool operator <(const char * s) const;
00182         bool operator <(const unsigned char * s) const;
00183         bool operator <=(const String& b) const;
00184         bool operator <=(const char * s) const;
00185         bool operator <=(const unsigned char * s) const;
00186         bool operator >(const String& b) const;
00187         bool operator >(const char * s) const;
00188         bool operator >(const unsigned char * s) const;
00189         bool operator >=(const String& b) const;
00190         bool operator >=(const char * s) const;
00191         bool operator >=(const unsigned char * s) const;
00192         
00193     // Casting 
00194     //public:
00196         inline operator const char* () const            { return (const char *)data; }
00197         inline operator const unsigned char* () const   { return (const unsigned char *)data; }
00198 
00200         operator double() const;
00202         operator float() const;
00204         operator signed int() const;
00206         operator unsigned int() const;
00207         
00208     // Accessors
00209     //public:
00211         inline int getLength() const {return slen;}
00213         inline unsigned char Character(int i) const 
00214         {
00215             if (((unsigned) i) >=(unsigned) slen)
00216             {
00217 #ifdef BSTRLIB_THROWS_EXCEPTIONS
00218                 bstringThrow("character idx out of bounds");
00219 #else
00220                 return '\0';
00221 #endif
00222             }
00223             return data[i];
00224         }
00226         inline unsigned char operator[](int i) const { return Character(i); }
00228         inline CharWriteProtected Character(int i) { return CharWriteProtected(*this, i); }
00229         inline CharWriteProtected operator[](int i) { return Character(i); }
00230         
00232         char * Alloc(int length);
00233         
00234         // Search methods.
00236         int caselessEqual(const String& b) const;
00238         int caselessCmp(const String& b) const;
00240         int Find(const String& b, int pos = 0) const;
00242         int Find(const char * b, int pos = 0) const;
00244         int caselessFind(const String& b, int pos = 0) const;
00246         int caselessFind(const char * b, int pos = 0) const;
00248         int Find(char c, int pos = 0) const;
00250         int reverseFind(const String& b, int pos) const;
00252         int reverseFind(const char * b, int pos) const;
00254         int caselessReverseFind(const String& b, int pos) const;
00256         int caselessReverseFind(const char * b, int pos) const;
00258         int reverseFind(char c, int pos) const;
00260         int findAnyChar(const String& b, int pos = 0) const;
00262         int findAnyChar(const char * s, int pos = 0) const;
00264         int reverseFindAnyChar(const String& b, int pos) const;
00266         int reverseFindAnyChar(const char * s, int pos) const;
00268         int invFindAnyChar(const String& b, int pos = 0) const;
00270         int invFindAnyChar(const char * b, int pos = 0) const;
00272         int invReverseFindAnyChar(const String& b, int pos) const;
00274         int invReverseFindAnyChar(const char * b, int pos) const;
00276         int Count(const String & b) const;
00278         String extractToken(char c, int & pos) const;
00279         
00280         // Search and substitute methods.
00282         void findAndReplace(const String& find, const String& repl, int pos = 0);
00284         void findAndReplace(const String& find, const char * repl, int pos = 0);
00286         void findAndReplace(const char * find, const String& repl, int pos = 0);
00288         void findAndReplace(const char * find, const char * repl, int pos = 0);
00290         void findAndReplaceCaseless(const String& find, const String& repl, int pos = 0);
00292         void findAndReplaceCaseless(const String& find, const char * repl, int pos = 0);
00294         void findAndReplaceCaseless(const char * find, const String& repl, int pos = 0);
00296         void findAndReplaceCaseless(const char * find, const char * repl, int pos = 0);
00297         
00298         // Extraction method.
00300         const String midString(int left, int len) const;
00301         
00302         // Standard manipulation methods.
00304         void setSubstring(int pos, const String& b, unsigned char fill = ' ');
00306         void setSubstring(int pos, const char * b, unsigned char fill = ' ');
00308         void Insert(int pos, const String& b, unsigned char fill = ' ');
00310         void Insert(int pos, const char * b, unsigned char fill = ' ');
00312         void insertChars(int pos, int len, unsigned char fill = ' ');
00315         void Replace(int pos, int len, const String& b, unsigned char fill = ' ');
00318         void Replace(int pos, int len, const char * s, unsigned char fill = ' ');
00320         void Remove(int pos, int len);
00322         void Truncate(int len);
00323         
00324         // Miscellaneous methods.
00326         void Scan(const char * fmt, void * data) const;
00328         String & Format(const char * fmt, ...);
00330         void Formata(const char * fmt, ...);
00332         void Fill(int length, unsigned char fill = ' ');
00334         void Repeat(int count);
00336         void leftTrim(const String& b = String(" \t\v\f\r\n", sizeof(" \t\v\f\r\n")));
00338         void rightTrim(const String& b = String(" \t\v\f\r\n", sizeof(" \t\v\f\r\n")));
00340         inline void Trim(const String& b = String(" \t\v\f\r\n", sizeof(" \t\v\f\r\n"))) 
00341         {
00342             rightTrim(b);
00343             leftTrim(b);
00344         }
00346         void toUppercase();
00348         void toLowercase();
00349         
00350         // Write protection methods.
00352         void writeProtect();
00354         void writeAllow();
00356         inline bool isWriteProtected() const { return mlen <= 0; }
00357 
00358         // Unlocking mechanism for char * like access
00360         inline void releaseLock(const int & len ) { slen = len; mlen = slen + 1; }
00361     };
00362 
00363     extern const String operator+(const char *a, const String& b);
00364     extern const String operator+(const unsigned char *a, const String& b);
00365     extern const String operator+(char c, const String& b);
00366     extern const String operator+(unsigned char c, const String& b);
00367     extern const String operator+(const tagbstring& x, const String& b);
00368     inline const String operator * (int count, const String& b) 
00369     {
00370         String retval(b);
00371         retval.Repeat(count);
00372         return retval;
00373     }
00374 
00375 } // namespace Bstrlib
00376 /*
00377 #if !defined(BSTRLIB_DONT_ASSUME_NAMESPACE)
00378 using namespace Bstrlib;
00379 #endif
00380 */
00381 #endif
00382 #endif

(C) An X-Ryl669 project 2007

This document describes Unlimited Zooming Interface source code. UZI stands for Unlimited Zooming Interface, and source code license is