Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileData.h « Storage « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8cb4d0f3c12fd71ea434b16b9525efb676c7c37a (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * FileData.h
 *
 *  Created on: 16 Sep 2016
 *      Author: Christian
 */

#ifndef FILEDATA_H_
#define FILEDATA_H_

#include "FileStore.h"

#if HAS_MASS_STORAGE || HAS_SBC_INTERFACE || HAS_EMBEDDED_FILES

class FileGCodeInput;

// Small class to hold an open file and data relating to it.
// This is designed so that files are never left open and we never duplicate a file reference.
class FileData
{
public:
	FileData() noexcept : f(nullptr) {}

	~FileData() { (void)Close(); }

	FileData(const FileData& other) noexcept
	{
		f = other.f;
		if (f != nullptr)
		{
			not_null(f)->Duplicate();
		}
	}

	FileData(FileData&& other) noexcept
	{
		f = other.f;
		other.f = nullptr;
	}

	// Make sure we don't assign these objects
	FileData& operator=(const FileData&) = delete;

	// Set this to refer to a newly-opened file
	void Set(FileStore* pfile) noexcept
	{
		Close();	// close any existing file
		f = pfile;
	}

	bool IsLive() const noexcept { return f != nullptr; }

	bool operator==(const FileData& other) const noexcept
	{
		return f == other.f;
	}

	bool operator!=(const FileData& other) const noexcept
	{
		return f != other.f;
	}

	bool Close() noexcept
	{
		if (f != nullptr)
		{
			bool ok = not_null(f)->Close();
			f = nullptr;
			return ok;
		}
		return false;
	}

	bool Read(char& b) noexcept
	pre(IsLive())
	{
		return not_null(f)->Read(b);
	}

	int Read(char *_ecv_array buf, size_t nBytes) noexcept
	pre(IsLive())
	{
		return not_null(f)->Read(buf, nBytes);
	}

# if HAS_MASS_STORAGE || HAS_SBC_INTERFACE
	bool Write(char b) noexcept
	pre(IsLive())
	{
		return not_null(f)->Write(b);
	}

	bool Write(const char *_ecv_array s) noexcept
	pre(IsLive())
	{
		return not_null(f)->Write(s, strlen(s));
	}

	bool Write(const char *_ecv_array s, size_t len) noexcept
	pre(IsLive())
	{
		return not_null(f)->Write(s, len);
	}

	bool Write(const uint8_t *_ecv_array s, size_t len) noexcept
	pre(IsLive())
	{
		return not_null(f)->Write(s, len);
	}

	// This returns the CRC32 of data written to a newly-created file. It does not calculate the CRC of an existing file.
	uint32_t GetCrc32() const noexcept
	pre(IsLive())
	{
		return not_null(f)->GetCRC32();
	}

	bool Flush() noexcept
	pre(IsLive())
	{
		return not_null(f)->Flush();
	}
# endif

	FilePosition GetPosition() const noexcept
	pre(IsLive())
	{
		return not_null(f)->Position();
	}

	bool Seek(FilePosition position) noexcept
	pre(IsLive())
	{
		return not_null(f)->Seek(position);
	}

	FilePosition Length() const noexcept
	pre(IsLive())
	{
		return not_null(f)->Length();
	}

	// Move operator
	void MoveFrom(FileData& other) noexcept
	{
		Close();
		f = other.f;
		other.Init();
	}

	// Copy operator
	void CopyFrom(const FileData& other) noexcept
	{
		Close();
		f = other.f;
		if (f != nullptr)
		{
			not_null(f)->Duplicate();
		}
	}

private:
	FileStore * null f;

	void Init() noexcept
	{
		f = nullptr;
	}
};

#endif

#endif