StringHelper.cpp

Go to the documentation of this file.
00001 //===========================================================================
00002 //Copyright (C) 2003, 2004 Zentaro Kavanagh
00003 //
00004 //Redistribution and use in source and binary forms, with or without
00005 //modification, are permitted provided that the following conditions
00006 //are met:
00007 //
00008 //- Redistributions of source code must retain the above copyright
00009 //  notice, this list of conditions and the following disclaimer.
00010 //
00011 //- Redistributions in binary form must reproduce the above copyright
00012 //  notice, this list of conditions and the following disclaimer in the
00013 //  documentation and/or other materials provided with the distribution.
00014 //
00015 //- Neither the name of Zentaro Kavanagh nor the names of contributors 
00016 //  may be used to endorse or promote products derived from this software 
00017 //  without specific prior written permission.
00018 //
00019 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00022 //PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00023 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00024 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00025 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 //===========================================================================
00031 
00032 #include "stdafx.h"
00033 #include <libilliCore/StringHelper.h>
00034 
00035 StringHelper::StringHelper(void)
00036 {
00037 }
00038 
00039 StringHelper::~StringHelper(void)
00040 {
00041 }
00042 
00043 
00044 wstring StringHelper::toWStr(string inString) {
00045         wstring retVal;
00046 
00047         //LPCWSTR retPtr = new wchar_t[retVal.length() + 1];
00048         for (std::string::const_iterator i = inString.begin(); i != inString.end(); i++) {
00049                 retVal.append(1, *i);
00050         }
00051         
00052         return retVal;
00053 }
00054 
00055 string StringHelper::toUTF8Str(wstring inString) {
00056         string retVal;
00057 
00058         unsigned char a;
00059         //LPCWSTR retPtr = new wchar_t[retVal.length() + 1];
00060         for (std::wstring::const_iterator i = inString.begin(); i != inString.end(); i++) 
00061         {
00062                 // 0xxxxxxx
00063                 if (*i < 0x80)
00064                 {
00065                         retVal.append(1, (unsigned char) *i);
00066                 }
00067                 // 110xxxxx 10xxxxxx
00068                 else if (*i < 0x800)
00069                 {
00070                         a = (unsigned char)(0xC0 | (unsigned int)*i >> 6);
00071                         retVal.append(1, a);
00072                         a = (unsigned char)(0x80 | (unsigned int)*i & 0x3F);
00073                         retVal.append(1, a);
00074                 }
00075                 // 1110xxxx 10xxxxxx 10xxxxxx
00076                 else if (*i < 0x10000)
00077                 {
00078                         a = (unsigned char)(0xE0 | (unsigned int)*i >> 12);
00079                         retVal.append(1, a);
00080                         a = (unsigned char)(0x80 | (unsigned int)*i >> 6 & 0x3F);
00081                         retVal.append(1, a);
00082                         a = (unsigned char)(0x80 | (unsigned int)*i & 0x3F);
00083                         retVal.append(1, a);
00084                 }
00085                 // 1111xxxx 10xxxxxx 10xxxxxx 10xxxxxx
00086                 else if (*i < 0x200000)
00087                 {
00088                         a = (unsigned char)(0xF0 | (unsigned int)*i >> 18);
00089                         retVal.append(1, a);
00090                         a = (unsigned char)(0x80 | (unsigned int)*i >> 12 & 0x3F);
00091                         retVal.append(1, a);
00092                         a = (unsigned char)(0x80 | (unsigned int)*i >> 6 & 0x3F);
00093                         retVal.append(1, a);
00094                         a = (unsigned char)(0x80 | (unsigned int)*i & 0x3F);
00095                         retVal.append(1, a);
00096                 }
00097         }
00098 
00099         return retVal;
00100 }
00101 
00102 
00103 wstring StringHelper::fromUTF8Str(string inString) {
00104         wstring retVal;
00105 
00106     wchar_t a;
00107 
00108         //LPCWSTR retPtr = new wchar_t[retVal.length() + 1];
00109         for (unsigned int i=0; i < inString.length(); )
00110         {
00111                 // 0xxxxxxx
00112                 if ((inString[i] & 0x80) == 0)
00113                 {
00114                         a = inString[i];
00115                         i++;
00116                 }
00117                 // 1111xxxx 10xxxxxx 10xxxxxx 10xxxxxx
00118                 else if ((inString[i] & 0xF0) == 0xF0)
00119                 {
00120                         a = ((inString[i] & 0x0F) << 18) | 
00121                                 ((inString[i+1] & 0x3F) << 12) | 
00122                                 ((inString[i+2] & 0x3F) << 6) | 
00123                                 (inString[i+3] & 0x3F);
00124                         i += 3;
00125                 }
00126                 // 1110xxxx 10xxxxxx 10xxxxxx
00127                 else if ((inString[i] & 0xE0) == 0xE0)
00128                 {
00129                         a = ((inString[i] & 0x0F) << 12) | 
00130                                 ((inString[i+1] & 0x3F) << 6) | 
00131                                 (inString[i+2] & 0x3F);
00132                         i += 3;
00133                 }
00134                 // 110xxxxx 10xxxxxx
00135                 else if ((inString[i] & 0xC0) == 0xC0)
00136                 {
00137                         a = ((inString[i] & 0x1F) << 6) | (inString[i+1] & 0x3F);
00138                         i += 2;
00139                 }
00140                 else
00141                 {
00142                         // something has gone wrong. Get out!
00143                         break;
00144                 }
00145 
00146                 retVal.append(1, a);
00147         }
00148 
00149         return retVal;
00150 }
00151 
00152 string StringHelper::toNarrowStr(wstring inString) {
00153         string retVal;
00154 
00155         //TODO::: This conversion may result in loss of data. Warning stays.
00156 
00157         //LPCWSTR retPtr = new wchar_t[retVal.length() + 1];
00158         for (std::wstring::const_iterator i = inString.begin(); i != inString.end(); i++) {
00159                 retVal.append(1, (char) *i);
00160         }
00161         
00162 
00163         return retVal;
00164 }
00165 
00166 string StringHelper::numToString(LOOG_UINT64 inNum) {
00167         char locDigit = 0;
00168         string retStr = "";
00169         string temp = "";
00170 
00171         if (inNum == 0) return "0";
00172 
00173         while (inNum > 0) {
00174                 locDigit = ((char)(inNum % 10)) + '0';
00175                 inNum /= 10;
00176                 temp = locDigit;
00177                 temp.append(retStr);
00178                 retStr = temp;
00179                 //retStr.append(1, locDigit);
00180         }
00181         return retStr;
00182 }
00183 
00184 //Returns a value between 0 and 9 999 999 to represent a fraction / 10 000 000
00185 LOOG_UINT64 StringHelper::stringToFractNum(string inString) {
00186         int locDigit = 0;
00187         LOOG_UINT64 retVal = 0;
00188 
00189         LOOG_UINT64 locMult = 1000000;
00190 
00191         size_t locStrLen = inString.length();
00192 
00193         for (unsigned long i = 0; i < locStrLen; i++) {
00194                 locDigit = inString[i] - '0';
00195                 //If it's not in the range 0-9 we bail out
00196                 if ( !((locDigit >= 0) && (locDigit <=9)) ) {
00197                         //FIX::: throw exception
00198                         throw 0;
00199                 }
00200                 //retVal *= 10;
00201                 retVal += (locDigit * locMult);
00202                 locMult /= 10;
00203 
00204         }
00205         return retVal;
00206 
00207 }
00208 
00209 LOOG_UINT64 StringHelper::stringToNum(string inString) {
00210         int locDigit = 0;
00211         LOOG_UINT64 retVal = 0;
00212         size_t locStrLen = inString.length();
00213 
00214         for (unsigned long i = 0; i < locStrLen; i++) {
00215                 locDigit = inString[i] - '0';
00216                 //If it's not in the range 0-9 we bail out
00217                 if ( !((locDigit >= 0) && (locDigit <=9)) ) {
00218                         //FIX::: throw exception
00219                         throw 0;
00220                 }
00221                 retVal *= 10;
00222                 retVal += locDigit;
00223 
00224         }
00225         return retVal;
00226 
00227 }
00228 
00229 
00230 unsigned char StringHelper::digitToHex(unsigned char inDigit) {
00231         
00232         unsigned char locDigit = (inDigit > 9)          ?       (inDigit  - 10) + A_BASE
00233                                                                                                 :       (inDigit) + ZERO_BASE;
00234         return locDigit;
00235 
00236 }
00237 
00238 string StringHelper::charToHexString(unsigned char inChar) {
00239         
00240         string retStr ="";
00241         retStr +=digitToHex(inChar / 16);
00242 
00243         retStr+= digitToHex(inChar % 16);
00244         return retStr;
00245 }
00246 

Generated on Thu Feb 16 23:48:18 2006 for oggdsf by  doxygen 1.3.9