Scopira  20080306
util.h
1 
2 /*
3  * Copyright (c) 2002-2007 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INLUCDED__SCOPIRA_TOOL_UTIL_H_
15 #define __INLUCDED__SCOPIRA_TOOL_UTIL_H_
16 
17 #include <limits.h>
18 #include <string>
19 
20 #include <scopira/tool/array.h>
21 #include <scopira/tool/platform.h>
22 #include <scopira/tool/export.h>
23 
24 // THIS FILE HAS BEEN FULLY DOCUMENTED
25 
26 namespace scopira
27 {
28  namespace tool
29  {
36  inline intptr_t ptr_to_int(const void *ptr) { return reinterpret_cast<intptr_t>(ptr); }
42  template <class C>
43  inline C * int_to_ptr(intptr_t x) { return reinterpret_cast<C*>(x); }
48  inline void * int_to_void(intptr_t x) { return reinterpret_cast<void*>(x); }
49 
56  inline std::string bool_to_string(bool val){ if( val ) return "true"; else return "false"; };
63  inline void int_to_string(bool val, std::string &out) { if( val ) out="true"; else out="false"; }
64 
71  SCOPIRA_EXPORT std::string int_to_string(int i);
78  inline void int_to_string(int i, std::string &out) { out = int_to_string(i); }
87  SCOPIRA_EXPORT int int_to_string(int i, char *out, int buflen);
94  SCOPIRA_EXPORT int string_to_int(const std::string &s);
102  SCOPIRA_EXPORT bool string_to_int(const std::string &s, int &outint);
103 
110  SCOPIRA_EXPORT std::string long_to_string(long i);
117  inline void long_to_string(long i, std::string &out) { out = long_to_string(i); }
124  SCOPIRA_EXPORT long string_to_long(const std::string &s);
132  SCOPIRA_EXPORT bool string_to_long(const std::string &s, long &outlong);
133 
140  SCOPIRA_EXPORT std::string size_t_to_string(size_t i);
147  inline void size_t_to_string(size_t i, std::string &out) { out = size_t_to_string(i); }
154  SCOPIRA_EXPORT size_t string_to_size_t(const std::string &s);
162  SCOPIRA_EXPORT bool string_to_size_t(const std::string &s, size_t &outsize_t);
163 
170  SCOPIRA_EXPORT std::string int64_t_to_string(int64_t i);
177  inline void int64_t_to_string(int64_t i, std::string &out) { out = int64_t_to_string(i); }
184  SCOPIRA_EXPORT int64_t string_to_int64_t(const std::string &s);
192  SCOPIRA_EXPORT bool string_to_int64_t(const std::string &s, int64_t &outint64_t);
193 
200  SCOPIRA_EXPORT std::string double_to_string(double i);
207  inline void double_to_string(double i, std::string &out) { out = double_to_string(i); }
215  SCOPIRA_EXPORT std::string double_to_string(double i, int pres);
224  SCOPIRA_EXPORT int double_to_string(double i, char *out, int buflen);
232  SCOPIRA_EXPORT std::string double_to_exp_string(double i, int pres = 25);
239  SCOPIRA_EXPORT double string_to_double(const std::string &s);
247  SCOPIRA_EXPORT bool string_to_double(const std::string &s, double &outdouble);
248 
256  template <class T>
257  char compare(const T &lhs, const T &rhs) {
258  if (lhs<rhs)
259  return -1;
260  else if (rhs<lhs)
261  return 1;
262  else
263  return 0;
264  }
265 
273  SCOPIRA_EXPORT void uppercase(const std::string& s, std::string& upper);
280  SCOPIRA_EXPORT std::string uppercase(const std::string& s);
288  SCOPIRA_EXPORT void lowercase(const std::string& s, std::string& lower);
295  SCOPIRA_EXPORT std::string lowercase(const std::string& s);
296 
297 
307  SCOPIRA_EXPORT std::string pad_left(const std::string &s, int width, char padchar = ' ');
308 
309  // internal func
310  SCOPIRA_EXPORT void impl_cstring_to_fixed_array_impl(const char *in, size_t srclen, char *out, size_t N) throw();
311 
317  template <size_t N>
318  inline void string_to_fixed_array(const std::string &src, fixed_array<char, N> &out) throw()
319  { impl_cstring_to_fixed_array_impl(src.c_str(), src.size(), out.c_array(), N); }
325  template <size_t N>
326  inline void c_string_to_fixed_array(const char *in, fixed_array<char, N> &out) throw()
327  { impl_cstring_to_fixed_array_impl(in, in?strlen(in):0, out.c_array(), N); }
328 
339  SCOPIRA_EXPORT bool split_char(const std::string &src, char split, std::string &left_out, std::string &right_out);
340 
350  SCOPIRA_EXPORT std::string filename_number(const std::string &fname, int num);
351 
358  SCOPIRA_EXPORT void trim_left(std::string& str);
359 
366  void trim_right(std::string& str);
367 
374  template<class container>
375  void string_tokenize_word(const std::string& src, container& result, const std::string& substr)
376  {
377  std::string::size_type start = 0;
378  std::string::size_type pos;
379 
380  // clear vector
381  result.clear();
382 
383  // loop through source string to split
384  while ((pos = src.find(substr, start)) != std::string::npos) {
385  // push back found sub-string
386  result.push_back(src.substr(start, pos-start));
387  // advance starting position
388  start = pos + substr.length();
389  }
390 
391  if (start != src.length())
392  result.push_back(src.substr(start, src.length()-start)); // push back last sub-string
393  }
394 
405  template<class container>
406  void string_tokenize(const std::string& src, container& result, const std::string& delimiters = " \t\n")
407  {
408  const std::string::size_type len = src.length();
409  std::string::size_type i = 0;
410 
411  result.clear();
412 
413  while (i < len) {
414  // remove leading whitespace
415  i = src.find_first_not_of(delimiters, i);
416  if (i == std::string::npos)
417  return; // nothing left but white space
418 
419  // find the end of the token
420  std::string::size_type j = src.find_first_of(delimiters, i);
421 
422  // push token
423  if (j == std::string::npos) {
424  result.push_back(src.substr(i));
425  return;
426  } else
427  result.push_back(src.substr(i, j-i));
428 
429  // set up for next loop
430  i = j + 1;
431  }
432  }//string_tokenize
433 
438  template <class T>
439  inline T byte_swap(T x) { return x; }
440 
446  template <>
447  inline short byte_swap<short>(short x)
448  {
449  return
450  ( (x & 0xFF) << 8 ) |
451  ( (x & 0xFF00) >> 8 );
452  }
453 
459  template <>
460  inline int byte_swap<int>(int x)
461  {
462  return
463  ( (x & 0xFF) << 24 ) |
464  ( (x & 0xFF00) << 8 ) |
465  ( (x & 0xFF0000) >> 8 ) |
466  ( (x & 0xFF000000) >> 24 );
467  }
472  template <>
473  inline int64_t byte_swap<int64_t>(int64_t x)
474  {
475  return
476  ( (x & 0xFFll) << (8*7) ) |
477  ( (x & 0xFF00ll) << (8*5) ) |
478  ( (x & 0xFF0000ll) << (8*3) ) |
479  ( (x & 0xFF000000ll) << 8 ) |
480  ( (x & 0xFF00000000ll) >> 8 ) |
481  ( (x & 0xFF0000000000ll) >> (8*3) ) |
482  ( (x & 0xFF000000000000ll) >> (8*5) ) |
483  ( (x & 0xFF00000000000000ll) >> (8*7) );
484  }
485 
490  template <>
491  inline uint64_t byte_swap<uint64_t>(uint64_t x)
492  {
493  return
494  ( (x & 0xFFull) << (8*7) ) |
495  ( (x & 0xFF00ull) << (8*5) ) |
496  ( (x & 0xFF0000ull) << (8*3) ) |
497  ( (x & 0xFF000000ull) << 8 ) |
498  ( (x & 0xFF00000000ull) >> 8 ) |
499  ( (x & 0xFF0000000000ull) >> (8*3) ) |
500  ( (x & 0xFF000000000000ull) >> (8*5) ) |
501  ( (x & 0xFF00000000000000ull) >> (8*7) );
502  }
503 
509  template <>
510  inline float byte_swap<float>(float x)
511  {
512  float ret;
513  char *I = reinterpret_cast<char*>(&x);
514  char *O = reinterpret_cast<char*>(&ret);
515  O[0] = I[3];
516  O[1] = I[2];
517  O[2] = I[1];
518  O[3] = I[0];
519  return ret;
520  }
521 
527  template <>
528  inline double byte_swap<double>(double x)
529  {
530  double ret;
531  char *I = reinterpret_cast<char*>(&x);
532  char *O = reinterpret_cast<char*>(&ret);
533  O[0] = I[7];
534  O[1] = I[6];
535  O[2] = I[5];
536  O[3] = I[4];
537  O[4] = I[3];
538  O[5] = I[2];
539  O[6] = I[1];
540  O[7] = I[0];
541  return ret;
542  }
543 
548  template <class ITER>
549  void byte_swap_all(ITER head, ITER tail);
550  }//namespace scopira::tool
551 }
552 
553 template <class ITER>
554  void scopira::tool::byte_swap_all(ITER head, ITER tail)
555 {
556  for (; head != tail; ++head)
557  *head = byte_swap(*head);
558 }
559 
560 #endif
561 
float byte_swap< float >(float x)
Definition: util.h:510
void string_to_fixed_array(const std::string &src, fixed_array< char, N > &out)
Definition: util.h:318
double byte_swap< double >(double x)
Definition: util.h:528
uint64_t byte_swap< uint64_t >(uint64_t x)
Definition: util.h:491
void trim_left(std::string &str)
std::string pad_left(const std::string &s, int width, char padchar=' ')
Definition: archiveflow.h:20
int64_t string_to_int64_t(const std::string &s)
std::string long_to_string(long i)
int string_to_int(const std::string &s)
int byte_swap< int >(int x)
Definition: util.h:460
void c_string_to_fixed_array(const char *in, fixed_array< char, N > &out)
Definition: util.h:326
std::string bool_to_string(bool val)
Definition: util.h:56
void string_tokenize(const std::string &src, container &result, const std::string &delimiters=" \")
Definition: util.h:406
int64_t byte_swap< int64_t >(int64_t x)
Definition: util.h:473
std::string double_to_string(double i)
void byte_swap_all(ITER head, ITER tail)
Definition: util.h:554
void uppercase(const std::string &s, std::string &upper)
std::string size_t_to_string(size_t i)
std::string int64_t_to_string(int64_t i)
char compare(const T &lhs, const T &rhs)
Definition: util.h:257
void int_to_string(bool val, std::string &out)
Definition: util.h:63
T byte_swap(T x)
Definition: util.h:439
std::string filename_number(const std::string &fname, int num)
bool split_char(const std::string &src, char split, std::string &left_out, std::string &right_out)
size_t string_to_size_t(const std::string &s)
void lowercase(const std::string &s, std::string &lower)
void trim_right(std::string &str)
long string_to_long(const std::string &s)
intptr_t ptr_to_int(const void *ptr)
Definition: util.h:36
Definition: array.h:32
void * int_to_void(intptr_t x)
Definition: util.h:48
void string_tokenize_word(const std::string &src, container &result, const std::string &substr)
Definition: util.h:375
C * int_to_ptr(intptr_t x)
Definition: util.h:43
short byte_swap< short >(short x)
Definition: util.h:447
double string_to_double(const std::string &s)
std::string double_to_exp_string(double i, int pres=25)