1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef PSPSTREAM_H_
24 #define PSPSTREAM_H_
25 
26 #include <pspkerneltypes.h>
27 #include "backends/platform/psp/powerman.h"
28 //#include "common/list.h"
29 #include "common/noncopyable.h"
30 #include "common/stream.h"
31 #include "common/str.h"
32 
33 /**
34  *  Class to handle special suspend/resume needs of PSP IO Streams
35  */
36 class PspIoStream final : public Common::SeekableReadStream, public Common::SeekableWriteStream, public Common::NonCopyable, public Suspendable {
37 protected:
38 	SceUID _handle;		// file handle
39 	Common::String _path;
40 	int _fileSize;
41 	bool _writeMode;	// for resuming in the right mode
42 	int _physicalPos;	// physical position in file
43 	int _pos;			// position. Sometimes virtual
44 	bool _eos;			// EOS flag
45 
46 	enum {
47 		SuspendError = 2,
48 		ResumeError = 3
49 	};
50 
51 	// debug stuff
52 	mutable int _error;		// file error state
53 	int _errorSuspend;			// for debugging
54 	mutable int _errorSource;
55 	int _errorPos;
56 	SceUID _errorHandle;
57 	int _suspendCount;
58 
59 	bool physicalSeekFromCur(int32 offset);
60 
61 public:
62 
63 	/**
64 	 * Given a path, invoke fopen on that path and wrap the result in a
65 	 * PspIoStream instance.
66 	 */
67 	static PspIoStream *makeFromPath(const Common::String &path, bool writeMode);
68 
69 	PspIoStream(const Common::String &path, bool writeMode);
70 	virtual ~PspIoStream() override;
71 
72 	SceUID open();		// open the file pointed to by the file path
73 
74 	bool err() const override;
75 	void clearErr() override;
76 	bool eos() const override;
77 
78 	virtual uint32 write(const void *dataPtr, uint32 dataSize) override;
79 	virtual bool flush() override;
80 
81 	virtual int64 pos() const override;
82 	virtual int64 size() const override;
83 	virtual bool seek(int64 offs, int whence = SEEK_SET) override;
84 	virtual uint32 read(void *dataPtr, uint32 dataSize) override;
85 
86 	// for suspending
87 	int suspend() override;		/* Suspendable interface (power manager) */
88 	int resume() override;		/* " " */
89 };
90 
91 #endif /* PSPSTREAM_H_ */
92