Skip to content

Commit e85743f

Browse files
authored
Merge pull request InsightSoftwareConsortium#5964 from N-Dekker/RAII-for-input-file-OBJMeshIO
2 parents d4c4785 + 753e775 commit e85743f

File tree

2 files changed

+15
-39
lines changed

2 files changed

+15
-39
lines changed

Modules/IO/MeshOBJ/include/itkOBJMeshIO.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,9 @@ class ITKIOMeshOBJ_EXPORT OBJMeshIO : public MeshIOBase
178178
void
179179
PrintSelf(std::ostream & os, Indent indent) const override;
180180

181-
void
182-
OpenFile();
183-
184-
void
185-
CloseFile();
186-
187181
private:
188-
std::ifstream m_InputFile{};
189-
std::streampos m_PointsStartPosition{}; // file position for points relative to
190-
// std::ios::beg
182+
[[nodiscard]] std::ifstream
183+
OpenFile() const;
191184
};
192185
} // end namespace itk
193186

Modules/IO/MeshOBJ/src/itkOBJMeshIO.cxx

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ OBJMeshIO::CanWriteFile(const char * fileName)
5757
return true;
5858
}
5959

60-
void
61-
OBJMeshIO::OpenFile()
60+
std::ifstream
61+
OBJMeshIO::OpenFile() const
6262
{
6363
if (this->m_FileName.empty())
6464
{
@@ -74,26 +74,17 @@ OBJMeshIO::OpenFile()
7474
// Due to the windows couldn't work well for tellg() and seekg() for ASCII
7575
// mode, hence we
7676
// open the file with std::ios::binary
77-
m_InputFile.open(this->m_FileName.c_str(), std::ios_base::in | std::ios::binary);
77+
std::ifstream inputFile(m_FileName, std::ios::binary);
7878

7979
// Test whether the file was opened
80-
if (!m_InputFile.is_open())
80+
if (!inputFile.is_open())
8181
{
8282
itkExceptionMacro("Unable to open file " << this->m_FileName);
8383
}
8484

85-
// If not set to start of file stream, windows won't work properly
86-
m_InputFile.seekg(0, std::ios::beg);
85+
return inputFile;
8786
}
8887

89-
void
90-
OBJMeshIO::CloseFile()
91-
{
92-
if (m_InputFile.is_open())
93-
{
94-
m_InputFile.close();
95-
}
96-
}
9788

9889
bool
9990
OBJMeshIO::SplitLine(const std::string & line, std::string & type, std::string & content)
@@ -127,7 +118,7 @@ void
127118
OBJMeshIO::ReadMeshInformation()
128119
{
129120
// Define input file stream and attach it to input file
130-
OpenFile();
121+
std::ifstream inputFile = OpenFile();
131122

132123
// Read and analyze the first line in the file
133124
SizeValueType numberOfCellPoints = 0;
@@ -138,7 +129,7 @@ OBJMeshIO::ReadMeshInformation()
138129
std::string inputLine;
139130
std::string type;
140131
const std::locale loc;
141-
while (std::getline(m_InputFile, line, '\n'))
132+
while (std::getline(inputFile, line, '\n'))
142133
{
143134
if (SplitLine(line, type, inputLine) && !inputLine.empty())
144135
{
@@ -191,15 +182,13 @@ OBJMeshIO::ReadMeshInformation()
191182
this->m_CellPixelType = IOPixelEnum::VECTOR;
192183
this->m_NumberOfCellPixelComponents = 3;
193184
this->m_UpdateCellData = false;
194-
195-
CloseFile();
196185
}
197186

198187
void
199188
OBJMeshIO::ReadPoints(void * buffer)
200189
{
201190
// Define input file stream and attach it to input file
202-
OpenFile();
191+
std::ifstream inputFile = OpenFile();
203192

204193
// Number of data array
205194
auto * data = static_cast<float *>(buffer);
@@ -210,7 +199,7 @@ OBJMeshIO::ReadPoints(void * buffer)
210199
std::string inputLine;
211200
std::string type;
212201
const std::locale loc;
213-
while (std::getline(m_InputFile, line, '\n'))
202+
while (std::getline(inputFile, line, '\n'))
214203
{
215204
if (SplitLine(line, type, inputLine) && !inputLine.empty())
216205
{
@@ -224,15 +213,13 @@ OBJMeshIO::ReadPoints(void * buffer)
224213
}
225214
}
226215
}
227-
228-
CloseFile();
229216
}
230217

231218
void
232219
OBJMeshIO::ReadCells(void * buffer)
233220
{
234221
// Define input file stream and attach it to input file
235-
OpenFile();
222+
std::ifstream inputFile = OpenFile();
236223

237224
// Read and analyze the first line in the file
238225
const auto data = make_unique_for_overwrite<long[]>(this->m_CellBufferSize - this->m_NumberOfCells);
@@ -242,7 +229,7 @@ OBJMeshIO::ReadCells(void * buffer)
242229
std::string inputLine;
243230
std::string type;
244231
const std::locale loc;
245-
while (std::getline(m_InputFile, line, '\n'))
232+
while (std::getline(inputFile, line, '\n'))
246233
{
247234
if (SplitLine(line, type, inputLine) && !inputLine.empty())
248235
{
@@ -276,8 +263,6 @@ OBJMeshIO::ReadCells(void * buffer)
276263
}
277264
}
278265

279-
CloseFile();
280-
281266
this->WriteCellsBuffer(
282267
data.get(), static_cast<long *>(buffer), CellGeometryEnum::POLYGON_CELL, this->m_NumberOfCells);
283268
// this->WriteCellsBuffer(data, static_cast<unsigned int *>(buffer),
@@ -288,7 +273,7 @@ void
288273
OBJMeshIO::ReadPointData(void * buffer)
289274
{
290275
// Define input file stream and attach it to input file
291-
OpenFile();
276+
std::ifstream inputFile = OpenFile();
292277

293278
// Number of data array
294279
auto * data = static_cast<float *>(buffer);
@@ -299,7 +284,7 @@ OBJMeshIO::ReadPointData(void * buffer)
299284
std::string inputLine;
300285
std::string type;
301286
const std::locale loc;
302-
while (std::getline(m_InputFile, line, '\n'))
287+
while (std::getline(inputFile, line, '\n'))
303288
{
304289
if (SplitLine(line, type, inputLine) && !inputLine.empty())
305290
{
@@ -313,8 +298,6 @@ OBJMeshIO::ReadPointData(void * buffer)
313298
}
314299
}
315300
}
316-
317-
CloseFile();
318301
}
319302

320303
void

0 commit comments

Comments
 (0)