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 wstring StringHelper::toWStr(string inString) {
00044         wstring retVal;
00045 
00046         //LPCWSTR retPtr = new wchar_t[retVal.length() + 1];
00047         for (std::string::const_iterator i = inString.begin(); i != inString.end(); i++) {
00048                 retVal.append(1, *i);
00049         }
00050         
00051 
00052         return retVal;
00053 }
00054 
00055 
00056 string StringHelper::toNarrowStr(wstring inString) {
00057         string retVal;
00058 
00059         //TODO::: This conversion may result in loss of data. Warning stays.
00060 
00061         //LPCWSTR retPtr = new wchar_t[retVal.length() + 1];
00062         for (std::wstring::const_iterator i = inString.begin(); i != inString.end(); i++) {
00063                 retVal.append(1, (char) *i);
00064         }
00065         
00066 
00067         return retVal;
00068 }
00069 
00070 string StringHelper::numToString(LOOG_UINT64 inNum) {
00071         char locDigit = 0;
00072         string retStr = "";
00073         string temp = "";
00074 
00075         if (inNum == 0) return "0";
00076 
00077         while (inNum > 0) {
00078                 locDigit = ((char)(inNum % 10)) + '0';
00079                 inNum /= 10;
00080                 temp = locDigit;
00081                 temp.append(retStr);
00082                 retStr = temp;
00083                 //retStr.append(1, locDigit);
00084         }
00085         return retStr;
00086 }
00087 
00088 //Returns a value between 0 and 9 999 999 to represent a fraction / 10 000 000
00089 LOOG_UINT64 StringHelper::stringToFractNum(string inString) {
00090         int locDigit = 0;
00091         LOOG_UINT64 retVal = 0;
00092 
00093         LOOG_UINT64 locMult = 1000000;
00094 
00095         size_t locStrLen = inString.length();
00096 
00097         for (unsigned long i = 0; i < locStrLen; i++) {
00098                 locDigit = inString[i] - '0';
00099                 //If it's not in the range 0-9 we bail out
00100                 if ( !((locDigit >= 0) && (locDigit <=9)) ) {
00101                         //FIX::: throw exception
00102                         throw 0;
00103                 }
00104                 //retVal *= 10;
00105                 retVal += (locDigit * locMult);
00106                 locMult /= 10;
00107 
00108         }
00109         return retVal;
00110 
00111 }
00112 
00113 LOOG_UINT64 StringHelper::stringToNum(string inString) {
00114         int locDigit = 0;
00115         LOOG_UINT64 retVal = 0;
00116         size_t locStrLen = inString.length();
00117 
00118         for (unsigned long i = 0; i < locStrLen; i++) {
00119                 locDigit = inString[i] - '0';
00120                 //If it's not in the range 0-9 we bail out
00121                 if ( !((locDigit >= 0) && (locDigit <=9)) ) {
00122                         //FIX::: throw exception
00123                         throw 0;
00124                 }
00125                 retVal *= 10;
00126                 retVal += locDigit;
00127 
00128         }
00129         return retVal;
00130 
00131 }
00132 
00133 
00134 unsigned char StringHelper::digitToHex(unsigned char inDigit) {
00135         
00136         unsigned char locDigit = (inDigit > 9)          ?       (inDigit  - 10) + A_BASE
00137                                                                                                 :       (inDigit) + ZERO_BASE;
00138         return locDigit;
00139 
00140 }
00141 
00142 string StringHelper::charToHexString(unsigned char inChar) {
00143         
00144         string retStr ="";
00145         retStr +=digitToHex(inChar / 16);
00146 
00147         retStr+= digitToHex(inChar % 16);
00148         return retStr;
00149 }
00150 

Generated on Tue Feb 15 14:54:22 2005 for oggdsf by  doxygen 1.3.9