Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
utility.h
Go to the documentation of this file.
1 #ifndef utility_H__
2 #define utility_H__
3 
4 #include <cstdlib>
5 #include <cstring>
6 #include <cstdio>
7 #include <cstdarg>
8 
9 #include <string>
10 
11 namespace meow {
12 
18 template<class F1, class F2 = F1, class T1 = F1, class T2 = T1>
19 struct PairToPair {
20  std::pair<F1, F2> from;
21  std::pair<T1, T2> to;
22 
24  }
25  PairToPair(PairToPair const& pp): from(pp.from), to(pp.to) {
26  }
27  PairToPair(F1 const& f1, F2 const& f2, T1 const& t1, T2 const& t2):
28  from(f1, f2), to(t1, t2) {
29  }
30  bool operator==(PairToPair const& p) const {
31  return (from == p.from && to == p.to);
32  }
33 };
34 
42 inline std::string stringPrintf(char const * fmt, ...) {
43  char str[8192];
44  va_list args;
45  va_start(args, fmt);
46  vsnprintf(str, 8192, fmt, args);
47  va_end(args);
48  return std::string(str);
49 }
50 
60 inline std::string stringReplace(std::string str,
61  std::string const& from,
62  std::string const& to) {
63  std::string out = str;
64  int len = from.length();
65  for (size_t pos; (pos = out.find(from)) != std::string::npos; ) {
66  out.replace(pos, len, to);
67  }
68  return out;
69 }
70 
81 inline bool cstringEndWith(char const* str, int n, ...) {
82  int len = strlen(str);
83  va_list args;
84  va_start(args, n);
85  for (int i = 0; i < n; i++) {
86  char const* arg = va_arg(args, char const*);
87  int arglen = strlen(arg);
88  if (arglen <= len && strcmp(str + len - arglen, arg) == 0) {
89  return true;
90  }
91  }
92  va_end(args);
93  return false;
94 }
95 
103 #define debugPrintf(str) \
104 debugPrintf_(\
105  __FILE__,\
106  __FUNCTION__,\
107  __LINE__,\
108  str)
109 inline void debugPrintf_(char const* file,
110  char const* func,
111  size_t line,
112  char const* msg) {
113 #ifdef DEBUG
114  fprintf(stderr, "%s[%d] %s >> %s", file, line, func, msg);
115 #endif // DEBUG
116 }
117 
145 inline void messagePrintf(int level_change, char const* fmt, ...) {
146  static int level = 0;
147  static int last_level = -5;
148  char str[8192];
149  va_list args;
150  va_start(args, fmt);
151  vsnprintf(str, 8192, fmt, args);
152  va_end(args);
153  if (last_level == 1 && level_change == -1) {
154  printf(" ...%s\n", str);
155  }
156  else {
157  if (last_level == 1) printf("\n");
158  int level2 = level + (level_change == -1 ? -1 : 0);
159  for (int i = 0; i < level2; i++) printf("| ");
160  printf("%s%s", (level_change == -1 ? "..." : ""), str);
161  if (level_change != 1) printf("\n");
162  }
163  level += level_change;
164  last_level = level_change;
165  fflush(stdout);
166 }
167 
178 inline bool filenameCompare(std::string const& f1, std::string const& f2) {
179  char const* s1 = f1.c_str();
180  char const* s2 = f2.c_str();
181  int l1 = f1.length();
182  int l2 = f2.length();
183  int i1, i2;
184  for (i1 = i2 = 0; i1 < l1 || i2 < l2; i1++, i2++) {
185  if (isdigit(s1[i1]) && isdigit(s2[i2])) {
186  int n1 = atoi(s1 + i1);
187  int n2 = atoi(s2 + i2);
188  if (n1 != n2)
189  return (n1 < n2);
190  while(i1 + 1 < l1 && isdigit(s1[i1 + 1])) i1++;
191  while(i2 + 1 < l2 && isdigit(s2[i2 + 1])) i2++;
192  }
193  else {
194  if(s1[i1] != s2[i2])
195  return s1[i1] < s2[i2];
196  }
197  }
198  return false;
199 }
200 
201 } // meow
202 
203 #endif // utility_H__
std::pair< T1, T2 > to
Definition: utility.h:21
bool filenameCompare(std::string const &f1, std::string const &f2)
將兩個字串用人類習慣的檔名排序方式排序
Definition: utility.h:178
有.from.first, .from.second, .to.first, .to.second
Definition: utility.h:19
bool operator==(PairToPair const &p) const
Definition: utility.h:30
bool cstringEndWith(char const *str, int n,...)
檢查給定字串的結尾是否符合給定的數個patterns中的一個
Definition: utility.h:81
void debugPrintf_(char const *file, char const *func, size_t line, char const *msg)
Definition: utility.h:109
std::string stringReplace(std::string str, std::string const &from, std::string const &to)
將輸入字串中的某個pattern取代成另一個pattern
Definition: utility.h:60
PairToPair(F1 const &f1, F2 const &f2, T1 const &t1, T2 const &t2)
Definition: utility.h:27
std::pair< F1, F2 > from
Definition: utility.h:20
std::string stringPrintf(char const *fmt,...)
類似C的printf, 不過是將格式化的字串丟到 std::string 裡回傳
Definition: utility.h:42
PairToPair(PairToPair const &pp)
Definition: utility.h:25
void messagePrintf(int level_change, char const *fmt,...)
階層式輸出
Definition: utility.h:145