Developer Documentation
Loading...
Searching...
No Matches
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#define FILEMANAGERT_CC
37
38#include <vector>
39#include <iostream>
40#include <sstream>
41#include <algorithm>
42#include <typeinfo>
43
44#include <OpenVolumeMesh/Geometry/VectorT.hh>
45#include <OpenVolumeMesh/Mesh/PolyhedralMesh.hh>
46
47#include <OpenVolumeMesh/FileManager/FileManager.hh>
48
49namespace OpenVolumeMesh {
50
51namespace IO {
52
53//==================================================
54
58
59//==================================================
60
64
65//==================================================
66
67void FileManager::trimString(std::string& _string) const {
68
69 // Trim Both leading and trailing spaces
70 size_t start = _string.find_first_not_of(" \t\r\n");
71 size_t end = _string.find_last_not_of(" \t\r\n");
72
73 if((std::string::npos == start) || (std::string::npos == end)) {
74 _string = "";
75 } else {
76 _string = _string.substr(start, end - start + 1);
77 }
78}
79
80//==================================================
81
82void FileManager::extractQuotedText(std::string& _string) const {
83
84 // Trim Both leading and trailing quote marks
85 size_t start = _string.find_first_of("\""); ++start;
86 size_t end = _string.find_last_not_of("\"");
87
88 if((std::string::npos == start) || (std::string::npos == end)) {
89 _string = "";
90 } else {
91 _string = _string.substr(start, end - start + 1);
92 }
93}
94
95//==================================================
96
97bool FileManager::getCleanLine(std::istream& _ifs, std::string& _string, bool _skipEmptyLines) const {
98
99 // While we are not at the end of the file
100 while(true) {
101
102 // Get the current line:
103 std::getline(_ifs, _string);
104
105 // Remove whitespace at beginning and end
106 trimString(_string);
107
108 // Check if string is not empty ( otherwise we continue
109 if(_string.size() != 0) {
110
111 // Check if string is a comment ( starting with # )
112 if(_string[0] != '#') {
113 return true;
114 }
115
116 } else {
117 if(!_skipEmptyLines)
118 return true;
119 }
120
121 if(_ifs.eof()) {
122 if (verbosity_level_ >= 2) {
123 std::cerr << "End of file reached while searching for input!" << std::endl;
124 }
125 return false;
126 }
127 }
128
129 return false;
130}
131
132//==================================================
133
134bool FileManager::isHexahedralMesh(const std::string& _filename) const {
135
136 std::ifstream iff(_filename.c_str(), std::ios::in);
137
138 if(!iff.good()) {
139 if (verbosity_level_ >= 1) {
140 std::cerr << "Could not open file " << _filename << " for reading!" << std::endl;
141 }
142 iff.close();
143 return false;
144 }
145
146 std::string s;
147 unsigned int n = 0u;
148
149 // Skip until we find polyhedra section
150 while (iff.good()) {
151 iff >> s;
152 if (s == "Polyhedra") {
153 break;
154 }
155 }
156
157 if (iff.eof()) {
158 // Polyhedra section not found in file. Defaulting to polyhedral type.
159 iff.close();
160 return false;
161 }
162
163 // Read in number of cells
164 iff >> n;
165 if(n == 0) return false;
166 unsigned int v = 0;
167 char tmp[256];
168 for (unsigned int i = 0; i < n; ++i) {
169 iff >> v;
170 iff.getline(tmp, 256);
171 if (v != 6u) {
172 iff.close();
173 return false;
174 }
175 }
176 iff.close();
177 return true;
178}
179
180// cppcheck-suppress unusedFunction ; public interface
181bool FileManager::isTetrahedralMesh(const std::string& _filename) const {
182
183 std::ifstream iff(_filename.c_str(), std::ios::in);
184
185 if(!iff.good()) {
186 if (verbosity_level_ >= 1) {
187 std::cerr << "Could not open file " << _filename << " for reading!" << std::endl;
188 }
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 (iff.good()) {
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.
bool isHexahedralMesh(const std::string &_filename) const
Test whether given file contains a hexahedral mesh.
~FileManager()
Default destructor.
FileManager()
Default constructor.