Developer Documentation
FileManager.cc
1 /*===========================================================================*\
2  * *
3  * OpenVolumeMesh *
4  * Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openvolumemesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenVolumeMesh. *
9  * *
10  * OpenVolumeMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenVolumeMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenVolumeMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision$ *
38  * $Date$ *
39  * $LastChangedBy$ *
40  * *
41 \*===========================================================================*/
42 
43 #define FILEMANAGERT_CC
44 
45 #include <vector>
46 #include <iostream>
47 #include <sstream>
48 #include <algorithm>
49 #include <typeinfo>
50 
51 #include <OpenVolumeMesh/Geometry/VectorT.hh>
52 #include <OpenVolumeMesh/Mesh/PolyhedralMesh.hh>
53 
54 #include "FileManager.hh"
55 
56 namespace OpenVolumeMesh {
57 
58 namespace IO {
59 
60 //==================================================
61 
63 
64 }
65 
66 //==================================================
67 
69 
70 }
71 
72 //==================================================
73 
74 void FileManager::trimString(std::string& _string) const {
75 
76  // Trim Both leading and trailing spaces
77  size_t start = _string.find_first_not_of(" \t\r\n");
78  size_t end = _string.find_last_not_of(" \t\r\n");
79 
80  if((std::string::npos == start) || (std::string::npos == end)) {
81  _string = "";
82  } else {
83  _string = _string.substr(start, end - start + 1);
84  }
85 }
86 
87 //==================================================
88 
89 void FileManager::extractQuotedText(std::string& _string) const {
90 
91  // Trim Both leading and trailing quote marks
92  size_t start = _string.find_first_of("\""); ++start;
93  size_t end = _string.find_last_not_of("\"");
94 
95  if((std::string::npos == start) || (std::string::npos == end)) {
96  _string = "";
97  } else {
98  _string = _string.substr(start, end - start + 1);
99  }
100 }
101 
102 //==================================================
103 
104 bool FileManager::getCleanLine(std::istream& _ifs, std::string& _string, bool _skipEmptyLines) const {
105 
106  // While we are not at the end of the file
107  while(true) {
108 
109  // Get the current line:
110  std::getline(_ifs, _string);
111 
112  // Remove whitespace at beginning and end
113  trimString(_string);
114 
115  // Check if string is not empty ( otherwise we continue
116  if(_string.size() != 0) {
117 
118  // Check if string is a comment ( starting with # )
119  if(_string[0] != '#') {
120  return true;
121  }
122 
123  } else {
124  if(!_skipEmptyLines)
125  return true;
126  }
127 
128  if(_ifs.eof()) {
129  if (verbosity_level_ >= 2) {
130  std::cerr << "End of file reached while searching for input!" << std::endl;
131  }
132  return false;
133  }
134  }
135 
136  return false;
137 }
138 
139 //==================================================
140 
141 bool FileManager::isHexahedralMesh(const std::string& _filename) const {
142 
143  std::ifstream iff(_filename.c_str(), std::ios::in);
144 
145  if(!iff.good()) {
146  if (verbosity_level_ >= 1) {
147  std::cerr << "Could not open file " << _filename << " for reading!" << std::endl;
148  }
149  iff.close();
150  return false;
151  }
152 
153  std::string s;
154  unsigned int n = 0u;
155 
156  // Skip until we find polyhedra section
157  while (iff.good()) {
158  iff >> s;
159  if (s == "Polyhedra") {
160  break;
161  }
162  }
163 
164  if (iff.eof()) {
165  // Polyhedra section not found in file. Defaulting to polyhedral type.
166  iff.close();
167  return false;
168  }
169 
170  // Read in number of cells
171  iff >> n;
172  if(n == 0) return false;
173  unsigned int v = 0;
174  char tmp[256];
175  for (unsigned int i = 0; i < n; ++i) {
176  iff >> v;
177  iff.getline(tmp, 256);
178  if (v != 6u) {
179  iff.close();
180  return false;
181  }
182  }
183  iff.close();
184  return true;
185 }
186 
187 bool FileManager::isTetrahedralMesh(const std::string& _filename) const {
188 
189  std::ifstream iff(_filename.c_str(), std::ios::in);
190 
191  if(!iff.good()) {
192  if (verbosity_level_ >= 1) {
193  std::cerr << "Could not open file " << _filename << " for reading!" << std::endl;
194  }
195  iff.close();
196  return false;
197  }
198 
199  std::string s;
200  unsigned int n = 0u;
201 
202  // Skip until we find polyhedra section
203  while (iff.good()) {
204  iff >> s;
205  if (s == "Polyhedra") {
206  break;
207  }
208  }
209 
210  if (iff.eof()) {
211  // Polyhedra section not found in file. Defaulting to polyhedral type.
212  iff.close();
213  return false;
214  }
215 
216  // Read in number of cells
217  iff >> n;
218  if(n == 0) return false;
219  unsigned int v = 0;
220  char tmp[256];
221  for (unsigned int i = 0; i < n; ++i) {
222  iff >> v;
223  iff.getline(tmp, 256);
224  if (v != 4u) {
225  iff.close();
226  return false;
227  }
228  }
229  iff.close();
230  return true;
231 }
232 
233 //==================================================
234 
235 } // Namespace IO
236 } // Namespace OpenVolumeMesh
~FileManager()
Default destructor.
Definition: FileManager.cc:68
bool isHexahedralMesh(const std::string &_filename) const
Test whether given file contains a hexahedral mesh.
Definition: FileManager.cc:141
FileManager()
Default constructor.
Definition: FileManager.cc:62
bool isTetrahedralMesh(const std::string &_filename) const
Test whether given file contains a tetrahedral mesh.
Definition: FileManager.cc:187