soma-io 6.0.6
pythonreader_d.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
34#ifndef SOMAIO_READER_PYTHONREADER_D_H
35#define SOMAIO_READER_PYTHONREADER_D_H
36
37//--- soma-io ----------------------------------------------------------------
41//----------------------------------------------------------------------------
42
43struct soma::PythonReader::Private
44{
45 Private( const SyntaxSet & r, carto::rc_ptr<soma::DataSource> ds )
46 : rules( r ), datasource( ds ), eof( false )
47 {}
48
49 Private( const SyntaxSet & r )
50 : rules( r ), datasource(), eof( false )
51 {}
52
53 SyntaxSet rules;
56 bool eof;
57};
58
59
60namespace
61{
62
63 template<typename T>
65 genericHelper( carto::GenericObject*, const std::string &,
66 soma::PythonReader & r )
67 {
68 r.skipWhile( " \t\n\\\r" );
69 soma::DataSource & ds = *r.dataSource();
70 if( r.eof() )
72 T x;
73 *r.dataSource() >> x;
74 if( r.eof() )
76 return( new carto::ValueObject<T>( x ) );
77 }
78
79
80 template<>
82 genericHelper<std::vector<int> >(carto::GenericObject* o, const std::string & s,
83 soma::PythonReader & r )
84 {
85 return soma::PythonReader::genericSequenceHelper<std::vector<int> >(
86 o, s, r );
87 }
88
89 template<>
91 genericHelper<char>( carto::GenericObject*, const std::string &,
92 soma::PythonReader & r )
93 {
94 r.skipWhile( " \t\n\\\r" );
95 soma::DataSource & ds = *r.dataSource();
96 if( r.eof() )
98 int x;
99 *r.dataSource() >> x;
100 if( r.eof() )
102 //cout << "number: " << x << std::endl;
103 return( new carto::ValueObject<char>( char( x ) ) );
104 }
105
106
107 template<>
109 genericHelper<unsigned char>( carto::GenericObject*, const std::string &,
110 soma::PythonReader & r )
111 {
112 r.skipWhile( " \t\n\\\r" );
113 soma::DataSource & ds = *r.dataSource();
114 if( r.eof() )
116 unsigned x;
117 *r.dataSource() >> x;
118 if( r.eof() )
120 //cout << "number: " << x << std::endl;
121 return( new carto::ValueObject<unsigned char>( static_cast<unsigned char>( x ) ) );
122 }
123
124
125 template<>
127 genericHelper<std::string>( carto::GenericObject*, const std::string &,
128 soma::PythonReader & r )
129 {
130 std::string x;
131 char quote;
132 soma::DataSource & ds = *r.dataSource();
133 if( !ds.isOpen() )
134 throw carto::eof_error( std::string( "EOF: line" ) + r.lineString(), r.name() );
135
136 r.readWhile( " \t\n\\\r" );
137 if( ds.eof() )
139 quote = ds.getch();
140 if( ds.eof() )
142 if( quote == 'u' ) // unicode string
143 {
144 quote = ds.getch();
145 if( ds.eof() )
147 }
148 if( quote != '"' && quote != '\'' )
149 {
150 std::cerr << "string reader: NOT a string: " << quote << std::endl;
151 return 0; // not a string
152 }
153 unsigned n = 0;
154 x = r.readString( quote, n );
155 if( ds.eof() )
157
158 //cout << "string: " << x << std::endl;
159 return( new carto::ValueObject<std::string>( x ) );
160 }
161
162 carto::GenericObject* listHelper( carto::GenericObject*, const std::string &,
163 soma::PythonReader & r )
164 {
165 // cout << "list\n";
166 std::string id, type;
167 soma::DataSource & ds = *r.dataSource();
168
169 if( !ds.isOpen() )
170 return 0;
171 r.skipWhile( " \t\n\\\r" );
172 if( r.eof() )
174 char c = ds.getch(), mark;
175 if( ds.eof() )
177 if( c == '(' )
178 mark = ')';
179 else if( c == '[' )
180 mark = ']';
181 else
182 {
183 // cout << "char: " << c << std::endl;
184 throw std::runtime_error( std::string( "PythonReader: Not a list/tuple: " )
185 + r.name() + ", line " + r.lineString() );
186 }
187
189
190 try
191 {
192 do
193 {
194 r.skipWhile( " \t\n\\\r" );
195 if( r.eof() )
197 c = ds.getch();
198 if( ds.eof() )
200 if( c == ',' ) // separator
201 {
202 r.skipWhile( " \t\n\\\r" );
203 if( r.eof() )
205 c = ds.getch();
206 if( ds.eof() )
208 }
209 if( c == mark )
210 {
211 break; // end
212 }
213 ds.ungetch( c );
214 obj->getValue().push_back( r.read( obj, "" ) );
215 }
216 while( ds.isOpen() );
217
218 return( obj );
219 }
220 catch( std::exception & e )
221 {
222 delete obj;
223 throw;
224 }
225 }
226
227
228 carto::GenericObject* noneHelper( carto::GenericObject*, const std::string &,
229 soma::PythonReader & )
230 {
231 return 0;
232 }
233
234
235} // namespace (internal linkage)
236
237namespace soma
238{
239
240template<typename T>
242 const std::string &,
243 PythonReader & r )
244{
245 std::string id, type;
246 DataSource & ds = *r.dataSource();
247
248 if( !ds.isOpen() )
249 return( 0 );
250 r.skipWhile( " \t\n\\\r" );
251 if( r.eof() )
253 char c = ds.getch(), mark;
254 if( r.eof() )
256 if( c == '(' )
257 mark = ')';
258 else if( c == '[' )
259 mark = ']';
260 else
261 {
262 //cout << "char: " << c << std::endl;
263 throw std::runtime_error( std::string( "PythonReader: Not a list/tuple: " )
264 + r.name() + ", line " + r.lineString() );
265 }
266
270
271 try
272 {
273 do
274 {
275 r.skipWhile( " \t\n\\\r" );
276 if( r.eof() )
278 c = ds.getch();
279 if( ds.eof() )
281 if( c == ',' ) // separator
282 {
283 r.skipWhile( " \t\n\\\r" );
284 if( r.eof() )
286 c = ds.getch();
287 if( ds.eof() )
289 }
290 if( c == mark )
291 {
292 break; // end
293 }
294 ds.ungetch( c );
295 de = genericHelper<typename T::value_type>( obj, "", r );
296 dev = dynamic_cast<carto::ValueObject<typename T::value_type>*>( de );
297 if( dev )
298 obj->getValue().push_back( dev->getValue() );
299 else
300 {
301 delete de;
302 throw std::runtime_error( std::string( "PythonReader: wrong type "
303 "inserted in homogen sequence: "
304 )
305 + r.name() + ", line " + r.lineString() );
306 }
307 delete de;
308 }
309 while( ds.isOpen() );
310
311 return( obj );
312 }
313 catch( std::exception & e )
314 {
315 delete obj;
316 throw;
317 }
318}
319
320template<typename T>
322 const std::string &,
323 PythonReader & r )
324{
325 std::string synt;
326 DataSource & ds = *r.dataSource();
327 char c;
328
329 r.skipWhile( " \t\n\\\r" );
330 if( r.eof() )
332 c = ds.getch();
333 if( ds.eof() )
335 if ( c != '{' ) {
336 std::cerr << "Attempt to read a dictionary not starting with '{'" << std::endl;
337 return 0;
338 }
339 r.skipWhile( " \t\n\\\r" );
340 if( r.eof() )
342
344
345 try
346 {
347 r.readDictionary2( *obj );
348 }
349 catch( std::exception & e )
350 {
351 delete obj;
352 throw;
353 }
354
355 return obj;
356}
357
358} // namespace soma
359
360#endif
361
virtual T & getValue()
static void launchErrnoExcept(const std::string &filename="")
Abstraction layer for various data sources (file, buffer, socket...).
Definition datasource.h:65
virtual int getch()=0
virtual bool ungetch(int ch)=0
virtual bool eof() const
virtual bool isOpen() const =0
virtual std::string url() const
std::string readString(char separator, unsigned &charsread)
std::string readWhile(const std::string &s)
read characters in s
void readDictionary2(carto::GenericObject &obj)
reads the remaining attributes of a string dictionary
virtual std::string name() const
file name
static carto::GenericObject * genericSequenceHelper(carto::GenericObject *, const std::string &, PythonReader &r)
unsigned skipWhile(const std::string &s)
skip characters in s
PythonReader(const std::string &filename, const carto::SyntaxSet &rules=carto::SyntaxSet(), const HelperSet &helpers=HelperSet())
std::map< std::string, Helper > HelperSet
static carto::GenericObject * genericDictHelper(carto::GenericObject *, const std::string &, PythonReader &r)
bool eof() const
have we hit EOF?
std::string lineString() const
carto::rc_ptr< DataSource > dataSource()
virtual carto::GenericObject * read()
carto::rc_ptr< soma::DataSource > datasource
Private(const SyntaxSet &r)
Private(const SyntaxSet &r, carto::rc_ptr< soma::DataSource > ds)