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

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

コンパイラのstdafx.h, .cppを32/64ビットで共通化。Windows SDKに含まれるライブラリ・ヘッダの除去。VC++ 2010 Express with WDKのATL環境で_SECURE_ATLがエラーを起こす問題の修正。4996警告の抑制pragmaを削除。ほか。

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