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 /* DcBuffersList.h: interface for the CdcBuffersList class. */ 00021 00022 #ifndef __CDC_BUFFERS_LIST 00023 #define __CDC_BUFFERS_LIST 00024 00025 /** 00026 * CdcBuffersList is a class to store sections of preallocated buffers in a 00027 * list. The user can add a new section to the list, or replace (parts of) an 00028 * existing section with a new buffer. The user can iterate through the list. 00029 * The default iterator place is in the end though. 00030 * 00031 * <p> 00032 * @author Erez Bibi 00033 * @version 1.0 00034 */ 00035 00036 #include "DcBuffer.h" 00037 #include "DcFileAttrib.h" 00038 00039 /* typedef char t_buff_unit - Come from DcBuffer.h */ 00040 00041 class CdcBuffersList 00042 { 00043 private: 00044 /** 00045 * This private inner class (struct) stores one section of a buffer. 00046 * It holds a "copy" of the entire buffer, but since this is just a 00047 * reference copy, we don't really copy the buffer. Same thing for the 00048 * FileAttrib object. 00049 */ 00050 class Buff 00051 { 00052 public: 00053 /* oBuffer holds a copy of the buffer just so it will not be deleted. */ 00054 CdcBuffer oBuff; 00055 00056 /* nOffset is the offset in the buffer for the section to store. */ 00057 int nOffset; 00058 00059 /* nLength is the length of the section to store. */ 00060 int nLength; 00061 00062 /* oFileAttrib holds a copy of the buffer file attributes, or an empty 00063 file attributes object, if there is not file for this buffer. */ 00064 CdcFileAttrib oFileAttrib; 00065 00066 /* Constructor */ 00067 Buff (const CdcBuffer& buff, const CdcFileAttrib& attr, 00068 int offset=0, int length=-1) 00069 :oBuff (buff), oFileAttrib (attr), nOffset (offset), nLength (length) 00070 {} 00071 00072 /* Constructor - No file attributes. */ 00073 Buff (const CdcBuffer& buff, int offset=0, int length=-1) 00074 :oBuff (buff), nOffset (offset), nLength (length) 00075 {} 00076 }; 00077 00078 /* This is our STL list. */ 00079 typedef list <Buff> t_buff_list; 00080 t_buff_list oBuffList; 00081 /* Iterator on this list. */ 00082 t_buff_list::iterator oIter; 00083 00084 public: 00085 /** Constructor - Set the iterator to the end of list. */ 00086 CdcBuffersList () {oIter = oBuffList.end ();} 00087 00088 /** Destructor - Just call Clear. */ 00089 virtual ~CdcBuffersList () {Clear ();} 00090 00091 /** 00092 * Clear - Clear the list from all buffers. The copy of each of the buffers 00093 * will be erased (count down one on the reference). 00094 */ 00095 void Clear (); 00096 00097 /** 00098 * Add a hole buffer just before the current node (this is the end of the 00099 * list by default). The function validates the parameters, so if offset + 00100 * length is more then the total buffer length it will (silently) not 00101 * insert the section to the list. 00102 * <p> 00103 * @param buff Reference to the buffer to add. When the buffer will be 00104 * copied into the list, the reference copy will be made. 00105 * @param attr Reference to a file attributes object. When the object will 00106 * be copied into the list, the reference copy will be made. 00107 * @param offset The offset in the buffer of the section to store. 00108 * Default is 0. 00109 * @param length The length of the section to store. Default is -1 which 00110 * mean the section is to the end of the buffer. 00111 */ 00112 void Add (const CdcBuffer& buff, const CdcFileAttrib& attr, 00113 int offset=0, int length=-1); 00114 00115 /** 00116 * Add function with empty file attributes (for buffers not from a file) 00117 */ 00118 void Add (const CdcBuffer& buff, int offset=0, int length=-1) 00119 { Add (buff, CdcFileAttrib (), offset, length); } 00120 00121 /** 00122 * Insert a hole new buffer in the middle of an existing one (old). 00123 * The function will split the old buffer into two sections. One will be 00124 * just before the new added buffer, and one just after it. In addition 00125 * a portion of the old buffer may be dropped. 00126 * <p> 00127 * For example: 00128 * <pre> 00129 * |old-buff-offset------------start---------length---------old-buff-len| 00130 * </pre> 00131 * old-buff-offset .. start - will be the first new buffer. 00132 * start .. length - will be dropped. 00133 * the new buffer will be the next one. 00134 * length .. old-buff-len - will be the third buffer. 00135 * <p> 00136 * The new current buffer will be the new buffer added in the middle. 00137 * <p> 00138 * This function inserts a buffer without file attributes. 00139 * As for now, I do not use this function! 00140 * <p> 00141 * @param buff The new buffer to insert. 00142 * @param start The function will cut the current buffer to this point. 00143 * If 'start' is 0 (default) the first part of the current buffer will 00144 * be dropped. 00145 * @param length The function will cut out this length of the current 00146 * buffer, and insert the rest just after the new buffer. If length is 00147 * -1 (default), the rest of the current buffer will be dropped. 00148 */ 00149 void Insert (const CdcBuffer& buff, int start=0, int length=-1); 00150 00151 /** 00152 * SeekStart get the values of GetPointer and GetLength to be of the 00153 * first section in the list 00154 * <p> 00155 * @return True if the list is not empty. If the list is empty, the 00156 * function returns False. In this case, calling GetPointer will 00157 * return NULL, and calling GetLength will return 0. 00158 */ 00159 bool SeekFirst (); 00160 00161 /** 00162 * SeekNext will move the current section to be the next one. 00163 * <p> 00164 * @return True if there is a next section in the list. If we are in the 00165 * end of the list (or the list is empty), this function will return 00166 * False. GetPointer and GetLength will return NULL and 0. 00167 */ 00168 bool SeekNext (); 00169 00170 /** 00171 * GetPointer returns a pointer to the current section in the list. 00172 * Note that the pointer is the buffer pointer + offset. 00173 * <p> 00174 * @return Pointer to a buffer (char*) or NULL if cannot return a 00175 * pointer (see SeekFirst and SeekNext). 00176 */ 00177 t_buff_unit* GetPointer (); 00178 00179 /** 00180 * GetLength return the length of the current section in the list. 00181 * <p> 00182 * @return The length of the section or 0 if cannot return the length 00183 * (see SeekFirst and SeekNext). 00184 */ 00185 int GetLength (); 00186 00187 /** 00188 * GetBufferPointer returns a const pointer to the current buffer in the 00189 * list. If no buffers are in the list it returns NULL. 00190 */ 00191 const CdcBuffer* GetBufferPointer (); 00192 00193 /** 00194 * GetBufferOffset returns the offset in the current buffer, or 0 if 00195 * there is no current buffer. 00196 */ 00197 int GetBufferOffset (); 00198 00199 /** 00200 * Returns the total number of bytes in the list. 00201 */ 00202 int GetDataSize (); 00203 00204 /** 00205 * Returns the file attribute object. Not every buffer has to have 00206 * a file attributes. 00207 */ 00208 CdcFileAttrib GetFileAttrib (); 00209 }; 00210 00211 #endif /* __CDC_BUFFER */