-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.cpp
More file actions
478 lines (422 loc) · 11.9 KB
/
Main.cpp
File metadata and controls
478 lines (422 loc) · 11.9 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include "ncbind.hpp"
#include <map>
#define BASENAME TJS_W("var")
// 辞書かどうかの判定
static bool isDirectory(tTJSVariant &base) {
return base.Type() == tvtObject && base.AsObjectNoAddRef() != NULL;
}
// ファイルかどうかの判定
static bool isFile(tTJSVariant &file) {
return file.Type() == tvtOctet;
}
/**
* Variant参照型ストリーム
*/
class VariantStream : public IStream {
public:
/**
* コンストラクタ
*/
VariantStream(tTJSVariant &parent) : refCount(1), parent(parent), hBuffer(0), stream(0), cur(0) {};
/**
* ファイルを開く
*/
bool open(const ttstr &name, tjs_uint32 flags) {
close();
this->name = name;
// 読み込みのみの場合
if (flags == TJS_BS_READ) {
parent.AsObjectClosureNoAddRef().PropGet(0, name.c_str(), NULL, &value, NULL);
return isFile(value);
}
// 書き込みが必要な場合
hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, 0);
if (FAILED(::CreateStreamOnHGlobal(hBuffer, FALSE, &stream))) {
::GlobalFree(hBuffer);
hBuffer = 0;
return false;
}
// オブジェクトの内容を複製
if (flags == TJS_BS_UPDATE || flags == TJS_BS_APPEND) {
parent.AsObjectClosureNoAddRef().PropGet(0, name.c_str(), NULL, &value, NULL);
if (isFile(value)) {
stream->Write(value.AsOctetNoAddRef()->GetData(), value.AsOctetNoAddRef()->GetLength(), NULL);
LARGE_INTEGER n;
n.QuadPart = 0;
stream->Seek(n, flags == TJS_BS_UPDATE ? STREAM_SEEK_SET : STREAM_SEEK_END, NULL);
}
}
return true;
}
// IUnknown
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) {
if (riid == IID_IUnknown || riid == IID_ISequentialStream || riid == IID_IStream) {
if (ppvObject == NULL)
return E_POINTER;
*ppvObject = this;
AddRef();
return S_OK;
} else {
*ppvObject = 0;
return E_NOINTERFACE;
}
}
ULONG STDMETHODCALLTYPE AddRef(void) {
refCount++;
return refCount;
}
ULONG STDMETHODCALLTYPE Release(void) {
int ret = --refCount;
if (ret <= 0) {
delete this;
ret = 0;
}
return ret;
}
// ISequentialStream
HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead) {
if (stream) {
return stream->Read(pv, cb, pcbRead);
} else {
const tjs_uint8 *base = getBase();
tTVInteger size = getSize() - cur;
if (base && cb > 0 && size > 0) {
if (cb > size) {
cb = (ULONG)size;
}
memcpy(pv, base + cur, cb);
cur += cb;
if (pcbRead) {
*pcbRead = cb;
}
return S_OK;
} else {
if (pcbRead) {
*pcbRead = 0;
}
return S_FALSE;
}
}
}
HRESULT STDMETHODCALLTYPE Write(const void *pv, ULONG cb, ULONG *pcbWritten) {
if (stream) {
return stream->Write(pv, cb, pcbWritten);
} else {
return E_NOTIMPL;
}
}
// IStream
HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition) {
if (stream) {
return stream->Seek(dlibMove, dwOrigin, plibNewPosition);
} else {
switch (dwOrigin) {
case STREAM_SEEK_CUR:
cur += dlibMove.QuadPart;
break;
case STREAM_SEEK_SET:
cur = dlibMove.QuadPart;
break;
case STREAM_SEEK_END:
cur = getSize();
cur += dlibMove.QuadPart;
break;
}
if (plibNewPosition) {
plibNewPosition->QuadPart = cur;
}
return S_OK;
}
}
HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize) {
return stream ? stream ->SetSize(libNewSize) : E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten) {
return stream ? stream->CopyTo(pstm, cb, pcbRead, pcbWritten) : E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags) {
return stream ? stream->Commit(grfCommitFlags) : E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE Revert(void) {
return stream ? stream->Revert() : E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) {
return stream ? stream->LockRegion(libOffset, cb, dwLockType) : E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) {
return stream ? stream->UnlockRegion(libOffset, cb, dwLockType) : E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE Stat(STATSTG *pstatstg, DWORD grfStatFlag) {
return stream ? stream->Stat(pstatstg, grfStatFlag) : E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE Clone(IStream **ppstm) {
return stream ? stream->Clone(ppstm) : E_NOTIMPL;
}
protected:
/**
* ファイルを閉じる処理
*/
void close() {
if (stream) {
stream->Release();
stream = NULL;
}
if (hBuffer) {
// 書き戻し処理
if (name != "") {
unsigned char* pBuffer = (unsigned char*)::GlobalLock(hBuffer);
if (pBuffer) {
value = tTJSVariant(pBuffer, GlobalSize(hBuffer));
parent.AsObjectClosureNoAddRef().PropSet(TJS_MEMBERENSURE, name.c_str(), NULL, &value, NULL);
::GlobalUnlock(hBuffer);
}
}
::GlobalFree(hBuffer);
hBuffer = 0;
}
value.Clear();
cur = 0;
}
/**
* デストラクタ
*/
virtual ~VariantStream() {
close();
}
// 読み込み用メモリ領域取得
const tjs_uint8 *getBase() {
return isFile(value) ? value.AsOctetNoAddRef()->GetData() : NULL;
}
// 読み込み用メモリサイズ取得
tTVInteger getSize() {
return isFile(value) ? value.AsOctetNoAddRef()->GetLength() : 0;
}
private:
int refCount;
tTJSVariant parent;
ttstr name;
tTJSVariant value;
HGLOBAL hBuffer;
IStream *stream;
tTVInteger cur;
};
/**
* メンバ登録処理用
*/
class GetLister : public tTJSDispatch /** EnumMembers 用 */
{
public:
// コンストラクタ
GetLister(iTVPStorageLister *lister) : lister(lister) {};
// EnumMember用繰り返し実行部
// param[0] メンバ名
// param[1] フラグ
// param[2] メンバの値
virtual tjs_error TJS_INTF_METHOD FuncCall( // function invocation
tjs_uint32 flag, // calling flag
const tjs_char * membername,// member name ( NULL for a default member )
tjs_uint32 *hint, // hint for the member name (in/out)
tTJSVariant *result, // result
tjs_int numparams, // number of parameters
tTJSVariant **param, // parameters
iTJSDispatch2 *objthis // object as "this"
) {
if (numparams > 1) {
tTVInteger flag = param[1]->AsInteger();
if (!(flag & TJS_HIDDENMEMBER) && isFile(*param[2])) {
lister->Add(ttstr(param[0]->GetString()));
}
}
if (result) {
*result = true;
}
return TJS_S_OK;
}
private:
iTVPStorageLister *lister;
};
/**
* Varストレージ
*/
class VarStorage : public iTVPStorageMedia
{
public:
/**
* コンストラクタ
*/
VarStorage() : refCount(1) {
}
// -----------------------------------
// 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) {
tTJSVariant f = getFile(name);
return isFile(f);
}
// open a storage and return a tTJSBinaryStream instance.
// name does not contain in-archive storage name but
// is normalized.
virtual iTJSBinaryStream * TJS_INTF_METHOD Open(const ttstr & name, tjs_uint32 flags) {
iTJSBinaryStream *ret = NULL;
ttstr fname;
tTJSVariant parent = getParentName(name, fname);
if (isDirectory(parent) && fname.length() > 0) {
VariantStream *stream = new VariantStream(parent);
if (stream) {
if (stream->open(fname, flags)) {
ret = TVPCreateBinaryStreamAdapter(stream);
}
stream->Release();
}
}
if (!ret) {
TVPThrowExceptionMessage(TJS_W("cannot open memfile:%1"), name);
}
return ret;
}
// list files at given place
virtual void TJS_INTF_METHOD GetListAt(const ttstr &name, iTVPStorageLister * lister) {
tTJSVariant base = getFile(name);
if (isDirectory(base)) {
tTJSVariantClosure closure(new GetLister(lister));
base.AsObjectClosureNoAddRef().EnumMembers(TJS_IGNOREPROP, &closure, NULL);
closure.Release();
}
}
// 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 = "";
}
protected:
/**
* デストラクタ
*/
virtual ~VarStorage() {
}
/*
* 親フォルダとパスを返す
* @param name ファイル名
* @param fname ファイル名を返す
* @return 親フォルダ
*/
tTJSVariant getParentName(const ttstr &name, ttstr &fname) {
// ドメイン部を分離
const tjs_char *p = name.c_str();
const tjs_char *q;
if ((q = TJS_strchr(p, '/'))) {
ttstr dname = ttstr(p, q-p);
if (dname != TJS_W(".")) {
TVPThrowExceptionMessage(TJS_W("no such domain:%1"), dname);
}
} else {
TVPThrowExceptionMessage(TJS_W("invalid path:%1"), name);
}
// パス名
ttstr path = ttstr(q+1);
iTJSDispatch2 *global = TVPGetScriptDispatch();
tTJSVariant base(global, global);
while (path.length() > 0) {
p = path.c_str();
q = TJS_strchr(p, '/');
if (q == NULL) {
// ファイル
break;
} else if (q == p) {
// フォルダ名が空
base.Clear();
break;
} else {
// フォルダ
ttstr member = ttstr(p, q-p);
tTJSVariant value;
tTJSVariantClosure &o = base.AsObjectClosureNoAddRef();
if (((o.IsInstanceOf(0, NULL, NULL, TJS_W("Array"), NULL) == TJS_S_TRUE &&
TJS_SUCCEEDED(o.PropGetByNum(0, (tjs_int)TJSStringToInteger(member.c_str()), &value, NULL))) ||
(TJS_SUCCEEDED(o.PropGet(0, member.c_str(), NULL, &value, NULL)))) && isDirectory(value)) {
base = value;
path = ttstr(q+1);
} else {
base.Clear();
break;
}
}
}
fname = path;
return base;
}
/*
* ファイル名に合致する変数を探して返す
* @param name ファイル名
* @return 発見したファイルまたはフォルダ。見つからない場合は tvtVoid
*/
tTJSVariant getFile(const ttstr &name) {
ttstr fname;
tTJSVariant base = getParentName(name, fname);
if (isDirectory(base) && fname.length() > 0) {
// ファイル
tTJSVariant value;
if (TJS_SUCCEEDED(base.AsObjectClosureNoAddRef().PropGet(0, fname.c_str(), NULL, &value, NULL))) {
base = value;
} else {
base.Clear();
}
}
return base;
}
private:
tjs_uint refCount; //< リファレンスカウント
};
VarStorage *var = NULL;
/**
* 開放処理後
*/
static void PreRegistCallback()
{
if (var == NULL) {
var = new VarStorage();
TVPRegisterStorageMedia(var);
}
}
/**
* 開放処理後
*/
static void PostUnregistCallback()
{
if (var != NULL) {
TVPUnregisterStorageMedia(var);
var->Release();
var = NULL;
}
}
NCB_PRE_REGIST_CALLBACK(PreRegistCallback);
NCB_POST_UNREGIST_CALLBACK(PostUnregistCallback);