mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-17 18:27:56 -05:00
Replace FileAPI with c++ streams and std::filesystem
This commit is contained in:
@ -1,49 +1,74 @@
|
||||
#include "FileToZlibWrapper.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
|
||||
voidpf Wrapper_Zlib_FileOpen(voidpf opaque, const char*, int)
|
||||
{
|
||||
return opaque;
|
||||
}
|
||||
|
||||
uLong Wrapper_Zlib_FileRead(voidpf, voidpf stream, void* buf, const uLong size)
|
||||
template<typename T>
|
||||
uLong Wrapper_Zlib_FileRead(voidpf opaque, voidpf stream, void* buf, const uLong size)
|
||||
{
|
||||
auto* file = reinterpret_cast<FileAPI::IFile*>(stream);
|
||||
auto* file = static_cast<T*>(stream);
|
||||
|
||||
return file->Read(buf, 1, size);
|
||||
file->read(static_cast<char*>(buf), size);
|
||||
|
||||
return static_cast<uLong>(file->gcount());
|
||||
}
|
||||
|
||||
uLong Wrapper_Zlib_NoFileRead(voidpf opaque, voidpf stream, void* buf, const uLong size)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
uLong Wrapper_Zlib_FileWrite(voidpf opaque, voidpf stream, const void* buf, const uLong size)
|
||||
{
|
||||
auto* file = reinterpret_cast<FileAPI::IFile*>(stream);
|
||||
|
||||
return file->Write(buf, 1, size);
|
||||
auto* file = static_cast<T*>(stream);
|
||||
file->write(static_cast<const char*>(buf), size);
|
||||
return size;
|
||||
}
|
||||
|
||||
long Wrapper_Zlib_FileTell(voidpf opaque, voidpf stream)
|
||||
uLong Wrapper_Zlib_NoFileWrite(voidpf opaque, voidpf stream, const void* buf, const uLong size)
|
||||
{
|
||||
auto* file = reinterpret_cast<FileAPI::IFile*>(stream);
|
||||
|
||||
return static_cast<long>(file->Pos());
|
||||
return 0;
|
||||
}
|
||||
|
||||
long Wrapper_Zlib_FileSeek(voidpf opaque, voidpf stream, const uLong offset, const int origin)
|
||||
template<typename T>
|
||||
long Wrapper_Zlib_FileTellRead(voidpf opaque, voidpf stream)
|
||||
{
|
||||
auto* file = reinterpret_cast<FileAPI::IFile*>(stream);
|
||||
auto* file = static_cast<T*>(stream);
|
||||
|
||||
return static_cast<long>(file->tellg());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
long Wrapper_Zlib_FileTellWrite(voidpf opaque, voidpf stream)
|
||||
{
|
||||
auto* file = static_cast<T*>(stream);
|
||||
|
||||
return static_cast<long>(file->tellp());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
long Wrapper_Zlib_FileSeekRead(voidpf opaque, voidpf stream, const uLong offset, const int origin)
|
||||
{
|
||||
auto* file = static_cast<T*>(stream);
|
||||
|
||||
switch (origin)
|
||||
{
|
||||
case ZLIB_FILEFUNC_SEEK_CUR:
|
||||
file->Skip(offset);
|
||||
file->seekg(offset, SEEK_CUR);
|
||||
break;
|
||||
|
||||
case ZLIB_FILEFUNC_SEEK_END:
|
||||
assert(offset == 0);
|
||||
file->GotoEnd();
|
||||
file->seekg(offset, SEEK_END);
|
||||
break;
|
||||
|
||||
case ZLIB_FILEFUNC_SEEK_SET:
|
||||
file->Goto(offset);
|
||||
file->seekg(offset, SEEK_SET);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -53,17 +78,45 @@ long Wrapper_Zlib_FileSeek(voidpf opaque, voidpf stream, const uLong offset, con
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Wrapper_Zlib_FileClose(voidpf opaque, voidpf stream)
|
||||
template<typename T>
|
||||
long Wrapper_Zlib_FileSeekWrite(voidpf opaque, voidpf stream, const uLong offset, const int origin)
|
||||
{
|
||||
auto* file = reinterpret_cast<FileAPI::IFile*>(stream);
|
||||
auto* file = static_cast<T*>(stream);
|
||||
|
||||
if (file->IsOpen())
|
||||
switch (origin)
|
||||
{
|
||||
file->Close();
|
||||
return 0;
|
||||
case ZLIB_FILEFUNC_SEEK_CUR:
|
||||
file->seekp(offset, SEEK_CUR);
|
||||
break;
|
||||
|
||||
case ZLIB_FILEFUNC_SEEK_END:
|
||||
file->seekp(offset, SEEK_END);
|
||||
break;
|
||||
|
||||
case ZLIB_FILEFUNC_SEEK_SET:
|
||||
file->seekp(offset, SEEK_SET);
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int Wrapper_Zlib_FileClose(voidpf opaque, voidpf stream)
|
||||
{
|
||||
return 0;
|
||||
// auto* file = static_cast<T*>(stream);
|
||||
//
|
||||
// if (file->is_open())
|
||||
// {
|
||||
// file->close();
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// return -1;
|
||||
}
|
||||
|
||||
int Wrapper_Zlib_FileError(voidpf opaque, voidpf stream)
|
||||
@ -71,17 +124,47 @@ int Wrapper_Zlib_FileError(voidpf opaque, voidpf stream)
|
||||
return 0;
|
||||
}
|
||||
|
||||
zlib_filefunc_def FileToZlibWrapper::CreateFunctions32ForFile(FileAPI::IFile* file)
|
||||
zlib_filefunc_def FileToZlibWrapper::CreateFunctions32ForFile(std::iostream* stream)
|
||||
{
|
||||
return zlib_filefunc_def_s
|
||||
{
|
||||
Wrapper_Zlib_FileOpen,
|
||||
Wrapper_Zlib_FileRead,
|
||||
Wrapper_Zlib_FileWrite,
|
||||
Wrapper_Zlib_FileTell,
|
||||
Wrapper_Zlib_FileSeek,
|
||||
Wrapper_Zlib_FileClose,
|
||||
Wrapper_Zlib_FileRead<std::iostream>,
|
||||
Wrapper_Zlib_FileWrite<std::iostream>,
|
||||
Wrapper_Zlib_FileTellRead<std::iostream>,
|
||||
Wrapper_Zlib_FileSeekRead<std::iostream>,
|
||||
Wrapper_Zlib_FileClose<std::iostream>,
|
||||
Wrapper_Zlib_FileError,
|
||||
file
|
||||
stream
|
||||
};
|
||||
}
|
||||
|
||||
zlib_filefunc_def FileToZlibWrapper::CreateFunctions32ForFile(std::istream* stream)
|
||||
{
|
||||
return zlib_filefunc_def_s
|
||||
{
|
||||
Wrapper_Zlib_FileOpen,
|
||||
Wrapper_Zlib_FileRead<std::istream>,
|
||||
Wrapper_Zlib_NoFileWrite,
|
||||
Wrapper_Zlib_FileTellRead<std::istream>,
|
||||
Wrapper_Zlib_FileSeekRead<std::istream>,
|
||||
Wrapper_Zlib_FileClose<std::istream>,
|
||||
Wrapper_Zlib_FileError,
|
||||
stream
|
||||
};
|
||||
}
|
||||
|
||||
zlib_filefunc_def FileToZlibWrapper::CreateFunctions32ForFile(std::ostream* stream)
|
||||
{
|
||||
return zlib_filefunc_def_s
|
||||
{
|
||||
Wrapper_Zlib_FileOpen,
|
||||
Wrapper_Zlib_NoFileRead,
|
||||
Wrapper_Zlib_FileWrite<std::ostream>,
|
||||
Wrapper_Zlib_FileTellWrite<std::ostream>,
|
||||
Wrapper_Zlib_FileSeekWrite<std::ostream>,
|
||||
Wrapper_Zlib_FileClose<std::ostream>,
|
||||
Wrapper_Zlib_FileError,
|
||||
stream
|
||||
};
|
||||
}
|
||||
|
@ -1,10 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <ioapi.h>
|
||||
#include "Utils/FileAPI.h"
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
|
||||
#include "ObjStream.h"
|
||||
|
||||
class FileToZlibWrapper
|
||||
{
|
||||
public:
|
||||
static zlib_filefunc_def CreateFunctions32ForFile(FileAPI::IFile* file);
|
||||
static zlib_filefunc_def CreateFunctions32ForFile(std::iostream* stream);
|
||||
static zlib_filefunc_def CreateFunctions32ForFile(std::istream* stream);
|
||||
static zlib_filefunc_def CreateFunctions32ForFile(std::ostream* stream);
|
||||
};
|
||||
|
103
src/ObjCommon/Utils/ObjFileStream.h
Normal file
103
src/ObjCommon/Utils/ObjFileStream.h
Normal file
@ -0,0 +1,103 @@
|
||||
// ReSharper disable CppHidingFunction
|
||||
// ReSharper disable IdentifierTypo
|
||||
// ReSharper disable CppInconsistentNaming
|
||||
#pragma once
|
||||
|
||||
// #include <fstream>
|
||||
//
|
||||
// #include "ObjStream.h"
|
||||
//
|
||||
// template <class Elem, class Traits>
|
||||
// class basic_fobjbuf final : public basic_objbuf<Elem, Traits>, public std::basic_filebuf<Elem, Traits>
|
||||
// {
|
||||
// public:
|
||||
// using myfb = std::basic_filebuf<Elem, Traits>;
|
||||
//
|
||||
// explicit basic_fobjbuf(myfb* fb)
|
||||
// {
|
||||
// m_fb.swap(*fb);
|
||||
// }
|
||||
//
|
||||
// _NODISCARD bool is_open() const override
|
||||
// {
|
||||
// return m_fb.is_open();
|
||||
// }
|
||||
//
|
||||
// bool close() override
|
||||
// {
|
||||
// return m_fb.close();
|
||||
// }
|
||||
//
|
||||
// private:
|
||||
// myfb m_fb;
|
||||
// };
|
||||
//
|
||||
// template <class Elem, class Traits>
|
||||
// class basic_fobjstream final : public basic_objstream<Elem, Traits>
|
||||
// {
|
||||
// public:
|
||||
// using mybase = basic_objstream<Elem, Traits>;
|
||||
// using myfb = basic_fobjbuf<Elem, Traits>;
|
||||
//
|
||||
// using mybase::m_ob;
|
||||
//
|
||||
// explicit basic_fobjstream(std::fstream&& stream)
|
||||
// : mybase(std::make_unique<myfb>(std::move(*stream.rdbuf())))
|
||||
// {
|
||||
// // reinterpret_cast<myfb*>(m_ob.get())->swapp(*stream.rdbuf());
|
||||
// // reinterpret_cast<myfb*>(m_ob.get())->swap(*stream.rdbuf());
|
||||
// }
|
||||
//
|
||||
// basic_fobjstream(const basic_fobjstream& other) = delete;
|
||||
// basic_fobjstream(basic_fobjstream&& other) noexcept = default;
|
||||
// basic_fobjstream& operator=(const basic_fobjstream& other) = delete;
|
||||
// basic_fobjstream& operator=(basic_fobjstream&& other) noexcept = default;
|
||||
// };
|
||||
//
|
||||
// template <class Elem, class Traits>
|
||||
// class basic_ifobjstream final : public basic_iobjstream<Elem, Traits>
|
||||
// {
|
||||
// public:
|
||||
// using mybase = basic_iobjstream<Elem, Traits>;
|
||||
// using myfb = basic_fobjbuf<Elem, Traits>;
|
||||
//
|
||||
// using mybase::m_ob;
|
||||
//
|
||||
// explicit basic_ifobjstream(std::ifstream&& stream)
|
||||
// : mybase(std::make_unique<myfb>(std::move(*stream.rdbuf())))
|
||||
// {
|
||||
// // reinterpret_cast<myfb*>(m_ob.get())->swapp(*stream.rdbuf());
|
||||
// // m_ob->swapp(*stream.rdbuf());
|
||||
// }
|
||||
// ~basic_ifobjstream() = default;
|
||||
//
|
||||
// basic_ifobjstream(const basic_ifobjstream& other) = delete;
|
||||
// basic_ifobjstream(basic_ifobjstream&& other) noexcept = default;
|
||||
// basic_ifobjstream& operator=(const basic_ifobjstream& other) = delete;
|
||||
// basic_ifobjstream& operator=(basic_ifobjstream&& other) noexcept = default;
|
||||
// };
|
||||
//
|
||||
// template <class Elem, class Traits>
|
||||
// class basic_ofobjstream final : public basic_oobjstream<Elem, Traits>
|
||||
// {
|
||||
// public:
|
||||
// using mybase = basic_oobjstream<Elem, Traits>;
|
||||
// using myfb = basic_fobjbuf<Elem, Traits>;
|
||||
//
|
||||
// using mybase::m_ob;
|
||||
//
|
||||
// explicit basic_ofobjstream(std::ofstream file)
|
||||
// : mybase(std::make_unique<myfb>(file.rdbuf()))
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// basic_ofobjstream(const basic_ofobjstream& other) = delete;
|
||||
// basic_ofobjstream(basic_ofobjstream&& other) noexcept = default;
|
||||
// basic_ofobjstream& operator=(const basic_ofobjstream& other) = delete;
|
||||
// basic_ofobjstream& operator=(basic_ofobjstream&& other) noexcept = default;
|
||||
// };
|
||||
//
|
||||
// using fobjbuf = basic_fobjbuf<char, std::char_traits<char>>;
|
||||
// using fobjstream = basic_fobjstream<char, std::char_traits<char>>;
|
||||
// using ifobjstream = basic_ifobjstream<char, std::char_traits<char>>;
|
||||
// using ofobjstream = basic_ofobjstream<char, std::char_traits<char>>;
|
222
src/ObjCommon/Utils/ObjStream.h
Normal file
222
src/ObjCommon/Utils/ObjStream.h
Normal file
@ -0,0 +1,222 @@
|
||||
// ReSharper disable CppInconsistentNaming
|
||||
// ReSharper disable IdentifierTypo
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include <streambuf>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
template <class Elem, class Traits>
|
||||
class basic_objbuf : public std::basic_streambuf<Elem, Traits>
|
||||
{
|
||||
public:
|
||||
_NODISCARD virtual bool is_open() const = 0;
|
||||
virtual bool close() = 0;
|
||||
};
|
||||
|
||||
template <class Elem, class Traits>
|
||||
class basic_objstream : public std::basic_iostream<Elem, Traits>
|
||||
{
|
||||
public:
|
||||
using mybase = std::basic_iostream<Elem, Traits>;
|
||||
using myob = basic_objbuf<Elem, Traits>;
|
||||
using myios = std::basic_ios<Elem, Traits>;
|
||||
|
||||
explicit basic_objstream(std::unique_ptr<myob> objbuf)
|
||||
: std::basic_iostream<Elem, Traits>(objbuf.get()),
|
||||
m_ob(std::move(objbuf))
|
||||
{
|
||||
assert(m_ob);
|
||||
}
|
||||
|
||||
explicit basic_objstream(basic_objstream<Elem, Traits>&& right) noexcept
|
||||
: std::basic_iostream<Elem, Traits>(right),
|
||||
m_ob(std::move(right.m_ob))
|
||||
{
|
||||
assert(m_ob != nullptr);
|
||||
}
|
||||
|
||||
basic_objstream(const basic_objstream& other) = delete;
|
||||
basic_objstream& operator=(const basic_objstream& other) = delete;
|
||||
|
||||
~basic_objstream() override
|
||||
{
|
||||
if(m_ob)
|
||||
m_ob->close();
|
||||
}
|
||||
|
||||
void swap(basic_objstream& right) noexcept
|
||||
{
|
||||
if (this != _STD addressof(right))
|
||||
{
|
||||
mybase::swap(right);
|
||||
m_ob = std::move(right.m_ob);
|
||||
}
|
||||
}
|
||||
|
||||
basic_objstream& operator=(basic_objstream&& other) noexcept
|
||||
{
|
||||
swap(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ReSharper disable once CppHidingFunction
|
||||
_NODISCARD myob* rdbuf() const
|
||||
{
|
||||
return const_cast<myob*>(m_ob.get());
|
||||
}
|
||||
|
||||
_NODISCARD bool is_open() const
|
||||
{
|
||||
return m_ob->is_open();
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
if (!m_ob->close())
|
||||
{
|
||||
myios::setstate(std::ios_base::failbit);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<myob> m_ob;
|
||||
};
|
||||
|
||||
template <class Elem, class Traits>
|
||||
class basic_iobjstream : public std::basic_istream<Elem, Traits>
|
||||
{
|
||||
public:
|
||||
using mybase = std::basic_istream<Elem, Traits>;
|
||||
using myob = basic_objbuf<Elem, Traits>;
|
||||
using myios = std::basic_ios<Elem, Traits>;
|
||||
|
||||
explicit basic_iobjstream(std::unique_ptr<myob> objbuf)
|
||||
: std::basic_istream<Elem, Traits>(objbuf.get()),
|
||||
m_ob(std::move(objbuf))
|
||||
{
|
||||
assert(m_ob);
|
||||
}
|
||||
|
||||
explicit basic_iobjstream(basic_iobjstream<Elem, Traits>&& right) noexcept
|
||||
: std::basic_istream<Elem, Traits>(right),
|
||||
m_ob(std::move(right.m_ob))
|
||||
{
|
||||
assert(m_ob != nullptr);
|
||||
}
|
||||
|
||||
basic_iobjstream(const basic_iobjstream& other) = delete;
|
||||
basic_iobjstream& operator=(const basic_iobjstream& other) = delete;
|
||||
|
||||
~basic_iobjstream() override = default;
|
||||
|
||||
void swap(basic_iobjstream& right) noexcept
|
||||
{
|
||||
if (this != _STD addressof(right))
|
||||
{
|
||||
mybase::swap(right);
|
||||
m_ob = std::move(right.m_ob);
|
||||
}
|
||||
}
|
||||
|
||||
basic_iobjstream& operator=(basic_iobjstream&& other) noexcept
|
||||
{
|
||||
swap(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ReSharper disable once CppHidingFunction
|
||||
_NODISCARD myob* rdbuf() const
|
||||
{
|
||||
return const_cast<myob*>(m_ob.get());
|
||||
}
|
||||
|
||||
_NODISCARD bool is_open() const
|
||||
{
|
||||
return m_ob->is_open();
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
if (!m_ob->close())
|
||||
{
|
||||
myios::setstate(std::ios_base::failbit);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<myob> m_ob;
|
||||
};
|
||||
|
||||
template <class Elem, class Traits>
|
||||
class basic_oobjstream : public std::basic_ostream<Elem, Traits>
|
||||
{
|
||||
public:
|
||||
using mybase = std::basic_ostream<Elem, Traits>;
|
||||
using myob = basic_objbuf<Elem, Traits>;
|
||||
using myios = std::basic_ios<Elem, Traits>;
|
||||
|
||||
explicit basic_oobjstream(std::unique_ptr<myob> objbuf)
|
||||
: std::basic_ostream<Elem, Traits>(objbuf.get()),
|
||||
m_ob(std::move(objbuf))
|
||||
{
|
||||
assert(m_ob);
|
||||
}
|
||||
|
||||
explicit basic_oobjstream(basic_oobjstream<Elem, Traits>&& right) noexcept
|
||||
: std::basic_ostream<Elem, Traits>(right),
|
||||
m_ob(std::move(right.m_ob))
|
||||
{
|
||||
assert(m_ob != nullptr);
|
||||
}
|
||||
|
||||
~basic_oobjstream() override = default;
|
||||
|
||||
basic_oobjstream(const basic_oobjstream& other) = delete;
|
||||
basic_oobjstream& operator=(const basic_oobjstream& other) = delete;
|
||||
|
||||
void swap(basic_oobjstream& right) noexcept
|
||||
{
|
||||
if (this != _STD addressof(right))
|
||||
{
|
||||
mybase::swap(right);
|
||||
m_ob = std::move(right.m_ob);
|
||||
}
|
||||
}
|
||||
|
||||
basic_oobjstream& operator=(basic_oobjstream&& other) noexcept
|
||||
{
|
||||
swap(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ReSharper disable once CppHidingFunction
|
||||
_NODISCARD myob* rdbuf() const
|
||||
{
|
||||
return const_cast<myob*>(m_ob.get());
|
||||
}
|
||||
|
||||
_NODISCARD bool is_open() const
|
||||
{
|
||||
return m_ob->is_open();
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
if (!m_ob->close())
|
||||
{
|
||||
myios::setstate(std::ios_base::failbit);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<myob> m_ob;
|
||||
};
|
||||
|
||||
using objbuf = basic_objbuf<char, std::char_traits<char>>;
|
||||
using objstream = basic_objstream<char, std::char_traits<char>>;
|
||||
using iobjstream = basic_iobjstream<char, std::char_traits<char>>;
|
||||
using oobjstream = basic_oobjstream<char, std::char_traits<char>>;
|
Reference in New Issue
Block a user