C++ Utilities 5.26.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
nativefilestream.h
Go to the documentation of this file.
1#ifndef IOUTILITIES_NATIVE_FILE_STREAM
2#define IOUTILITIES_NATIVE_FILE_STREAM
3
4#include "../global.h"
5
6#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
7#include <iostream>
8#include <memory>
9#include <streambuf>
10#include <string>
11#include <string_view>
12#endif
13#include <fstream>
14
15namespace CppUtilities {
16
17#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
18
19class CPP_UTILITIES_EXPORT NativeFileStream : public std::iostream {
20public:
21#ifdef PLATFORM_WINDOWS
22 using Handle = void *;
23#endif
24 struct CPP_UTILITIES_EXPORT FileBuffer {
25 FileBuffer(std::basic_streambuf<char> *buffer);
26 FileBuffer(const char *path, ios_base::openmode openMode);
27 FileBuffer(const std::string &path, ios_base::openmode openMode);
28 FileBuffer(int fileDescriptor, ios_base::openmode openMode);
29
30 std::unique_ptr<std::basic_streambuf<char>> buffer;
31#ifdef PLATFORM_WINDOWS
32 Handle handle = nullptr;
33#endif
34 int descriptor = -1;
35 };
36
38 NativeFileStream(const char *path, std::ios_base::openmode openMode);
39 NativeFileStream(const std::string &path, std::ios_base::openmode openMode);
40 NativeFileStream(int fileDescriptor, std::ios_base::openmode openMode);
41 NativeFileStream(NativeFileStream &&);
42 ~NativeFileStream() override;
43
44 bool is_open() const;
45 bool isOpen() const;
46 void open(const char *path, std::ios_base::openmode openMode);
47 void open(const std::string &path, std::ios_base::openmode openMode);
48 void open(int fileDescriptor, std::ios_base::openmode openMode);
49 void close();
50 int fileDescriptor();
51#ifdef PLATFORM_WINDOWS
52 Handle fileHandle();
53 static std::unique_ptr<wchar_t[]> makeWidePath(std::string_view path);
54 static std::unique_ptr<wchar_t[]> makeWidePath(const char *path);
55 static std::unique_ptr<wchar_t[]> makeWidePath(const std::string &path);
56#endif
57
58private:
59 void setData(FileBuffer data, std::ios_base::openmode openMode);
60
61 FileBuffer m_data;
62 std::ios_base::openmode m_openMode;
63};
64
68inline NativeFileStream::NativeFileStream(const char *path, ios_base::openmode openMode)
70{
71 open(path, openMode);
72}
73
77inline NativeFileStream::NativeFileStream(const std::string &path, ios_base::openmode openMode)
78 : NativeFileStream(path.data(), openMode)
79{
80}
81
85inline NativeFileStream::NativeFileStream(int fileDescriptor, ios_base::openmode openMode)
87{
88 open(fileDescriptor, openMode);
89}
90
95inline int NativeFileStream::fileDescriptor()
96{
97 return m_data.descriptor;
98}
99
100#ifdef PLATFORM_WINDOWS
105inline NativeFileStream::Handle NativeFileStream::fileHandle()
106{
107 return m_data.handle;
108}
109
114inline std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(const char *path)
115{
116 return makeWidePath(std::string_view(path));
117}
118#endif
119
124inline bool NativeFileStream::is_open() const
125{
126 return isOpen();
127}
128
129#else // CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
130
131using NativeFileStream = std::fstream;
132
133#endif
134
135} // namespace CppUtilities
136
137#endif // IOUTILITIES_NATIVE_FILE_STREAM
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
Contains all utilities provides by the c++utilities library.
std::fstream NativeFileStream