File: mbl_parse_tuple.h

package info (click to toggle)
vxl 1.17.0.dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 153,280 kB
  • ctags: 105,123
  • sloc: cpp: 747,420; ansic: 209,130; fortran: 34,230; lisp: 14,915; sh: 6,187; python: 5,856; makefile: 340; perl: 294; xml: 160
file content (247 lines) | stat: -rw-r--r-- 7,831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// This is mul/mbl/mbl_parse_tuple.h
#ifndef mbl_parse_tuple_h_
#define mbl_parse_tuple_h_
//:
// \file
// \author Ian Scott
// \date  6-Feb-2008
// \brief Convenience function a tuple of PODs from a config file.

#include <vcl_istream.h>
#include <vcl_sstream.h>
#include <mbl/mbl_exception.h>

//: Read a 2-tuple of PODs from a config file.
// This function will read through a stream, and store the text found to a string.
// The function reads 2 elements. If there was an opening brace it will also consume the closing brace.
// Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
//
// \throws mbl_exception_parse_error if unrecoverable parse error.
//
// Example:
// \code
//   vcl_istringstream ss("{ 1.0 4 }");
//   float a;
//   int b;
//   mbl_parse_tuple(ss, a, b)
// \endcode
//
template <class T, class U>
inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b)
{
  if (!afs) return;
  char brace1, brace2;
  afs >> vcl_ws >> brace1;
  if (afs.eof())
    throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
  if ( brace1 == '{')
  {
    afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> brace2;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
    if (brace2 != '}')
    {
      afs.putback(brace2);
      afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream

      throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
    }
  }
  else
  {
    afs.putback(brace1);
    afs >> vcl_ws >> a >> vcl_ws >> b;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
  }
}

//: Read a 3-tuple of PODs from a config file.
// This function will read through a stream, and store the text found to a string.
// The function reads 3 elements. If there was an opening brace it will also consume the closing brace.
// Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
//
// \throws mbl_exception_parse_error if unrecoverable parse error.
//
// \verbatim
// Example:
// vcl_istringstream ss("{ 1.0 -5 4 }");
// float a;
// int b;
// unsigned c;
// mbl_parse_tuple(ss, a, b, c)
// \endverbatim
template <class T, class U, class V>
inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c)
{
  if (!afs) return;
  char brace1, brace2;
  afs >> vcl_ws >> brace1;
  if (afs.eof())
    throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
  if ( brace1 == '{')
  {
    afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> brace2;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
    if (brace2 != '}')
    {
      afs.putback(brace2);
      afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream

      throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
    }
  }
  else
  {
    afs.putback(brace1);
    afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
  }
}


//: Read a 4-tuple of PODs from a config file.
// This function will read through a stream, and store the text found to a string.
// The function reads 4 elements. If there was an opening brace it will also consume the closing brace.
// Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
//
// \throws mbl_exception_parse_error if unrecoverable parse error.
//
// \verbatim
// Example:
// vcl_istringstream ss("{ 1.0 -5 4 a }");
// float a;
// int b;
// unsigned c;
// char d;
// mbl_parse_tuple(ss, a, b, c, d)
// \endverbatim
template <class T, class U, class V, class W>
inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c, W& d)
{
  if (!afs) return;
  char brace1, brace2;
  afs >> vcl_ws >> brace1;
  if (afs.eof())
    throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
  if ( brace1 == '{')
  {
    afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >> vcl_ws >> brace2;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
    if (brace2 != '}')
    {
      afs.putback(brace2);
      afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream

      throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
    }
  }
  else
  {
    afs.putback(brace1);
    afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
  }
}


//: Read a 5-tuple of PODs from a config file.
// This function will read through a stream, and store the text found to a string.
// The function reads 5 elements. If there was an opening brace it will also consume the closing brace.
// Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
//
// \throws mbl_exception_parse_error if unrecoverable parse error.
//
// \verbatim
// Example:
// vcl_istringstream ss("{ 1.0 -5 4 k l }");
// float a;
// int b;
// unsigned c;
// char d, e;
// mbl_parse_tuple(ss, a, b, c, d, e)
// \endverbatim
template <class T, class U, class V, class W, class X>
inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c, W& d, X& e)
{
  if (!afs) return;
  char brace1, brace2;
  afs >> vcl_ws >> brace1;
  if (afs.eof())
    throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
  if ( brace1 == '{')
  {
    afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >> vcl_ws >> e >> vcl_ws >> brace2;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
    if (brace2 != '}')
    {
      afs.putback(brace2);
      afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream

      throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
    }
  }
  else
  {
    afs.putback(brace1);
    afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >> vcl_ws >> e;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
  }
}


//: Read a 6-tuple of PODs from a config file.
// This function will read through a stream, and store the text found to a string.
// The function reads 6 elements. If there was an opening brace it will also consume the closing brace.
// Other conditions will cause an exception to be thrown, and the stream's fail bit to be set
//
// \throws mbl_exception_parse_error if unrecoverable parse error.
//
// \verbatim
// Example:
// vcl_istringstream ss("{ 1.0 -3.4 -5 4 k l }");
// float a;
// double b;
// int c;
// unsigned d;
// char e, f;
// mbl_parse_tuple(ss, a, b, c, d, e, f)
// \endverbatim
template <class T, class U, class V, class W, class X, class Y>
inline void mbl_parse_tuple(vcl_istream &afs, T& a, U& b, V& c, W& d, X& e, Y& f)
{
  if (!afs) return;
  char brace1, brace2;
  afs >> vcl_ws >> brace1;
  if (afs.eof())
    throw mbl_exception_parse_error("mbl_parse_tuple failed unexpected eof");
  if ( brace1 == '{')
  {
    afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >> vcl_ws >>
      e >> vcl_ws >> f >> vcl_ws >> brace2;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
    if (brace2 != '}')
    {
      afs.putback(brace2);
      afs.clear(vcl_ios::failbit); // Set a recoverable IO error on stream

      throw mbl_exception_parse_error("mbl_parse_tuple failed to find closing brace");
    }
  }
  else
  {
    afs.putback(brace1);
    afs >> vcl_ws >> a >> vcl_ws >> b >> vcl_ws >> c >> vcl_ws >> d >>
      vcl_ws >> e >> vcl_ws >> f;
    if (!afs)
      throw mbl_exception_parse_error("mbl_parse_tuple failed with stream error");
  }
}
#endif // mbl_parse_tuple_h_