C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
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 #else
12 #endif
13 #include <fstream>
14 
15 namespace IoUtilities {
16 
17 #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
18 
19 class CPP_UTILITIES_EXPORT NativeFileStream : public std::iostream {
20 public:
22  NativeFileStream(const std::string &path, std::ios_base::openmode openMode);
23  NativeFileStream(int fileDescriptor, std::ios_base::openmode openMode);
26 
27  bool is_open() const;
28  void open(const std::string &path, std::ios_base::openmode openMode);
29  void openFromFileDescriptor(int fileDescriptor, std::ios_base::openmode openMode);
30  void close();
31 #if !defined(__ANDROID_API__) || !defined(__ANDROID_API_N__) || (__ANDROID_API__ < __ANDROID_API_N__)
32  FILE fileHandle();
33 #endif
34 
35  static std::unique_ptr<std::basic_streambuf<char>> makeFileBuffer(const std::string &path, ios_base::openmode openMode);
36  static std::unique_ptr<std::basic_streambuf<char>> makeFileBuffer(int fileDescriptor, ios_base::openmode openMode);
37 #ifdef PLATFORM_WINDOWS
38  static std::unique_ptr<wchar_t[]> makeWidePath(const std::string &path);
39 #endif
40 
41 private:
42  void setFileBuffer(std::unique_ptr<std::basic_streambuf<char>> buffer);
43 
44  std::unique_ptr<std::basic_streambuf<char>> m_filebuf;
45 #if !defined(__ANDROID_API__) || !defined(__ANDROID_API_N__) || (__ANDROID_API__ < __ANDROID_API_N__)
46  FILE m_fileHandle;
47 #endif
48 };
49 
50 inline NativeFileStream::NativeFileStream(const std::string &path, ios_base::openmode openMode)
52 {
53  open(path, openMode);
54 }
55 
56 inline NativeFileStream::NativeFileStream(int fileDescriptor, ios_base::openmode openMode)
58 {
59  openFromFileDescriptor(fileDescriptor, openMode);
60 }
61 
62 #if !defined(__ANDROID_API__) || !defined(__ANDROID_API_N__) || (__ANDROID_API__ < __ANDROID_API_N__)
63 
68 inline FILE NativeFileStream::fileHandle()
69 {
70  return m_fileHandle;
71 }
72 #endif
73 
74 #else // CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
75 
76 using NativeFileStream = std::fstream;
77 
78 #endif
79 
80 } // namespace IoUtilities
81 
82 #endif // IOUTILITIES_NATIVE_FILE_STREAM
#define CPP_UTILITIES_EXPORT
std::fstream NativeFileStream
Contains utility classes helping to read and write streams.
Definition: binaryreader.h:10