00001 /* 00002 * WOscLib, an object oriented OSC library. 00003 * Copyright (C) 2005 Uli Clemens Franke, Weiss Engineering LTD, Switzerland. 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library 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 GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 * 00019 * For details see lgpl.txt 00020 * 00021 * Weiss Engineering LTD. 00022 * Florastrass 42 00023 * 8610 Uster 00024 * Switzerland 00025 * 00026 * uli.franke@weiss.ch 00027 */ 00028 00029 /** WOscString source file. 00030 * \file 00031 * 00032 * $Author: cls-nebadje $ ( \ref _UcfWOscLib ) 00033 * $Date: 2006-03-20 19:41:37 $ 00034 * $Revision: 1.2 $ 00035 * 00036 * Copyright (c) Weiss Engineering Ltd 00037 * 00038 */ 00039 #include "WOscString.h" 00040 #include <string.h> 00041 #include "WOscUtil.h" 00042 #include "WOscException.h" 00043 00044 /** Constructs an OSC-String from a string. 00045 * Aligns it internally to a 4-byte boundary. 00046 * 00047 * \param string 00048 * Initializing string. 00049 */ 00050 WOscString::WOscString(const char* string){ 00051 /* get four byte aligned size */ 00052 m_bufferLen = WOscUtil::GetSizeFourByteAligned(string); 00053 /* allocate mem */ 00054 m_buffer = new char[m_bufferLen]; 00055 /* copy and pad */ 00056 WOscUtil::PadStringWithZeros(m_buffer, string); 00057 } 00058 00059 00060 /** Copy constructor. 00061 * Allocates new memory and copies the aligned string 00062 * from the referenced object. 00063 * 00064 * \param rhs 00065 * Reference of an OSC-string object. 00066 */ 00067 WOscString::WOscString(const WOscString& rhs){ 00068 /* allocate */ 00069 m_buffer = new char[m_bufferLen = rhs.m_bufferLen]; 00070 /* copy */ 00071 memcpy(m_buffer,rhs.m_buffer,m_bufferLen); 00072 } 00073 00074 /** Destructor. 00075 * Frees the memory required by the internal buffer (if not empty). 00076 */ 00077 WOscString::~WOscString() 00078 { 00079 delete [] m_buffer; 00080 } 00081 00082 /** OSC string char assignment operator. 00083 * Assigns the right-hand char to the OSC string object. 00084 * 00085 * \param rhs 00086 * Char beeing assigned to the current OSC string. 00087 * 00088 * \returns 00089 * Reference of the current OSC string object. 00090 * 00091 */ 00092 WOscString& WOscString::operator= (const char rhs){ 00093 if ( m_buffer ){ 00094 delete [] m_buffer; 00095 m_buffer = NULL; 00096 } 00097 return *this += rhs; 00098 } 00099 00100 /** String to OSC-String assignment operator. 00101 * Formats (padding) the right-hand string and copies 00102 * it into the left-hand object. Frees the old memory and 00103 * allocates new. 00104 * 00105 * \param rhs 00106 * Reference of the source string. 00107 * 00108 * \returns 00109 * Reference of the destination object. 00110 */ 00111 WOscString& WOscString::operator= (const char* rhs){ 00112 delete [] m_buffer; 00113 00114 /* get four byte aligned size */ 00115 m_bufferLen = WOscUtil::GetSizeFourByteAligned(rhs); 00116 /* allocate mem */ 00117 m_buffer = new char[m_bufferLen]; 00118 /* copy and pad */ 00119 WOscUtil::PadStringWithZeros(m_buffer, rhs); 00120 00121 return *this; 00122 } 00123 00124 /** Assignment operator. 00125 * Copies the right-hand object-reference into the 00126 * left-hand object. Frees the old memory and 00127 * allocates new. 00128 * 00129 * \param rhs 00130 * Reference of the source object. 00131 * 00132 * \returns 00133 * Reference of the destination object. 00134 */ 00135 WOscString& WOscString::operator= (const WOscString& rhs){ 00136 if ( m_buffer ) 00137 delete [] m_buffer; 00138 00139 m_buffer = new char[m_bufferLen = rhs.m_bufferLen]; 00140 memcpy(m_buffer,rhs.m_buffer,m_bufferLen); 00141 00142 return *this; 00143 } 00144 00145 /** OSC-String concatenation without assignment. 00146 * Appends the right hand object to the left-hand object but 00147 * does not save it in the left-hand object. 00148 * The zeros from the left hand object are removed and the 00149 * resulting OSC-string is padded to a multiple of 4. 00150 * 00151 * \param rhs 00152 * Reference to an object to append. 00153 * 00154 * \returns 00155 * The concatenated object. 00156 * 00157 * \remarks 00158 * Since the operator does not assign, the 00159 * return value can not be a reference, because 00160 * its an local and automatic variable. 00161 * 00162 */ 00163 WOscString WOscString::operator+ (const WOscString& rhs){ 00164 WOscString retVal; 00165 if ( m_buffer ){ 00166 retVal += *this; 00167 retVal += rhs; 00168 }else{ 00169 retVal = rhs; 00170 } 00171 return retVal; 00172 } 00173 00174 /** Character to OSC-String concatenation with assignment. 00175 * Appends the right hand char to the left-hand object and 00176 * saves it in the left-hand object. 00177 * The zeros from the left hand object are removed and the 00178 * resulting OSC-string is padded to a multiple of 4. 00179 * 00180 * \param rhs 00181 * Character to be appended to the left hand object. 00182 * 00183 * \returns 00184 * The reference to the concatenated object. 00185 */ 00186 WOscString& WOscString::operator+= (const char rhs){ 00187 char str[] = {rhs,'\0'}; 00188 return *this += str; 00189 } 00190 00191 /** String to OSC-String concatenation with assignment. 00192 * Formats and appends the right hand string to the left-hand object and 00193 * saves it in the left-hand object. 00194 * The zeros from the left hand object are removed and the 00195 * resulting OSC-string is padded to a multiple of 4. 00196 * 00197 * \param rhs 00198 * String to be appended to the left hand object. 00199 * 00200 * \returns 00201 * The reference to the concatenated object. 00202 */ 00203 WOscString& WOscString::operator+= (const char* rhs){ 00204 WOscString tmp(rhs); 00205 return *this += tmp; 00206 } 00207 00208 /** OSC-String concatenation with assignment. 00209 * Appends the right hand object to the left-hand object and 00210 * saves it in the left-hand object. 00211 * The zeros from the left hand object are removed and the 00212 * resulting OSC-string is padded to a multiple of 4. 00213 * 00214 * \param rhs 00215 * Reference to an object to append. 00216 * 00217 * \returns 00218 * The a reference to the concatenated object. 00219 */ 00220 WOscString& WOscString::operator+= (const WOscString& rhs){ 00221 00222 if ( m_buffer ){ 00223 00224 /* get length of both strings */ 00225 int lenLhs = (int)strlen(m_buffer); 00226 int lenRhs = (int)strlen(rhs.m_buffer); 00227 00228 /* get the size of the combined string aligned to four. */ 00229 int newLen = WOscUtil::GetSizeFourByteAligned(lenRhs + lenLhs + 1); 00230 00231 /* allocate memory */ 00232 char* newBuf = new char[newLen]; 00233 00234 /* copy left and right hand side */ 00235 memcpy(newBuf, m_buffer, lenLhs); 00236 memcpy(newBuf+lenLhs, rhs.m_buffer, lenRhs); 00237 00238 /* pad rest with zeros */ 00239 for ( int i = lenLhs + lenRhs; i < newLen; i++ ) 00240 newBuf[i] = '\0'; 00241 00242 delete [] m_buffer; // delete old buffer 00243 m_buffer = newBuf; // set new string buffer as buffer 00244 m_bufferLen = newLen; // set length 00245 00246 }else{ 00247 /* if empty, just assign */ 00248 *this = rhs; 00249 } 00250 return *this; 00251 } 00252 00253 /** Returns the size of the zero-padded OSC-string. 00254 * Can be used to allocate memory for instance. 00255 * 00256 * \returns 00257 * Size of the internal buffer. 00258 * 00259 * \remarks 00260 * Remains valid until the content of the WOscString-object is changed. See 00261 */ 00262 int WOscString::GetSize(){ 00263 return m_bufferLen; 00264 } 00265 00266 /** Fills the buffer with the internal (padded) OSC-string representation. 00267 * The user must supply a buffer of apropriate size. 00268 * 00269 * \param buffer 00270 * Caller supplied destination buffer. 00271 * 00272 * \param bufferLen 00273 * Length of the supplied buffer. 00274 * 00275 * \throws WOscException 00276 * When caller supplied buffer too small. 00277 * 00278 * \remarks 00279 * This interface makes sure that the buffer remains valid 00280 * when modifying the WOscString-object afterwards. (contrary to 00281 * WOscString::getBuffer() ) 00282 * 00283 * \see 00284 * WOscString::getBuffer() 00285 */ 00286 void 00287 WOscString::GetBuffer(char* buffer, int bufferLen) 00288 { 00289 if ( bufferLen < m_bufferLen ) 00290 throw WOscException(ERR_OSC_STR_BUFF_TOO_SMALL, __FILE__, __LINE__); 00291 00292 for ( int i = 0; i < bufferLen; i++ ) 00293 buffer[i] = m_buffer[i]; 00294 } 00295 00296 /** Returns a pointer to an internal OSC-string-buffer. 00297 * Please See remarks for safety issues. 00298 * 00299 * \returns 00300 * Pointer to the internal OSC-string buffer. 00301 * 00302 * \remarks 00303 * The pointer remains valid until the modification of this WOscString-object. 00304 * The next time the object is destination of an operation (check operators) 00305 * or the object will be deleted, this pointer possibly gets invalid. If you are 00306 * not sure about that issue, please use the other getBuffer()- function, which 00307 * copies the string. 00308 * 00309 * \see 00310 * WOscString::getBuffer(char* buffer, int bufferLen) 00311 */ 00312 const char* 00313 WOscString::GetBuffer() const 00314 { 00315 return m_buffer; 00316 } 00317 00318 00319 /** Comparison operator. 00320 * Compares the right-hand OSC string reference with the current OSC object. 00321 * 00322 * \param rhs 00323 * Reference of an OSC string object. 00324 * 00325 * \returns 00326 * True when equal, false when not equal. 00327 * 00328 */ 00329 bool 00330 WOscString::operator == (const WOscString& rhs) const 00331 { 00332 return (strcmp(m_buffer, rhs.m_buffer) == 0); 00333 } 00334 00335 bool 00336 WOscString::operator != (const WOscString& rhs) const 00337 { 00338 return (strcmp(m_buffer, rhs.m_buffer) != 0); 00339 } 00340 00341 bool 00342 WOscString::operator == (const char* str) const 00343 { 00344 return (strcmp(m_buffer, str) == 0); 00345 } 00346 00347 bool 00348 WOscString::operator != (const char* str) const 00349 { 00350 return (strcmp(m_buffer, str) != 0); 00351 } 00352 00353 /** Typecast operator for convenience. */ 00354 WOscString::operator const char*() const 00355 { 00356 return m_buffer; 00357 } 00358