OpenWalnut  1.5.0dev
hierarchicalClustering/hierchClustDisplay/WFileParser.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 //---------------------------------------------------------------------------
26 //
27 // Project: hClustering
28 //
29 // Whole-Brain Connectivity-Based Hierarchical Parcellation Project
30 // David Moreno-Dominguez
31 // d.mor.dom@gmail.com
32 // moreno@cbs.mpg.de
33 // www.cbs.mpg.de/~moreno//
34 // This file is also part of OpenWalnut ( http://www.openwalnut.org ).
35 //
36 // hClustering is free software: you can redistribute it and/or modify
37 // it under the terms of the GNU Lesser General Public License as published by
38 // the Free Software Foundation, either version 3 of the License, or
39 // (at your option) any later version.
40 // http://creativecommons.org/licenses/by-nc/3.0
41 //
42 // hClustering is distributed in the hope that it will be useful,
43 // but WITHOUT ANY WARRANTY; without even the implied warranty of
44 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 // GNU Lesser General Public License for more details.
46 //
47 //---------------------------------------------------------------------------
48 
49 
50 #include <string>
51 #include <vector>
52 
53 #include "WFileParser.h"
54 
55 
56 WFileParser::WFileParser( const std::string fileName ) :
57  m_fileName( fileName ),
58  m_tagIndicator( "#" ),
59  m_endIndicator( "end" ),
60  m_delimiter( " " )
61 {
62 }
63 
65 {
66 }
67 
69 {
70  using namespace boost::filesystem; //NOLINT
71 
72  if( !exists( m_fileName ) )
73  {
74  //debugLog() << "file doesn't exist";
75  return false;
76  }
77 
78  std::ifstream ifs( m_fileName.c_str(), std::ifstream::in );
79 
80  std::string line;
81 
82  if( !ifs.is_open() )
83  {
84  //debugLog() << "file open failed";
85  ifs.close();
86  return false;
87  }
88 
89  while( !ifs.eof() )
90  {
91  getline( ifs, line );
92 
93  m_rawLines.push_back( line );
94  }
95 
96  ifs.close();
97 
98  return true;
99 }
100 
101 std::vector<std::string>WFileParser::getLinesForTag( std::string tag )
102 {
103  std::string startTag = m_tagIndicator + tag;
104  std::string endTag = m_tagIndicator + m_endIndicator + tag;
105 
106  std::vector<std::string>returnVector;
107 
108  size_t i = 0;
109  while( i < m_rawLines.size() )
110  {
111  if( m_rawLines[i] == startTag )
112  {
113  //debugLog() << "coordinates tag at line " << i;
114  ++i;
115  break;
116  }
117  else
118  {
119  ++i;
120  }
121  }
122  while( i < m_rawLines.size() )
123  {
124  if( m_rawLines[i] == endTag )
125  {
126  //debugLog() << "endcoordinates tag at line " << i;
127  ++i;
128  break;
129  }
130  else
131  {
132  returnVector.push_back( m_rawLines[i] );
133  ++i;
134  }
135  }
136  return returnVector;
137 }
138 
139 std::vector< std::vector<std::string> >WFileParser::getLinesForTagSeparated( std::string tag )
140 {
141  std::string startTag = m_tagIndicator + tag;
142  std::string endTag = m_tagIndicator + m_endIndicator + tag;
143 
144  std::vector<std::vector<std::string > >returnVector;
145 
146  size_t i = 0;
147  while( i < m_rawLines.size() )
148  {
149  if( m_rawLines[i] == startTag )
150  {
151  //debugLog() << "coordinates tag at line " << i;
152  ++i;
153  break;
154  }
155  else
156  {
157  ++i;
158  }
159  }
160 
161  while( i < m_rawLines.size() )
162  {
163  if( m_rawLines[i] == endTag )
164  {
165  //debugLog() << "endcoordinates tag at line " << i;
166  ++i;
167  break;
168  }
169  else
170  {
171  std::vector<std::string>svec;
172  boost::regex reg( m_delimiter );
173  boost::sregex_token_iterator it( m_rawLines[i].begin(), m_rawLines[i].end(), reg, -1 );
174  boost::sregex_token_iterator end;
175  while( it != end )
176  {
177  svec.push_back( *it++ );
178  }
179 
180  returnVector.push_back( svec );
181  ++i;
182  }
183  }
184  return returnVector;
185 }
std::string m_fileName
the file name of the file to parse
std::vector< std::string > m_rawLines
vector of every line in the file
std::vector< std::vector< std::string > > getLinesForTagSeparated(std::string tag)
getter
WFileParser(const std::string fileName)
constructor
std::string m_delimiter
delimiter for entries in a line
bool readFile()
helper function to read a text file
std::vector< std::string > getLinesForTag(std::string tag)
getter
std::string m_endIndicator
string marking the end of a tagged area
std::string m_tagIndicator
string marking a line as tag