SDL Window Engine  20200905
swe_streamfile.h
1 /***************************************************************************
2  * Copyright (C) 2017 by SWE team <sdl.window.engine@gmail.com> *
3  * *
4  * Part of the SWE: SDL Window Engine: *
5  * https://github.com/AndreyBarmaley/sdl-window-engine *
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, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #ifndef _SWE_STREAMFILE_
24 #define _SWE_STREAMFILE_
25 
26 #include "swe_streambuf.h"
27 
29 namespace SWE
30 {
31 /*
32  mode:
33  r Open a file for reading. The file must exist.
34  w Create an empty file for writing.
35  If a file with the same name already exists its content is erased and the file is treated as a new empty file.
36  a Append to a file.
37  Writing operations append data at the end of the file.
38  The file is created if it does not exist.
39  r+ Open a file for update both reading and writing.
40  The file must exist.
41  w+ Create an empty file for both reading and writing.
42  If a file with the same name already exists its content is erased and the file is treated as a new empty file.
43  a+ Open a file for reading and appending.
44  All writing operations are performed at the end of the file, protecting the previous content to be overwritten.
45  You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file.
46  The file is created if it does not exist.
47 
48  In order to open a file as a binary file, a "b" character has to be included in the mode string.
49  This additional "b" character can either be appended at the end of the string
50  (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and
51  the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
52 */
53  class StreamFile : public StreamBase
54  {
55  std::string filename;
56  const char* filemode;
57  StreamRWops rw;
58 
59  public:
60  StreamFile() : filemode(nullptr) {}
61  StreamFile(const std::string &, const char* mode);
62  StreamFile(const StreamFile &);
63  ~StreamFile();
64 
65  StreamFile & operator= (const StreamFile &);
66 
67  size_t size(void) const;
68  bool open(const std::string &, const char* mode);
69  void close(void);
70  bool isValid(void) const;
71 
72  StreamBuf toStreamBuf(size_t = 0 /* all data */);
73 
74  bool seek(int offset, int whence = RW_SEEK_SET) const override;
75  size_t tell(void) const override;
76  bool skip(size_t len) const override;
77 
78  int get8(void) const override;
79  int getBE16(void) const override;
80  int getLE16(void) const override;
81  int getBE32(void) const override;
82  int getLE32(void) const override;
83  s64 getBE64(void) const override;
84  s64 getLE64(void) const override;
85  BinaryBuf get(size_t = 0 /* all data */) const override;
86 
87  void put8(char) override;
88  void putBE64(u64) override;
89  void putLE64(u64) override;
90  void putBE32(u32) override;
91  void putLE32(u32) override;
92  void putBE16(u16) override;
93  void putLE16(u16) override;
94  void put(const char*, size_t) override;
95  };
96 
97 } // SWE
98 #endif
Definition: swe_streamfile.h:53
пространство SWE.
Definition: swe_binarybuf.cpp:30
Definition: swe_serialize.h:225
класс бинарного массива
Definition: swe_binarybuf.h:35
Definition: swe_streambuf.h:121
Definition: swe_serialize.h:44