#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_PIXELS_STORAGE_REALFILE_H #define NUCLEX_PIXELS_STORAGE_REALFILE_H #include "Nuclex/Pixels/Config.h" #include "Nuclex/Pixels/Storage/VirtualFile.h" #if defined(NUCLEX_PIXELS_LINUX) // File handles are ints, we don't need no headers! #elif defined(NUCLEX_PIXELS_WINDOWS) #include "../Platform/WindowsApi.h" // for HANDLE, etc. #else // No Windows, no Linux -> use Posix! #include // for FILE, ::fopen(), etc. #endif namespace Nuclex { namespace Pixels { namespace Storage { // ------------------------------------------------------------------------------------------- // /// Reads and saves data to a normal file in the OS' file system class RealFile : public VirtualFile { /// Initializes a new instance accessing the file at the specified path /// Path of the file that will be accessed or created /// /// Whether you promise to write to the file sequentially only /// /// Whether write accesses to the file will be denied public: RealFile( const std::string &path, bool promiseSequentialAccess, bool readOnly ); /// Closes the file and frees all memory used by the instance public: virtual ~RealFile(); /// Determines the current size of the file in bytes /// The size of the file in bytes public: std::uint64_t GetSize() const override { return this->length; } /// Reads data from the file at an absolute position /// Offset in the file at which to begin reading /// Number of bytes that will be read /// Buffer into which the data will be read public: void ReadAt( std::uint64_t, std::size_t byteCount, std::uint8_t *buffer ) const override; /// Writes data into the file at an absolute position /// Offset at which writing will begin in the file /// Number of bytes that should be written /// Buffer holding the data that should be written public: void WriteAt( std::uint64_t start, std::size_t byteCount, const std::uint8_t *buffer ) override; #if defined(NUCLEX_PIXELS_LINUX) /// File descriptor returned by ::open() private: int fileDescriptor; #elif defined(NUCLEX_PIXELS_WINDOWS) /// File handle returned by ::CreateFile() or ::OpenFile() private: ::HANDLE fileHandle; #else // Go with old-school Posix and hope for the best /// File pointer returned by ::fopen() private: ::FILE *file; #endif /// Length of the file in bytes private: std::uint64_t length; /// Current position within the file private: mutable std::uint64_t position; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Pixels::Storage #endif // NUCLEX_PIXELS_STORAGE_REALFILE_H