![[]](/images/special/trans.gif)
LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference babelFish.cppGo to the documentation of this file.00001 /************************************************************************ 00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library 00003 Copyright (C) 2002 Aaron Cameron 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 A copy of the GNU Lesser General Public License has been provided with 00020 this library in the file 'COPYING'. 00021 00022 Contact information for the author of this library has been provided 00023 with this library in the file 'AUTHOR'. 00024 ************************************************************************/ 00025 #include "babelFish.h" 00026 00027 #include <stdio.h> 00028 #include <stdlib.h> 00029 00030 namespace n2l 00031 { 00032 00033 // These figures include a potential negative sign and the null character 00034 #define MaxInt8CharLength tUint(5) 00035 #define MaxInt16CharLength tUint(7) 00036 #define MaxInt32CharLength tUint(16) 00037 #define MaxDoubleCharLength tUint(32) 00038 00039 /**************************************************************************/ 00040 const tString asString( const tUint32 iNum ) 00041 { 00042 static char buffer[ MaxInt32CharLength ]; 00043 sprintf(buffer,"%lu",(unsigned long)(iNum)); 00044 return tString(buffer); 00045 } 00046 00047 00048 /**************************************************************************/ 00049 const tString asString( const tSint32 iNum ) 00050 { 00051 static char buffer[ MaxInt32CharLength ]; 00052 sprintf(buffer,"%li",(long)(iNum)); 00053 return tString(buffer); 00054 } 00055 00056 00057 /**************************************************************************/ 00058 const tString asString( const tDouble iNum, 00059 const tUint iPrecision ) 00060 { 00061 static char buffer[ MaxDoubleCharLength ]; 00062 // Welcome to the ugliest, dumbest hack in the WORLD! 00063 switch (iPrecision) 00064 { 00065 case 0: 00066 sprintf(buffer,"%.0f",iNum); 00067 break; 00068 case 1: 00069 sprintf(buffer,"%.1f",iNum); 00070 break; 00071 case 2: 00072 sprintf(buffer,"%.2f",iNum); 00073 break; 00074 case 3: 00075 sprintf(buffer,"%.3f",iNum); 00076 break; 00077 case 4: 00078 sprintf(buffer,"%.4f",iNum); 00079 break; 00080 case 5: 00081 sprintf(buffer,"%.5f",iNum); 00082 break; 00083 case 6: 00084 sprintf(buffer,"%.6f",iNum); 00085 break; 00086 default: 00087 sprintf(buffer,"%.7f",iNum); 00088 break; 00089 00090 } 00091 return tString(buffer); 00092 } 00093 00094 00095 /**************************************************************************/ 00096 const tString asString( const tBool iBool ) 00097 { 00098 if (iBool) return "1"; 00099 return "0"; 00100 } 00101 00102 00103 /**************************************************************************/ 00104 const tString asBinaryMaskString( const tUint32 iMask ) 00105 { 00106 return _n2l::asArbitraryBinaryMaskString( iMask,32 ); 00107 } 00108 00109 00110 /**************************************************************************/ 00111 const tUint32 asUint32( const tString & iNum ) 00112 { 00113 if (iNum.size()) return atol(iNum.c_str()); 00114 return 0; 00115 } 00116 00117 00118 /**************************************************************************/ 00119 const tSint32 asSint32( const tString & iNum ) 00120 { 00121 if (iNum.size()) return atol(iNum.c_str()); 00122 return 0; 00123 } 00124 00125 00126 /**************************************************************************/ 00127 const tUint16 asUint16( const tString & iNum ) 00128 { 00129 if (iNum.size()) return atoi(iNum.c_str()); 00130 return 0; 00131 } 00132 00133 00134 /**************************************************************************/ 00135 const tSint16 asSint16( const tString & iNum ) 00136 { 00137 if (iNum.size()) return atoi(iNum.c_str()); 00138 return 0; 00139 } 00140 00141 00142 /**************************************************************************/ 00143 const tUint8 asUint8( const tString & iNum ) 00144 { 00145 if (iNum.size()) return tUint8(atoi(iNum.c_str())); 00146 return 0; 00147 } 00148 00149 00150 /**************************************************************************/ 00151 const tSint8 asSint8( const tString & iNum ) 00152 { 00153 if (iNum.size()) return tUint8(atoi(iNum.c_str())); 00154 return 0; 00155 } 00156 00157 00158 /**************************************************************************/ 00159 const tFloat asFloat( const tString & iNum ) 00160 { 00161 if (iNum.size()) return float(atof(iNum.c_str())); 00162 return 0.0f; 00163 } 00164 00165 00166 /**************************************************************************/ 00167 const tDouble asDouble( const tString & iNum ) 00168 { 00169 if (iNum.size()) return atof(iNum.c_str()); 00170 return 0.0; 00171 } 00172 00173 /**************************************************************************/ 00174 const tString &ordinalSuffix( const tUint &iNum ) 00175 { 00176 static const tString smSt = "st"; 00177 static const tString smNd = "nd"; 00178 static const tString smRd = "rd"; 00179 static const tString smTh = "th"; 00180 00181 switch (iNum%10) { 00182 case 1: 00183 return smSt; 00184 00185 case 2: 00186 return smNd; 00187 00188 case 3: 00189 return smRd; 00190 00191 default: 00192 return smTh; 00193 } 00194 } 00195 00196 /**************************************************************************/ 00197 const tBool isNumericStr( const tString & iString, 00198 const bool iDecimalsAllowed ) 00199 { 00200 if (iString.empty()) return false; 00201 tString::const_iterator charIt = iString.begin(); 00202 const tString::const_iterator CharsEndIt = iString.end(); 00203 for (; CharsEndIt!=charIt; ++charIt) 00204 if ((*charIt < '0' || *charIt > '9') && 00205 (*charIt != '-') && 00206 (!iDecimalsAllowed || *charIt != '.')) return false; 00207 return true; 00208 } 00209 00210 00211 } // namespace n2l 00212 00213 00214 namespace _n2l 00215 { 00216 using namespace n2l; 00217 00218 const tString asArbitraryBinaryMaskString( const tUint32 i_mask, 00219 const tUint8 i_bits ) 00220 { 00221 tUint32 cMask = 0x01; 00222 tString response(i_bits,'0'); 00223 for (tSint16 bit=i_bits-1; bit>=0; --bit,cMask=cMask<<1) 00224 if (i_mask&cMask) response[bit] = '1'; 00225 return response; 00226 } 00227 } 00228 |