1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkFileResourceStream.h"
#include "vtkObjectFactory.h"
#include <vtksys/FStream.hxx>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkFileResourceStream);
//------------------------------------------------------------------------------
struct vtkFileResourceStream::vtkInternals
{
vtksys::ifstream File;
};
//------------------------------------------------------------------------------
vtkFileResourceStream::vtkFileResourceStream()
: vtkResourceStream{ true }
, Impl{ new vtkFileResourceStream::vtkInternals }
{
}
//------------------------------------------------------------------------------
vtkFileResourceStream::~vtkFileResourceStream() = default;
//------------------------------------------------------------------------------
bool vtkFileResourceStream::Open(VTK_FILEPATH const char* path)
{
if (this->Impl->File.is_open())
{
this->Impl->File.close();
}
if (path)
{
this->Impl->File.open(path, std::ios_base::binary);
}
this->Modified();
return this->Impl->File.is_open();
}
//------------------------------------------------------------------------------
std::size_t vtkFileResourceStream::Read(void* buffer, std::size_t bytes)
{
if (bytes == 0)
{
return 0;
}
this->Impl->File.read(static_cast<char*>(buffer), bytes);
return static_cast<std::size_t>(this->Impl->File.gcount());
}
//------------------------------------------------------------------------------
bool vtkFileResourceStream::EndOfStream()
{
return !(this->Impl->File && this->Impl->File.is_open());
}
//------------------------------------------------------------------------------
vtkTypeInt64 vtkFileResourceStream::Seek(vtkTypeInt64 pos, SeekDirection dir)
{
// We want EndOfFile to be false after a Seek call, so we have to clear the stream
// seekg will clear eofbit, but we need to clear failbit. badbit should be kept.
this->Impl->File.clear(this->Impl->File.rdstate() & ~(std::ios_base::failbit));
switch (dir)
{
case SeekDirection::Begin:
return static_cast<vtkTypeInt64>(this->Impl->File.seekg(pos, std::ios_base::beg).tellg());
case SeekDirection::Current:
return static_cast<vtkTypeInt64>(this->Impl->File.seekg(pos, std::ios_base::cur).tellg());
case SeekDirection::End:
return static_cast<vtkTypeInt64>(this->Impl->File.seekg(pos, std::ios_base::end).tellg());
}
return -1;
}
//------------------------------------------------------------------------------
vtkTypeInt64 vtkFileResourceStream::Tell()
{
return static_cast<vtkTypeInt64>(this->Impl->File.tellg());
}
//------------------------------------------------------------------------------
void vtkFileResourceStream::PrintSelf(ostream& os, vtkIndent indent)
{
Superclass::PrintSelf(os, indent);
os << indent << "Open: " << (this->Impl->File.is_open() ? "yes" : "no") << "\n";
}
VTK_ABI_NAMESPACE_END
|