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 /** WOscUtil source file. 00030 * \file 00031 * 00032 * $Author: cls-nebadje $ ( \ref _UcfWOscLib ) 00033 * $Date: 2006-08-13 16:38:01 $ 00034 * $Revision: 1.3 $ 00035 * 00036 * Copyright (c) Weiss Engineering Ltd 00037 * 00038 */ 00039 #include "WOscUtil.h" 00040 #include "WOscLib.h" 00041 #include <string.h> 00042 00043 /** Get length of string and round up to the next multiple of 4. 00044 * Gets the size of the string passed as parameter and rounds it 00045 * up to the next multiple of four. 00046 * 00047 * \param string 00048 * String which size should be rounded. 00049 * 00050 * \returns 00051 * Rounded size of the string. 00052 * 00053 * \see 00054 * WOscUtil::getSizeFourByteAligned(const int length) 00055 */ 00056 int 00057 WOscUtil::GetSizeFourByteAligned(const char* string) 00058 { 00059 int length = (int)strlen(string) + 1; 00060 return length + ( 4 - length % 4 ) % 4; 00061 } 00062 00063 // 00064 /** Rounds up the given integer to the next multiple of 4. 00065 * Similar to getSizeFourByteAligned(const char* string). 00066 * 00067 * \param length 00068 * Integer which sould be rounded up to the next multiple of 4. 00069 * 00070 * \returns 00071 * The rounded integer. 00072 * 00073 * \see 00074 * getSizeFourByteAligned(const char* string) 00075 */ 00076 int 00077 WOscUtil::GetSizeFourByteAligned(const int length) 00078 { 00079 return length + ( 4 - length % 4 ) % 4; 00080 } 00081 00082 /** Copies the source string to the destination string 00083 * and pads it with zeros to the next multiple of four, 00084 * the destination buffer must be large enough. 00085 * 00086 * \param destString 00087 * Destination string buffer (length must be the rounded-up 00088 * length of the source string), which receives the padded 00089 * string. 00090 * 00091 * \param sourceString 00092 * The string which should be padded. 00093 * 00094 * \remarks 00095 * Works only with strings!!! 00096 * 00097 * \see 00098 * For determination of the destination string-buffer-length: 00099 * getSizeFourByteAligned(const char* string) 00100 */ 00101 void 00102 WOscUtil::PadStringWithZeros(char* destString, const char* sourceString) 00103 { 00104 int length = (int)strlen(sourceString) + 1; 00105 int osclength = GetSizeFourByteAligned(sourceString); 00106 00107 strcpy(destString, sourceString); 00108 00109 for ( int i = length; i < osclength; i++ ) 00110 destString[i] = '\0'; 00111 } 00112 00113 /** Copies the source buffer to the destination buffer 00114 * and pads the difference with zeros. 00115 * The destination buffer must be large enough, i.e. destLen >= srcLen. 00116 * 00117 * Works for every buffer, assumed destLen >= srcLen. If destLen is smaller 00118 * than srcLen only srcLen data is copied and nothing padded. 00119 * 00120 * \param destBuffer 00121 * Destination string buffer (length must be the rounded-up 00122 * length of the source string), which receives the padded 00123 * string. 00124 * 00125 * \param sourceBuffer 00126 * Source buffer. 00127 * 00128 * \param destLen 00129 * Size of the destination-buffer. 00130 * 00131 * \param srcLen 00132 * Size of the source-buffer. 00133 * 00134 * \see 00135 * padStringWithZeros(char* destString, const char* sourceString) 00136 */ 00137 void 00138 WOscUtil::PadBufferWithZeros(char* destBuffer, const char* sourceBuffer, 00139 int destLen, int srcLen) 00140 { 00141 if ( destLen <= srcLen ) 00142 memcpy(destBuffer, sourceBuffer, destLen); 00143 else { 00144 memcpy(destBuffer, sourceBuffer, srcLen); 00145 memset(destBuffer+srcLen, 0, destLen-srcLen); 00146 } 00147 }