-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.cpp
More file actions
741 lines (665 loc) · 19.3 KB
/
Copy pathMain.cpp
File metadata and controls
741 lines (665 loc) · 19.3 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#include <stdio.h>
#include <string.h>
#include "ncbind.hpp"
#include <map>
#include <vector>
#define BASENAME TJS_W("mem")
/**
* 移植可能なメモリ上バイナリストリーム
* 旧実装は HGLOBAL + CreateStreamOnHGlobal(IStream) + LARGE_INTEGER という
* Windows/COM 依存だったため、NX/PS5 等ではビルドできなかった。
* FileInfo が保持する可変長バッファ (std::vector) を参照して読み書き/シーク
* する自己完結実装に置き換え、windows.h 依存を排除する。
* ストリームはバッファ本体 (vector) への参照のみ保持し、書き込み時の再確保
* でも毎回添字アクセスするため問題ない。ストリーム破棄後もバッファは
* FileInfo 側に残る (旧 CreateStreamOnHGlobal(fDeleteOnRelease=FALSE) と同じ)。
*/
class MemFileStream : public iTJSBinaryStream {
std::vector<unsigned char> &buf;
tjs_uint64 pos;
public:
MemFileStream(std::vector<unsigned char> &b, bool append)
: buf(b), pos(append ? (tjs_uint64)b.size() : 0) {}
virtual ~MemFileStream() {}
tjs_uint64 TJS_INTF_METHOD Seek(tjs_int64 offset, tjs_int whence) {
tjs_int64 np;
switch (whence) {
case TJS_BS_SEEK_SET: np = offset; break;
case TJS_BS_SEEK_CUR: np = (tjs_int64)pos + offset; break;
case TJS_BS_SEEK_END: np = (tjs_int64)buf.size() + offset; break;
default: np = (tjs_int64)pos; break;
}
if (np < 0) np = 0;
pos = (tjs_uint64)np;
return pos;
}
tjs_uint TJS_INTF_METHOD Read(void *buffer, tjs_uint read_size) {
if (pos >= (tjs_uint64)buf.size()) return 0;
tjs_uint64 avail = (tjs_uint64)buf.size() - pos;
tjs_uint n = (read_size < avail) ? read_size : (tjs_uint)avail;
if (n) memcpy(buffer, &buf[(size_t)pos], n);
pos += n;
return n;
}
tjs_uint TJS_INTF_METHOD Write(const void *buffer, tjs_uint write_size) {
if (write_size == 0) return 0;
tjs_uint64 end = pos + write_size;
if (end > (tjs_uint64)buf.size()) buf.resize((size_t)end); // 隙間はゼロ埋め
memcpy(&buf[(size_t)pos], buffer, write_size);
pos += write_size;
return write_size;
}
void TJS_INTF_METHOD SetEndOfStorage() {
buf.resize((size_t)pos);
}
tjs_uint64 TJS_INTF_METHOD GetSize() {
return (tjs_uint64)buf.size();
}
};
/**
* ファイル情報保持用クラス
*/
class FileInfo {
public:
typedef std::map<ttstr,FileInfo*> Directory;
/**
* コンストラクタ
* @param pParent 親ディレクトリ
* @param name 名前
* @param directory ディレクトリなら true
*/
FileInfo(FileInfo *pParent, const ttstr &name, bool directory=false) : pParent(pParent), name(name), pBuffer(0), pDirectory(0) {
if (directory) {
pDirectory = new Directory();
}
}
/**
* デストラクタ
*/
~FileInfo() {
if (pDirectory) {
Directory::iterator it = pDirectory->begin();
while (it != pDirectory->end()) {
delete it->second;
it++;
}
delete pDirectory;
}
if (pBuffer) {
delete pBuffer;
pBuffer = 0;
}
}
// -----------------------------------------------------
// ファイル情報参照
// -----------------------------------------------------
/**
* @return ディレクトリかどうか
*/
bool isDirectory() const {
return pDirectory != NULL;
}
/**
* ファイルをデータとして返す
* @return octetデータ
*/
tTJSVariant getData() const {
tTJSVariant ret;
if (pBuffer && !pBuffer->empty()) {
ret = tTJSVariant((const tjs_uint8*)&(*pBuffer)[0], (tjs_uint)pBuffer->size());
}
return ret;
}
/**
* @return ファイルサイズ
*/
tjs_uint getSize() const {
return pBuffer ? (tjs_uint)pBuffer->size() : 0;
}
/**
* ファイル情報を返す
* @return ファイル情報 %[name:名前, size:サイズ, isDirectory:ディレクトリならtrue]
*/
tTJSVariant getInfo() const {
iTJSDispatch2 *dict = TJSCreateDictionaryObject();
if (dict != NULL) {
tTJSVariant name(this->name);
tTJSVariant size((tjs_int64)getSize());
tTJSVariant isDirectory_var(isDirectory() ? 1 : 0);
dict->PropSet(TJS_MEMBERENSURE, TJS_W("name"), NULL, &name, dict);
dict->PropSet(TJS_MEMBERENSURE, TJS_W("size"), NULL, &size, dict);
dict->PropSet(TJS_MEMBERENSURE, TJS_W("isDirectory"), NULL, &isDirectory_var, dict);
tTJSVariant ret(dict, dict);
dict->Release();
return ret;
}
return tTJSVariant();
}
// -----------------------------------------------------
// ディレクトリ情報
// -----------------------------------------------------
/**
* @return ディレクトリエントリの数
*/
int getDirectoryCount() const {
return pDirectory ? pDirectory->size() : 0;
}
/**
* ディレクトリ情報取得
* @param lister 格納先
*/
void getDirectory(iTVPStorageLister * lister) const {
if (pDirectory) {
Directory::const_iterator it = pDirectory->begin();
while (it != pDirectory->end()) {
if (!it->second->isDirectory()) {
lister->Add(it->first);
}
it++;
}
}
}
/**
* ディレクトリ情報取得
* @param array 格納先
*/
void getDirectory(iTJSDispatch2 *array) const {
if (pDirectory) {
Directory::const_iterator it = pDirectory->begin();
while (it != pDirectory->end()) {
tTJSVariant result = it->second->getInfo(), *param = &result;
array->FuncCall(0, TJS_W("add"), NULL, 0, 1, ¶m, array);
it++;
}
}
}
// -----------------------------------------------------
// ファイル検索
// -----------------------------------------------------
/**
* ファイルまたはファイルを探す
* @param name ファイル名
* @return 検索されたファイル情報
*/
const FileInfo *find(const ttstr &name) const {
if (pDirectory) {
const tjs_char *p = name.c_str();
const tjs_char *q;
if ((q = TJS_strchr(p, '/'))) {
if (q == p) {
return NULL;
}
Directory::const_iterator it = pDirectory->find(ttstr(p, q-p));
if (it != pDirectory->end()) {
FileInfo *info = it->second;
q++;
if (*q) {
if (info->isDirectory()) {
return info->find(ttstr(q));
}
} else {
return info;
}
}
} else {
Directory::const_iterator it = pDirectory->find(name);
if (it != pDirectory->end()) {
return it->second;
}
}
}
return NULL;
}
/**
* ファイルを探す
* @param name ファイル名
* @return 検索されたファイル情報
*/
const FileInfo *findFile(const ttstr &name) const {
if (name.GetLastChar() != '/') {
const FileInfo *file = find(name);
if (file && !file->isDirectory()) {
return file;
}
}
return NULL;
}
/**
* ディレクトリを探す
* @param name ファイル名
* @return 検索されたファイル情報
*/
const FileInfo *findDirectory(const ttstr &name) const {
const FileInfo *file = find(name);
return file && file->isDirectory() ? file : NULL;
}
// -----------------------------------------------------
// ディレクトリ操作
// -----------------------------------------------------
/**
* ファイルを開く
* @param name 相対パスでのファイル名
* @param flags オープンフラグ
* @return 開いたストリーム
*/
iTJSBinaryStream *open(const ttstr &name, tjs_uint32 flags) {
if (pDirectory && name != "" && name.GetLastChar() != '/') {
const tjs_char *p = name.c_str();
const tjs_char *q;
if ((q = TJS_strchr(p, '/'))) {
if (q == p) {
return NULL;
}
ttstr dirname(p, q-p);
Directory::iterator it = pDirectory->find(dirname);
FileInfo *dir = it != pDirectory->end() ? it->second : _mkdir(dirname);
if (dir && dir->isDirectory()) {
return dir->open(ttstr(q+1), flags);
}
} else {
return _open(name, flags);
}
}
return NULL;
}
/**
* 指定されたディレクトリを作成
* @return 作成したエントリ
*/
const FileInfo *mkdir(const ttstr &name) {
if (pDirectory && name != "") {
const tjs_char *p = name.c_str();
const tjs_char *q;
if ((q = TJS_strchr(p, '/'))) {
if (q == p) {
return NULL;
}
ttstr dirname(p, q-p);
Directory::const_iterator it = pDirectory->find(dirname);
FileInfo *dir = it != pDirectory->end() ? it->second : _mkdir(dirname);
if (dir && dir->isDirectory()) {
return dir->mkdir(ttstr(q+1));
}
} else {
return _mkdir(name);
}
}
return NULL;
}
/**
* 指定されたファイルを削除する
* @param name ファイル名
* @return 削除したら true
*/
bool remove(const ttstr &name) {
const FileInfo *file = findFile(name);
if (file) {
file = file->pParent->_remove(name);
if (file) {
delete file;
return true;
}
}
return false;
}
/**
* 指定されたディレクトリを削除する
* @param name ディレクトリ名
* @return 削除したら true
*/
bool rmdir(const ttstr &name) {
const FileInfo *dir = findDirectory(name);
if (dir && dir->getDirectoryCount() == 0) {
dir = dir->pParent->_remove(name);
if (dir) {
delete dir;
return true;
}
}
return false;
}
protected:
// -----------------------------------------------------
// ファイル用処理
// -----------------------------------------------------
/**
* @return ファイルストリームを返す
* @param append 末尾追記モード (現在位置を末尾に置く)
*/
iTJSBinaryStream *getStream(bool append) {
if (!pDirectory) {
if (pBuffer == 0) {
pBuffer = new std::vector<unsigned char>();
}
return new MemFileStream(*pBuffer, append);
}
return NULL;
}
// -----------------------------------------------------
// ディレクトリ用処理
// -----------------------------------------------------
/**
* ファイルを開く
* @param name ファイル名
* @param flags
* @return ストリーム
*/
iTJSBinaryStream *_open(const ttstr &name, tjs_uint32 flags) {
if (pDirectory) {
bool append = (flags == TJS_BS_APPEND);
Directory::const_iterator it = pDirectory->find(name);
if (it != pDirectory->end()) {
return it->second->getStream(append);
} else {
if (flags == TJS_BS_WRITE) {
FileInfo *file = new FileInfo(this, name);
if (file) {
(*pDirectory)[name] = file;
return file->getStream(false);
}
}
}
}
return NULL;
}
/**
* ディレクトリエントリの作成
* @param name ファイル名
* @return 成功したらそのエントリ
*/
FileInfo *_mkdir(const ttstr &name) {
FileInfo *info = NULL;
if (pDirectory) {
Directory::const_iterator it = pDirectory->find(name);
if (it == pDirectory->end()) {
info = new FileInfo(this, name, true);
(*pDirectory)[name] = info;
}
}
return info;
}
/**
* ファイル情報の削除
* @param name 名前
*/
const FileInfo *_remove(const ttstr &name) {
FileInfo *info = NULL;
if (pDirectory) {
Directory::iterator it = pDirectory->find(name);
if (it != pDirectory->end()) {
info = it->second;
pDirectory->erase(it);
}
}
return info;
}
private:
FileInfo *pParent;
ttstr name;
std::vector<unsigned char> *pBuffer;
Directory *pDirectory;
};
class MemStorage : public iTVPStorageMedia
{
public:
MemStorage() : refCount(1), root(NULL, ttstr("root"), true) {}
~MemStorage() {;}
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
}
void getFilename(const ttstr &name, ttstr &dname, ttstr &fname) {
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);
if (dname != TJS_W(".")) {
TVPThrowExceptionMessage(TJS_W("no such domain:%1"), dname);
}
} else {
TVPThrowExceptionMessage(TJS_W("invalid path:%1"), name);
}
}
// check file existence
virtual bool TJS_INTF_METHOD CheckExistentStorage(const ttstr &name) {
ttstr dname, fname;
getFilename(name, dname, fname);
return root.findFile(fname) != NULL;
}
// 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) {
ttstr dname, fname;
getFilename(name, dname, fname);
iTJSBinaryStream *ret = root.open(fname, flags);
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) {
ttstr dname, fname;
getFilename(name, dname, fname);
const FileInfo *directory = root.findDirectory(fname);
if (directory) {
directory->getDirectory(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:
/**
* 指定されたディレクトリを作成
* @return 作成したら true
*/
bool mkdir(const ttstr &name) {
return root.mkdir(name) != NULL;
}
/**
* 指定されたファイルの存在確認
* @param name ファイル名
* @return 存在したら true
*/
bool isExistFile(const ttstr &name) {
return root.findFile(name) != NULL;
}
/**
* 指定されたファイルの存在確認
* @param name ファイル名
* @return 存在したら true
*/
bool isExistDirectory(const ttstr &name) {
return root.findDirectory(name) != NULL;
}
/**
* 指定されたファイルを削除する
* @param name ファイル名
* @return 削除したら true
*/
bool remove(const ttstr &name) {
return root.remove(name);
}
/**
* 指定されたディレクトリを削除する
* @param name ディレクトリ名
* @return 削除したら true
*/
bool rmdir(const ttstr &name) {
return root.rmdir(name);
}
/**
* 指定されたファイルの内容を octet で返す
* @param name ファイル名
* @return ファイル情報 %[name:名前, size:サイズ, isDirectory:ディレクトリならtrue]
*/
tTJSVariant getInfo(const ttstr &name) {
const FileInfo *file = root.findFile(name);
if (file) {
return file->getInfo();
}
return tTJSVariant();
}
/**
* 指定されたファイルの内容を octet で返す
* @param name ファイル名
* @return データ
*/
tTJSVariant getData(const ttstr &name) {
const FileInfo *file = root.findFile(name);
if (file) {
return file->getData();
}
return tTJSVariant();
}
/**
* 指定されたディレクトリのファイル名一覧を取得
* @param name ディレクトリ名
* @return ファイル情報の配列 %[name:名前, size:サイズ, isDirectory:ディレクトリならtrue]
*/
tTJSVariant getDirectory(const ttstr &name) {
const FileInfo *dir = root.findDirectory(name);
if (dir) {
iTJSDispatch2 *array = TJSCreateArrayObject();
if (array) {
dir->getDirectory(array);
tTJSVariant ret(array, array);
array->Release();
return ret;
}
}
return tTJSVariant();
}
private:
tjs_uint refCount;
FileInfo root;
};
/**
* メソッド追加用
*/
class StoragesMemFile {
public:
static void initMemoryFile() {
if (mem == NULL) {
mem = new MemStorage();
TVPRegisterStorageMedia(mem);
}
}
static void doneMemoryFile() {
if (mem != NULL) {
TVPUnregisterStorageMedia(mem);
mem->Release();
mem = NULL;
}
}
/**
* メモリファイルの存在確認
* @param filename 対象ファイル名 (mem:///はつけない)
* @return 存在したら true
*/
static bool isExistMemoryFile(ttstr filename) {
return mem && mem->isExistFile(filename);
}
/**
* メモリディレクトリの存在確認
* @param dirname 対象ディレクトリ名 (mem:///はつけない)
* @return 存在したら true
*/
static bool isExistMemoryDirectory(ttstr filename) {
return mem && mem->isExistDirectory(filename);
}
/**
* メモリファイルを削除する
* @param filename 対象ファイル名 (mem:///はつけない)
* @return ファイルが削除されたら true
*/
static bool deleteMemoryFile(ttstr filename) {
return mem && mem->remove(filename);
}
/**
* メモリディレクトリを削除する
* @param dirname 対象ディレクトリ名 (mem:///はつけない)
* @return ディレクトリが削除されたら true
*/
static bool deleteMemoryDirectory(ttstr dirname) {
return mem && mem->rmdir(dirname);
}
/**
* メモリファイル情報を取得する
* @param filename 対象ファイル名 (mem:///はつけない)
* @return ファイル情報 %[name:名前, size:サイズ, isDirectory:ディレクトリならtrue]
*/
static tTJSVariant getMemoryFileInfo(ttstr filename) {
return mem ? mem->getInfo(filename) : (tTJSVariant)NULL;
}
/**
* メモリファイル情報を取得する
* @param filename 対象ファイル名 (mem:///はつけない)
* @return ファイルが存在したら内容を octet で返す。なければ void
*/
static tTJSVariant getMemoryFileData(ttstr filename) {
return mem ? mem->getData(filename) : (tTJSVariant)NULL;
}
/**
* メモリディレクトリ情報を取得する
* @param dirname 対象ディレクトリ名 (mem:///はつけない)
* @return ファイル情報の配列 %[name:名前, size:サイズ, isDirectory:ディレクトリならtrue]
*/
static tTJSVariant getMemoryDirectory(ttstr dirname) {
return mem ? mem->getDirectory(dirname) : (tTJSVariant)NULL;
}
protected:
static MemStorage *mem;
};
MemStorage *StoragesMemFile::mem = NULL;
NCB_ATTACH_CLASS(StoragesMemFile, Storages) {
NCB_METHOD(isExistMemoryFile);
NCB_METHOD(isExistMemoryDirectory);
NCB_METHOD(deleteMemoryFile);
NCB_METHOD(deleteMemoryDirectory);
NCB_METHOD(getMemoryFileInfo);
NCB_METHOD(getMemoryFileData);
NCB_METHOD(getMemoryDirectory);
};
/**
* 開放処理後
*/
static void PreRegistCallback()
{
StoragesMemFile::initMemoryFile();
}
/**
* 開放処理後
*/
static void PostUnregistCallback()
{
StoragesMemFile::doneMemoryFile();
}
NCB_PRE_REGIST_CALLBACK(PreRegistCallback);
NCB_POST_UNREGIST_CALLBACK(PostUnregistCallback);