00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef MIMOTERAUTILS_HH
00024 #define MIMOTERAUTILS_HH
00025
00026
00027 #include <iostream>
00028 #include <sstream>
00029 #include <string>
00030 #include <iomanip>
00031 #include <vector>
00032
00033 namespace Mimotera {
00034
00036
00050 template <typename T>
00051 inline std::string to_string(const T & x, int digits = 0) {
00052 std::ostringstream s;
00053 s << std::setfill('0') << std::setw(digits) << x;
00054 return s.str();
00055 }
00056
00058
00072 template <typename T>
00073 inline std::string to_string(const std::vector<T> & x, int digits = 0) {
00074 std::ostringstream s;
00075 if (x.size() > 0) s << to_string(x[0], digits);
00076 for (size_t i = 1; i < x.size(); ++i) {
00077 s << "," << to_string(x[i], digits);
00078 }
00079 return s.str();
00080 }
00081
00082
00084
00093 inline std::string to_string(const std::string & x, int = 0) {
00094 return x;
00095 }
00096
00098
00107 inline std::string to_string(const char * x, int = 0) {
00108 return x;
00109 }
00110
00111
00113
00152 template <typename T>
00153 inline T getbigendian(const unsigned char * ptr) {
00154 #if (defined( __BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN) || \
00155 (defined(__DARWIN_BYTE_ORDER) && __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN)
00156 return *reinterpret_cast<const T *>(ptr);
00157 #else
00158 T result = 0;
00159 for (size_t i = 0; i < sizeof (T); ++i) {
00160 result <<= 8;
00161 result += *ptr++;
00162 }
00163 return result;
00164 #endif
00165 }
00166
00167
00169
00181 template <typename T>
00182 inline T getlittleendian(const unsigned char * ptr) {
00183 #if (defined( __BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
00184 (defined(__DARWIN_BYTE_ORDER) && __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
00185 return *reinterpret_cast<const T *>(ptr);
00186 #else
00187 T result = 0;
00188 for (size_t i = 0; i < sizeof (T); ++i) {
00189 result += *ptr++ << (8*i);
00190 }
00191 return result;
00192 #endif
00193 }
00194
00195
00196
00197 }
00198
00199 #endif
00200
00201