rtconversions.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon May 10 19:10:29 CEST 2004  rtconversions.cxx
00003 
00004                         rtconversions.cxx -  description
00005                            -------------------
00006     begin                : Mon May 10 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 #include "os/rtconversions.hpp"
00039 
00040 #include <string>
00041 #include <cstdio>
00042 #include <cstdlib>
00043 #include <stdio.h>
00044 
00045 using namespace std;
00046 
00055 int string_to_int( const string& s )
00056 {
00057     bool neg = false;
00058     int hulp = 0;
00059 
00060     for ( unsigned int i = 0; i < s.size(); i++ )
00061     {
00062         if ( ( i == 0 ) && ( s[ 0 ] == '-' ) )
00063         {
00064             neg = true;
00065             continue;
00066         }
00067         if ( ( s[ i ] >= '0' ) && ( s[ i ] <= '9' ) )
00068         {
00069             hulp *= 10;
00070             hulp += ( s[ i ] - '0' );
00071             continue;
00072         }
00073         else
00074         {
00075             // error detected
00076             return 0;
00077         }
00078     }
00079 
00080     if ( neg )
00081         hulp *= -1;
00082 
00083     return hulp;
00084 }
00085 
00086 // float string_to_float(const string& s)
00087 // {
00088 //  float f;
00089 //  sscanf(s.c_str(),"%f", &f);
00090 //  return f;
00091 // };
00092 
00093 char string_to_char( const string& s )
00094 {
00095     if ( s.length() > 0 )
00096     {
00097         return s[ 0 ];
00098     }
00099 
00100     else
00101     {
00102         return '0';
00103     }
00104 
00105 }
00106 
00107 unsigned int string_to_unsigned_int( const string& s )
00108 {
00109     unsigned int hulp = 0;
00110 
00111     for ( unsigned int i = 0; i < s.size(); i++ )
00112     {
00113         if ( ( s[ i ] >= '0' ) && ( s[ i ] <= '9' ) )
00114         {
00115             hulp *= 10;
00116             hulp += ( s[ i ] - '0' );
00117             continue;
00118         }
00119 
00120         break;
00121     }
00122 
00123     return hulp;
00124 }
00125 
00126 string int_to_string( int i )
00127 {
00128     string hulp;
00129 
00130     if ( i < 0 )
00131     {
00132         hulp += '-';
00133         i *= -1;
00134     }
00135 
00136     if ( i == 0 )
00137     {
00138         hulp = "0";
00139         return hulp;
00140     }
00141 
00142     int t = 1000000000;
00143     bool start = true;
00144 
00145     for ( int j = 0; t != 0; j++ )
00146     {
00147         if ( start )
00148         {
00149             if ( ( i / t ) == 0 )
00150             {
00151                 t /= 10;
00152                 continue;
00153             }
00154 
00155             else
00156             {
00157                 start = false;
00158             }
00159         }
00160 
00161         hulp += ( '0' + ( i / t ) );
00162         i %= t;
00163         t /= 10;
00164     }
00165 
00166     return hulp;
00167 }
00168 
00169 string float_to_string(float f)
00170 {
00171     char buffer[ 128 ];
00172     buffer[ 127 ] = '\0';
00173     char s = ' '; // sign of integer
00174     int pre = int( f ); // voor de comma
00175     int post = int( ( f - pre ) * 1000.0 ); // na de comma
00176 
00177     if ( post != 0 )
00178         post = abs( post );
00179 
00180     if ( pre == 0 && f < 0.0 )
00181         s = '-';
00182     else
00183         s = ' ';
00184 
00185     //snprintf( buffer, 127, "%c%d.%03d", s, pre, post );
00186         //replacement with MSVC`s snprintf but it is not ALWAYS null terminating
00187 #ifdef _MSC_VER
00188         _snprintf( buffer, 127, "%c%d.%03d", s, pre, post );
00189 #else
00190         snprintf( buffer, 127, "%c%d.%03d", s, pre, post );
00191 #endif
00192     return string(buffer);
00193 }
00194 
00195 string unsigned_int_to_string( unsigned int u )
00196 {
00197     return int_to_string( u );
00198 }


rtt
Author(s): RTT Developers
autogenerated on Mon Oct 6 2014 03:13:39