Cupt
file.hpp
1/**************************************************************************
2* Copyright (C) 2010 by Eugene V. Lyubimkin *
3* *
4* This program is free software; you can redistribute it and/or modify *
5* it under the terms of the GNU General Public License *
6* (version 3 or above) as published by the Free Software Foundation. *
7* *
8* This program is distributed in the hope that it will be useful, *
9* but WITHOUT ANY WARRANTY; without even the implied warranty of *
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11* GNU General Public License for more details. *
12* *
13* You should have received a copy of the GNU GPL *
14* along with this program; if not, write to the *
15* Free Software Foundation, Inc., *
16* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA *
17**************************************************************************/
18#ifndef CUPT_FILE_SEEN
19#define CUPT_FILE_SEEN
20
22
23#include <functional>
24
25#include <cupt/common.hpp>
26
27namespace cupt {
28
29namespace internal {
30
31struct FileImpl;
32
33}
34
36class CUPT_API File
37{
38 internal::FileImpl* __impl;
39 File(const File&) = delete;
40 public:
41 struct RawBuffer
42 {
43 const char* data;
44 size_t size;
45
46 operator bool() const { return size; }
47 operator string() const { return string(data, size); }
48
49 RawBuffer chompAsRecord() const;
50 };
52
63 File(const string& path, const char* mode, string& error);
64 File(File&&);
66 virtual ~File() noexcept(false);
68
79 File& rawGetLine(const char*& buffer, size_t& size);
81
100 File& getLine(string& line);
101 RawBuffer getRecord();
102 RawBuffer getBlock(size_t size);
104
109 void getFile(string& block);
111
114 void put(const string& data);
116
120 void put(const char* data, size_t size);
121 void unbufferedPut(const char* data, size_t size);
122
123
125 bool eof() const;
127
133 void seek(size_t newPosition);
135 size_t tell() const;
136
138
141 void lock(int flags);
142};
143
144// File wrapper which throws on open errors
145class CUPT_API RequiredFile: public File
146{
147 public:
148 /*
149 * Passes @a path and @a mode to File::File(). If file failed to open (i.e.
150 * !openError.empty()), throws the exception.
151 */
152 RequiredFile(const string& path, const char* mode);
153};
154
155} // namespace
156
158
159#endif
160