Stephen Paulger / ttmath

Dependents:   PIDHeater82 Conceptcontroller_v_1_0 AlarmClockApp COG4050_adxl355_tilt ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ttmathmisc.h Source File

ttmathmisc.h

Go to the documentation of this file.
00001 /*
00002  * This file is a part of TTMath Bignum Library
00003  * and is distributed under the (new) BSD licence.
00004  * Author: Tomasz Sowa <t.sowa@ttmath.org>
00005  */
00006 
00007 /* 
00008  * Copyright (c) 2006-2010, Tomasz Sowa
00009  * All rights reserved.
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are met:
00013  * 
00014  *  * Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  *    
00017  *  * Redistributions in binary form must reproduce the above copyright
00018  *    notice, this list of conditions and the following disclaimer in the
00019  *    documentation and/or other materials provided with the distribution.
00020  *    
00021  *  * Neither the name Tomasz Sowa nor the names of contributors to this
00022  *    project may be used to endorse or promote products derived
00023  *    from this software without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00035  * THE POSSIBILITY OF SUCH DAMAGE.
00036  */
00037 
00038 #ifndef headerfilettmathmisc
00039 #define headerfilettmathmisc
00040 
00041 
00042 /*!
00043     \file ttmathmisc.h
00044     \brief some helpful functions
00045 */
00046 
00047 
00048 #include <string>
00049 
00050 
00051 namespace ttmath
00052 {
00053 
00054 /*!
00055     some helpful functions
00056 */
00057 class Misc 
00058 {
00059 public:
00060 
00061 
00062 /*
00063  *
00064  *    AssignString(result, str)
00065  *    result = str
00066  *
00067  */
00068 
00069 /*!
00070     result = str
00071 */
00072 static void AssignString (std::string & result, const char * str)
00073 {
00074     result = str;
00075 }
00076 
00077 
00078 #ifndef TTMATH_DONT_USE_WCHAR
00079 
00080 /*!
00081     result = str
00082 */
00083 static void AssignString (std::wstring & result, const char * str)
00084 {
00085     result.clear();
00086 
00087     for( ; *str ; ++str )
00088         result += *str;
00089 }
00090 
00091 
00092 /*!
00093     result = str
00094 */
00095 static void AssignString (std::wstring & result, const std::string & str)
00096 {
00097     return AssignString (result, str.c_str());
00098 }
00099 
00100 
00101 /*!
00102     result = str
00103 */
00104 static void AssignString (std::string & result, const wchar_t * str)
00105 {
00106     result.clear();
00107 
00108     for( ; *str ; ++str )
00109         result += static_cast<char>(*str);
00110 }
00111 
00112 
00113 /*!
00114     result = str
00115 */
00116 static void AssignString (std::string & result, const std::wstring & str)
00117 {
00118     return AssignString (result, str.c_str());
00119 }
00120 
00121 #endif
00122 
00123 
00124 /*
00125  *
00126  *    AddString(result, str)
00127  *    result += str
00128  *
00129  */
00130 
00131 
00132 /*!
00133     result += str
00134 */
00135 static void AddString (std::string & result, const char * str)
00136 {
00137     result += str;
00138 }
00139 
00140 
00141 #ifndef TTMATH_DONT_USE_WCHAR
00142 
00143 /*!
00144     result += str
00145 */
00146 static void AddString (std::wstring & result, const char * str)
00147 {
00148     for( ; *str ; ++str )
00149         result += *str;
00150 }
00151 
00152 #endif
00153 
00154 
00155 /*
00156     this method omits any white characters from the string
00157     char_type is char or wchar_t
00158 */
00159 template<class char_type>
00160 static void SkipWhiteCharacters(const char_type * & c)
00161 {
00162     // 13 is at the end in a DOS text file (\r\n)
00163     while( (*c==' ' ) || (*c=='\t') || (*c==13 ) || (*c=='\n') )
00164         ++c;
00165 }
00166 
00167 
00168 
00169 
00170 /*!
00171     this static method converts one character into its value
00172 
00173     for example:
00174         1 -> 1
00175         8 -> 8
00176         A -> 10
00177         f -> 15
00178 
00179     this method don't check whether c is correct or not
00180 */
00181 static uint  CharToDigit (uint  c)
00182 {
00183     if(c>='0' && c<='9')
00184         return c-'0';
00185 
00186     if(c>='a' && c<='z')
00187         return c-'a'+10;
00188 
00189 return c-'A'+10;
00190 }
00191 
00192 
00193 /*!
00194     this method changes a character 'c' into its value
00195     (if there can't be a correct value it returns -1)
00196 
00197     for example:
00198     c=2, base=10 -> function returns 2
00199     c=A, base=10 -> function returns -1
00200     c=A, base=16 -> function returns 10
00201 */
00202 static sint CharToDigit (uint  c, uint  base)
00203 {
00204     if( c>='0' && c<='9' )
00205         c=c-'0';
00206     else
00207     if( c>='a' && c<='z' )
00208         c=c-'a'+10;
00209     else
00210     if( c>='A' && c<='Z' )
00211         c=c-'A'+10;
00212     else
00213         return -1;
00214 
00215 
00216     if( c >= base )
00217         return -1;
00218 
00219 
00220 return sint(c);
00221 }
00222 
00223 
00224 
00225 /*!
00226     this method converts a digit into a char
00227     digit should be from <0,F>
00228     (we don't have to get a base)
00229     
00230     for example:
00231         1  -> 1
00232         8  -> 8
00233         10 -> A
00234         15 -> F
00235 */
00236 static uint  DigitToChar (uint  digit)
00237 {
00238     if( digit < 10 )
00239         return digit + '0';
00240 
00241 return digit - 10 + 'A';
00242 }
00243 
00244 
00245 }; // struct Misc
00246 
00247 }
00248 
00249 
00250 #endif
00251