DGtal  0.9.2
TableReader.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file TableReader.ih
19  * @author Bertrand Kerautret (\c kerautre@loria.fr )
20  * LORIA (CNRS, UMR 7503), University of Nancy, France
21  *
22  * @date 2013/11/30
23  *
24  * Implementation of inline methods defined in TableReader.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 //////////////////////////////////////////////////////////////////////////////
34 #include <cstdlib>
35 #include <sstream>
36 #include <fstream>
37 //////////////////////////////////////////////////////////////////////////////
38 
39 
40 
41 ///////////////////////////////////////////////////////////////////////////////
42 // Implementation of inline methods //
43 
44 
45 
46 
47 template<typename TQuantity>
48 inline
49 std::vector<TQuantity>
50 DGtal::TableReader<TQuantity>::getColumnElementsFromFile (const std::string &aFilename, unsigned int aPosition)
51 {
52  std::ifstream infile;
53  infile.open (aFilename.c_str(), std::ifstream::in);
54  return TableReader<TQuantity>::getColumnElementsFromInputStream(infile, aPosition);
55 }
56 
57 
58 template<typename TQuantity>
59 inline
60 std::vector< TQuantity >
61 DGtal::TableReader<TQuantity>::getColumnElementsFromInputStream (std::istream &in,
62  unsigned int aPosition)
63 {
64  std::vector<TQuantity> vectResult;
65  std::string str;
66  getline(in, str );
67  while ( in.good() ){
68  if ( ( str != "" ) && ( str[ 0 ] != '#' ) ){
69  std::istringstream in_str( str );
70  std::string wordVal;
71  TQuantity val;
72  bool found = false;
73  unsigned int idx = 0;
74  while ( in_str.good()&& !found){
75  std::operator>>(in_str, wordVal);
76  std::istringstream word_str( wordVal );
77  word_str >> val;
78  bool isOK = !word_str.fail();
79  if (isOK && idx == aPosition) {
80  found=true;
81  vectResult.push_back(val);
82  }
83  idx++;
84  }
85  }
86  getline(in, str );
87  }
88  return vectResult;
89 
90 }
91 
92 
93 // //
94 ///////////////////////////////////////////////////////////////////////////////