Swarm-NG  1.1
util.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Mario Juric *
3  * mjuric@astro.Princeton.EDU *
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 2 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, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
26 #ifndef _astro_util_h
27 #define _astro_util_h
28 
29 #include "macros.h"
30 #include "astro_types.hpp"
31 #include "constants.hpp"
32 
33 #include <cmath>
34 #include <string>
35 #include <cstdlib>
36 #include <memory.h>
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <exception>
40 #include <stdexcept>
41 
42 #ifndef _WIN32
43 #include <cxxabi.h>
44 #endif
45 
46 #ifndef NULL
47 #define NULL 0
48 #endif
49 
50 namespace peyton {
52 namespace util {
53 
55  // Radians approxSunLongitude(peyton::MJD t);
56 
58  std::string ltrim(const std::string &str, const std::string &whitespace = "\t ");
60  std::string rtrim(const std::string &str, const std::string &whitespace = "\t ");
62  std::string trim(const std::string &str, const std::string &whitespace = "\t ");
64  char *trim(char *txt);
66  char *trim(char *dest, const char *src);
67 
69  std::string pad(const std::string &s, size_t n, char c = ' ');
70 
72  std::string unescape(const std::string &str);
73 
75  inline std::string tolower(const std::string &s) { std::string o(s); FOREACH2(std::string::iterator, o) { *i = ::tolower(*i); }; return o; }
77  inline std::string toupper(const std::string &s) { std::string o(s); FOREACH2(std::string::iterator, o) { *i = ::toupper(*i); }; return o; }
78 
80  inline std::string str(size_t n) { char buf[20]; sprintf(buf, "%lu", n); return buf; }
82  inline std::string str(int n) { char buf[20]; sprintf(buf, "%d", n); return buf; }
84  inline std::string str(char c) { char buf[2] = {c, 0}; return buf; }
86  inline std::string str(double n, const char *fmt = "%f") { char buf[20]; sprintf(buf, fmt, n); return buf; }
87  #if 0
88  template<typename T>
91  inline std::string str(const T& var)
92  {
93  std::ostringstream ss;
94  ss << var;
95  return ss.str();
96  }
97  #endif
98 
100  struct demangling_error : public std::runtime_error {
101  demangling_error(const char* s): std::runtime_error(("While demangling " + std::string(s)).c_str()) {}
102  };
103 
104 #ifdef _WIN32
105  inline std::string type_name(const std::type_info &ti){
106  // TODO: find out how to do demangling in Windows
107  return ti.name();
108  }
109 #else
110  inline std::string type_name(const std::type_info &ti){
112  int status = 0;
113  char * tmp = abi::__cxa_demangle(ti.name(), 0, 0, &status);
114  std::string name = tmp;
115  free(tmp);
116  if(status != 0)
117  throw demangling_error(ti.name());
118  return name;
119  }
120 
121 #endif
122 
124  template<typename T>
125  std::string type_name()
126  {
127  return type_name(typeid(T));
128  }
129 
131  template<typename T>
132  std::string type_name(const T& t)
133  {
134  return type_name<T>();
135  }
136 }
137 namespace Util = util; // backwards compatibility hack
138 
139 }
140 
141 using namespace peyton::util;
142 
143 #endif