// Cystem.File.h #pragma once #pragma unmanaged #include #include #include class File { public: File() { filestream = new std::ifstream(); } bool Open(std::string filename) { filestream->open(filename.c_str(), std::ios::in | std::ios::binary); return filestream->is_open(); } void ReadStruct(char* buffer, long count) { filestream->read(buffer, count); } void ReadStruct(char* buffer, long startIndex, long count) { filestream->seekg(startIndex); filestream->read(buffer, count); } ~File() { filestream->close(); } std::ifstream* filestream; }; #pragma managed using namespace System; //using namespace Cystem; #include "StringConvertor.h" namespace Cystem { namespace IO { public ref class FileReader { public: typedef std::pair FilePair; typedef std::map FileMap; FileReader() { _files = new std::map(); } ~FileReader() { for(FileMap::const_iterator i = _files->begin(); i != _files->end(); ++i) { delete i->second; } _files->clear(); } bool OpenFile(System::String^ fileName, System::String^ alias) { StringUtilities::StringConvertor tempfile(fileName); StringUtilities::StringConvertor tempalias(alias); std::string filename = tempfile.STLAnsiString; std::string _alias = tempalias.STLAnsiString; File* file = new File(); _files->insert(FilePair(_alias, file)); return file->Open(filename); } generic where T : value class void ReadStruct(System::String^ alias, T% buffer, long size, System::Type^ type) { StringUtilities::StringConvertor tempfile(alias); std::string filename = tempfile.STLAnsiString; array^ tempbuf = gcnew array(size); pin_ptr buf = &tempbuf[0]; _files->find(filename)->second->ReadStruct(reinterpret_cast(buf), size); System::IntPtr _buffer = Marshal::AllocHGlobal(size); Marshal::Copy(tempbuf, 0, _buffer, size); buffer = (T)Marshal::PtrToStructure(_buffer, type); Marshal::FreeHGlobal(_buffer); } generic void ReadStruct(System::String^ alias, T% buffer, long startIndex, long size, System::Type^ type) { StringUtilities::StringConvertor tempfile(alias); std::string filename = tempfile.STLAnsiString; array^ tempbuf = gcnew array(size); pin_ptr buf = &tempbuf[0]; _files->find(filename)->second->ReadStruct(reinterpret_cast(buf), startIndex, size); System::IntPtr _buffer = Marshal::AllocHGlobal(size); Marshal::Copy(tempbuf, 0, _buffer, size); buffer = (T)Marshal::PtrToStructure(_buffer, type); Marshal::FreeHGlobal(_buffer); } void CloseFile(System::String^ alias) { StringUtilities::StringConvertor tempfile(alias); std::string filename = tempfile.STLAnsiString; std::map::iterator fileIter; fileIter = _files->find(filename); delete fileIter->second; _files->erase(fileIter); } private: std::map* _files; }; using namespace System::Threading; public ref class FilesystemManager sealed abstract { private: static FileReader^ _reader; static System::Object^ _lockObject; public: static FilesystemManager() { _reader = gcnew FileReader(); _lockObject = gcnew System::Object(); } static bool OpenFile(System::String^ fileName, bool readOnly) { readOnly = true; try { Monitor::Enter(_lockObject); if (readOnly == true) return _reader->OpenFile(fileName, fileName); } finally { Monitor::Exit(_lockObject); } //else // return new Objects.Resources.File(fileName, FileAccess.ReadWrite, FileMode.OpenOrCreate); return false; } static bool OpenFile(System::String^ fileName, System::String^ alias, bool readOnly) { readOnly = true; try { Monitor::Enter(_lockObject); if (readOnly == true) return _reader->OpenFile(fileName, alias); } finally { Monitor::Exit(_lockObject); } //else // return new Objects.Resources.File(fileName, FileAccess.ReadWrite, FileMode.OpenOrCreate); return false; } static void CloseFile(System::String^ alias) { _reader->CloseFile(alias); } static FileReader^ LockReader() { Monitor::Enter(_lockObject); return _reader; } static FileReader^ ReleaseReader(FileReader^ reader) { reader = nullptr; Monitor::Exit(_lockObject); return reader; } }; } }