aboutsummaryrefslogtreecommitdiff
path: root/app/source/Cplt/Utils/IO/FileStream_Cstdio.inl
blob: ff2ca01a964251d9f31614668396fb732d30dd2a (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
// Note: included by FileStream.cpp conditionally, not compiled separately
#include "FileStream.hpp"

#include <Cplt/Utils/IO/CstdioFile.hpp>

#include <cstdio>
#include <filesystem>

namespace fs = std::filesystem;

InputFileStream::InputFileStream(const fs::path& path)
	: mFile{ FileUtils::OpenCstdioFile(path, FileUtils::IM_Read) }
{
}

InputFileStream::~InputFileStream()
{
	if (mFile) {
		std::fclose(mFile);
	}
}

InputFileStream::InputFileStream(InputFileStream&& that)
	: mFile{ that.mFile }
{
	that.mFile = nullptr;
}

InputFileStream& InputFileStream::operator=(InputFileStream&& that)
{
	if (this == &that) return *this;

	if (mFile) {
		std::fclose(mFile);
	}
	mFile = that.mFile;
	that.mFile = nullptr;

	return *this;
}

OutputFileStream::OutputFileStream(const fs::path& path, WriteMode mode)
{
	switch (mode) {
		case AppendFile: mFile = FileUtils::OpenCstdioFile(path, FileUtils::IM_WriteAppend); break;
		case TruncateFile: mFile = FileUtils::OpenCstdioFile(path, FileUtils::IM_WriteTruncate); break;
	}
}

OutputFileStream::~OutputFileStream()
{
	if (mFile) {
		std::fclose(mFile);
	}
}

OutputFileStream::OutputFileStream(OutputFileStream&& that)
	: mFile{ that.mFile }
{
	that.mFile = nullptr;
}

OutputFileStream& OutputFileStream::operator=(OutputFileStream&& that)
{
	if (this == &that) return *this;

	if (mFile) {
		std::fclose(mFile);
	}
	mFile = that.mFile;
	that.mFile = nullptr;

	return *this;
}

int InputFileStream::GetReadInSize() const
{
	return 0;
}

void InputFileStream::SetReadInSize(int size)
{
	// No-op
}

bool InputFileStream::IsEof() const
{
	return std::feof(mFile);
}

IoResult InputFileStream::ReadBytes(size_t bufferLength, std::byte* buffer)
{
	auto bytesRead = std::fread(buffer, 1, bufferLength, mFile);

	return {
		.Error = IoResult::ERR_None,
		.SystemError = 0,
		.BytesMoved = bytesRead,
	};
}

int OutputFileStream::GetMaxBufferSize() const
{
	return 0;
}

void OutputFileStream::SetMaxBufferSize(int maxSize)
{
	// No-op
}

IoResult OutputFileStream::WriteBytes(size_t bufferLength, const std::byte* buffer)
{
	auto bytesWritten = std::fwrite(buffer, 1, bufferLength, mFile);

	return IoResult{
		.Error = IoResult::ERR_None,
		.SystemError = 0,
		.BytesMoved = bytesWritten,
	};
}

void OutputFileStream::FlushBuffer()
{
	// No-op
}