comparison system/file.cc @ 53:ddbcbd000206

* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons * ParseMoji moved to TextStream * Some cleaning (0 -> NULL when needed, removal of useless returns, ...)
author thib
date Sun, 19 Apr 2009 11:44:05 +0000
parents 15a18fbe6f21
children 4416cfac86ae
comparison
equal deleted inserted replaced
52:15a18fbe6f21 53:ddbcbd000206
81 #include "file.h" 81 #include "file.h"
82 #include "file_impl.h" 82 #include "file_impl.h"
83 83
84 using namespace std; 84 using namespace std;
85 85
86 FILESEARCH file_searcher;
87 KEYHOLDER key_holder;
88 // #define delete fprintf(stderr,"file.cc: %d.",__LINE__), delete 86 // #define delete fprintf(stderr,"file.cc: %d.",__LINE__), delete
89 87
90 /* FILESEARCH class の default の振る舞い */ 88 /* FileSearcher class の default の振る舞い */
91 FILESEARCH::ARCTYPE FILESEARCH::default_is_archived[TYPEMAX] = { 89 FileSearcher *FileSearcher::_singleton = NULL;
90 FileSearcher::ARCTYPE FileSearcher::default_is_archived[TYPEMAX] = {
92 ATYPE_DIR, ATYPE_DIR, ATYPE_DIR, ATYPE_DIR, 91 ATYPE_DIR, ATYPE_DIR, ATYPE_DIR, ATYPE_DIR,
93 ATYPE_ARC, ATYPE_ARC, ATYPE_ARC, ATYPE_ARC, 92 ATYPE_ARC, ATYPE_ARC, ATYPE_ARC, ATYPE_ARC,
94 ATYPE_DIR, ATYPE_DIR, ATYPE_DIR, ATYPE_DIR, 93 ATYPE_DIR, ATYPE_DIR, ATYPE_DIR, ATYPE_DIR,
95 ATYPE_DIR, ATYPE_DIR 94 ATYPE_DIR, ATYPE_DIR
96 }; 95 };
97 const char* FILESEARCH::default_dirnames[TYPEMAX] = { 96 const char* FileSearcher::default_dirnames[TYPEMAX] = {
98 0, 0, "", "pdt", 97 0, 0, "", "pdt",
99 "seen.txt", "allanm.anl", "allard.ard", "allcur.cur", 98 "seen.txt", "allanm.anl", "allard.ard", "allcur.cur",
100 0, 0, "koe", "bgm", "mov", "gan"}; 99 0, 0, "koe", "bgm", "mov", "gan"};
101 100
102 /********************************************* 101 /*********************************************
113 /* 変数初期化 */ 112 /* 変数初期化 */
114 arcname = NULL; 113 arcname = NULL;
115 list_point = 0; 114 list_point = 0;
116 filenames_orig = NULL; 115 filenames_orig = NULL;
117 next = NULL; 116 next = NULL;
118 if (aname[0] == '\0') {arcname=new char[1]; arcname[0]='\0';return;} // NULFILE 117 if (aname[0] == '\0') {
118 arcname = new char[1];
119 arcname[0]='\0';
120 return;
121 } // NULFILE
119 /* ディレクトリか否かのチェック */ 122 /* ディレクトリか否かのチェック */
120 if (stat(aname,&sb) == -1) { /* error */ 123 if (stat(aname,&sb) == -1) { /* error */
121 perror("stat"); 124 perror("stat");
122 } 125 }
123 if ( (sb.st_mode&S_IFMT) == S_IFDIR) { 126 if ( (sb.st_mode&S_IFMT) == S_IFDIR) {
534 delete[] buf; 537 delete[] buf;
535 fclose(stream); 538 fclose(stream);
536 } 539 }
537 540
538 /******************************************************** 541 /********************************************************
539 ** KEYHOLDER 542 ** KeyHolder
540 */ 543 */
541 void KEYHOLDER::SetKey(char new_key[16]) 544
545 KeyHolder* KeyHolder::_singleton = NULL;
546
547 KeyHolder* KeyHolder::GetInstance(void)
548 {
549 if (_singleton == NULL) _singleton = new KeyHolder;
550 return _singleton;
551 }
552
553 void KeyHolder::Quit(void)
554 {
555 if (_singleton != NULL) {
556 delete _singleton;
557 _singleton = NULL;
558 }
559 }
560
561 void KeyHolder::SetKey(char new_key[16])
542 { 562 {
543 unsigned short int i; 563 unsigned short int i;
544 for (i=0; i < 16; i++) 564 for (i=0; i < 16; i++)
545 key[i] = new_key[i]; 565 key[i] = new_key[i];
546 } 566 }
547 567
548 void KEYHOLDER::SetKey2(char new_key[33]) 568 void KeyHolder::SetKey2(char new_key[33])
549 { 569 {
550 unsigned short int i; 570 unsigned short int i;
551 char tmp[3]; 571 char tmp[3];
552 tmp[2] = '\0'; 572 tmp[2] = '\0';
553 for (i=0; i < 16; i++) { 573 for (i=0; i < 16; i++) {
555 tmp[1] = new_key[i*2+1]; 575 tmp[1] = new_key[i*2+1];
556 key[i] = strtoul(tmp, NULL, 16); 576 key[i] = strtoul(tmp, NULL, 16);
557 } 577 }
558 } 578 }
559 579
560 void KEYHOLDER::GuessKey(char *regname) 580 void KeyHolder::GuessKey(char *regname)
561 { 581 {
562 char key1[16] = { 582 char key1[16] = {
563 0xa8, 0x28, 0xfd, 0x66, 583 0xa8, 0x28, 0xfd, 0x66,
564 0xa0, 0x23, 0x77, 0x69, 584 0xa0, 0x23, 0x77, 0x69,
565 0xf9, 0x45, 0xf8, 0x2c, 585 0xf9, 0x45, 0xf8, 0x2c,
580 else { 600 else {
581 SetKey(key1); 601 SetKey(key1);
582 } 602 }
583 } 603 }
584 604
585 const char * KEYHOLDER::GetKey(void) 605 const char * KeyHolder::GetKey(void)
586 { 606 {
587 return key; 607 return key;
588 } 608 }
589 609
590 /******************************************************** 610 /********************************************************
591 ** FILESEARCH クラスの実装 611 ** FileSearcher クラスの実装
592 */ 612 */
593 613
594 FILESEARCH::FILESEARCH(void) { 614 FileSearcher* FileSearcher::GetInstance(void) {
615 if (_singleton == NULL) _singleton = new FileSearcher;
616 return _singleton;
617 }
618
619 void FileSearcher::Quit(void) {
620 if (_singleton != NULL) {
621 delete _singleton;
622 _singleton = NULL;
623 }
624 }
625
626 FileSearcher::FileSearcher(void) {
595 int i; 627 int i;
596 root_dir = NULL; 628 root_dir = NULL;
597 dat_dir = NULL; 629 dat_dir = NULL;
598 for (i=0; i<TYPEMAX; i++) { 630 for (i=0; i<TYPEMAX; i++) {
599 searcher[i] = NULL; 631 searcher[i] = NULL;
600 filenames[i] = default_dirnames[i]; 632 filenames[i] = default_dirnames[i];
601 is_archived[i] = default_is_archived[i]; 633 is_archived[i] = default_is_archived[i];
602 } 634 }
603 } 635 }
604 FILESEARCH::~FILESEARCH(void) { 636
637 FileSearcher::~FileSearcher(void) {
605 int i; 638 int i;
606 for (i=0; i<TYPEMAX; i++) { 639 for (i=0; i<TYPEMAX; i++) {
607 if (filenames[i] != NULL && filenames[i] != default_dirnames[i]) delete[] filenames[i]; 640 if (filenames[i] != NULL && filenames[i] != default_dirnames[i]) delete[] filenames[i];
608 if (searcher[i] && searcher[i] != dat_dir && searcher[i] != root_dir) { 641 if (searcher[i] && searcher[i] != dat_dir && searcher[i] != root_dir) {
609 delete searcher[i]; 642 delete searcher[i];
611 } 644 }
612 if (dat_dir && dat_dir != root_dir) delete dat_dir; 645 if (dat_dir && dat_dir != root_dir) delete dat_dir;
613 if (root_dir) delete root_dir; 646 if (root_dir) delete root_dir;
614 } 647 }
615 648
616 int FILESEARCH::InitRoot(char* root) { 649 int FileSearcher::InitRoot(char* root) {
617 /* 必要に応じて ~/ を展開 */ 650 /* 必要に応じて ~/ を展開 */
618 if (root[0] == '~' && root[1] == '/') { 651 if (root[0] == '~' && root[1] == '/') {
619 char* home = getenv("HOME"); 652 char* home = getenv("HOME");
620 if (home != NULL) { 653 if (home != NULL) {
621 char* new_root = new char[strlen(home)+strlen(root)]; 654 char* new_root = new char[strlen(home)+strlen(root)];
654 searcher[ALL] = dat_dir; 687 searcher[ALL] = dat_dir;
655 searcher[ROOT] = root_dir; 688 searcher[ROOT] = root_dir;
656 return 0; 689 return 0;
657 } 690 }
658 691
659 void FILESEARCH::SetFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) { 692 void FileSearcher::SetFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) {
660 int type = tp; 693 int type = tp;
661 if (type < 0 || type >= TYPEMAX) return; 694 if (type < 0 || type >= TYPEMAX) return;
662 ARCFILE* next_arc = NULL; 695 ARCFILE* next_arc = NULL;
663 /* すでに searcher が存在すれば解放 */ 696 /* すでに searcher が存在すれば解放 */
664 if (searcher[type] != NULL && 697 if (searcher[type] != NULL &&
676 searcher[type] = MakeARCFILE(is_arc, filename); 709 searcher[type] = MakeARCFILE(is_arc, filename);
677 if (searcher[type] != NULL && next_arc) 710 if (searcher[type] != NULL && next_arc)
678 searcher[type]->SetNext(next_arc); 711 searcher[type]->SetNext(next_arc);
679 } 712 }
680 713
681 void FILESEARCH::AppendFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) { 714 void FileSearcher::AppendFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) {
682 int type = tp; 715 int type = tp;
683 if (type < 0 || type >= TYPEMAX) return; 716 if (type < 0 || type >= TYPEMAX) return;
684 /* searcher がまだ割り当てられてない場合 */ 717 /* searcher がまだ割り当てられてない場合 */
685 if (searcher[type] == NULL || 718 if (searcher[type] == NULL ||
686 searcher[type] == root_dir || 719 searcher[type] == root_dir ||
698 ARCFILE* cur; 731 ARCFILE* cur;
699 for (cur=searcher[type]; cur->Next() != NULL; cur = cur->Next()) ; 732 for (cur=searcher[type]; cur->Next() != NULL; cur = cur->Next()) ;
700 cur->SetNext(arc); 733 cur->SetNext(arc);
701 } 734 }
702 735
703 ARCFILE* FILESEARCH::MakeARCFILE(ARCTYPE tp, const char* filename) { 736 ARCFILE* FileSearcher::MakeARCFILE(ARCTYPE tp, const char* filename) {
704 ARCFILE* arc = NULL; 737 ARCFILE* arc = NULL;
705 char* file; 738 char* file;
706 if (filename == NULL) goto err; 739 if (filename == NULL) goto err;
707 if (tp == ATYPE_DIR) { 740 if (tp == ATYPE_DIR) {
708 file = root_dir->SearchFile(filename); 741 file = root_dir->SearchFile(filename);
725 char magic_raf[8] = {'C','A','P','F',1,0,0,0}; 758 char magic_raf[8] = {'C','A','P','F',1,0,0,0};
726 if (strncmp(header, "PACL", 4) == 0) arc = new ARCFILE(file); 759 if (strncmp(header, "PACL", 4) == 0) arc = new ARCFILE(file);
727 else arc = new SCN2kFILE(file); 760 else arc = new SCN2kFILE(file);
728 } 761 }
729 break; 762 break;
730 default: fprintf(stderr,"FILESEARCH::MAKEARCFILE : invalid archive type; type %d name %s\n",tp,filename); 763 default: fprintf(stderr,"FileSearcher::MAKEARCFILE : invalid archive type; type %d name %s\n",tp,filename);
731 delete[] file; 764 delete[] file;
732 goto err; 765 goto err;
733 } 766 }
734 delete[] file; 767 delete[] file;
735 return arc; 768 return arc;
736 err: 769 err:
737 arc = new NULFILE; 770 arc = new NULFILE;
738 return arc; 771 return arc;
739 } 772 }
740 773
741 ARCINFO* FILESEARCH::Find(FILETYPE type, const char* fname, const char* ext) { 774 ARCINFO* FileSearcher::Find(FILETYPE type, const char* fname, const char* ext) {
742 if (searcher[type] == NULL) { 775 if (searcher[type] == NULL) {
743 /* searcher 作成 */ 776 /* searcher 作成 */
744 if (filenames[type] == NULL) { 777 if (filenames[type] == NULL) {
745 searcher[type] = dat_dir; 778 searcher[type] = dat_dir;
746 } else { 779 } else {
747 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]); 780 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]);
748 if (searcher[type] == NULL) { 781 if (searcher[type] == NULL) {
749 fprintf(stderr,"FILESEARCH::Find : invalid archive type; type %d name %s\n",type,fname); 782 fprintf(stderr,"FileSearcher::Find : invalid archive type; type %d name %s\n",type,fname);
750 return NULL; 783 return NULL;
751 } 784 }
752 } 785 }
753 } 786 }
754 return searcher[type]->Find(fname,ext); 787 return searcher[type]->Find(fname,ext);
755 } 788 }
756 789
757 char** FILESEARCH::ListAll(FILETYPE type) { 790 char** FileSearcher::ListAll(FILETYPE type) {
758 /* とりあえず searcher を初期化 */ 791 /* とりあえず searcher を初期化 */
759 Find(type, "THIS FILENAME MAY NOT EXIST IN THE FILE SYSTEM !!!"); 792 Find(type, "THIS FILENAME MAY NOT EXIST IN THE FILE SYSTEM !!!");
760 if (searcher[type] == NULL) return NULL; 793 if (searcher[type] == NULL) return NULL;
761 /* 全ファイルのリストアップ */ 794 /* 全ファイルのリストアップ */
762 int deal = 0; 795 int deal = 0;
1501 d = ret_data + info.private_data; 1534 d = ret_data + info.private_data;
1502 dend = ret_data + info.filesize; 1535 dend = ret_data + info.filesize;
1503 while(lzExtract(Extract_DataType_SCN2k(), char(), s, d, send, dend)) ; 1536 while(lzExtract(Extract_DataType_SCN2k(), char(), s, d, send, dend)) ;
1504 } 1537 }
1505 if (read_little_endian_int(data+4) == 0x1adb2) { // Little Busters! 1538 if (read_little_endian_int(data+4) == 0x1adb2) { // Little Busters!
1506 const char *decode_key = key_holder.GetKey(); 1539 const char *decode_key = KeyHolder::GetInstance()->GetKey();
1507 int header_size = info.private_data; 1540 int header_size = info.private_data;
1508 for (i=0x100; i<=0x200 && header_size+i < info.filesize; i++) { 1541 for (i=0x100; i<=0x200 && header_size+i < info.filesize; i++) {
1509 ret_data[header_size+i] ^= decode_key[i&0x0f]; 1542 ret_data[header_size+i] ^= decode_key[i&0x0f];
1510 } 1543 }
1511 } 1544 }