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