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  std::cerr << "End of file reached while searching for input!" << std::endl;
130  return false;
131  }
132  }
133 
134  return false;
135 }
136 
137 //==================================================
138 
139 bool FileManager::isHexahedralMesh(const std::string& _filename) const {
140 
141  std::ifstream iff(_filename.c_str(), std::ios::in);
142 
143  if(!iff.good()) {
144  std::cerr << "Could not open file " << _filename << " for reading!" << std::endl;
145  iff.close();
146  return false;
147  }
148 
149  std::string s;
150  unsigned int n = 0u;
151 
152  // Skip until we find polyhedra section
153  while (true || !iff.eof()) {
154  iff >> s;
155  if (s == "Polyhedra") {
156  break;
157  }
158  }
159 
160  if (iff.eof()) {
161  // Polyhedra section not found in file. Defaulting to polyhedral type.
162  iff.close();
163  return false;
164  }
165 
166  // Read in number of cells
167  iff >> n;
168  if(n == 0) return false;
169  unsigned int v = 0;
170  char tmp[256];
171  for (unsigned int i = 0; i < n; ++i) {
172  iff >> v;
173  iff.getline(tmp, 256);
174  if (v != 6u) {
175  iff.close();
176  return false;
177  }
178  }
179  iff.close();
180  return true;
181 }
182 
183 bool FileManager::isTetrahedralMesh(const std::string& _filename) const {
184 
185  std::ifstream iff(_filename.c_str(), std::ios::in);
186 
187  if(!iff.good()) {
188  std::cerr << "Could not open file " << _filename << " for reading!" << std::endl;
189  iff.close();
190  return false;
191  }
192 
193  std::string s;
194  unsigned int n = 0u;
195 
196  // Skip until we find polyhedra section
197  while (true || !iff.eof()) {
198  iff >> s;
199  if (s == "Polyhedra") {
200  break;
201  }
202  }
203 
204  if (iff.eof()) {
205  // Polyhedra section not found in file. Defaulting to polyhedral type.
206  iff.close();
207  return false;
208  }
209 
210  // Read in number of cells
211  iff >> n;
212  if(n == 0) return false;
213  unsigned int v = 0;
214  char tmp[256];
215  for (unsigned int i = 0; i < n; ++i) {
216  iff >> v;
217  iff.getline(tmp, 256);
218  if (v != 4u) {
219  iff.close();
220  return false;
221  }
222  }
223  iff.close();
224  return true;
225 }
226 
227 //==================================================
228 
229 } // Namespace IO
230 } // Namespace OpenVolumeMesh
bool isTetrahedralMesh(const std::string &_filename) const
Test whether given file contains a tetrahedral mesh.
Definition: FileManager.cc:183
bool isHexahedralMesh(const std::string &_filename) const
Test whether given file contains a hexahedral mesh.
Definition: FileManager.cc:139
FileManager()
Default constructor.
Definition: FileManager.cc:62
~FileManager()
Default destructor.
Definition: FileManager.cc:68