Leonardus
Loading...
Searching...
No Matches
sof.h
Go to the documentation of this file.
1
17#pragma once
18
19// Inc Library
20#include <string>
21#include <string_view>
22#include <cstring>
23#include <memory>
24
25// Inc Pure
26#include "dbc.h"
27#include "helper.h"
28
29// Inc Rich
30#include "so.h"
31
32
36class SOF : public SOshptr {
37
41 struct SOFData {
42
43 FILE * file_{};
44 std::string name_{};
45 std::string mode_{};
49 if( file_ )
50 (void) fclose( file_ );
51 }
52
53 };
54
55
57 std::shared_ptr<SOFData> f_;
58
59protected:
60
61 void * shptr() const override { return static_cast<void *>( f_.get() ); }
62
63public:
64
69 explicit SOF( const std::string & p_name, const std::string & p_mode )
70 : SOshptr(),
71 f_(std::make_shared<SOFData>()) {
72 f_->file_ = nullptr;
73 f_->name_ = p_name;
74 f_->mode_ = p_mode;
75
77 }
78
79
81 SOF( const SOF & p_other )
82 : SOshptr(p_other),
83 f_(p_other.f_) {
85 }
86
87
89 ~SOF() override {
91 }
92
93
94 SOF() = delete;
95 SOF( SOF && ) = delete;
96 SOF & operator=( const SOF & ) = delete;
97 SOF & operator=( SOF && ) = delete;
98
99public: /* accessor */
100
102 std::string getFName() const { return f_->name_; }
103
104
106 std::string getFMode() const { return f_->mode_; }
107
108public: /* virtual */
109
110 [[nodiscard]] SOF * dup() const override { return new SOF( *this ); }
111
112 // TODO: clone() ?
113
114 std::string opequalequal() const override { return "-file-"; }
115
116
117 OTCode ot() const override { return OTCode::F; }
118
119
120 std::string_view type() const override { return "filetype"; }
121
122public: /* other */
123
128 bool isOpen() const { return f_->file_ != nullptr; }
129
130
136 bool readLeonLine( std::string & p_str );
137
138
143 OpError open();
144
145
150 OpError flush();
151
152
158 OpError close( int (*p_fclose)(FILE *) = fclose );
159
160
167 OpError readline( std::string & p_str, bool & p_eof );
168
169
176 OpError readstring( std::string & p_str, bool & p_eof );
177
178
183 OpError writestring( const std::string & p_data );
184
185
190 OpError writestring( std::string_view p_data );
191
192
198 OpError writestring( const char * p_data );
199
200};
Semantic Object File.
Definition sof.h:36
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sof.h:114
std::shared_ptr< SOFData > f_
The data.
Definition sof.h:57
std::string getFMode() const
Returns the file mode.
Definition sof.h:106
OpError writestring(const std::string &p_data)
Writes to file.
Definition sof.cpp:133
std::string_view type() const override
Returns a type name.
Definition sof.h:120
bool isOpen() const
Checks if the file is open.
Definition sof.h:128
OpError readline(std::string &p_str, bool &p_eof)
Reads a line of text from file.
Definition sof.cpp:94
std::string getFName() const
Returns the file name.
Definition sof.h:102
OpError flush()
Flushes the file.
Definition sof.cpp:82
void * shptr() const override
Pointer to the shared object.
Definition sof.h:61
SOF(const std::string &p_name, const std::string &p_mode)
Ctor from a name.
Definition sof.h:69
OpError open()
Opens the file.
Definition sof.cpp:35
SOF * dup() const override
Creates a new instance as copy following the red book definition.
Definition sof.h:110
OpError readstring(std::string &p_str, bool &p_eof)
Reads text from file.
Definition sof.cpp:114
OTCode ot() const override
Returns an OTCode.
Definition sof.h:117
OpError close(int(*p_fclose)(FILE *)=fclose)
Closes the file.
Definition sof.cpp:69
bool readLeonLine(std::string &p_str)
Our std::getline( istream, string ) version.
Definition sof.cpp:168
SOF(const SOF &p_other)
Copy ctor.
Definition sof.h:81
~SOF() override
Dtor.
Definition sof.h:89
Semantic Object with shared pointer to data.
Definition so.h:210
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:122
OpError
Operator error code enum.
Definition error.h:30
Miscellaneous definitions and functions.
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:38
@ F
SOF.
Semantic Object File - data member class.
Definition sof.h:41
FILE * file_
The C FILE.
Definition sof.h:43
std::string name_
The file name.
Definition sof.h:44
std::string mode_
The file open mode.
Definition sof.h:45
~SOFData()
Dtor.
Definition sof.h:48