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 HAA
26#include "dbc.h"
27#include "helper.h"
28
29// Inc Rich
30#include "so.h"
31
32
35class SOF : public SO {
36
40 struct SOFData {
41 FILE * file_{};
42 std::string name_{};
43 std::string mode_{};
47 if( file_ )
48 (void) fclose( file_ );
49 }
50 };
51
53 std::shared_ptr<SOFData> f_;
54
55
56protected:
57#ifndef DBC_IS_VOID
62 bool invariant() const noexcept override { /* LCOV_EXCL_START */
63 return f_ != nullptr;
64 } /* LCOV_EXCL_STOP */
65#endif
66
67
68public:
73 explicit SOF( const std::string & p_name, const std::string & p_mode )
74 : SO(),
75 f_(std::make_shared<SOFData>()) {
76 f_->file_ = nullptr;
77 f_->name_ = p_name;
78 f_->mode_ = p_mode;
79
81 }
82
83
85 SOF( const SOF & p_other ) : SO(p_other), f_(p_other.f_) {
87 }
88
89
91 ~SOF() override {
93 }
94
95
96 SOF() = delete;
97 SOF( SOF && ) = delete;
98 SOF & operator=( const SOF & ) = delete;
99 SOF & operator=( SOF && ) = delete;
100
101
102public: /* accessor */
104 std::string getFName() const { return f_->name_; }
105
106
108 std::string getFMode() const { return f_->mode_; }
109
110
111public: /* virtual */
112 [[nodiscard]] SOF * dup() const override { return new SOF( *this ); }
113
114
115 std::string opequal() const override { return "--nostringval--"; }
116
117
118 std::string opequalequal() const override { return "-file-"; }
119
120
121 OTCode ot() const override { return OTCode::F; }
122
123
124 std::string type() const override { return "filetype"; }
125
126
129 bool equal( const SO * p_other ) const override {
130 auto o = dynamic_cast<const SOF *>( p_other );
131 return o ? f_ == o->f_ : false;
132 }
133
134
135public: /* other */
140 bool isOpen() const { return f_->file_ != nullptr; }
141
142
148 bool readLeonLine( std::string & p_str );
149
150
155 OpError open();
156
157
162 OpError flush();
163
164
170 OpError close( int (*p_fclose)(FILE *) = fclose );
171
172
179 OpError readline( std::string & p_str, bool & p_eof );
180
181
188 OpError readstring( std::string & p_str, bool & p_eof );
189
190
195 OpError writestring( const std::string & p_data );
196
197
202 OpError writestring( std::string_view p_data );
203
204};
Semantic Object File.
Definition sof.h:35
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sof.h:118
std::shared_ptr< SOFData > f_
The data.
Definition sof.h:53
std::string getFMode() const
Returns the file mode.
Definition sof.h:108
OpError writestring(const std::string &p_data)
Writes to file.
Definition sof.cpp:130
std::string type() const override
Returns a type name.
Definition sof.h:124
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sof.h:129
bool isOpen() const
Checks if the file is open.
Definition sof.h:140
OpError readline(std::string &p_str, bool &p_eof)
Reads a line of text from file.
Definition sof.cpp:91
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sof.h:115
std::string getFName() const
Returns the file name.
Definition sof.h:104
OpError flush()
Flushes the file.
Definition sof.cpp:79
SOF(const std::string &p_name, const std::string &p_mode)
Ctor from a name.
Definition sof.h:73
OpError open()
Opens the file.
Definition sof.cpp:31
SOF * dup() const override
Creates a new instance as copy following the red book definition.
Definition sof.h:112
bool invariant() const noexcept override
Checks class invariants.
Definition sof.h:62
OpError readstring(std::string &p_str, bool &p_eof)
Reads text from file.
Definition sof.cpp:111
OTCode ot() const override
Returns an OTCode.
Definition sof.h:121
OpError close(int(*p_fclose)(FILE *)=fclose)
Closes the file.
Definition sof.cpp:66
bool readLeonLine(std::string &p_str)
Our std::getline( istream, string ) version.
Definition sof.cpp:153
SOF(const SOF &p_other)
Copy ctor.
Definition sof.h:85
~SOF() override
Dtor.
Definition sof.h:91
Semantic Object.
Definition so.h:60
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:121
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:35
@ F
SOF.
@ o
SOo.
Semantic Object File - data member class.
Definition sof.h:40
FILE * file_
The C FILE.
Definition sof.h:41
std::string name_
The file name.
Definition sof.h:42
std::string mode_
The file open mode.
Definition sof.h:43
~SOFData()
Dtor.
Definition sof.h:46