All pastes #102986 Raw Edit

ibutsu

public cpp v1 · immutable
#102986 ·published 2006-07-28 00:51 UTC
rendered paste body
// Cystem.File.h#pragma once#pragma unmanaged#include <string>#include <fstream>#include <map>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 managedusing namespace System;//using namespace Cystem;#include "StringConvertor.h"namespace Cystem {namespace IO{	public ref class FileReader	{	public:		typedef std::pair<std::string, File*> FilePair;		typedef std::map<std::string, File*> FileMap;		FileReader()		{			_files = new std::map<std::string, File*>();		}		~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 <typename T> 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<System::Byte>^ tempbuf = gcnew array<System::Byte>(size);			pin_ptr<System::Byte> buf = &tempbuf[0];			_files->find(filename)->second->ReadStruct(reinterpret_cast<char*>(buf), size);			System::IntPtr _buffer = Marshal::AllocHGlobal(size);			Marshal::Copy(tempbuf, 0, _buffer, size);			buffer = (T)Marshal::PtrToStructure(_buffer, type);			Marshal::FreeHGlobal(_buffer);		}		generic <typename T> void ReadStruct(System::String^ alias, T% buffer, long startIndex, long size, System::Type^ type)		{			StringUtilities::StringConvertor tempfile(alias);			std::string filename = tempfile.STLAnsiString;			array<System::Byte>^ tempbuf = gcnew array<System::Byte>(size);			pin_ptr<System::Byte> buf = &tempbuf[0];			_files->find(filename)->second->ReadStruct(reinterpret_cast<char*>(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<std::string, File*>::iterator fileIter;			fileIter = _files->find(filename);			delete fileIter->second;			_files->erase(fileIter);		}	private:		std::map<std::string, File*>*	_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;		}	};}}