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 // cppcheck-suppress unusedFunction ; public interface
188 bool FileManager::isTetrahedralMesh(const std::string& _filename) const {
189 
190  std::ifstream iff(_filename.c_str(), std::ios::in);
191 
192  if(!iff.good()) {
193  if (verbosity_level_ >= 1) {
194  std::cerr << "Could not open file " << _filename << " for reading!" << std::endl;
195  }
196  iff.close();
197  return false;
198  }
199 
200  std::string s;
201  unsigned int n = 0u;
202 
203  // Skip until we find polyhedra section
204  while (iff.good()) {
205  iff >> s;
206  if (s == "Polyhedra") {
207  break;
208  }
209  }
210 
211  if (iff.eof()) {
212  // Polyhedra section not found in file. Defaulting to polyhedral type.
213  iff.close();
214  return false;
215  }
216 
217  // Read in number of cells
218  iff >> n;
219  if(n == 0) return false;
220  unsigned int v = 0;
221  char tmp[256];
222  for (unsigned int i = 0; i < n; ++i) {
223  iff >> v;
224  iff.getline(tmp, 256);
225  if (v != 4u) {
226  iff.close();
227  return false;
228  }
229  }
230  iff.close();
231  return true;
232 }
233 
234 //==================================================
235 
236 } // Namespace IO
237 } // Namespace OpenVolumeMesh
bool isTetrahedralMesh(const std::string &_filename) const
Test whether given file contains a tetrahedral mesh.
Definition: FileManager.cc:188
~FileManager()
Default destructor.
Definition: FileManager.cc:68
FileManager()
Default constructor.
Definition: FileManager.cc:62
bool isHexahedralMesh(const std::string &_filename) const
Test whether given file contains a hexahedral mesh.
Definition: FileManager.cc:141