Simpact Cyan
Population based event driven simulation using mNRM
util.h
1 #ifndef UTIL_H
2 
3 #define UTIL_H
4 
5 #define __STDC_FORMAT_MACROS // Need this for PRId64
6 #include <inttypes.h>
7 #include <stdarg.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string>
11 #include <vector>
12 
13 //long double GetSystemTime();
14 bool ReadInputLine(FILE *fi, std::string &line);
15 void SplitLine(const std::string &line, std::vector<std::string> &args, const std::string &separatorChars = " \t",
16  const std::string &quoteChars = "\"'", const std::string &commentStartChars = "#",
17  bool ignoreZeroLengthFields = true);
18 
19 std::string createFullPath(const std::string &dir, const std::string &file);
20 
21 void abortWithMessage(const std::string &msg);
22 
23 bool parseAsInt(const std::string &str, int &number);
24 bool parseAsInt(const std::string &str, int64_t &number);
25 bool parseAsDouble(const std::string &str, double &number);
26 bool parseAsDoubleVector(const std::string &str, std::vector<double> &numbers, std::string &badField);
27 std::string doubleToString(double x);
28 std::string doublesToString(const std::vector<double> &values);
29 std::string intToString(int x);
30 std::string intToString(int64_t x);
31 std::string stringToString(const std::string &str);
32 
33 std::string trim(const std::string &str, const std::string &trimChars = " \t\r\n");
34 std::string replace(const std::string &input, const std::string &target, const std::string &replacement);
35 
36 std::string strprintf_cstr(const char *format, ...);
37 
38 #define strprintf(format, ...) strprintf_cstr(stringToString(format).c_str(), __VA_ARGS__ )
39 
40 //
41 // inline implementations
42 //
43 
44 //inline std::string strprintf(const std::string &format, ...)
45 inline std::string strprintf_cstr(const char *format, ...)
46 {
47  const int MAXBUFLEN = 8192;
48  char buf[MAXBUFLEN+1];
49  va_list ap;
50 
51  va_start(ap, format);
52 #ifndef WIN32
53  vsnprintf(buf, MAXBUFLEN, format, ap);
54 #else
55  vsnprintf_s(buf, MAXBUFLEN, _TRUNCATE, format, ap);
56 #endif // WIN32
57  va_end(ap);
58 
59  buf[MAXBUFLEN] = 0;
60  return std::string(buf);
61 }
62 
63 inline std::string doubleToString(double x)
64 {
65  std::string s = strprintf("%.15g", x);
66  if (s == "1.#INF")
67  return "inf";
68  if (s == "-1.#INF")
69  return "-inf";
70  return s;
71 }
72 
73 inline std::string intToString(int x)
74 {
75  return strprintf("%d", x);
76 }
77 
78 inline std::string intToString(int64_t x)
79 {
80  return strprintf("%" PRId64, x);
81 }
82 
83 inline std::string stringToString(const std::string &str)
84 {
85  return str;
86 }
87 
88 inline bool endsWith(const std::string &value, const std::string &ending, bool ignoreCase = false)
89 {
90  if (ending.size() > value.size())
91  return false;
92 
93  if (!ignoreCase)
94  return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
95 
96  auto it1 = ending.rbegin();
97  auto it1end = ending.rend();
98  auto it2 = value.rbegin();
99 
100  for ( ; it1 != it1end ; ++it1, ++it2)
101  {
102  if (tolower(*it1) != tolower(*it2))
103  return false;
104  }
105  return true;
106 }
107 
108 #endif // UTIL_H
109