tobicore  7.0.0
 All Classes Functions Variables Typedefs Enumerator Friends Groups Pages
TCTools.cpp
1 /*
2  Copyright (C) 2009-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
3  Michele Tavella <michele.tavella@epfl.ch>
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "TCTools.hpp"
20 #include "TCException.hpp"
21 #include <stdlib.h>
22 #include <string.h>
23 #include <cstdio>
24 #include <sstream>
25 
26 #ifdef __BORLANDC__
27 using namespace std;
28 #endif
29 
30 char* TCTools::itoa(int value, char* result, int base) {
31  // check that the base if valid
32  if (base < 2 || base > 36) { *result = '\0'; return result; }
33 
34  char* ptr = result, *ptr1 = result, tmp_char;
35  int tmp_value;
36 
37  do {
38  tmp_value = value;
39  value /= base;
40  *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
41  } while ( value );
42 
43  // Apply negative sign
44  if (tmp_value < 0) *ptr++ = '-';
45  *ptr-- = '\0';
46  while(ptr1 < ptr) {
47  tmp_char = *ptr;
48  *ptr--= *ptr1;
49  *ptr1++ = tmp_char;
50  }
51  return result;
52 }
53 
54 int TCTools::ftoa(float value, char* result) {
55  return sprintf(result, "%.6f", value);
56 }
57 
58 float TCTools::atof(const char* value) {
59  float retval = 0.00f;
60 
61  int fields = sscanf(value, "%f", &retval);
62  if(fields != 1)
63  throw TCException("Field number exceeded",
64  #ifdef _WIN32
65  __FUNCSIG__
66  #else
67  __PRETTY_FUNCTION__
68  #endif
69  );
70 
71  return retval;
72 }
73 
74 std::string TCTools::itos(int value) {
75  std::stringstream out;
76  out << value;
77  return out.str();
78 }