[323] | 1 | #pragma once
|
---|
| 2 | #pragma warning(disable : 4996)
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 | namespace Jenga{
|
---|
| 6 | namespace Common{
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | class File
|
---|
| 10 | {
|
---|
| 11 | const std::string filePath;
|
---|
| 12 | public:
|
---|
| 13 | File( const std::string &filePath )
|
---|
| 14 | : filePath( filePath )
|
---|
| 15 | {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | std::string Read()
|
---|
| 19 | {
|
---|
[769] | 20 | HANDLE hFile = CreateFile( filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
|
---|
[323] | 21 | if( hFile == INVALID_HANDLE_VALUE )
|
---|
| 22 | {
|
---|
| 23 | Jenga::Throw( filePath + " がオープンできません" );
|
---|
| 24 | }
|
---|
| 25 | int size = GetFileSize( hFile, NULL );
|
---|
| 26 |
|
---|
| 27 | char *temp = static_cast<char *>(calloc( size + 1, 1 ));
|
---|
| 28 | DWORD dummy;
|
---|
| 29 | ReadFile( hFile, temp, size, &dummy, NULL );
|
---|
| 30 | CloseHandle(hFile);
|
---|
| 31 |
|
---|
| 32 | std::string result = temp;
|
---|
| 33 | free( temp );
|
---|
| 34 |
|
---|
| 35 | return result;
|
---|
| 36 | }
|
---|
[583] | 37 |
|
---|
| 38 | bool ReadBinary( Binary &binary )
|
---|
| 39 | {
|
---|
[769] | 40 | HANDLE hFile = CreateFile( filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
|
---|
[583] | 41 | if( hFile == INVALID_HANDLE_VALUE )
|
---|
| 42 | {
|
---|
| 43 | return false;
|
---|
| 44 | }
|
---|
| 45 | int size = GetFileSize( hFile, NULL );
|
---|
| 46 |
|
---|
| 47 | char *temp = static_cast<char *>(calloc( size + 1, 1 ));
|
---|
| 48 | DWORD dummy;
|
---|
| 49 | ReadFile( hFile, temp, size, &dummy, NULL );
|
---|
| 50 | CloseHandle(hFile);
|
---|
| 51 |
|
---|
| 52 | binary.Put( temp, size );
|
---|
| 53 |
|
---|
| 54 | return true;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | bool WriteBinary( const Binary &binary )
|
---|
| 58 | {
|
---|
| 59 | HANDLE hFile = CreateFile(filePath.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
|
---|
| 60 | if(hFile==INVALID_HANDLE_VALUE)
|
---|
| 61 | {
|
---|
| 62 | return false;
|
---|
| 63 | }
|
---|
| 64 | DWORD dw;
|
---|
| 65 | WriteFile(hFile,binary.GetBuffer(),binary.GetSize(),&dw,NULL);
|
---|
| 66 | CloseHandle(hFile);
|
---|
| 67 |
|
---|
| 68 | return true;
|
---|
| 69 | }
|
---|
[323] | 70 | };
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | }}
|
---|