OpenWalnut  1.5.0dev
clusterDisplayVoxels/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 #include <fstream>
26 #include <string>
27 #include <vector>
28 
29 #include <boost/filesystem.hpp>
30 #include <boost/regex.hpp>
31 
32 #include "core/common/WLogger.h"
33 
34 #include "WFileParser.h"
35 
36 WFileParser::WFileParser( const std::string fileName ) :
37  m_fileName( fileName ),
38  m_tagIndicator( "#" ),
39  m_endIndicator( "end" ),
40  m_delimiter( "," )
41 {
42 }
43 
45 {
46 }
47 
49 {
50  using namespace boost::filesystem; //NOLINT
51 
52  if( !exists( m_fileName ) )
53  {
54  //debugLog() << "file doesn't exist";
55  return false;
56  }
57 
58  std::ifstream ifs( m_fileName.c_str(), std::ifstream::in );
59 
60  std::string line;
61 
62  if( !ifs.is_open() )
63  {
64  //debugLog() << "file open failed";
65  ifs.close();
66  return false;
67  }
68 
69  while( !ifs.eof() )
70  {
71  getline( ifs, line );
72 
73  m_rawLines.push_back( std::string( line ) );
74  }
75 
76  ifs.close();
77 
78  return true;
79 }
80 
81 std::vector<std::string>WFileParser::getLinesForTag( std::string tag )
82 {
83  std::string startTag = m_tagIndicator + tag;
84  std::string endTag = m_tagIndicator + m_endIndicator + tag;
85 
86  std::vector<std::string>returnVector;
87 
88  size_t i = 0;
89  while( i < m_rawLines.size() )
90  {
91  if( m_rawLines[i] == startTag )
92  {
93  //debugLog() << "coordinates tag at line " << i;
94  ++i;
95  break;
96  }
97  else
98  {
99  ++i;
100  }
101  }
102  while( i < m_rawLines.size() )
103  {
104  if( m_rawLines[i] == endTag )
105  {
106  //debugLog() << "endcoordinates tag at line " << i;
107  ++i;
108  break;
109  }
110  else
111  {
112  returnVector.push_back( m_rawLines[i] );
113  ++i;
114  }
115  }
116  return returnVector;
117 }
118 
119 std::vector< std::vector<std::string> >WFileParser::getLinesForTagSeparated( std::string tag )
120 {
121  std::string startTag = m_tagIndicator + tag;
122  std::string endTag = m_tagIndicator + m_endIndicator + tag;
123 
124  std::vector<std::vector<std::string > >returnVector;
125 
126  size_t i = 0;
127  while( i < m_rawLines.size() )
128  {
129  if( m_rawLines[i] == startTag )
130  {
131  //debugLog() << "coordinates tag at line " << i;
132  ++i;
133  break;
134  }
135  else
136  {
137  ++i;
138  }
139  }
140  while( i < m_rawLines.size() )
141  {
142  if( m_rawLines[i] == endTag )
143  {
144  //debugLog() << "endcoordinates tag at line " << i;
145  ++i;
146  break;
147  }
148  else
149  {
150  std::vector<std::string>svec;
151  boost::regex reg( m_delimiter );
152  boost::sregex_token_iterator it( m_rawLines[i].begin(), m_rawLines[i].end(), reg, -1 );
153  boost::sregex_token_iterator end;
154  while( it != end )
155  {
156  svec.push_back( *it++ );
157  }
158 
159  returnVector.push_back( svec );
160  ++i;
161  }
162  }
163  return returnVector;
164 }
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