C++ Utilities  4.15.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 #if defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER)
7 #if defined(PLATFORM_MINGW) || defined(PLATFORM_LINUX)
8 #include <ext/stdio_filebuf.h>
9 #include <iostream>
10 #include <memory>
11 #include <string>
12 #else
13 #error "Platform not supported by NativeFileStream."
14 #endif
15 
16 #else
17 #include <fstream>
18 #endif
19 
20 namespace IoUtilities {
21 
22 #if defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER) && (defined(PLATFORM_MINGW) || defined(PLATFORM_UNIX))
23 
24 class CPP_UTILITIES_EXPORT NativeFileStream : public std::iostream {
25 public:
29 
30  bool is_open() const;
31  void open(const std::string &path, std::ios_base::openmode openMode);
32  void openFromFileDescriptor(int fileDescriptor, std::ios_base::openmode openMode);
33  void close();
34  std::__c_file fileHandle();
35 
36 private:
37  std::unique_ptr<__gnu_cxx::stdio_filebuf<char>> m_filebuf;
38  std::__c_file m_cfile;
39 };
40 
41 inline bool NativeFileStream::is_open() const
42 {
43  return m_filebuf && m_filebuf->is_open();
44 }
45 
46 inline std::__c_file NativeFileStream::fileHandle()
47 {
48  return m_cfile;
49 }
50 
51 #else
52 
53 using NativeFileStream = std::fstream;
54 
55 #endif
56 
57 } // namespace IoUtilities
58 
59 #endif // IOUTILITIES_NATIVE_FILE_STREAM
std::fstream NativeFileStream
Contains utility classes helping to read and write streams.
Definition: binaryreader.h:10
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.