Main Page   Class Hierarchy   Compound List   File List   Compound Members  

baStringUtil.cpp

00001 //////////////////////////////////////////////////////////////////////
00002 // Copyright (c) 2001-2002 David Pritchard <drpritch@alumni.uwaterloo.ca>
00003 // 
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either
00007 // version 2 of the License, or (at your option) any later
00008 // version.
00009 // 
00010 // This program 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
00013 // GNU Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 #include <freecloth/base/baStringUtil.h>
00020 #include <freecloth/base/debug.h>
00021 #include <freecloth/base/string>
00022 #include <freecloth/base/ctype.h>
00023 
00024 FREECLOTH_NAMESPACE_START
00025 
00026 ////////////////////////////////////////////////////////////////////////////////
00027 // CLASS BaStringUtil
00028 
00029 //------------------------------------------------------------------------------
00030 
00031 String BaStringUtil::toLower( const String& s )
00032 {
00033     String result( s );
00034 
00035     for( UInt32 i = 0; i < s.length(); ++i ) {
00036         if( isupper( s[ i ] ) ) {
00037             result[ i ] = tolower( s[ i ] );
00038         }
00039     }
00040     return result; 
00041 } 
00042  
00043 //------------------------------------------------------------------------------
00044 
00045 String BaStringUtil::toUpper( const String& s )
00046 {
00047     String result( s );
00048 
00049     for( UInt32 i = 0; i < s.length(); ++i ) {
00050         if( islower( s[ i ] ) ) {
00051             result[ i ] = toupper( s[ i ] );
00052         }
00053     }
00054     return result; 
00055 } 
00056 
00057 //------------------------------------------------------------------------------
00058 
00059 Float BaStringUtil::toFloat( const String& s )
00060 {
00061     return ::atof( s.c_str() );
00062 } 
00063 
00064 //------------------------------------------------------------------------------
00065 
00066 Int32 BaStringUtil::toInt32( const String& s )
00067 {
00068     return ::atol( s.c_str() );
00069 } 
00070 
00071 //------------------------------------------------------------------------------
00072 
00073 String BaStringUtil::fromUInt32( UInt32 n )
00074 {
00075     String result;
00076 
00077     if ( n == 0 ) {
00078         result = "0";
00079     }
00080     else {
00081         char temp[ 2 ] = "1";
00082         // FIXME: reserve space in result in advance...
00083         while ( n != 0 ) {
00084             temp[ 0 ] = n % 10 + '0';
00085             result = temp + result;
00086             n /= 10;
00087         }
00088     }
00089     return result;
00090 }
00091 
00092 //------------------------------------------------------------------------------
00093 
00094 String BaStringUtil::fromUInt32( UInt32 n, UInt32 zeroPad )
00095 {
00096     char buf[ 256 ];
00097     DGFX_ASSERT( zeroPad < sizeof(buf) );
00098 
00099     buf[ zeroPad ] = 0;
00100     for ( UInt32 i = zeroPad; i > 0; --i ) {
00101         buf[ i - 1 ] = n % 10 + '0';
00102         n /= 10;
00103     }
00104     return String( buf );
00105 }
00106 
00107 //------------------------------------------------------------------------------
00108 
00109 String BaStringUtil::fromFloat( Float n )
00110 {
00111     char buf[ 64 ];
00112     ::sprintf( buf, "%f", n );
00113     return String( buf );
00114 }
00115 
00116 //------------------------------------------------------------------------------
00117 
00118 String BaStringUtil::fromFloat( Float n, UInt32 nbFracDigits )
00119 {
00120     String fmt( "%." + fromUInt32( nbFracDigits ) + "f" );
00121     char buf[ 64 ];
00122     ::sprintf( buf, fmt.c_str(), n );
00123     return String( buf );
00124 }
00125 
00126 FREECLOTH_NAMESPACE_END

Generated on Fri May 2 13:04:24 2003 for Freecloth by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002