-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
279 lines (239 loc) · 6.36 KB
/
main.cpp
File metadata and controls
279 lines (239 loc) · 6.36 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <ncbind.hpp>
#include "psdclass.h"
// -----------------------------------------------------------------------------
// ストレージ機能
// -----------------------------------------------------------------------------
#define BASENAME TJS_W("psd")
/**
* PSDストレージ
*/
class PSDStorage : public iTVPStorageMedia, tTVPCompactEventCallbackIntf
{
public:
/**
* コンストラクタ
*/
PSDStorage() : refCount(1) {
}
/**
* デストラクタ
*/
virtual ~PSDStorage() {
clearCache();
}
/*
* PSDオブジェクト参照を追加
* @param filename 参照ファイル名
* @param psd PSDオブジェクト
*/
void add(ttstr filename, PSD *psd) {
psdMap[filename] = psd;
}
/**
* PSDオブジェクト参照の消去要求
* @param psd PSDオブジェクト
*/
void remove(PSD *psd) {
PSDMap::iterator it = psdMap.begin();
while (it != psdMap.end()) {
if (it->second == psd) {
it = psdMap.erase(it);
} else {
it++;
}
}
}
/**
* キャッシュ情報をクリアする
*/
void clearCache() {
cache.Clear();
}
public:
// -----------------------------------
// iTVPStorageMedia Intefaces
// -----------------------------------
virtual void TJS_INTF_METHOD AddRef() {
refCount++;
};
virtual void TJS_INTF_METHOD Release() {
if (refCount == 1) {
delete this;
} else {
refCount--;
}
};
// returns media name like "file", "http" etc.
virtual void TJS_INTF_METHOD GetName(ttstr &name) {
name = BASENAME;
}
// virtual ttstr TJS_INTF_METHOD IsCaseSensitive() = 0;
// returns whether this media is case sensitive or not
// normalize domain name according with the media's rule
virtual void TJS_INTF_METHOD NormalizeDomainName(ttstr &name) {
// nothing to do
}
// normalize path name according with the media's rule
virtual void TJS_INTF_METHOD NormalizePathName(ttstr &name) {
// nothing to do
}
// check file existence
virtual bool TJS_INTF_METHOD CheckExistentStorage(const ttstr &name) {
ttstr fname;
PSD *psd = getPSD(name, fname);
if (psd) {
bool ret = psd->CheckExistentStorage(fname);
return ret;
}
return false;
}
// open a storage and return a iTJSBinaryStream instance.
// name does not contain in-archive storage name but
// is normalized.
virtual iTJSBinaryStream * TJS_INTF_METHOD Open(const ttstr & name, tjs_uint32 flags) {
if (flags == TJS_BS_READ) { // 読み込みのみ
ttstr fname;
PSD *psd = getPSD(name, fname);
if (psd) {
IStream *stream = psd->openLayerImage(fname);
if (stream) {
iTJSBinaryStream *ret = TVPCreateBinaryStreamAdapter(stream);
stream->Release();
return ret;
}
}
}
TVPThrowExceptionMessage(TJS_W("%1:cannot open psdfile"), name);
return NULL;
}
// list files at given place
virtual void TJS_INTF_METHOD GetListAt(const ttstr &name, iTVPStorageLister * lister) {
ttstr fname;
PSD *psd = getPSD(name, fname);
if (psd) {
psd->GetListAt(fname, lister);
}
}
// basically the same as above,
// check wether given name is easily accessible from local OS filesystem.
// if true, returns local OS native name. otherwise returns an empty string.
virtual void TJS_INTF_METHOD GetLocallyAccessibleName(ttstr &name) {
name = "";
}
public:
// -----------------------------------
// tTVPCompactEventCallbackIntf
// -----------------------------------
virtual void TJS_INTF_METHOD OnCompact(tjs_int level) {
if (level > TVP_COMPACT_LEVEL_MINIMIZE) {
clearCache();
}
}
protected:
/*
* ファイル名に合致した PSD 情報を取得
* @param name ファイル名
* @param fname ファイル名を返す
* @return PSD情報
*/
PSD *getPSD(ttstr name, ttstr &fname) {
// 小文字で正規化
name.ToLowerCase();
// ドメイン名とそれ以降を分離
ttstr dname;
const tjs_char *p = name.c_str();
const tjs_char *q;
if ((q = TJS_strchr(p, '/'))) {
dname = ttstr(p, q-p);
fname = ttstr(q+1);
} else {
TVPThrowExceptionMessage(TJS_W("invalid path:%1"), name);
}
PSD *psd = 0;
// 直近のキャッシュが合致する場合はそれを返す
if (cache.Type() == tvtObject &&
(psd = ncbInstanceAdaptor<PSD>::GetNativeInstance(cache.AsObjectNoAddRef())) &&
psd->dname == dname) {
return psd;
}
// PSDオブジェクトの弱参照から探す
PSDMap::iterator it = psdMap.find(dname);
if (it != psdMap.end()) {
// 既存データがある
cache = it->second->getSelf();
return it->second;
}
// 自分でopenしてそのままキャッシュとして持つ
TVPExecuteExpression(TJS_W("new PSD()"), &cache);
psd = ncbInstanceAdaptor<PSD>::GetNativeInstance(cache.AsObjectNoAddRef());
if (psd) {
if (psd->load(dname)) {
return psd;
} else {
clearCache();
}
}
return 0;
}
private:
tjs_uint refCount; //< リファレンスカウント
// PSDオブジェクトのキャッシュ参照
tTJSVariant cache;
// PSDオブジェクトの弱参照
typedef std::map<ttstr, PSD*> PSDMap;
PSDMap psdMap;
};
static PSDStorage *psdStorage = 0;
// 弱参照追加
void
PSD::addToStorage(const ttstr &filename)
{
// 登録用ベース名を生成
const tjs_char *p = filename.c_str();
const tjs_char *q;
if ((q = wcsrchr(p, '/'))) {
dname = ttstr(q+1);
} else {
dname = filename;
}
// 小文字で正規化
dname.ToLowerCase();
if (psdStorage != NULL) {
psdStorage->add(dname, this);
}
}
// 弱参照解除
void
PSD::removeFromStorage()
{
if (psdStorage != NULL) {
psdStorage->remove(this);
}
}
void
PSD::clearStorageCache()
{
if (psdStorage != NULL) {
psdStorage->clearCache();
}
}
// --------------------------------------------------------------------
void initStorage()
{
if (psdStorage== NULL) {
psdStorage = new PSDStorage();
TVPRegisterStorageMedia(psdStorage);
TVPAddCompactEventHook((tTVPCompactEventCallbackIntf*)psdStorage);
}
}
void doneStorage()
{
if (psdStorage != NULL) {
TVPRemoveCompactEventHook((tTVPCompactEventCallbackIntf*)psdStorage);
TVPUnregisterStorageMedia(psdStorage);
psdStorage->Release();
psdStorage = NULL;
}
}
NCB_PRE_REGIST_CALLBACK(initStorage);
NCB_POST_UNREGIST_CALLBACK(doneStorage);