2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: FileStream.Generic.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-06 10:58:08 +00:00
|
|
|
// TODO: 64
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
#include "FS.hpp"
|
|
|
|
#include "FileStream.Generic.hpp"
|
|
|
|
|
|
|
|
#if defined(_AURUNTIME_GENERICFILESTREAM)
|
|
|
|
|
|
|
|
namespace Aurora::IO::FS
|
|
|
|
{
|
|
|
|
static const AuUInt64 kFileCopyBlock = 0x4000; // 16KiB
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class StdFS : public IFileStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Close() override
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (stream_.is_open())
|
|
|
|
{
|
|
|
|
stream_.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPanic("Critical File IO error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Open(const AuString &str)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#if defined(NO_STD_FS)
|
2021-10-02 16:07:33 +00:00
|
|
|
stream_.open(NormalizePathRet(str), std::ios_base::binary)
|
2021-06-27 21:25:29 +00:00
|
|
|
#else
|
2021-10-02 16:07:33 +00:00
|
|
|
stream_.open(std::filesystem::u8path(NormalizePathRet(str)), std::ios_base::binary);
|
2021-06-27 21:25:29 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return stream_.is_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 GetOffset() override
|
|
|
|
{
|
|
|
|
return offset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 GetLength() override
|
|
|
|
{
|
|
|
|
auto startingOffset = GetOffset();
|
|
|
|
GoToEof();
|
|
|
|
auto ret = GetOffset();
|
|
|
|
SetOffset(startingOffset);
|
|
|
|
return startingOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Flush()
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2022-01-17 15:21:36 +00:00
|
|
|
|
|
|
|
void WriteEoS() override
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
virtual void GoToEof() = 0;
|
|
|
|
protected:
|
|
|
|
AuUInt64 offset_ = 0;
|
|
|
|
T stream_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StdReadFS : public StdFS<std::ifstream>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void GoToEof() override
|
|
|
|
{
|
|
|
|
stream_.seekg(0, std::ios_base::end);
|
|
|
|
offset_ = stream_.tellg();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetOffset(AuUInt64 offset) override
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
stream_.seekg(offset, std::ios_base::beg);
|
|
|
|
offset_ = offset;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-15 18:55:22 +00:00
|
|
|
bool Read(const Memory::MemoryViewStreamWrite ¶meters) override
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!stream_.is_open())
|
|
|
|
{
|
2021-09-14 23:56:26 +00:00
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-09-14 23:56:26 +00:00
|
|
|
stream_.read(reinterpret_cast<char *>(parameters.ptr), parameters.length);
|
|
|
|
parameters.outVariable = stream_.gcount();
|
|
|
|
offset_ += parameters.outVariable;
|
|
|
|
return true;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2021-09-14 23:56:26 +00:00
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:37:22 +00:00
|
|
|
bool Write(const AuMemoryViewStreamRead & parameters) override
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-14 23:56:26 +00:00
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class StdWriteFS : public StdFS<std::ofstream>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void GoToEof() override
|
|
|
|
{
|
|
|
|
stream_.seekp(0, std::ios_base::end);
|
|
|
|
offset_ = stream_.tellp();
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:37:22 +00:00
|
|
|
bool Read(const AuMemoryViewStreamWrite ¶meters) override
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-14 23:56:26 +00:00
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 16:37:22 +00:00
|
|
|
bool Write(const AuMemoryViewStreamRead & parameters) override
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!stream_.is_open())
|
|
|
|
{
|
2021-09-14 23:56:26 +00:00
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
auto start = stream_.tellp();
|
2021-09-14 23:56:26 +00:00
|
|
|
stream_.write(reinterpret_cast<const char *>(parameters.ptr), parameters.length);
|
|
|
|
parameters.outVariable = stream_.tellp() - start;
|
|
|
|
offset_ += parameters.outVariable;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2021-09-14 23:56:26 +00:00
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-09-14 23:56:26 +00:00
|
|
|
|
|
|
|
return true;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SetOffset(AuUInt64 offset) override
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
stream_.seekp(offset, std::ios_base::beg);
|
|
|
|
offset_ = offset;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flush() override
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
stream_.flush();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
AuLogWarn("Couldn't flush file stream");
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AUKN_SYM IFileStream *OpenReadNew(const AuString &path)
|
|
|
|
{
|
|
|
|
auto stream = _new StdReadFS();
|
|
|
|
if (!stream)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream->Open(NormalizePathRet(path)))
|
|
|
|
{
|
|
|
|
delete stream;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void OpenReadRelease(IFileStream * that)
|
|
|
|
{
|
[*/+/-] MEGA COMMIT. ~2 weeks compressed.
The intention is to quickly improve and add util apis, enhance functionality given current demands, go back to the build pipeline, finish that, publish runtime tests, and then use what we have to go back to to linux support with a more stable api.
[+] AuMakeSharedArray
[+] Technet ArgvQuote
[+] Grug subsystem (UNIX signal thread async safe ipc + telemetry flusher + log flusher.)
[+] auEndianness -> Endian swap utils
[+] AuGet<N>(...)
[*] AUE_DEFINE conversion for
ECompresionType, EAnsiColor, EHashType, EStreamError, EHexDump
[+] ConsoleMessage ByteBuffer serialization
[+] CmdLine subsystem for parsing command line arguments and simple switch/flag checks
[*] Split logger from console subsystem
[+] StartupParameters -> A part of a clean up effort under Process
[*] Refactor SysErrors header + get caller hack
[+] Atomic APIs
[+] popcnt
[+] Ring Buffer sink
[+] Added more standard errors
Catch,
Submission,
LockError,
NoAccess,
ResourceMissing,
ResourceLocked,
MalformedData,
InSandboxContext,
ParseError
[+] Added ErrorCategorySet, ErrorCategoryClear, GetStackTrace
[+] IExitSubscriber, ETriggerLevel
[*] Write bias the high performance RWLockImpl read-lock operation operation
[+] ExitHandlerAdd/ExitHandlerRemove (exit subsystem)
[*] Updated API style
Digests
[+] CpuId::CpuBitCount
[+] GetUserProgramsFolder
[+] GetPackagePath
[*] Split IStreamReader with an inl file
[*] BlobWriter/BlobReader/BlobArbitraryReader can now take shared pointers to bytebuffers. default constructor allocates a new scalable bytebuffer
[+] ICharacterProvider
[+] ICharacterProviderEx
[+] IBufferedCharacterConsumer
[+] ProviderFromSharedString
[+] ProviderFromString
[+] BufferConsumerFromProvider
[*] Parse Subsystem uses character io bufferer
[*] Rewritten NT's high perf semaphore to use userland SRW/ConVars [like mutex, based on generic semaphore]
[+] ByteBuffer::ResetReadPointer
[*] Bug fix bytebuffer base not reset on free and some scaling issues
[+] ProcessMap -> Added kSectionNameStack, kSectionNameFile, kSectionNameHeap for Section
[*] ProcessMap -> Refactor Segment to Section. I was stupid for keeping a type conflict hack API facing
[+] Added 64 *byte* fast RNG seeds
[+] File Advisorys/File Lock Awareness
[+] Added extended IAuroraThread from OS identifier caches for debug purposes
[*] Tweaked how memory is reported on Windows. Better consistency of what values mean across functions.
[*] Broke AuroraUtils/Typedefs out into a separate library
[*] Update build script
[+] Put some more effort into adding detail to the readme before rewriting it, plus, added some media
[*] Improved public API documentation
[*] Bug fix `SetConsoleCtrlHandler`
[+] Locale TimeDateToFileNameISO8601
[+] Console config stdOutShortTime
[*] Begin using internal UTF8/16 decoders when platform support isnt available (instead of stl)
[*] Bug fixes in decoders
[*] Major bug fix, AuMax
[+] RateLimiter
[+] Binary file sink
[+] Log directory sink
[*] Data header usability (more operators)
[+] AuRemoveRange
[+] AuRemove
[+] AuTryRemove
[+] AuTryRemoveRange
[+] auCastUtils
[+] Finish NewLSWin32Source
[+] AuTryFindByTupleN, AuTryRemoveByTupleN
[+] Separated AuRead/Write types, now in auTypeUtils
[+] Added GetPosition/SetPosition to FileWriter
[*] Fix stupid AuMin in place of AuMax in SpawnThread.Unix.Cpp
[*] Refactored Arbitrary readers to SeekingReaders (as in, they could be atomic and/or parallelized, and accept an arbitrary position as a work parameter -> not Seekable, as in, you can simply set the position)
[*] Hack back in the sched deinit
[+] File AIO loop source interop
[+] Begin to prototype a LoopQueue object I had in mind for NT, untested btw
[+] Stub code for networking
[+] Compression BaseStream/IngestableStreamBase
[*] Major: read/write locks now support write-entrant read routines.
[*] Compression subsystem now uses the MemoryView concept
[*] Rewrite the base stream compressions, made them less broken
[*] Update hashing api
[*] WriterTryGoForward and ReaderTryGoForward now revert to the previous relative index instead of panicing
[+] Added new AuByteBuffer apis
Trim, Pad, WriteFrom, WriteString, [TODO: ReadString]
[+] Added ByteBufferPushReadState
[+] Added ByteBufferPushWriteState
[*] Move from USC-16 to full UTF-16. Win32 can handle full UTF-16.
[*] ELogLevel is now an Aurora enum
[+] Raised arbitrary limit in header to 255, the max filter buffer
[+] Explicit GZip support
[+] Explicit Zip support
[+] Added [some] compressors
et al
2022-02-17 00:11:40 +00:00
|
|
|
AuSafeDelete<StdReadFS *>(that);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM IFileStream *OpenWriteNew(const AuString &path)
|
|
|
|
{
|
|
|
|
auto pathex = NormalizePathRet(path);
|
|
|
|
|
|
|
|
auto stream = _new StdWriteFS();
|
|
|
|
if (!stream)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateDirectories(pathex, true);
|
|
|
|
|
|
|
|
if (!stream->Open(pathex))
|
|
|
|
{
|
|
|
|
delete stream;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void OpenWriteRelease(IFileStream * that)
|
|
|
|
{
|
[*/+/-] MEGA COMMIT. ~2 weeks compressed.
The intention is to quickly improve and add util apis, enhance functionality given current demands, go back to the build pipeline, finish that, publish runtime tests, and then use what we have to go back to to linux support with a more stable api.
[+] AuMakeSharedArray
[+] Technet ArgvQuote
[+] Grug subsystem (UNIX signal thread async safe ipc + telemetry flusher + log flusher.)
[+] auEndianness -> Endian swap utils
[+] AuGet<N>(...)
[*] AUE_DEFINE conversion for
ECompresionType, EAnsiColor, EHashType, EStreamError, EHexDump
[+] ConsoleMessage ByteBuffer serialization
[+] CmdLine subsystem for parsing command line arguments and simple switch/flag checks
[*] Split logger from console subsystem
[+] StartupParameters -> A part of a clean up effort under Process
[*] Refactor SysErrors header + get caller hack
[+] Atomic APIs
[+] popcnt
[+] Ring Buffer sink
[+] Added more standard errors
Catch,
Submission,
LockError,
NoAccess,
ResourceMissing,
ResourceLocked,
MalformedData,
InSandboxContext,
ParseError
[+] Added ErrorCategorySet, ErrorCategoryClear, GetStackTrace
[+] IExitSubscriber, ETriggerLevel
[*] Write bias the high performance RWLockImpl read-lock operation operation
[+] ExitHandlerAdd/ExitHandlerRemove (exit subsystem)
[*] Updated API style
Digests
[+] CpuId::CpuBitCount
[+] GetUserProgramsFolder
[+] GetPackagePath
[*] Split IStreamReader with an inl file
[*] BlobWriter/BlobReader/BlobArbitraryReader can now take shared pointers to bytebuffers. default constructor allocates a new scalable bytebuffer
[+] ICharacterProvider
[+] ICharacterProviderEx
[+] IBufferedCharacterConsumer
[+] ProviderFromSharedString
[+] ProviderFromString
[+] BufferConsumerFromProvider
[*] Parse Subsystem uses character io bufferer
[*] Rewritten NT's high perf semaphore to use userland SRW/ConVars [like mutex, based on generic semaphore]
[+] ByteBuffer::ResetReadPointer
[*] Bug fix bytebuffer base not reset on free and some scaling issues
[+] ProcessMap -> Added kSectionNameStack, kSectionNameFile, kSectionNameHeap for Section
[*] ProcessMap -> Refactor Segment to Section. I was stupid for keeping a type conflict hack API facing
[+] Added 64 *byte* fast RNG seeds
[+] File Advisorys/File Lock Awareness
[+] Added extended IAuroraThread from OS identifier caches for debug purposes
[*] Tweaked how memory is reported on Windows. Better consistency of what values mean across functions.
[*] Broke AuroraUtils/Typedefs out into a separate library
[*] Update build script
[+] Put some more effort into adding detail to the readme before rewriting it, plus, added some media
[*] Improved public API documentation
[*] Bug fix `SetConsoleCtrlHandler`
[+] Locale TimeDateToFileNameISO8601
[+] Console config stdOutShortTime
[*] Begin using internal UTF8/16 decoders when platform support isnt available (instead of stl)
[*] Bug fixes in decoders
[*] Major bug fix, AuMax
[+] RateLimiter
[+] Binary file sink
[+] Log directory sink
[*] Data header usability (more operators)
[+] AuRemoveRange
[+] AuRemove
[+] AuTryRemove
[+] AuTryRemoveRange
[+] auCastUtils
[+] Finish NewLSWin32Source
[+] AuTryFindByTupleN, AuTryRemoveByTupleN
[+] Separated AuRead/Write types, now in auTypeUtils
[+] Added GetPosition/SetPosition to FileWriter
[*] Fix stupid AuMin in place of AuMax in SpawnThread.Unix.Cpp
[*] Refactored Arbitrary readers to SeekingReaders (as in, they could be atomic and/or parallelized, and accept an arbitrary position as a work parameter -> not Seekable, as in, you can simply set the position)
[*] Hack back in the sched deinit
[+] File AIO loop source interop
[+] Begin to prototype a LoopQueue object I had in mind for NT, untested btw
[+] Stub code for networking
[+] Compression BaseStream/IngestableStreamBase
[*] Major: read/write locks now support write-entrant read routines.
[*] Compression subsystem now uses the MemoryView concept
[*] Rewrite the base stream compressions, made them less broken
[*] Update hashing api
[*] WriterTryGoForward and ReaderTryGoForward now revert to the previous relative index instead of panicing
[+] Added new AuByteBuffer apis
Trim, Pad, WriteFrom, WriteString, [TODO: ReadString]
[+] Added ByteBufferPushReadState
[+] Added ByteBufferPushWriteState
[*] Move from USC-16 to full UTF-16. Win32 can handle full UTF-16.
[*] ELogLevel is now an Aurora enum
[+] Raised arbitrary limit in header to 255, the max filter buffer
[+] Explicit GZip support
[+] Explicit Zip support
[+] Added [some] compressors
et al
2022-02-17 00:11:40 +00:00
|
|
|
AuSafeDelete<StdWriteFS *>(that);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-02 16:07:33 +00:00
|
|
|
#endif
|