libdballe  9.14
file.h
1 #ifndef DBALLE_FILE_H
2 #define DBALLE_FILE_H
3 
4 #include <dballe/fwd.h>
5 #include <functional>
6 #include <iosfwd>
7 #include <memory>
8 #include <string>
9 
10 namespace dballe {
11 
17 class File
18 {
19 public:
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,
68  const char* mode);
69 
82  static std::unique_ptr<File>
83  create(Encoding type, const std::string& pathname, const char* mode);
84 
100  static std::unique_ptr<File> create(FILE* file, bool close_on_exit,
101  const std::string& name = "(fp)");
102 
119  static std::unique_ptr<File> create(Encoding type, FILE* file,
120  bool close_on_exit,
121  const std::string& name = "(fp)");
122 
124  static const char* encoding_name(Encoding enc);
125 
127  static Encoding parse_encoding(const char* s);
128 
130  static Encoding parse_encoding(const std::string& s);
131 };
132 
135 {
136 public:
138  Encoding encoding;
139 
141  std::string data;
142 
148  std::string pathname;
149 
151  off_t offset = (off_t)-1;
152 
154  int index = MISSING_INT;
155 
156  BinaryMessage(Encoding encoding) : encoding(encoding) {}
157  BinaryMessage(const BinaryMessage&) = default;
158  BinaryMessage(BinaryMessage&&) = default;
159  BinaryMessage& operator=(const BinaryMessage&) = default;
160  BinaryMessage& operator=(BinaryMessage&&) = default;
161 
163  operator bool() const;
164 };
165 
167 std::ostream& operator<<(std::ostream&, const dballe::Encoding&);
168 
169 } // namespace dballe
170 
171 #endif
static Encoding parse_encoding(const char *s)
Return the Encoding corresponding to the given name.
Binary message.
Definition: file.h:134
static const char * encoding_name(Encoding enc)
Return a string with the name of this encoding.
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:141
virtual std::string pathname() const =0
Get the file pathname.
Definition: cmdline.h:18
virtual void close()=0
Close the underlying file.
File object for doing I/O on binary message streams.
Definition: file.h:17
virtual Encoding encoding() const =0
Get the file encoding.
std::string pathname
Pathname of the file from where the BinaryMessage has been read.
Definition: file.h:148
virtual BinaryMessage read()=0
Read a message from the file.
Encoding encoding
Format of the binary data.
Definition: file.h:138
off_t offset
Start offset of this message inside the file.
Definition: file.h:151
virtual void write(const std::string &msg)=0
Append the binary message to the file.
int index
Index of the message from the beginning of the file.
Definition: file.h:154