iRoCS Toolbox  1.1.0
StDataASCII.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: structured data class which stores all data as strings
26 ** $RCSfile$
27 ** $Revision: 4820 $$Name$
28 ** $Date: 2011-11-08 10:57:01 +0100 (Tue, 08 Nov 2011) $
29 ** Copyright: GPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.3 2005/10/26 07:37:05 ronneber
38 ** - made all get/read-functions const. They were non-const, because the
39 ** derived StDataCmdLine stores, wether parameters were accessed
40 ** not. Making the functions const and store these informations in a
41 ** mutable member seems to be the cleaner way
42 **
43 ** Revision 1.2 2004/09/13 10:04:04 ronneber
44 ** - documentation update
45 **
46 ** Revision 1.1 2004/08/26 08:36:59 ronneber
47 ** initital import
48 **
49 **
50 **
51 **************************************************************************/
52 
53 #ifndef STDATA_ASCII
54 #define STDATA_ASCII
55 
56 #ifdef HAVE_CONFIG_H
57 #include <config.hh>
58 #endif
59 
60 #include <string>
61 #include <map>
62 #include <algorithm>
63 #include <sstream>
64 #include <limits>
65 
66 #include "SVMError.hh"
67 #include "BasicFV.hh"
68 #include "SparseFV.hh"
69 
70 namespace svt
71 {
72  /*======================================================================*/
82 /*======================================================================*/
84  {
85  public:
86 
88  :_exceptionFlag( false),
89  _separatorChar(',')
90  {}
91 
92 /*-------------------------------------------------------------------------
93  * Required Methods (must be implemented in each StData... class)
94  *-------------------------------------------------------------------------*/
95 
96 
97  /*======================================================================*/
104  /*======================================================================*/
105  void setExceptionFlag( bool f)
106  {
107  _exceptionFlag = f;
108  }
109 
110 
111  /*======================================================================*/
115  /*======================================================================*/
116  bool exceptionFlag() const
117  {
118  return _exceptionFlag;
119  }
120 
121 
122  /*======================================================================*/
130  /*======================================================================*/
131  bool valueExists( const std::string& key) const
132  {
133  return (_map.find( key) != _map.end());
134  }
135 
136 
137 
138 
139  /*======================================================================*/
148  /*======================================================================*/
149  template<typename T>
150  void setValue( const std::string& key, const T& value);
151 
152  /*======================================================================*/
162  /*======================================================================*/
163  template<typename ForwardIter>
164  void setArray( const std::string& key,
165  const ForwardIter& arrBegin,
166  size_t size);
167 
168 
169  /*======================================================================*/
181  /*======================================================================*/
182  template<typename ForwardIter>
183  void setFVArray( const std::string& key,
184  const ForwardIter& arrBegin,
185  size_t size);
186 
187 
188  /*======================================================================*/
202  /*======================================================================*/
203  template<typename T>
204  void getValue( const std::string& key, T& value) const;
205 
206 
207  /*======================================================================*/
215  /*======================================================================*/
216  size_t getArraySize( std::string key) const;
217 
218  /*======================================================================*/
228  /*======================================================================*/
229  size_t getFVArraySize( std::string key) const;
230 
231 
232  /*======================================================================*/
251  /*======================================================================*/
252  template<typename ForwardIter>
253  void getArray( const std::string& key, const ForwardIter& arrBegin,
254  int containerSize=-1) const;
255 
256  /*======================================================================*/
275  /*======================================================================*/
276  template<typename ForwardIter>
277  void getFVArray( const std::string& key, const ForwardIter& arrBegin,
278  int containerSize=-1) const;
279 
280 
281 
282 /*-------------------------------------------------------------------------
283  * Optional Methods that need not to be implemented in each
284  * StData... class
285  *-------------------------------------------------------------------------*/
286 
287  /*======================================================================*/
292  /*======================================================================*/
293  void setSeparatorChar( char c)
294  {
295  _separatorChar = c;
296  }
297 
298  char separatorChar() const
299  {
300  return _separatorChar;
301  }
302 
303 
304  /*======================================================================*/
314  /*======================================================================*/
315  const std::string& asString( const std::string& key) const
316  {
317  std::map<std::string, std::string>::const_iterator
318  p = _map.find( key);
319 
320  if (p == _map.end())
321  {
323  e << "Key '" << key << "' not found.";
324  throw e;
325  }
326  return p->second;
327  }
328 
329 
330 
331  double asDouble( const std::string& key) const
332  {
333  double tmp = 0;
334  getValue( key, tmp);
335  return tmp;
336  }
337 
338 
339  unsigned int asUint( const std::string& key) const
340  {
341  unsigned int tmp = 0;
342  getValue( key, tmp);
343  return tmp;
344  }
345 
346 
347  /*======================================================================*/
358  /*======================================================================*/
359  template<typename T>
360  void stringToValue( const std::string& s, T& value) const
361  {
362  std::istringstream iss(s);
363  if( !(iss >> value))
364  {
366  e << "string '" << s
367  << "' could not be converted to requested type";
368  throw e;
369  }
370  }
371 
372  // optimized version for strings
373  void stringToValue( const std::string& s, std::string& value) const
374  {
375  value = s;
376  }
377 
378  void readArraySizeFromStream( std::istream& is, size_t& size) const;
379 
380 
381  /*======================================================================*/
391  /*======================================================================*/
392  std::map<std::string, std::string>::const_iterator begin() const
393  {
394  return _map.begin();
395  }
396 
397  std::map<std::string, std::string>::const_iterator end() const
398  {
399  return _map.end();
400  }
401 
402  /*======================================================================*/
412  /*======================================================================*/
413  void debugPrint( std::ostream& os) const
414  {
415  for( std::map<std::string,std::string>::const_iterator
416  p = _map.begin(); p != _map.end(); ++p)
417  {
418  os << p->first << ' ' << p->second << std::endl;
419  }
420  }
421 
422 
423 
424  private:
425  /*======================================================================*/
436  /*======================================================================*/
437 
438  std::map<std::string, std::string>::const_iterator
439  findKey( const std::string& key) const
440  {
441  std::map<std::string, std::string>::const_iterator p =
442  _map.find(key);
443  if (p == _map.end())
444  {
445  if( _exceptionFlag)
446  {
448  e << "Key '" << key << "' not found.";
449  throw e;
450  }
451  }
452  return p;
453  }
454 
455 
456  template< typename T>
457  void setPrecisionForType( std::ostream& os, const T&) const
458  {
459  os.precision( std::numeric_limits<float>::digits10);
460  }
461 
462  void setPrecisionForType( std::ostream& os, const double&) const
463  {
464  os.precision( std::numeric_limits<double>::digits10);
465  }
466 
467 
468  protected:
469  std::map<std::string, std::string> _map;
472  };
473 
474 }
475 
476 
477 #include "StDataASCII.icc"
478 #endif
std::map< std::string, std::string >::const_iterator begin() const
const access to internal map
Definition: StDataASCII.hh:392
std::map< std::string, std::string > _map
Definition: StDataASCII.hh:469
const std::string & asString(const std::string &key) const
return the associated string to the given key directly
Definition: StDataASCII.hh:315
void debugPrint(std::ostream &os) const
for debugging print whole internal map
Definition: StDataASCII.hh:413
size_t getArraySize(std::string key) const
get size of Array associated with given key.
char separatorChar() const
Definition: StDataASCII.hh:298
void setSeparatorChar(char c)
set character to separate elements of vectors within the value-string.
Definition: StDataASCII.hh:293
void stringToValue(const std::string &s, T &value) const
convert any std::string to any type using an std::istringstream.
Definition: StDataASCII.hh:360
bool valueExists(const std::string &key) const
check wether a value exists for requested key
Definition: StDataASCII.hh:131
void setExceptionFlag(bool f)
Specify, if getValue() should throw expcetions for unknown keys.
Definition: StDataASCII.hh:105
std::map< std::string, std::string >::const_iterator end() const
Definition: StDataASCII.hh:397
void getArray(const std::string &key, const ForwardIter &arrBegin, int containerSize=-1) const
getArray.
bool exceptionFlag() const
Definition: StDataASCII.hh:116
void getValue(const std::string &key, T &value) const
get a value (with arbitrary type) from the internal map<string,string> specified by the given key...
void setValue(const std::string &key, const T &value)
setValue writes/adds a value (with arbitrary type) to the internal map<string,string>.
void setFVArray(const std::string &key, const ForwardIter &arrBegin, size_t size)
setFVPList writes/adds a list (or a 1D array) of feature vectors to the internal map<string,string>.
void readArraySizeFromStream(std::istream &is, size_t &size) const
double asDouble(const std::string &key) const
Definition: StDataASCII.hh:331
size_t getFVArraySize(std::string key) const
get number of Feature vectors in the list associated with that kay
void stringToValue(const std::string &s, std::string &value) const
Definition: StDataASCII.hh:373
void getFVArray(const std::string &key, const ForwardIter &arrBegin, int containerSize=-1) const
get a list (or a 1D array) of feature vectors.
unsigned int asUint(const std::string &key) const
Definition: StDataASCII.hh:339
The StDataASCII class is a container for "structured data", that is kept completly in memory...
Definition: StDataASCII.hh:83
void setArray(const std::string &key, const ForwardIter &arrBegin, size_t size)
setArray writes/adds an array of values (with arbitrary type) to the internal map<string,string>.