source: dev/branches/egtra/ab5.0/jenga/include/common/File.h@ 820

Last change on this file since 820 was 818, checked in by イグトランス (egtra), 14 years ago

Jenga/common/String.hに対するテストを追加

File size: 1.6 KB
Line 
1#include <string>
2#include <Windows.h>
3
4#pragma once
5
6
7namespace Jenga{
8namespace Common{
9
10
11class File
12{
13 const std::string filePath;
14public:
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}}
Note: See TracBrowser for help on using the repository browser.