Orocos Real-Time Toolkit  2.8.3
rtconversions.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon May 10 19:10:29 CEST 2004 rtconversions.cxx
3 
4  rtconversions.cxx - description
5  -------------------
6  begin : Mon May 10 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 #include "os/rtconversions.hpp"
39 
40 #include <string>
41 #include <cstdio>
42 #include <cstdlib>
43 #include <stdio.h>
44 
45 using namespace std;
46 
55 int string_to_int( const string& s )
56 {
57  bool neg = false;
58  int hulp = 0;
59 
60  for ( unsigned int i = 0; i < s.size(); i++ )
61  {
62  if ( ( i == 0 ) && ( s[ 0 ] == '-' ) )
63  {
64  neg = true;
65  continue;
66  }
67  if ( ( s[ i ] >= '0' ) && ( s[ i ] <= '9' ) )
68  {
69  hulp *= 10;
70  hulp += ( s[ i ] - '0' );
71  continue;
72  }
73  else
74  {
75  // error detected
76  return 0;
77  }
78  }
79 
80  if ( neg )
81  hulp *= -1;
82 
83  return hulp;
84 }
85 
86 // float string_to_float(const string& s)
87 // {
88 // float f;
89 // sscanf(s.c_str(),"%f", &f);
90 // return f;
91 // };
92 
93 char string_to_char( const string& s )
94 {
95  if ( s.length() > 0 )
96  {
97  return s[ 0 ];
98  }
99 
100  else
101  {
102  return '0';
103  }
104 
105 }
106 
107 unsigned int string_to_unsigned_int( const string& s )
108 {
109  unsigned int hulp = 0;
110 
111  for ( unsigned int i = 0; i < s.size(); i++ )
112  {
113  if ( ( s[ i ] >= '0' ) && ( s[ i ] <= '9' ) )
114  {
115  hulp *= 10;
116  hulp += ( s[ i ] - '0' );
117  continue;
118  }
119 
120  break;
121  }
122 
123  return hulp;
124 }
125 
126 string int_to_string( int i )
127 {
128  string hulp;
129 
130  if ( i < 0 )
131  {
132  hulp += '-';
133  i *= -1;
134  }
135 
136  if ( i == 0 )
137  {
138  hulp = "0";
139  return hulp;
140  }
141 
142  int t = 1000000000;
143  bool start = true;
144 
145  for ( int j = 0; t != 0; j++ )
146  {
147  if ( start )
148  {
149  if ( ( i / t ) == 0 )
150  {
151  t /= 10;
152  continue;
153  }
154 
155  else
156  {
157  start = false;
158  }
159  }
160 
161  hulp += ( '0' + ( i / t ) );
162  i %= t;
163  t /= 10;
164  }
165 
166  return hulp;
167 }
168 
169 string float_to_string(float f)
170 {
171  char buffer[ 128 ];
172  buffer[ 127 ] = '\0';
173  char s = ' '; // sign of integer
174  int pre = int( f ); // voor de comma
175  int post = int( ( f - pre ) * 1000.0 ); // na de comma
176 
177  if ( post != 0 )
178  post = abs( post );
179 
180  if ( pre == 0 && f < 0.0 )
181  s = '-';
182  else
183  s = ' ';
184 
185  //snprintf( buffer, 127, "%c%d.%03d", s, pre, post );
186  //replacement with MSVC`s snprintf but it is not ALWAYS null terminating
187 #ifdef _MSC_VER
188  _snprintf( buffer, 127, "%c%d.%03d", s, pre, post );
189 #else
190  snprintf( buffer, 127, "%c%d.%03d", s, pre, post );
191 #endif
192  return string(buffer);
193 }
194 
195 string unsigned_int_to_string( unsigned int u )
196 {
197  return int_to_string( u );
198 }
string float_to_string(float f)
STL namespace.
int string_to_int(const string &s)
converts a string to an int
string unsigned_int_to_string(unsigned int u)
unsigned int string_to_unsigned_int(const string &s)
string int_to_string(int i)
char string_to_char(const string &s)