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