Otclient  14/8/2020
filestream.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef FILESTREAM_H
24 #define FILESTREAM_H
25 
26 #include "declarations.h"
29 #include <framework/util/point.h>
30 
31 struct PHYSFS_File;
32 
33 // @bindclass
34 class FileStream : public LuaObject
35 {
36 public:
37  FileStream(const std::string& name, PHYSFS_File *fileHandle, bool writeable);
38  FileStream(const std::string& name, const std::string& buffer);
39  ~FileStream();
40 
41  void cache();
42  void close();
43  void flush();
44  void write(const void *buffer, uint count);
45  int read(void *buffer, uint size, uint nmemb = 1);
46  void seek(uint pos);
47  void skip(uint len);
48  uint size();
49  uint tell();
50  bool eof();
51  std::string name() { return m_name; }
52 
53  uint8 getU8();
54  uint16 getU16();
55  uint32 getU32();
56  uint64 getU64();
57  int8 get8();
58  int16 get16();
59  int32 get32();
60  int64 get64();
61  std::string getString();
63 
64  void startNode(uint8 n);
65  void endNode();
66  void addU8(uint8 v);
67  void addU16(uint16 v);
68  void addU32(uint32 v);
69  void addU64(uint64 v);
70  void add8(int8 v);
71  void add16(int16 v);
72  void add32(int32 v);
73  void add64(int64 v);
74  void addString(const std::string& v);
75  void addPos(uint16 x, uint16 y, uint8 z) { addU16(x); addU16(y); addU8(z); }
76  void addPoint(const Point& p) { addU8(p.x); addU8(p.y); }
77 
78  FileStreamPtr asFileStream() { return static_self_cast<FileStream>(); }
79 
80 private:
81  void checkWrite();
82  void throwError(const std::string& message, bool physfsError = false);
83 
84  std::string m_name;
85  PHYSFS_File *m_fileHandle;
86  uint m_pos;
87  bool m_writeable;
88  bool m_caching;
89 
90  DataBuffer<uint8_t> m_data;
91 };
92 
93 #endif
FileStream::write
void write(const void *buffer, uint count)
Definition: filestream.cpp:130
FileStream::seek
void seek(uint pos)
Definition: filestream.cpp:142
FileStream::FileStream
FileStream(const std::string &name, PHYSFS_File *fileHandle, bool writeable)
Definition: filestream.cpp:29
point.h
databuffer.h
FileStream::add16
void add16(int16 v)
Definition: filestream.cpp:412
TPoint::y
T y
Definition: point.h:83
FileStream::close
void close()
Definition: filestream.cpp:78
z
gc sort z
Definition: CMakeLists.txt:176
FileStream::addU32
void addU32(uint32 v)
Definition: filestream.cpp:377
uint32
uint32_t uint32
Definition: types.h:35
DataBuffer< uint8_t >
FileStream::endNode
void endNode()
Definition: filestream.cpp:349
int64
int64_t int64
Definition: types.h:38
FileStream::getString
std::string getString()
Definition: filestream.cpp:309
luaobject.h
int8
int8_t int8
Definition: types.h:41
FileStream::addString
void addString(const std::string &v)
Definition: filestream.cpp:448
uint16
uint16_t uint16
Definition: types.h:36
FileStream::addU64
void addU64(uint64 v)
Definition: filestream.cpp:389
declarations.h
uint
unsigned int uint
Definition: types.h:31
FileStream::cache
void cache()
Definition: filestream.cpp:58
FileStream::getU16
uint16 getU16()
Definition: filestream.cpp:199
FileStream::getBinaryTree
BinaryTreePtr getBinaryTree()
Definition: filestream.cpp:334
int32
int32_t int32
Definition: types.h:39
FileStream::addPos
void addPos(uint16 x, uint16 y, uint8 z)
Definition: filestream.h:75
FileStream
Definition: filestream.h:34
FileStream::eof
bool eof()
Definition: filestream.cpp:175
TPoint::x
T x
Definition: point.h:83
FileStream::read
int read(void *buffer, uint size, uint nmemb=1)
Definition: filestream.cpp:109
FileStream::get8
int8 get8()
Definition: filestream.cpp:246
FileStream::name
std::string name()
Definition: filestream.h:51
FileStream::get64
int64 get64()
Definition: filestream.cpp:294
FileStream::add8
void add8(int8 v)
Definition: filestream.cpp:401
uint64
uint64_t uint64
Definition: types.h:34
FileStream::add32
void add32(int32 v)
Definition: filestream.cpp:424
FileStream::get32
int32 get32()
Definition: filestream.cpp:278
FileStream::getU64
uint64 getU64()
Definition: filestream.cpp:231
FileStream::tell
uint tell()
Definition: filestream.cpp:167
FileStream::skip
void skip(uint len)
Definition: filestream.cpp:154
stdext::shared_object_ptr
Definition: shared_object.h:39
FileStream::startNode
void startNode(uint8 n)
Definition: filestream.cpp:343
FileStream::getU32
uint32 getU32()
Definition: filestream.cpp:215
FileStream::flush
void flush()
Definition: filestream.cpp:90
FileStream::get16
int16 get16()
Definition: filestream.cpp:262
FileStream::~FileStream
~FileStream()
Definition: filestream.cpp:49
FileStream::size
uint size()
Definition: filestream.cpp:159
TPoint< int >
FileStream::addU8
void addU8(uint8 v)
Definition: filestream.cpp:354
FileStream::add64
void add64(int64 v)
Definition: filestream.cpp:436
int16
int16_t int16
Definition: types.h:40
LuaObject
LuaObject, all script-able classes have it as base.
Definition: luaobject.h:30
FileStream::asFileStream
FileStreamPtr asFileStream()
Definition: filestream.h:78
FileStream::getU8
uint8 getU8()
Definition: filestream.cpp:183
FileStream::addPoint
void addPoint(const Point &p)
Definition: filestream.h:76
uint8
uint8_t uint8
Definition: types.h:37
FileStream::addU16
void addU16(uint16 v)
Definition: filestream.cpp:365