File: filewrite.h

package info (click to toggle)
widelands 1%3A19%2Brepack-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 371,172 kB
  • sloc: cpp: 108,458; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 229
file content (109 lines) | stat: -rw-r--r-- 3,004 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2008 by the Widelands Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef WL_IO_FILEWRITE_H
#define WL_IO_FILEWRITE_H

#include <cassert>
#include <cstdarg>
#include <limits>
#include <memory>
#include <string>

#include "io/streamwrite.h"

class FileSystem;
class RealFSImpl;

/// Mirror of \ref FileRead : all writes are first stored in a block of memory
/// and finally written out when write() is called.
class FileWrite : public StreamWrite {
public:
	struct Pos {
		Pos(size_t const p = 0) : pos(p) {
		}

		/// Returns a special value indicating invalidity.
		static Pos null() {
			return std::numeric_limits<size_t>::max();
		}

		bool is_null() const {
			return *this == null();
		}
		operator size_t() const {
			return pos;
		}
		Pos operator++() {
			return ++pos;
		}
		Pos operator+=(Pos const other) {
			return pos += other.pos;
		}

	private:
		size_t pos;
	};

	struct Exception {};
	struct BufferOverflow : public Exception {};

	/// Set the buffer to empty.
	FileWrite();

	/// Clear any remaining allocated data.
	~FileWrite() override;

	/// Clears the object's buffer.
	void clear();

	/// Write the file out to disk. If successful, this clears the buffers.
	/// Otherwise, an exception is thrown but the buffer remains intact (don't
	/// worry, it will be cleared by the destructor).
	void write(FileSystem& fs, char const* const filename);

	/// Same as above, just that the data is appended to the file
	/// NOTE RealFSImpl is used by purpose - zip filesystems do not support appending
	void write_append(RealFSImpl& fs, char const* const filename);

	/// Get the position that will be written to in the next write operation that
	/// does not specify a position.
	Pos get_pos() const;

	/// Set the file pointer to a new location. The position can be beyond the
	/// current end of file.
	void set_pos(const Pos& pos);

	/// Write data at the given location.
	void data(const void* src, size_t size, Pos pos);

	/// Write data at the current file pointer and advance it.
	void data(void const* src, size_t size) override;

	/// Returns the current buffer. Use this for in_memory operations.
	std::string get_data() const;

private:
	char* data_;
	size_t length_;
	size_t max_size_;
	Pos filepos_;
};

#endif  // end of include guard: WL_IO_FILEWRITE_H