Drizzled Public API Documentation

CSString.h

00001 /* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
00002  *
00003  * PrimeBase Media Stream for MySQL
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  *
00019  * Original author: Paul McCullagh (H&G2JCtL)
00020  * Continued development: Barry Leslie
00021  *
00022  * 2007-06-15
00023  *
00024  * CORE SYSTEM
00025  * This class encapsulates a basic string.
00026  *
00027  */
00028 
00029 #pragma once
00030 #ifndef __CSSTRING_H__
00031 #define __CSSTRING_H__
00032 #include <string.h>
00033 
00034 #include "CSDefs.h"
00035 #include "CSObject.h"
00036 
00037 #ifdef OS_MACINTOSH
00038 #include <CoreFoundation/CFString.h>
00039 #endif
00040 
00041 
00042 class CSString;
00043 
00044 /*
00045  * A unsigned 16-bit unicode character:
00046  */
00047 #define unichar     uint16_t
00048 
00049 /* CSStringBufferImpl is the string buffer implementation.
00050  * Of this implementation we have various types.
00051  */
00052 class CSStringBufferImpl {
00053 public:
00054   CSStringBufferImpl();
00055   CSStringBufferImpl(uint32_t grow);
00056   ~CSStringBufferImpl();
00057 
00058   void clear();
00059 
00060   void append(char ch);
00061 
00062   void append(const char *str, size_t len);
00063 
00064   void append(const char *str) {append(str, strlen(str));}
00065 
00066   void append(int value);
00067 
00068   void append(uint32_t value);
00069 
00070   void append(uint64_t value);
00071 
00072   char *getCString();
00073 
00074   char *getBuffer(uint32_t pos) { return iBuffer ? iBuffer + pos : NULL; }
00075 
00076   char *take();
00077   
00078   void setLength(uint32_t len);
00079 
00080   void setGrowSize(uint32_t size) { iGrow = size; }
00081 
00082   uint32_t length() { return myStrLen; }
00083 
00084   uint32_t ignore(uint32_t pos, char ch);
00085 
00086   uint32_t find(uint32_t pos, char ch);
00087 
00088   uint32_t trim(uint32_t pos, char ch);
00089 
00090   CSString *substr(uint32_t pos, uint32_t len);
00091 
00092   void take(CSStringBufferImpl *buf) {
00093     clear();
00094     iGrow = buf->iGrow;
00095     iSize = buf->iSize;
00096     myStrLen = buf->myStrLen;
00097     iBuffer = buf->take();
00098   }
00099 
00100 private:
00101   char *iBuffer;
00102   uint32_t iGrow;
00103   uint32_t iSize;
00104   uint32_t myStrLen;
00105 };
00106 
00107 class CSStringBuffer : public CSStringBufferImpl, public CSObject {
00108 public:
00109   CSStringBuffer(): CSStringBufferImpl(), CSObject() { }
00110   CSStringBuffer(uint32_t grow): CSStringBufferImpl(grow), CSObject() { }
00111 };
00112 
00113 class CSStaticStringBuffer : public CSStringBufferImpl, public CSStaticObject {
00114   virtual void finalize() { clear(); }
00115 
00116 public:
00117   CSStaticStringBuffer(): CSStringBufferImpl(), CSStaticObject() { }
00118   CSStaticStringBuffer(uint32_t grow): CSStringBufferImpl(grow), CSStaticObject() { }
00119 };
00120 
00121 class CSRefStringBuffer : public CSStringBufferImpl, public CSRefObject {
00122 public:
00123   CSRefStringBuffer(): CSStringBufferImpl(), CSRefObject() { }
00124   CSRefStringBuffer(uint32_t grow): CSStringBufferImpl(grow), CSRefObject() { }
00125 };
00126 
00127 class CSSyncStringBuffer : public CSStringBuffer, public CSSync {
00128 public:
00129   CSSyncStringBuffer(uint32_t growSize): CSStringBuffer(growSize), CSSync() { }
00130   CSSyncStringBuffer(): CSStringBuffer(), CSSync() { }
00131 };
00132 
00133 #define CS_CHAR   int
00134 
00135 class CSString : public CSRefObject {
00136 public:
00137   CSString();
00138   CSString(const char *cstr);
00139   virtual ~CSString();
00140 
00141   /*
00142    * Construct a string from a C-style UTF-8
00143    * string.
00144    */
00145   static CSString *newString(const char *cstr);
00146 
00147   /* Construct a string from a UTF-8 byte array: */
00148   static CSString *newString(const char *bytes, uint32_t len);
00149 
00150   /* Construct a string from string buffer: */
00151   static CSString *newString(CSStringBuffer *sb);
00152 
00153   /*
00154    * Returns a pointer to a UTF-8 string.
00155    * The returned string must be
00156    * not be freed by the caller.
00157    */
00158   virtual const char *getCString();
00159 
00160   /*
00161    * Return the character at a certain point:
00162    */
00163   virtual CS_CHAR charAt(uint32_t pos);
00164   virtual CS_CHAR upperCharAt(uint32_t pos);
00165   virtual void setCharAt(uint32_t pos, CS_CHAR ch);
00166 
00167   /*
00168    * Returns < 0 if this string is
00169    * sorted before val, 0 if equal,
00170    * > 0 if sortede after.
00171    * The comparison is case-insensitive.
00172    */
00173   virtual int compare(CSString *val);
00174   virtual int compare(const char *val, uint32_t len = ((uint32_t) 0xFFFFFFFF));
00175 
00176   /*
00177    * Case sensitive match.
00178    */
00179   virtual bool startsWith(uint32_t index, const char *);
00180 
00181   /* Returns the string length in characters. */
00182   virtual uint32_t length() { return myStrLen; }
00183   virtual void setLength(uint32_t len);
00184 
00185   virtual bool equals(const char *str);
00186 
00187   /*
00188    * Return a copy of this string.
00189    */
00190   virtual CSString *clone(uint32_t pos, uint32_t len);
00191 
00192   /*
00193    * Concatinate 2 strings.
00194    */
00195   virtual CSString *concat(CSString *str);
00196   virtual CSString *concat(const char *str);
00197 
00198   /* Return an upper case version of the string: */
00199   virtual CSString *toUpper();
00200 
00201   /*
00202    * Returns a case-insensitive has
00203    * value.
00204    */
00205   virtual uint32_t hashKey();
00206 
00207   /*
00208    * Locate the count'th occurance of the given
00209    * string, moving from left to right or right to
00210    * left if count is negative.
00211    *
00212    * The index of the first character is zero.
00213    * If not found, then index returned depends
00214    * on the search direction.
00215    *
00216    * Search from left to right will return
00217    * the length of the string, and search
00218    * from right to left will return 0.
00219    */
00220   virtual uint32_t locate(const char *, int32_t count);
00221   virtual uint32_t locate(uint32_t pos, const char *);
00222   virtual uint32_t locate(uint32_t pos, CS_CHAR ch);
00223 
00224   virtual uint32_t skip(uint32_t pos, CS_CHAR ch);
00225 
00226   virtual CSString *substr(uint32_t index, uint32_t size);
00227   virtual CSString *substr(uint32_t index);
00228 
00229   virtual CSString *left(const char *, int32_t count);
00230   virtual CSString *left(const char *);
00231 
00232   virtual CSString *right(const char *, int32_t count);
00233   virtual CSString *right(const char *);
00234 
00235   virtual bool startsWith(const char *);
00236   virtual bool endsWith(const char *);
00237 
00238   /* Return the next position in the string, but do
00239    * not go past the length of the string.
00240    */
00241   virtual uint32_t nextPos(uint32_t pos);
00242 
00243   virtual CSString *clone(uint32_t len);
00244   virtual CSString *clone();
00245 
00246   virtual CSObject *getKey();
00247 
00248   virtual int compareKey(CSObject *);
00249 
00250 private:
00251   char *myCString;
00252   uint32_t myStrLen;
00253 };
00254 
00255 #endif
00256