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