ScummVM API documentation
file_stream.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AGS_SHARED_UTIL_FILE_STREAM_H
23 #define AGS_SHARED_UTIL_FILE_STREAM_H
24 
25 #include "common/savefile.h"
26 #include "common/stream.h"
27 #include "common/std/functional.h"
28 #include "ags/shared/util/data_stream.h"
29 #include "ags/shared/util/file.h" // TODO: extract filestream mode constants
30 
31 namespace AGS3 {
32 namespace AGS {
33 namespace Shared {
34 
35 class FileStream : public DataStream {
36 public:
37  struct CloseNotifyArgs {
38  String Filepath;
39  FileWorkMode WorkMode;
40  };
41 
42  // Represents an open file object
43  // The constructor may raise std::runtime_error if
44  // - there is an issue opening the file (does not exist, locked, permissions, etc)
45  // - the open mode could not be determined
46  FileStream(const String &file_name, FileOpenMode open_mode, FileWorkMode work_mode,
47  DataEndianess stream_endianess = kLittleEndian);
48  ~FileStream() override;
49 
50  FileWorkMode GetWorkMode() const { return _workMode; }
51 
52  bool HasErrors() const override;
53  void Close() override;
54  bool Flush() override;
55 
56  // Is stream valid (underlying data initialized properly)
57  bool IsValid() const override;
58  // Is end of stream
59  bool EOS() const override;
60  // Total length of stream (if known)
61  soff_t GetLength() const override;
62  // Current position (if known)
63  soff_t GetPosition() const override;
64  bool CanRead() const override;
65  bool CanWrite() const override;
66  bool CanSeek() const override;
67 
68  size_t Read(void *buffer, size_t size) override;
69  int32_t ReadByte() override;
70  size_t Write(const void *buffer, size_t size) override;
71  int32_t WriteByte(uint8_t b) override;
72 
73  bool Seek(soff_t offset, StreamSeek origin) override;
74 
75 private:
76  void Open(const String &file_name, FileOpenMode open_mode, FileWorkMode work_mode);
77  String getSaveName(const String &filename);
78  Common::OutSaveFile *openForWriting(const String &saveName, FileOpenMode open_mode, FileWorkMode work_mode);
79 
80  Common::Stream *_file;
81  const FileWorkMode _workMode;
82  String _fileName;
83 };
84 
85 } // namespace Shared
86 } // namespace AGS
87 } // namespace AGS3
88 
89 #endif
Definition: achievements_tables.h:27
Definition: file_stream.h:35
Definition: savefile.h:54
Definition: data_stream.h:40
Definition: string.h:62
Definition: stream.h:48
Definition: ags.h:40