File: stdiostream.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (216 lines) | stat: -rw-r--r-- 6,194 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#if !defined(DISABLE_STDIO_FILESTREAM)

// Disable symbol overrides so that we can use FILE, fopen etc.
#define FORBIDDEN_SYMBOL_ALLOW_ALL

// for Windows unicode fopen(): _wfopen() and for Win32::moveFile()
#if defined(WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "backends/platform/sdl/win32/win32_wrapper.h"
#endif

// Include this after windows.h so we don't get a warning for redefining ARRAYSIZE
#include "backends/fs/stdiostream.h"
#include "common/textconsole.h"

#if defined(__DC__)
// libronin doesn't support rename
#define STDIOSTREAM_NO_ATOMIC_SUPPORT
#endif
#if defined(ATARI)
// Atari file names must have a 8.3 format, atomic breaks this
#define STDIOSTREAM_NO_ATOMIC_SUPPORT
#endif
#if defined(RISCOS)
// RISC OS file names are expected to be 10 characters or less, atomic makes this hard to guarantee
#define STDIOSTREAM_NO_ATOMIC_SUPPORT
#endif

StdioStream::StdioStream(void *handle) : _handle(handle), _path(nullptr) {
	assert(handle);
}

StdioStream::~StdioStream() {
	fclose((FILE *)_handle);

	if (!_path) {
		return;
	}

	// _path is set: recreate the temporary file name and rename the file to
	// its real name
	Common::String tmpPath(*_path);
	tmpPath += ".tmp";

	if (!moveFile(tmpPath, *_path)) {
		warning("Couldn't save file %s", _path->c_str());
	}

	delete _path;
}

bool StdioStream::moveFile(const Common::String &src, const Common::String &dst) {
#ifndef STDIOSTREAM_NO_ATOMIC_SUPPORT
	// This piece of code can't be in a subclass override, as moveFile is called from the destructor.
	// In this case, the vtable is reset to the StdioStream one before calling moveFile.
#if defined(WIN32)
	return Win32::moveFile(src, dst);
#else
	if (!rename(src.c_str(), dst.c_str())) {
		return true;
	}

	// Error: try to delete the file first
	(void)remove(dst.c_str());
	return !rename(src.c_str(), dst.c_str());
#endif
#else // STDIOSTREAM_NO_ATOMIC_SUPPORT
	return false;
#endif
}

bool StdioStream::err() const {
	return ferror((FILE *)_handle) != 0;
}

void StdioStream::clearErr() {
	clearerr((FILE *)_handle);
}

bool StdioStream::eos() const {
	return feof((FILE *)_handle) != 0;
}

int64 StdioStream::pos() const {
#if defined(WIN32)
	return _ftelli64((FILE *)_handle);
#elif defined(HAS_FSEEKO_OFFT_64)
	return ftello((FILE *)_handle);
#elif defined(HAS_FSEEKO64)
	return ftello64((FILE *)_handle);
#else
	return ftell((FILE *)_handle);
#endif
}

int64 StdioStream::size() const {
#if defined(WIN32)
	int64 oldPos = _ftelli64((FILE *)_handle);
	_fseeki64((FILE *)_handle, 0, SEEK_END);
	int64 length = _ftelli64((FILE *)_handle);
	_fseeki64((FILE *)_handle, oldPos, SEEK_SET);
#elif defined(HAS_FSEEKO_OFFT_64)
	int64 oldPos = ftello((FILE *)_handle);
	fseeko((FILE *)_handle, 0, SEEK_END);
	int64 length = ftello((FILE *)_handle);
	fseeko((FILE *)_handle, oldPos, SEEK_SET);
#elif defined(HAS_FSEEKO64)
	int64 oldPos = ftello64((FILE *)_handle);
	fseeko64((FILE *)_handle, 0, SEEK_END);
	int64 length = ftello64((FILE *)_handle);
	fseeko64((FILE *)_handle, oldPos, SEEK_SET);
#else
	int64 oldPos = ftell((FILE *)_handle);
	fseek((FILE *)_handle, 0, SEEK_END);
	int64 length = ftell((FILE *)_handle);
	fseek((FILE *)_handle, oldPos, SEEK_SET);
#endif

	return length;
}

bool StdioStream::seek(int64 offs, int whence) {
#if defined(WIN32)
	return _fseeki64((FILE *)_handle, offs, whence) == 0;
#elif defined(HAS_FSEEKO_OFFT_64)
	return fseeko((FILE *)_handle, offs, whence) == 0;
#elif defined(HAS_FSEEKO64)
	return fseeko64((FILE *)_handle, offs, whence) == 0;
#else
	return fseek((FILE *)_handle, offs, whence) == 0;
#endif
}

uint32 StdioStream::read(void *ptr, uint32 len) {
	return fread((byte *)ptr, 1, len, (FILE *)_handle);
}

bool StdioStream::setBufferSize(uint32 bufferSize) {
	if (bufferSize != 0) {
		return setvbuf((FILE *)_handle, nullptr, _IOFBF, bufferSize) == 0;
	} else {
		return setvbuf((FILE *)_handle, nullptr, _IONBF, 0) == 0;
	}
}

uint32 StdioStream::write(const void *ptr, uint32 len) {
	return fwrite(ptr, 1, len, (FILE *)_handle);
}

bool StdioStream::flush() {
	return fflush((FILE *)_handle) == 0;
}

StdioStream *StdioStream::makeFromPathHelper(const Common::String &path, WriteMode writeMode,
		StdioStream *(*factory)(void *handle)) {
	Common::String tmpPath(path);

	// If no atmoic support is compiled in, WriteMode_WriteAtomic must behave like WriteMode_Write
#ifndef STDIOSTREAM_NO_ATOMIC_SUPPORT
	// In atomic mode we create a temporary file and rename it when closing the file descriptor
	if (writeMode == WriteMode_WriteAtomic) {
		tmpPath += ".tmp";
	}
#endif

#if defined(WIN32) && defined(UNICODE)
	wchar_t *wPath = Win32::stringToTchar(tmpPath);
	FILE *handle = _wfopen(wPath, writeMode == WriteMode_Read ? L"rb" : L"wb");
	free(wPath);
#elif defined(HAS_FOPEN64)
	FILE *handle = fopen64(tmpPath.c_str(), writeMode == WriteMode_Read ? "rb" : "wb");
#else
	FILE *handle = fopen(tmpPath.c_str(), writeMode == WriteMode_Read ? "rb" : "wb");
#endif

	if (!handle) {
		return nullptr;
	}

	StdioStream *stream = factory(handle);

#ifndef STDIOSTREAM_NO_ATOMIC_SUPPORT
	// Store the final path alongside the stream
	// If _path is not nullptr, it will be used to rename the file
	// when closing it
	if (writeMode == WriteMode_WriteAtomic) {
		stream->_path = new Common::String(path);
	}
#endif

	return stream;
}

#endif