Scopira 20080306

util.h

00001 
00002 /*
00003  *  Copyright (c) 2002-2007    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INLUCDED__SCOPIRA_TOOL_UTIL_H_
00015 #define __INLUCDED__SCOPIRA_TOOL_UTIL_H_
00016 
00017 #include <limits.h>
00018 #include <string>
00019 
00020 #include <scopira/tool/array.h>
00021 #include <scopira/tool/platform.h>
00022 #include <scopira/tool/export.h>
00023 
00024 // THIS FILE HAS BEEN FULLY DOCUMENTED
00025 
00026 namespace scopira
00027 {
00028   namespace tool
00029   {
00036     inline intptr_t ptr_to_int(const void *ptr) { return reinterpret_cast<intptr_t>(ptr); }
00042     template <class C>
00043       inline C * int_to_ptr(intptr_t x) { return reinterpret_cast<C*>(x); }
00048     inline void * int_to_void(intptr_t x) { return reinterpret_cast<void*>(x); }
00049 
00056     inline std::string bool_to_string(bool val){ if( val ) return "true"; else return "false"; };
00063     inline void int_to_string(bool val, std::string &out) { if( val ) out="true"; else out="false"; }
00064 
00071     SCOPIRA_EXPORT std::string int_to_string(int i);
00078     inline void int_to_string(int i, std::string &out) { out = int_to_string(i); }
00087     SCOPIRA_EXPORT int int_to_string(int i, char *out, int buflen);
00094     SCOPIRA_EXPORT int string_to_int(const std::string &s);
00102     SCOPIRA_EXPORT bool string_to_int(const std::string &s, int &outint);
00103 
00110     SCOPIRA_EXPORT std::string long_to_string(long i);
00117     inline void long_to_string(long i, std::string &out) { out = long_to_string(i); }
00124     SCOPIRA_EXPORT long string_to_long(const std::string &s);
00132     SCOPIRA_EXPORT bool string_to_long(const std::string &s, long &outlong);
00133 
00140     SCOPIRA_EXPORT std::string size_t_to_string(size_t i);
00147     inline void size_t_to_string(size_t i, std::string &out) { out = size_t_to_string(i); }
00154     SCOPIRA_EXPORT size_t string_to_size_t(const std::string &s);
00162     SCOPIRA_EXPORT bool string_to_size_t(const std::string &s, size_t &outsize_t);
00163 
00170     SCOPIRA_EXPORT std::string int64_t_to_string(int64_t i);
00177     inline void int64_t_to_string(int64_t i, std::string &out) { out = int64_t_to_string(i); }
00184     SCOPIRA_EXPORT int64_t string_to_int64_t(const std::string &s);
00192     SCOPIRA_EXPORT bool string_to_int64_t(const std::string &s, int64_t &outint64_t);
00193 
00200     SCOPIRA_EXPORT std::string double_to_string(double i);
00207     inline void double_to_string(double i, std::string &out) { out = double_to_string(i); }
00215     SCOPIRA_EXPORT std::string double_to_string(double i, int pres);
00224     SCOPIRA_EXPORT int double_to_string(double i, char *out, int buflen);
00232     SCOPIRA_EXPORT std::string double_to_exp_string(double i, int pres = 25);
00239     SCOPIRA_EXPORT double string_to_double(const std::string &s);
00247     SCOPIRA_EXPORT bool string_to_double(const std::string &s, double &outdouble);
00248 
00256     template <class T>
00257       char compare(const T &lhs, const T &rhs) {
00258         if (lhs<rhs)
00259           return -1;
00260         else if (rhs<lhs)
00261           return 1;
00262         else
00263           return 0;
00264       }
00265 
00273     SCOPIRA_EXPORT void uppercase(const std::string& s, std::string& upper);
00280     SCOPIRA_EXPORT std::string uppercase(const std::string& s);
00288     SCOPIRA_EXPORT void lowercase(const std::string& s, std::string& lower);
00295     SCOPIRA_EXPORT std::string lowercase(const std::string& s);
00296 
00297 
00307     SCOPIRA_EXPORT std::string pad_left(const std::string &s, int width, char padchar = ' ');
00308 
00309     // internal func
00310     SCOPIRA_EXPORT void impl_cstring_to_fixed_array_impl(const char *in, size_t srclen, char *out, size_t N) throw();
00311 
00317     template <size_t N>
00318       inline void string_to_fixed_array(const std::string &src, fixed_array<char, N> &out) throw()
00319         { impl_cstring_to_fixed_array_impl(src.c_str(), src.size(), out.c_array(), N); }
00325     template <size_t N>
00326       inline void c_string_to_fixed_array(const char *in, fixed_array<char, N> &out) throw()
00327         { impl_cstring_to_fixed_array_impl(in, in?strlen(in):0, out.c_array(), N); }
00328 
00339     SCOPIRA_EXPORT bool split_char(const std::string &src, char split, std::string &left_out, std::string &right_out);
00340 
00350     SCOPIRA_EXPORT std::string filename_number(const std::string &fname, int num);
00351 
00358     SCOPIRA_EXPORT void trim_left(std::string& str);
00359 
00366     void trim_right(std::string& str);
00367 
00374     template<class container>
00375       void string_tokenize_word(const std::string& src, container& result, const std::string& substr)
00376     {
00377       std::string::size_type start = 0;
00378       std::string::size_type pos;
00379 
00380       // clear vector
00381       result.clear();
00382 
00383       // loop through source string to split
00384       while ((pos = src.find(substr, start)) != std::string::npos) {
00385         // push back found sub-string
00386         result.push_back(src.substr(start, pos-start));
00387         // advance starting position
00388         start = pos + substr.length();
00389       }
00390 
00391       if (start != src.length())
00392         result.push_back(src.substr(start, src.length()-start)); // push back last sub-string
00393     }
00394 
00405     template<class container>
00406       void string_tokenize(const std::string& src, container& result, const std::string& delimiters = " \t\n")
00407     {
00408       const std::string::size_type len = src.length();
00409       std::string::size_type i = 0;
00410 
00411       result.clear();
00412 
00413       while (i < len) {
00414         // remove leading whitespace
00415         i = src.find_first_not_of(delimiters, i);
00416         if (i == std::string::npos)
00417             return;   // nothing left but white space
00418 
00419         // find the end of the token
00420         std::string::size_type j = src.find_first_of(delimiters, i);
00421 
00422         // push token
00423         if (j == std::string::npos) {
00424             result.push_back(src.substr(i));
00425             return;
00426         } else
00427             result.push_back(src.substr(i, j-i));
00428 
00429         // set up for next loop
00430         i = j + 1;
00431       }
00432     }//string_tokenize
00433 
00438     template <class T>
00439       inline T byte_swap(T x) { return x; }
00440 
00446     template <>
00447       inline short byte_swap<short>(short x)
00448     {
00449       return
00450         ( (x & 0xFF) << 8 ) |
00451         ( (x & 0xFF00) >> 8 );
00452     }
00453 
00459     template <>
00460       inline int byte_swap<int>(int x)
00461     {
00462       return
00463         ( (x & 0xFF) << 24 ) |
00464         ( (x & 0xFF00) << 8 ) |
00465         ( (x & 0xFF0000) >> 8 ) |
00466         ( (x & 0xFF000000) >> 24 );
00467     }
00472     template <>
00473       inline int64_t byte_swap<int64_t>(int64_t x)
00474     {
00475       return
00476         ( (x & 0xFFll) << (8*7) ) |
00477         ( (x & 0xFF00ll) << (8*5) ) |
00478         ( (x & 0xFF0000ll) << (8*3) ) |
00479         ( (x & 0xFF000000ll) << 8 ) |
00480         ( (x & 0xFF00000000ll) >> 8 ) |
00481         ( (x & 0xFF0000000000ll) >> (8*3) ) |
00482         ( (x & 0xFF000000000000ll) >> (8*5) ) |
00483         ( (x & 0xFF00000000000000ll) >> (8*7) );
00484     }
00485 
00490     template <>
00491       inline uint64_t byte_swap<uint64_t>(uint64_t x)
00492     {
00493       return
00494         ( (x & 0xFFull) << (8*7) ) |
00495         ( (x & 0xFF00ull) << (8*5) ) |
00496         ( (x & 0xFF0000ull) << (8*3) ) |
00497         ( (x & 0xFF000000ull) << 8 ) |
00498         ( (x & 0xFF00000000ull) >> 8 ) |
00499         ( (x & 0xFF0000000000ull) >> (8*3) ) |
00500         ( (x & 0xFF000000000000ull) >> (8*5) ) |
00501         ( (x & 0xFF00000000000000ull) >> (8*7) );
00502     }
00503 
00509     template <>
00510       inline float byte_swap<float>(float x)
00511     {
00512       float ret;
00513       char *I = reinterpret_cast<char*>(&x);
00514       char *O = reinterpret_cast<char*>(&ret);
00515       O[0] = I[3];
00516       O[1] = I[2];
00517       O[2] = I[1];
00518       O[3] = I[0];
00519       return ret;
00520     }
00521 
00527     template <>
00528       inline double byte_swap<double>(double x)
00529     {
00530       double ret;
00531       char *I = reinterpret_cast<char*>(&x);
00532       char *O = reinterpret_cast<char*>(&ret);
00533       O[0] = I[7];
00534       O[1] = I[6];
00535       O[2] = I[5];
00536       O[3] = I[4];
00537       O[4] = I[3];
00538       O[5] = I[2];
00539       O[6] = I[1];
00540       O[7] = I[0];
00541       return ret;
00542     }
00543 
00548     template <class ITER>
00549       void byte_swap_all(ITER head, ITER tail);
00550   }//namespace scopira::tool
00551 }
00552 
00553 template <class ITER>
00554   void scopira::tool::byte_swap_all(ITER head, ITER tail)
00555 {
00556   for (; head != tail; ++head)
00557     *head = byte_swap(*head);
00558 }
00559 
00560 #endif
00561