C++ Utilities: io/nativefilestream.h Source File

C++ Utilities 5.34.2 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#ifdef PLATFORM_WINDOWS
12#include <string_view>
13#endif
14#else
15#include <fstream>
16#endif
17
18namespace CppUtilities {
19
20#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
21
22class CPP_UTILITIES_EXPORT NativeFileStream : public std::iostream {
23public:
24#ifdef PLATFORM_WINDOWS
25 using Handle = void *;
26#endif
27 struct CPP_UTILITIES_EXPORT FileBuffer {
28 FileBuffer(std::basic_streambuf<char> *buffer);
29 FileBuffer(const char *path, ios_base::openmode openMode);
30 FileBuffer(const std::string &path, ios_base::openmode openMode);
31 FileBuffer(int fileDescriptor, ios_base::openmode openMode);
32
33 std::unique_ptr<std::basic_streambuf<char>> buffer;
34#ifdef PLATFORM_WINDOWS
35 Handle handle = nullptr;
36#endif
37 int descriptor = -1;
38 };
39
40 NativeFileStream();
41 NativeFileStream(const char *path, std::ios_base::openmode openMode);
42 NativeFileStream(const std::string &path, std::ios_base::openmode openMode);
43 NativeFileStream(int fileDescriptor, std::ios_base::openmode openMode);
44 NativeFileStream(NativeFileStream &&);
45 ~NativeFileStream() override;
46
47 bool is_open() const;
48 bool isOpen() const;
49 void open(const char *path, std::ios_base::openmode openMode);
50 void open(const std::string &path, std::ios_base::openmode openMode);
51 void open(int fileDescriptor, std::ios_base::openmode openMode);
52 void close();
53 int fileDescriptor();
54#ifdef PLATFORM_WINDOWS
55 Handle fileHandle();
56 static std::unique_ptr<wchar_t[]> makeWidePath(std::string_view path);
57 static std::unique_ptr<wchar_t[]> makeWidePath(const char *path);
58 static std::unique_ptr<wchar_t[]> makeWidePath(const std::string &path);
59#endif
60
61private:
62 void setData(FileBuffer data, std::ios_base::openmode openMode);
63
64 FileBuffer m_data;
65 std::ios_base::openmode m_openMode;
66};
67
71inline NativeFileStream::NativeFileStream(const char *path, ios_base::openmode openMode)
72 : NativeFileStream()
73{
74 open(path, openMode);
75}
76
80inline NativeFileStream::NativeFileStream(const std::string &path, ios_base::openmode openMode)
81 : NativeFileStream(path.data(), openMode)
82{
83}
84
88inline NativeFileStream::NativeFileStream(int fileDescriptor, ios_base::openmode openMode)
89 : NativeFileStream()
90{
91 open(fileDescriptor, openMode);
92}
93
98inline int NativeFileStream::fileDescriptor()
99{
100 return m_data.descriptor;
101}
102
103#ifdef PLATFORM_WINDOWS
108inline NativeFileStream::Handle NativeFileStream::fileHandle()
109{
110 return m_data.handle;
111}
112
117inline std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(const char *path)
118{
119 return makeWidePath(std::string_view(path));
120}
121#endif
122
127inline bool NativeFileStream::is_open() const
128{
129 return isOpen();
130}
131
132#else // CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
133
134 using NativeFileStream = std::fstream;
135
136#endif
137
138} // namespace CppUtilities
139
140#endif // IOUTILITIES_NATIVE_FILE_STREAM
global.h
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::NativeFileStream
std::fstream NativeFileStream
Definition nativefilestream.h:134