PLCnext API Documentation  22.9.0.33
FileStream.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Commons/Io/Stream.hpp"
9 #include "Arp/System/Commons/Io/FileMode.hpp"
10 #include "Arp/System/Commons/Io/FileAccess.hpp"
11 
12 // forwards
13 namespace Arp { namespace System { namespace Ve
14 {
15 class IFileService;
16 }}}
17 
18 namespace Arp { namespace System { namespace Commons { namespace Io
19 {
20 
24 class FileStream : public Stream
25 {
26 public: // typedefs/usings
27  using IFileService = Arp::System::Ve::IFileService;
28 
29 public: // construction/destruction
30 
41  FileStream(const String& path, FileMode mode);
42 
53  FileStream(const String& path, FileMode mode, FileAccess access);
54 
66  FileStream(const String& path, FileMode mode, FileAccess access, bool sync);
67 
69  FileStream(const FileStream& arg) = delete;
70 
72  FileStream& operator=(const FileStream& arg) = delete;
73 
75  ~FileStream(void)override;
76 
77 public: // setter/getter operations
80  boolean CanRead(void) override;
81 
84  boolean CanWrite(void) override;
85 
88  boolean CanSeek(void) override;
89 
93  size_t GetLength(void) override;
94 
99  void SetLength(size_t length) override;
100 
105  size_t GetPosition(void) override;
106 
111  void SetPosition(size_t position) override;
112 
113 
114 public: // operations
117  void Flush(void) override;
118 
124  size_t Seek(size_t offset, SeekOrigin origin) override;
125 
139  size_t Read(byte* pBuffer, size_t bufferSize, size_t offset, size_t count) override;
140 
154  void Write(const byte* pBuffer, size_t bufferSize, size_t offset, size_t count) override;
155 
156 protected: // operations
157  void InternalDispose(void) override;
158 
159 private: // methods
160  void InternalConstructor(const String& path, FileMode mode, FileAccess access);
161  void InternalConstructor(const String& path, FileMode mode, FileAccess access, bool sync);
162 
163 private: // fields
164  // flags storing internal status bits
165  boolean canRead = false;
166  boolean canWrite = false;
167  boolean canSeek = false;
168  // containing path and filename including extension
169  String pathName;
170  // native FileStream object providing system dependent part of implementation
171  IFileService* pFileService = nullptr;
172 };
173 
175 // inline methods of class FileStream
176 
177 inline FileStream::FileStream(const String& path, FileMode mode)
178 {
179  this->InternalConstructor(path, mode, FileAccess::ReadWrite);
180 }
181 
182 inline FileStream::FileStream(const String& path, FileMode mode, FileAccess access)
183 {
184  this->InternalConstructor(path, mode, access);
185 }
186 
187 inline FileStream::FileStream(const String& path, FileMode mode, FileAccess access, bool sync)
188 {
189  this->InternalConstructor(path, mode, access, sync);
190 }
191 
192 }}}} // end of namespace Arp::System::Commons::Io
Exposes a Stream around a file, supporting read and write operations.
Definition: FileStream.hpp:25
FileStream(const String &path, FileMode mode)
Initializes a new instance of the FileStream class.
Definition: FileStream.hpp:177
boolean CanWrite(void) override
Checks if the stream is opened for write-access.
boolean CanRead(void) override
Checks if the stream is opened for read-access.
void SetLength(size_t length) override
Reserves a specific amount of bytes for the opened file.
FileStream(const FileStream &arg)=delete
Copy constructor.
boolean CanSeek(void) override
Checks if seek operations are possible.
size_t Seek(size_t offset, SeekOrigin origin) override
Sets a new position if the internal stream pointer relative to a specific position.
~FileStream(void) override
Destructs this instance and frees all resources.
size_t Read(byte *pBuffer, size_t bufferSize, size_t offset, size_t count) override
Reads data from stream into assigned buffer at a specific offset.
void Write(const byte *pBuffer, size_t bufferSize, size_t offset, size_t count) override
Writes data from assigned buffer to stream.
size_t GetPosition(void) override
Returns the byte offset of the internal stream pointer from the beginning of the file.
void SetPosition(size_t position) override
Sets a new position of the internal stream pointer.
void Flush(void) override
Forces a write of all possibly buffered data (input stream) or discards any buffered not yet read by ...
FileStream & operator=(const FileStream &arg)=delete
Assignment operator.
size_t GetLength(void) override
Returns the size of the opened file in bytes.
Provides a generic view of a sequence of bytes.
Definition: Stream.hpp:20
@ System
System components used by the System, Device, Plc or Io domains.
SeekOrigin
Provides seek reference points. To seek to the end of a stream, call stream.Seek(0,...
Definition: SeekOrigin.hpp:20
FileMode
Contains constants for specifying how the OS should open a file.
Definition: FileMode.hpp:24
FileAccess
Contains constants for specifying the access you want for a file. You can have Read,...
Definition: FileAccess.hpp:18
@ ReadWrite
Specifies read and write access to the file. Data can be written to the file and the file pointer can...
Root namespace for the PLCnext API