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: aa4722399a8dc3bbf659277428d8299768930d2b (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
/*
 * FileData.h
 *
 *  Created on: 16 Sep 2016
 *      Author: Christian
 */

#ifndef FILEDATA_H_
#define FILEDATA_H_

#include "FileStore.h"

#if HAS_MASS_STORAGE

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:
	friend class FileGCodeInput;

	FileData() noexcept : f(nullptr) {}

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

	// 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 Close() noexcept
	{
		if (f != nullptr)
		{
			bool ok = f->Close();
			f = nullptr;
			return ok;
		}
		return false;
	}

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

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

	bool Write(char b) noexcept
	{
		return f->Write(b);
	}

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

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

	bool Write(const uint8_t *s, size_t len) noexcept
	{
		return 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
	{
		return f->GetCRC32();
	}

	bool Flush() noexcept
	{
		return f->Flush();
	}

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

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

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

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

private:
	FileStore *f;

	void Init() noexcept
	{
		f = nullptr;
	}

	// Private assignment operator to prevent us assigning these objects
	FileData& operator=(const FileData&) noexcept;
};

#endif

#endif