libdballe  8.3
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 
10 namespace dballe {
11 
17 struct File
18 {
19  virtual ~File();
20 
22  virtual std::string pathname() const = 0;
23 
25  virtual Encoding encoding() const = 0;
26 
30  virtual void close() = 0;
31 
39  virtual BinaryMessage read() = 0;
40 
51  virtual bool foreach(std::function<bool(const BinaryMessage&)> dest) = 0;
52 
54  virtual void write(const std::string& msg) = 0;
55 
66  static std::unique_ptr<File> create(const std::string& pathname, const char* mode);
67 
80  static std::unique_ptr<File> create(Encoding type, const std::string& pathname, const char* mode);
81 
97  static std::unique_ptr<File> create(FILE* file, bool close_on_exit, const std::string& name="(fp)");
98 
115  static std::unique_ptr<File> create(Encoding type, FILE* file, bool close_on_exit, const std::string& name="(fp)");
116 
118  static const char* encoding_name(Encoding enc);
119 
121  static Encoding parse_encoding(const char* s);
122 
124  static Encoding parse_encoding(const std::string& s);
125 
126 };
127 
130 {
132  Encoding encoding;
133 
135  std::string data;
136 
142  std::string pathname;
143 
145  off_t offset = (off_t)-1;
146 
148  int index = MISSING_INT;
149 
150  BinaryMessage(Encoding encoding)
151  : encoding(encoding) {}
152  BinaryMessage(const BinaryMessage&) = default;
153  BinaryMessage(BinaryMessage&&) = default;
154  BinaryMessage& operator=(const BinaryMessage&) = default;
155  BinaryMessage& operator=(BinaryMessage&&) = default;
156 
158  operator bool() const;
159 };
160 
161 
163 std::ostream& operator<<(std::ostream&, const dballe::Encoding&);
164 
165 }
166 
167 #endif
virtual BinaryMessage read()=0
Read a message from the file.
virtual void close()=0
Close the underlying file.
Definition: cmdline.h:18
virtual Encoding encoding() const =0
Get the file encoding.
virtual void write(const std::string &msg)=0
Append the binary message to the file.
static Encoding parse_encoding(const char *s)
Return the Encoding corresponding to the given name.
static std::unique_ptr< File > create(const std::string &pathname, const char *mode)
Open a file from the filesystem, autodetecting the encoding type.
std::string data
Binary message data.
Definition: file.h:135
Binary message.
Definition: file.h:129
static const char * encoding_name(Encoding enc)
Return a string with the name of this encoding.
Encoding encoding
Format of the binary data.
Definition: file.h:132
std::string pathname
Pathname of the file from where the BinaryMessage has been read.
Definition: file.h:142
virtual std::string pathname() const =0
Get the file pathname.
File object for doing I/O on binary message streams.
Definition: file.h:17