libdballe 9.6
file.h
1#ifndef DBALLE_FILE_H
2#define DBALLE_FILE_H
3
4#include <dballe/fwd.h>
5#include <memory>
6#include <string>
7#include <functional>
8#include <iosfwd>
9
10namespace dballe {
11
17class File
18{
19public:
20 virtual ~File();
21
23 virtual std::string pathname() const = 0;
24
26 virtual Encoding encoding() const = 0;
27
31 virtual void close() = 0;
32
40 virtual BinaryMessage read() = 0;
41
52 virtual bool foreach(std::function<bool(const BinaryMessage&)> dest) = 0;
53
55 virtual void write(const std::string& msg) = 0;
56
67 static std::unique_ptr<File> create(const std::string& pathname, const char* mode);
68
81 static std::unique_ptr<File> create(Encoding type, const std::string& pathname, const char* mode);
82
98 static std::unique_ptr<File> create(FILE* file, bool close_on_exit, const std::string& name="(fp)");
99
116 static std::unique_ptr<File> create(Encoding type, FILE* file, bool close_on_exit, const std::string& name="(fp)");
117
119 static const char* encoding_name(Encoding enc);
120
122 static Encoding parse_encoding(const char* s);
123
125 static Encoding parse_encoding(const std::string& s);
126
127};
128
131{
132public:
134 Encoding encoding;
135
137 std::string data;
138
144 std::string pathname;
145
147 off_t offset = (off_t)-1;
148
150 int index = MISSING_INT;
151
152 BinaryMessage(Encoding encoding)
153 : encoding(encoding) {}
154 BinaryMessage(const BinaryMessage&) = default;
155 BinaryMessage(BinaryMessage&&) = default;
156 BinaryMessage& operator=(const BinaryMessage&) = default;
157 BinaryMessage& operator=(BinaryMessage&&) = default;
158
160 operator bool() const;
161};
162
163
165std::ostream& operator<<(std::ostream&, const dballe::Encoding&);
166
167}
168
169#endif
Binary message.
Definition: file.h:131
Encoding encoding
Format of the binary data.
Definition: file.h:134
std::string data
Binary message data.
Definition: file.h:137
int index
Index of the message from the beginning of the file.
Definition: file.h:150
off_t offset
Start offset of this message inside the file.
Definition: file.h:147
std::string pathname
Pathname of the file from where the BinaryMessage has been read.
Definition: file.h:144
File object for doing I/O on binary message streams.
Definition: file.h:18
virtual Encoding encoding() const =0
Get the file encoding.
static Encoding parse_encoding(const std::string &s)
Return the Encoding corresponding to the given name.
virtual void close()=0
Close the underlying file.
virtual BinaryMessage read()=0
Read a message from the file.
static std::unique_ptr< File > create(Encoding type, const std::string &pathname, const char *mode)
Open a file from the filesystem.
static const char * encoding_name(Encoding enc)
Return a string with the name of this encoding.
static Encoding parse_encoding(const char *s)
Return the Encoding corresponding to the given name.
virtual void write(const std::string &msg)=0
Append the binary message to the file.
static std::unique_ptr< File > create(const std::string &pathname, const char *mode)
Open a file from the filesystem, autodetecting the encoding type.
static std::unique_ptr< File > create(Encoding type, FILE *file, bool close_on_exit, const std::string &name="(fp)")
Create a File from an existing FILE* stream.
virtual std::string pathname() const =0
Get the file pathname.
static std::unique_ptr< File > create(FILE *file, bool close_on_exit, const std::string &name="(fp)")
Create a File from an existing FILE* stream, autodetecting the encoding type.