Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
statement-processing-common.hxx
Go to the documentation of this file.
1 // file : odb/statement-processingc-common.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_STATEMENT_PROCESSING_COMMON_HXX
6 #define ODB_STATEMENT_PROCESSING_COMMON_HXX
7 
8 #include <odb/pre.hxx>
9 
10 //#define LIBODB_DEBUG_STATEMENT_PROCESSING 1
11 //#define LIBODB_TRACE_STATEMENT_PROCESSING 1
12 
13 #include <string>
14 #include <cstddef> // std::size_t
15 
16 namespace odb
17 {
18  typedef std::char_traits<char> traits;
19 
20  static inline const char*
21  find (const char* b, const char* e, char c)
22  {
23  return traits::find (b, e - b, c);
24  }
25 
26  static inline const char*
27  rfind (const char* b, const char* e, char c)
28  {
29  for (--e; b != e; --e)
30  if (*e == c)
31  return e;
32 
33  return 0;
34  }
35 
36  // Iterate over INSERT column/value list, UPDATE SET expression list,
37  // or SELECT column/join list.
38  //
39  // for (const char* b (columns_begin), *e (begin (b, end));
40  // e != 0;
41  // next (b, e, end))
42  // {
43  // // b points to the beginning of the value (i.e., one past '(').
44  // // e points one past the end of the value (i.e., to ',', ')', or '\n').
45  // }
46  //
47  // // b points one past the last value.
48  //
49  static inline const char*
50  paren_begin (const char*& b, const char* end)
51  {
52  // Note that the list may not end with '\n'.
53 
54  b++; // Skip '('.
55  const char* e (find (b, end, '\n'));
56  return (e != 0 ? e : end) - 1; // Skip ',' or ')'.
57  }
58 
59  static inline void
60  paren_next (const char*& b, const char*& e, const char* end)
61  {
62  if (*e == ',')
63  {
64  b = e + 2; // Skip past '\n'.
65  e = find (b, end, '\n');
66  e = (e != 0 ? e : end) - 1; // Skip ',' or ')'.
67  }
68  else
69  {
70  b = (e + 1 != end ? e + 2 : end); // Skip past '\n'.
71  e = 0;
72  }
73  }
74 
75  static inline const char*
76  comma_begin (const char* b, const char* end)
77  {
78  // Note that the list may not end with '\n'.
79 
80  const char* e (find (b, end, '\n'));
81  return e != 0 ? e - (*(e - 1) == ',' ? 1 : 0) : end; // Skip ','.
82  }
83 
84  static inline void
85  comma_next (const char*& b, const char*& e, const char* end)
86  {
87  if (*e == ',')
88  {
89  b = e + 2; // Skip past '\n'.
90  e = find (b, end, '\n');
91  e = (e != 0 ? e - (*(e - 1) == ',' ? 1 : 0) : end); // Skip ','.
92  }
93  else
94  {
95  b = (e != end ? e + 1 : end); // Skip past '\n'.
96  e = 0;
97  }
98  }
99 
100  static inline const char*
101  newline_begin (const char* b, const char* end)
102  {
103  // Note that the list may not end with '\n'.
104 
105  const char* e (find (b, end, '\n'));
106  return e != 0 ? e : end;
107  }
108 
109  static inline void
110  newline_next (const char*& b,
111  const char*& e,
112  const char* end,
113  const char* prefix,
114  std::size_t prefix_size)
115  {
116  if (e != end)
117  e++; // Skip past '\n'.
118 
119  b = e;
120 
121  // Do we have another element?
122  //
123  if (static_cast<std::size_t> (end - b) > prefix_size &&
124  traits::compare (b, prefix, prefix_size) == 0)
125  {
126  e = find (b, end, '\n');
127  if (e == 0)
128  e = end;
129  }
130  else
131  e = 0;
132  }
133 
134  // Note that end must point to the beginning of the list.
135  //
136  static inline const char*
137  newline_rbegin (const char* e, const char* end)
138  {
139  const char* b (rfind (end, e - 1, '\n'));
140  return b != 0 ? b + 1 : end; // Skip past '\n'.
141  }
142 
143  static inline void
144  newline_rnext (const char*& b, const char*& e, const char* end)
145  {
146  if (b != end)
147  {
148  e = b - 1; // Skip to previous '\n'.
149  b = rfind (end, e - 1, '\n');
150  b = (b != 0 ? b + 1 : end); // Skip past '\n'.
151  }
152  else
153  {
154  e = end - 1; // One before the first element.
155  b = 0;
156  }
157  }
158 
159  // Fast path: just remove the "structure".
160  //
161  static inline void
162  process_fast (const char* s, std::string& r)
163  {
164  r = s;
165  for (std::size_t i (r.find ('\n'));
166  i != std::string::npos;
167  i = r.find ('\n', i))
168  r[i++] = ' ';
169  }
170 }
171 
172 #include <odb/post.hxx>
173 
174 #endif // ODB_STATEMENT_PROCESSING_COMMON_HXX
std::char_traits< char > traits
Definition: statement-processing-common.hxx:18