00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest 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 Beesnest 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 Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* DcBuffer.h: interface for the CdcBuffer class. */ 00021 00022 #ifndef __CDC_BUFFER 00023 #define __CDC_BUFFER 00024 00025 typedef char t_buff_unit; 00026 00027 #include "DcGlobals.h" 00028 #include "DcException.h" 00029 #include _GET_INCLUDE_PATH (DcMutex.h) 00030 00031 #define DEFAULT_SIZE 1024 00032 00033 /** 00034 * CdcBuffer is a class that makes a buffer to store t_buff_unit (char) data. 00035 * The user can append the data in the end of the buffer, and then get a read 00036 * only pointer to the inner array. 00037 * 00038 * <p> 00039 * This buffer is working with the principles of "Copy On Write" and "Reference 00040 * Counting". The copy constructor and the assignment operator will copy just 00041 * the value of the pointers (shallow copy). Copying of the entire array will 00042 * occur just when one object is going to write into the array. 00043 * 00044 * <p> 00045 * This is dynamic allocated t_buff_unit array. The array can grow as necessary, 00046 * but it cannot get smaller. 00047 * 00048 * <p> 00049 * <b> It is very important that the user use the pointer to the buffer as a 00050 * READ ONLY buffer. </b> 00051 * 00052 * <p> 00053 * There is also an option for direct accessing every cell by the read only '[]' 00054 * operator. 00055 * 00056 * <p> 00057 * This class is thread safe. You can work with one object from several 00058 * different threads. 00059 * 00060 * <p> 00061 * I added two functions that doesn't directly connected to a buffer. 00062 * <b> Find </b> and <b> ReplaceAll </b> are in use just with HTML pages that 00063 * will be stored in the buffer. They will allow the system to manipulate HTML 00064 * pages before sending them to the client. 00065 * 00066 * <p> 00067 * @author Erez Bibi 00068 * @version 1.0 00069 */ 00070 class CdcBuffer 00071 { 00072 private: 00073 /** The pointer to the inner array. */ 00074 t_buff_unit* aBuffer; 00075 00076 /** Pointer to the reference counter. */ 00077 int* pRefCount; 00078 00079 /** The current size of the array. */ 00080 int nSize; 00081 00082 /** The length of the data in the array. */ 00083 int nDataSize; 00084 00085 /** The size to increase the buffer space on each allocation. */ 00086 int nGrow; 00087 00088 /** Pointer to a Mutex for the thread safe feature. */ 00089 CdcMutex* pMutex; 00090 00091 00092 /** 00093 * This function will be called just before a write operation. It detaches 00094 * the object from it's reference and make the real copy. 00095 * <p> 00096 * The function checks if there is a place to insert 'size' more cells in 00097 * the buffer. If not it reallocates the buffer, with the new size, and copy 00098 * the old data to the beginning of the new buffer. 00099 * <p> 00100 * @param (int) size The number of cells to fill. 00101 * @throw (CdcException) Out of memory exception. 00102 */ 00103 void PreperRoom (int size) throw (CdcException); 00104 00105 00106 /** 00107 * This function makes a shallow copy of <b>buff</b> into <b>this</b> and 00108 * increases by one the reference counter. 00109 * <p> 00110 * @param buff Reference to the buffer to copy. 00111 */ 00112 void Attach (const CdcBuffer& buff); 00113 00114 /** 00115 * This function releases this object from pointing to the buffer, and 00116 * decreases the reference counter by one. If the reference counter is zero 00117 * it deletes the buffer and the counter. 00118 */ 00119 void Detach (); 00120 00121 public: 00122 00123 /** 00124 * Constructor that construct an empty buffer. 00125 */ 00126 CdcBuffer (); 00127 00128 /** 00129 * Constructor that construct a buffer with initial size, and 00130 * a grow step. 00131 * <p> 00132 * @param size The initial size of the buffer. 00133 * @param grow The amount to increase the buffer space each time 00134 * it needs to grow. 00135 * @throw (CdcException) Out of memory exception. 00136 */ 00137 CdcBuffer (unsigned int size, int grow = DEFAULT_SIZE) throw (CdcException); 00138 00139 /** 00140 * Copy constructor - Shallow copy one buffer into the other. 00141 * <p> 00142 * @param buff The buffer to copy. 00143 */ 00144 CdcBuffer (const CdcBuffer& buff) 00145 { Attach (buff); } 00146 00147 /** 00148 * Destructor - Free the dynamic allocated memory. 00149 */ 00150 virtual ~CdcBuffer() 00151 { Detach (); } 00152 00153 /** 00154 * Assignment Operator - Shallow copy one buffer into the other. 00155 * <p> 00156 * @param buff The buffer to copy. 00157 * @return (CdcBuffer&) A reference to the buffer (this). 00158 */ 00159 const CdcBuffer& operator= (const CdcBuffer& buff); 00160 00161 00162 /** 00163 * Operator [] for reading the content of one cell in the buffer. 00164 * <p> 00165 * @param index The index to read form. 00166 * @return (t_buff_unit) Read only value of specific cell. 00167 * @throw (CdcException) Index out of bounds. 00168 */ 00169 t_buff_unit operator[] (int index) const throw (CdcException); 00170 00171 00172 /** 00173 * Append functions. Append some data to the end of the buffer. 00174 * <p> 00175 * This is a set of overloaded functions. 00176 * <p> 00177 * @param data The data to append. 00178 * @return (CdcBuffer&) A reference to the buffer (this). 00179 * @throw (CdcException) Out of memory exception. 00180 */ 00181 CdcBuffer& Append (const t_buff_unit data) throw (CdcException); 00182 CdcBuffer& Append (CdcString& data) throw (CdcException); 00183 CdcBuffer& Append (const CdcBuffer& data) throw (CdcException); 00184 CdcBuffer& Append (int data) throw (CdcException); 00185 /** Here if 'length' > 0 it copies 'length' cells from the array, if 00186 'length' = -1 it copies the array until the first 0. */ 00187 CdcBuffer& Append (const t_buff_unit* data, int length = -1) throw (CdcException); 00188 CdcBuffer& Append (const string& data) 00189 { return Append (data.c_str ()); } 00190 00191 /** 00192 * Remove all data from the buffer. Does not change the length of the 00193 * buffer. 00194 */ 00195 void RemoveAll () { PreperRoom (0); nDataSize = 0; } 00196 00197 /** 00198 * Get the maximum size of the buffer (without another allocation). 00199 * <p> 00200 * @return (int) The size of the buffer (array). 00201 */ 00202 int GetSize () const; 00203 00204 /** 00205 * Get the current number of cells in the buffer. 00206 * @return (int) The number of elements in the buffer. 00207 */ 00208 int GetDataSize () const; 00209 00210 00211 /** 00212 * Get a pointer to the inner array of the buffer 00213 * <p> 00214 * <b> Use this pointer as read only pointer! </b> 00215 * <p> 00216 * @return (t_buff_unit) A pointer to the inner buffer array. 00217 */ 00218 t_buff_unit* GetPointer () const; 00219 00220 00221 /** 00222 * This function tries to find a C style string inside the buffer. 00223 * <p> 00224 * This is a Read Only operation. It does not lock the buffer. 00225 * <p> 00226 * @param str Pointer to the string to find (or CdcString). 00227 * @param start The index in the buffer to start to look for <b>str</b>. 00228 * 0 is the default. 00229 * @param stop The index in the buffer to stop the searchat . 0 is the 00230 * default and mean to search the entire buffer. 00231 * @return (int) The first index of <b>str</b> inside the buffer. 00232 * If <b>str</b> is not in the buffer the function returns -1. 00233 */ 00234 int Find (const char* str, int start = 0, int stop = 0) const; 00235 int Find (CdcString& str, int start = 0, int stop = 0) const 00236 { return Find (str.GetBuffer (), start, stop); } 00237 00238 00239 /** 00240 * This find function finds the first specific char in a buffer. 00241 * All parameters and return value are the same as Find for strings. 00242 */ 00243 int Find (char chr, int start = 0, int stop = 0) const; 00244 00245 /** 00246 * This function will replace all the occurrences of the C style 00247 * string <b>strold</b> with the string <b>strnew</b>. 00248 * <p> 00249 * This function writes into the buffer. 00250 * <p> 00251 * @param strold Pointer to the string to find and replace. 00252 * @param strnew Pointer to the new string to insert. 00253 * @param stroldlen The length of the string to find and replace 00254 * -1 is the default and mean string until NULL. 00255 * @param strnewlen The length of the new string to insert. 00256 * -1 is the default and mean string until NULL. 00257 * @param start The place to start looking for <b>strold</b>. 0 is the 00258 * default. 00259 * @param stop The place t stop looking for <b>strold</b>. 0 is the 00260 * default and mean 'stop at the end of buffer'. 00261 * 00262 * @return (int) The number of replacements that took place. 00263 */ 00264 int ReplaceAll (const char* strold, const char* strnew, 00265 int stroldlen = -1, int strnewlen = -1, 00266 int start = 0, int stop = 0); 00267 00268 00269 /** 00270 * This is the ReplaceAll version for chars. Much more simple... 00271 * The returned value is the same as in ReplaceAll for strings. 00272 */ 00273 int ReplaceAll (char old_chr, char new_chr); 00274 00275 00276 /** 00277 * This function returns True if all chars in sections are white-spaces. 00278 * <p> 00279 * @param start The place to start the scan (0 is the default) 00280 * @param stop The place to stop the scan (0 is the default and will scan 00281 * to the end of the buffer). 00282 * @return True if the entire section is white spaces, otherwise False. 00283 */ 00284 bool IsWhiteSpaces (int start = 0, int stop = 0); 00285 }; 00286 00287 #endif /* __CDC_BUFFER */