-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpsdclass_loadstream.cpp
More file actions
193 lines (158 loc) · 3.82 KB
/
psdclass_loadstream.cpp
File metadata and controls
193 lines (158 loc) · 3.82 KB
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
#include "psdclass.h"
#ifndef LOAD_MEMORY
#include "psdparse/psdparse.h"
/**
* Stream 用の Iterator
*/
class PSDIterator : public std::iterator<std::random_access_iterator_tag, const unsigned char>
{
public:
typedef size_t diff_t;
PSDIterator() : _psd(0), _pos(0), _size(0) {
}
/**
* コンストラクタ
* @param srream 参照ストリーム
* @param begin true なら冒頭、false なら末尾
*/
PSDIterator(PSD* psd, bool begin) : _psd(psd), _pos(0), _size(0) {
_size = psd->mStreamSize;
_pos = begin ? 0 : _size;
}
// コピーコンストラクタ
PSDIterator(const PSDIterator& o) {
_psd = o._psd;
_pos = o._pos;
_size = o._size;
}
// 代入演算子
PSDIterator &operator=(const PSDIterator &o) {
_psd = o._psd;
_pos = o._pos;
_size = o._size;
return *this;
}
// デストラクタ
virtual ~PSDIterator() {
}
// イテレータを進める(前置)
PSDIterator& operator++() {
_pos++;
if (_pos > _size) _pos = _size;
return *this;
}
// イテレータを進める(後置)
PSDIterator operator++(int) {
PSDIterator ret = *this;
_pos++;
if (_pos > _size) _pos = _size;
return ret;
}
PSDIterator& operator+=(diff_t n) {
_pos += n;
if (_pos > _size) _pos = _size;
return *this;
}
PSDIterator operator+(diff_t n) {
PSDIterator ret = *this;
ret += n;
return ret;
}
// イテレータを戻す(前置)
PSDIterator& operator--() {
_pos--;
if (_pos < 0) _pos = 0;
return *this;
}
// イテレータを戻す(後置)
PSDIterator operator--(int) {
PSDIterator ret = *this;
_pos--;
if (_pos < 0) _pos = 0;
return ret;
}
PSDIterator& operator-=(diff_t n) {
_pos -= n;
if (_pos < 0) _pos = 0;
return *this;
}
PSDIterator operator-(diff_t n) {
PSDIterator ret = *this;
ret -= n;
return ret;
}
// 読み取り
const unsigned char& operator*() {
return _psd->getStreamValue(_pos);
}
void copyToBuffer(uint8_t *buf, int size) {
_psd->copyToBuffer(buf, _pos, size);
_pos += size;
}
// 差分
diff_t operator-(const PSDIterator& b) const {
return (diff_t)(_pos - b._pos);
}
// イテレータの一致判定
bool operator==(const PSDIterator& o) const {
return o._pos == _pos;
}
// イテレータの一致判定
bool operator!=(const PSDIterator& o) const {
return o._pos != _pos;
}
bool operator<(const PSDIterator& o) const {
return _pos < o._pos;
}
bool operator>(const PSDIterator& o) const {
return _pos > o._pos;
}
bool operator<=(const PSDIterator& o) const {
return _pos <= o._pos;
}
bool operator>=(const PSDIterator& o) const {
return _pos >= o._pos;
}
private:
PSD *_psd;
tTVInteger _size; //< ストリームサイズ保持用
tTVInteger _pos; //< 参照位置
};
namespace psd {
inline void copyToBuffer(uint8_t *buffer, PSDIterator &cur, int size) {
cur.copyToBuffer(buffer, size);
}
inline void getShortLE(uint8_t *buffer, PSDIterator &cur) {
cur.copyToBuffer(buffer, 2);
}
inline void getLongLE(uint8_t *buffer, PSDIterator &cur) {
cur.copyToBuffer(buffer, 4);
}
inline void getLongLongLE(uint8_t *buffer, PSDIterator &cur) {
cur.copyToBuffer(buffer, 8);
}
}
bool
PSD::loadStream(const ttstr &filename)
{
clearData();
// ストリームのまま
isLoaded = false;
pStream = TVPCreateStream(filename, TJS_BS_READ);
if (pStream) {
mStreamSize = (tTVInteger)pStream->GetSize();
PSDIterator begin(this, true);
PSDIterator end(this, false);
psd::Parser<PSDIterator> parser(*this);
bool r = parse(begin , end, parser);
if (r && begin == end) {
dprint("succeeded\n");
isLoaded = processParsed();
}
if (!isLoaded) {
clearData();
}
}
return isLoaded;
}
#endif