Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
filename.cpp
Go to the documentation of this file.
1 
14 #include <cstdio>
15 #include <regex.h>
16 #include <string.h>
17 #include <cassert>
18 #include "filename.h"
19 
20 #include "IO/bridgeIO.h"
21 using Bridge::vout;
22 
23 //====================================================================
24 Filename::Filename(const std::string& base)
25 {
27 }
28 
29 
30 //====================================================================
32 {
33  size_t nkeyword = keywords_.size();
34 
35  std::vector<int> v(nkeyword);
36 
37  for (size_t i = 0; i < nkeyword; ++i) {
38  Filename::keyvalue_type::const_iterator p = kv.find(keywords_[i]);
39 
40  if (p != kv.end()) {
41  v[i] = p->second;
42  } else {
43  vout.general("error: key \"%s\" not found.\n", keywords_[i].c_str());
44  return std::string();
45  }
46  }
47 
48  switch (nkeyword)
49  {
50  case 0:
51  return format_(0);
52 
53  case 1:
54  return format_(1, v[0]);
55 
56  case 2:
57  return format_(2, v[0], v[1]);
58 
59  case 3:
60  return format_(3, v[0], v[1], v[2]);
61 
62  case 4:
63  return format_(4, v[0], v[1], v[2], v[3]);
64 
65  case 5:
66  return format_(5, v[0], v[1], v[2], v[3], v[4]);
67 
68  case 6:
69  return format_(6, v[0], v[1], v[2], v[3], v[4], v[5]);
70 
71  default:
72  vout.general("error: too many parameters.\n");
73  }
74 
75  return std::string();
76 }
77 
78 
79 //====================================================================
80 std::string Filename::format_(int narg, ...)
81 {
82  assert(narg == int(keywords_.size()));
83 
84  static const int buf_size = 4096;
85  static char buf[buf_size];
86 
87  va_list arg;
88 
89  va_start(arg, narg);
90  vsnprintf(buf, buf_size, format_str_, arg);
91  va_end(arg);
92 
93  return std::string(buf);
94 }
95 
96 
97 //====================================================================
98 Filename::keywordlist_type Filename::generate_pattern(char *fmt, const size_t fmt_size, const char *msg)
99 {
100  keywordlist_type keyword_list;
101 
102  const char pattern[] = "\\{([._[:alnum:]]+)(:([[:digit:]]+))?\\}";
103  const int num_regmatch = 4; // number of parentheses + 1
104 
105  regex_t reg;
106 
107  if (int retv = regcomp(&reg, pattern, REG_EXTENDED)) {
108  vout.crucial("regcomp failed. %s(%d)\n", strerror(retv), retv);
109  exit(1);
110  }
111 
112  regmatch_t regmatch[num_regmatch];
113 
114  memset(fmt, '\0', fmt_size);
115  char *bufp = fmt;
116 
117  int nmatch = 0;
118  regoff_t idx = 0;
119 
120  while (true)
121  {
122  int v = regexec(&reg, msg + idx, num_regmatch, regmatch, 0);
123 
124  if (v != 0) break;
125 
126  ++nmatch;
127 
128  strncpy(bufp, msg + idx, regmatch[0].rm_so);
129  bufp += regmatch[0].rm_so;
130 
131  char keyword[128];
132  memset(keyword, '\0', 128);
133  strncpy(keyword, msg + idx + regmatch[1].rm_so, regmatch[1].rm_eo - regmatch[1].rm_so);
134 
135  keyword_list.push_back(keyword_type(keyword));
136 
137  int width = 0;
138  if (regmatch[3].rm_so != (regoff_t)-1) {
139  width = atoi(msg + idx + regmatch[3].rm_so);
140  }
141 
142  if (width > 0) {
143  char tmp[128];
144  memset(tmp, '\0', 128);
145 
146  snprintf(tmp, 128, "%%0%dd", width);
147 
148  strncpy(bufp, tmp, strlen(tmp));
149  bufp += strlen(tmp);
150  } else {
151  strncpy(bufp, "%d", strlen("%d"));
152  bufp += strlen("%d");
153  }
154 
155  idx += regmatch[0].rm_eo;
156  }
157 
158  strncpy(bufp, msg + idx, strlen(msg) - idx);
159 
160  regfree(&reg);
161 
162  return keyword_list;
163 }
164 
165 
166 //====================================================================
167 //============================================================END=====
BridgeIO vout
Definition: bridgeIO.cpp:503
std::string format()
Definition: filename.h:61
char format_str_[format_str_size_]
Definition: filename.h:101
Filename(const std::string &base)
Definition: filename.cpp:24
void general(const char *format,...)
Definition: bridgeIO.cpp:197
static const int format_str_size_
Definition: filename.h:100
std::map< keyword_type, value_type > keyvalue_type
Definition: filename.h:48
std::vector< keyword_type > keywordlist_type
Definition: filename.h:49
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
keywordlist_type keywords_
Definition: filename.h:103
std::string format_(int narg,...)
Definition: filename.cpp:80
std::string keyword_type
Definition: filename.h:46
keywordlist_type generate_pattern(char *fmt, const size_t fmt_size, const char *msg)
Definition: filename.cpp:98