DGtal 1.3.0
Loading...
Searching...
No Matches
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
47template<typename TQuantity>
48inline
49std::vector<TQuantity>
50DGtal::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
57template <typename TQuantity>
58inline std::vector<TQuantity>
59DGtal::TableReader<TQuantity>::getColumnElementsFromInputStream(
60std::istream & in, unsigned int aPosition )
61{
62 std::vector<TQuantity> vectResult;
63 std::string str;
64 getline(in, str );
65 while ( in.good() )
66 {
67 if ( ( str != "" ) && ( str[ 0 ] != '#' ) )
68 {
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 {
76 std::operator>>( in_str, wordVal );
77 std::istringstream word_str( wordVal );
78 word_str >> val;
79 bool isOK = !word_str.fail();
80 if ( isOK && idx == aPosition )
81 {
82 found = true;
83 vectResult.push_back( val );
84 }
85 idx++;
86 }
87 }
88 getline( in, str );
89 }
90 return vectResult;
91}
92
93template <typename TQuantity>
94inline std::vector<std::vector<TQuantity>>
95DGtal::TableReader<TQuantity>::getLinesElementsFromFile(
96const std::string & aFilename )
97{
98 std::ifstream infile;
99 infile.open( aFilename.c_str(), std::ifstream::in );
100 return DGtal::TableReader<TQuantity>::getLinesElementsFromInputStream(
101 infile );
102}
103
104template <typename TQuantity>
105inline std::vector<std::vector<TQuantity>>
106DGtal::TableReader<TQuantity>::getLinesElementsFromInputStream(
107std::istream & in )
108{
109 std::vector<std::vector<TQuantity>> vectResult;
110 std::string str;
111 getline( in, str );
112 while ( in.good() )
113 {
114 std::vector<TQuantity> aLine;
115 if ( ( str != "" ) && ( str[ 0 ] != '#' ) )
116 {
117 std::istringstream in_str( str );
118 std::string wordVal;
119 TQuantity val;
120 while ( in_str.good() )
121 {
122 std::operator>>( in_str, wordVal );
123 std::istringstream word_str( wordVal );
124 word_str >> val;
125 if ( !word_str.fail() )
126 {
127 aLine.push_back( val );
128 }
129 }
130 vectResult.push_back( aLine );
131 }
132 getline( in, str );
133 }
134 return vectResult;
135}
136
137// //
138///////////////////////////////////////////////////////////////////////////////