2020#include < io.h>
2121#include < direct.h>
2222#include < windows.h>
23- #include < winbase.h> /* needed for memory mapping of file functions */
2423#endif
2524
2625#ifdef SCP_UNIX
2726#include < glob.h>
28- #include < sys/mman.h>
2927#endif
3028
3129#include " cfile/cfile.h"
@@ -108,12 +106,6 @@ static CFILE *cf_open_fill_cfblock(const char* source, int line, const char* ori
108106static CFILE *cf_open_packed_cfblock (const char * source, int line, const char * original_filename, FILE *fp, int type, size_t offset, size_t size);
109107static CFILE *cf_open_memory_fill_cfblock (const char * source, int line, const char * original_filename, const void * data, size_t size, int dir_type);
110108
111- #if defined _WIN32
112- static CFILE *cf_open_mapped_fill_cfblock (const char * source, int line, const char * original_filename, HANDLE hFile, int type);
113- #elif defined SCP_UNIX
114- static CFILE *cf_open_mapped_fill_cfblock (const char * source, int line, const char * original_filename, FILE *fp, int type);
115- #endif
116-
117109static void cf_chksum_long_init ();
118110
119111static void dump_opened_files ()
@@ -632,18 +624,13 @@ void cf_create_directory(int dir_type, uint32_t location_flags)
632624// parameters: *filepath ==> name of file to open (may be path+name)
633625// *mode ==> specifies how file should be opened (eg "rb" for read binary)
634626// passing NULL to mode triggers an assert and returns NULL
635- // type ==> one of: CFILE_NORMAL
636- // CFILE_MEMORY_MAPPED
637627// dir_type => override extension check, value is one of CF_TYPE* #defines
638628//
639- // NOTE: type parameter is an optional parameter. The default value is CFILE_NORMAL
640- //
641- //
642629// returns: success ==> address of CFILE structure
643630// error ==> NULL
644631//
645632
646- CFILE* _cfopen (const char * source, int line, const char * file_path, const char * mode, int type, int dir_type,
633+ CFILE* _cfopen (const char * source, int line, const char * file_path, const char * mode, int dir_type,
647634 bool /* localize */ , uint32_t location_flags)
648635{
649636 /* Bobboau, what is this doing here? 31 is way too short... - Goober5000
@@ -659,12 +646,6 @@ CFILE* _cfopen(const char* source, int line, const char* file_path, const char*
659646 // Check that all the parameters make sense
660647 Assert (file_path && strlen (file_path));
661648 Assert ( mode != NULL );
662-
663- // Can only open read-only binary files in memory mapped mode.
664- if ( (type & CFILE_MEMORY_MAPPED) && strcmp (mode," rb" ) != 0 ) {
665- Int3 ();
666- return NULL ;
667- }
668649
669650 // ===========================================================
670651 // If in write mode, just try to open the file straight off
@@ -689,7 +670,6 @@ CFILE* _cfopen(const char* source, int line, const char* file_path, const char*
689670
690671 cf_create_default_path_string (longname, dir_type, file_path, location_flags);
691672 }
692- Assert ( !(type & CFILE_MEMORY_MAPPED) );
693673
694674 // JOHN: TODO, you should create the path if it doesn't exist.
695675
@@ -740,35 +720,11 @@ CFILE* _cfopen(const char* source, int line, const char* file_path, const char*
740720 auto find_res = cf_find_file_location (file_path, dir_type, location_flags);
741721
742722 if ( find_res.found ) {
743-
744723 // Fount it, now create a cfile out of it
745724 nprintf ((" CFileDebug" , " Requested file %s found at: %s\n " , file_path, find_res.full_name .c_str ()));
746725
747- if ( type & CFILE_MEMORY_MAPPED ) {
748-
749- // Can't open memory mapped files out of pack or memory files
750- if ( find_res.offset == 0 && find_res.data_ptr != nullptr ) {
751- #if defined _WIN32
752- HANDLE hFile;
753-
754- hFile = CreateFile (find_res.full_name .c_str (), GENERIC_READ, FILE_SHARE_READ, NULL , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
755-
756- if (hFile != INVALID_HANDLE_VALUE) {
757- return cf_open_mapped_fill_cfblock (source, line, file_path, hFile, dir_type);
758- }
759- #elif defined SCP_UNIX
760- FILE* fp = fopen (find_res.full_name .c_str (), " rb" );
761- if (fp) {
762- return cf_open_mapped_fill_cfblock (source, line, file_path, fp, dir_type);
763- }
764- #endif
765- }
766-
767- } else {
768- // since cfopen_special already has all the code to handle the opening we can just use that here
769- return _cfopen_special (source, line, find_res, mode, dir_type);
770- }
771-
726+ // since cfopen_special already has all the code to handle the opening we can just use that here
727+ return _cfopen_special (source, line, find_res, mode, dir_type);
772728 }
773729
774730 return NULL ;
@@ -893,27 +849,7 @@ int cfclose( CFILE * cfile )
893849 Assert (cfile != NULL );
894850
895851 result = 0 ;
896- if ( cfile->data && cfile->mem_mapped ) {
897- // close memory mapped file
898- #if defined _WIN32
899- result = UnmapViewOfFile ((void *)cfile->data );
900- Assert (result);
901- result = CloseHandle (cfile->hInFile );
902- Assert (result); // Ensure file handle is closed properly
903- result = CloseHandle (cfile->hMapFile );
904- Assert (result); // Ensure file handle is closed properly
905- result = 0 ;
906- #elif defined SCP_UNIX
907- // FIXME: result is wrong after munmap() but it is successful
908- // result = munmap(cfile->data, cfile->data_length);
909- // Assert(result);
910- // This const_cast is safe since the pointer returned by mmap was also non-const
911- munmap (const_cast <void *>(cfile->data ), cfile->data_length );
912- if ( cfile->fp != nullptr )
913- result = fclose (cfile->fp );
914- #endif
915-
916- } else if ( cfile->fp != nullptr ) {
852+ if ( cfile->fp != nullptr ) {
917853 Assert (cfile->fp != nullptr );
918854 result = fclose (cfile->fp );
919855 } else {
@@ -958,7 +894,6 @@ static CFILE *cf_open_fill_cfblock(const char* source, int line, const char* ori
958894 } else {
959895 CFILE *cfp = &Cfile_block_list[cfile_block_index];
960896 cfp->data = nullptr ;
961- cfp->mem_mapped = false ;
962897 cfp->fp = fp;
963898 cfp->dir_type = type;
964899 cfp->max_read_len = 0 ;
@@ -997,7 +932,6 @@ static CFILE *cf_open_packed_cfblock(const char* source, int line, const char* o
997932
998933 cfp->data = nullptr ;
999934 cfp->fp = fp;
1000- cfp->mem_mapped = false ;
1001935 cfp->dir_type = type;
1002936 cfp->max_read_len = 0 ;
1003937
@@ -1012,70 +946,6 @@ static CFILE *cf_open_packed_cfblock(const char* source, int line, const char* o
1012946
1013947}
1014948
1015-
1016-
1017- // cf_open_mapped_fill_cfblock() will fill up a Cfile_block element in the Cfile_block_list[] array
1018- // for the case of a file being opened by cf_open_mapped();
1019- //
1020- // returns: ptr CFILE structure.
1021- //
1022- #if defined _WIN32
1023- static CFILE *cf_open_mapped_fill_cfblock (const char * source, int line, const char * original_filename, HANDLE hFile, int type)
1024- #elif defined SCP_UNIX
1025- static CFILE *cf_open_mapped_fill_cfblock (const char * source, int line, const char * original_filename, FILE *fp, int type)
1026- #endif
1027- {
1028- int cfile_block_index;
1029-
1030- cfile_block_index = cfget_cfile_block ();
1031- if ( cfile_block_index == -1 ) {
1032- #ifdef SCP_UNIX
1033- fclose (fp);
1034- #endif
1035- return NULL ;
1036- }
1037- else {
1038- CFILE *cfp = &Cfile_block_list[cfile_block_index];
1039-
1040- cfp->max_read_len = 0 ;
1041- cfp->fp = nullptr ;
1042- cfp->mem_mapped = true ;
1043- #if defined _WIN32
1044- cfp->hInFile = hFile;
1045- #endif
1046- cfp->dir_type = type;
1047-
1048- cfp->original_filename = original_filename;
1049- cfp->source_file = source;
1050- cfp->line_num = line;
1051-
1052- cf_init_lowlevel_read_code (cfp, 0 , 0 , 0 );
1053-
1054- #if defined _WIN32
1055- cfp->hMapFile = CreateFileMapping (cfp->hInFile , NULL , PAGE_READONLY, 0 , 0 , NULL );
1056- if (cfp->hMapFile == NULL ) {
1057- nprintf ((" Error" , " Could not create file-mapping object.\n " ));
1058- return NULL ;
1059- }
1060-
1061- cfp->data = (ubyte*)MapViewOfFile (cfp->hMapFile , FILE_MAP_READ, 0 , 0 , 0 );
1062- Assert ( cfp->data != NULL );
1063- #elif defined SCP_UNIX
1064- cfp->fp = fp;
1065- cfp->data_length = filelength (fileno (fp));
1066- cfp->data = mmap (nullptr , // start
1067- cfp->data_length , // length
1068- PROT_READ, // prot
1069- MAP_SHARED, // flags
1070- fileno (fp), // fd
1071- 0 ); // offset
1072- Assert (cfp->data != nullptr );
1073- #endif
1074-
1075- return cfp;
1076- }
1077- }
1078-
1079949static CFILE *cf_open_memory_fill_cfblock (const char * source, int line, const char * original_filename, const void * data, size_t size, int dir_type)
1080950{
1081951 int cfile_block_index;
@@ -1089,7 +959,6 @@ static CFILE *cf_open_memory_fill_cfblock(const char* source, int line, const ch
1089959
1090960 cfp->max_read_len = 0 ;
1091961 cfp->fp = nullptr ;
1092- cfp->mem_mapped = false ;
1093962 cfp->dir_type = dir_type;
1094963
1095964 cfp->original_filename = original_filename;
@@ -1333,9 +1202,6 @@ int cfwrite_string_len(const char *buf, CFILE *file)
13331202int cfilelength (CFILE* cfile) {
13341203 Assert (cfile != NULL );
13351204
1336- // TODO: return length of memory mapped file
1337- Assert (!cfile->mem_mapped );
1338-
13391205 // cfile->size gets set at cfopen
13401206
13411207 // The rest of the code still uses ints, do an overflow check to detect cases where this fails
@@ -1728,7 +1594,7 @@ int cf_chksum_short(const char *filename, ushort *chksum, int max_size, int cf_t
17281594 *chksum = 0 ;
17291595
17301596 // attempt to open the file
1731- cfile = cfopen (filename," rt" ,CFILE_NORMAL, cf_type);
1597+ cfile = cfopen (filename," rt" ,cf_type);
17321598 if (cfile == NULL ){
17331599 return 0 ;
17341600 }
@@ -1778,7 +1644,7 @@ int cf_chksum_long(const char *filename, uint *chksum, int max_size, int cf_type
17781644 *chksum = 0 ;
17791645
17801646 // attempt to open the file
1781- cfile = cfopen (filename," rt" ,CFILE_NORMAL, cf_type);
1647+ cfile = cfopen (filename," rt" ,cf_type);
17821648 if (cfile == NULL ){
17831649 return 0 ;
17841650 }
0 commit comments