Aさん
Revision | 3e9d90eb2490ccf6bb74abed6e1defcac32a39b4 (tree) |
---|---|
Zeit | 2011-01-17 22:37:43 |
Autor | berupon <berupon@gmai...> |
Commiter | berupon |
planning interface
@@ -0,0 +1,60 @@ | ||
1 | +#include "stdafx.h" | |
2 | +#include "File.h" | |
3 | + | |
4 | +#include <io.h> | |
5 | +#include <assert.h> | |
6 | +#include <memory.h> | |
7 | +#include <stdio.h> | |
8 | + | |
9 | +#include <windows.h> | |
10 | + | |
11 | +File::File(HANDLE hFile) | |
12 | + : | |
13 | + hFile_(hFile) | |
14 | +{ | |
15 | +} | |
16 | + | |
17 | +File::File(FILE* pFile) | |
18 | +{ | |
19 | + hFile_ = (HANDLE) _get_osfhandle(_fileno(pFile)); | |
20 | +} | |
21 | + | |
22 | +BOOL File::Read(void* pBuffer, DWORD nNumberOfBytesToRead, DWORD& nNumberOfBytesRead) | |
23 | +{ | |
24 | + return ReadFile(hFile_, pBuffer, nNumberOfBytesToRead, &nNumberOfBytesRead, NULL); | |
25 | +} | |
26 | + | |
27 | +BOOL File::Write(const void* pBuffer, DWORD nNumberOfBytesToWrite, DWORD& nNumberOfBytesWritten) | |
28 | +{ | |
29 | + return WriteFile(hFile_, pBuffer, nNumberOfBytesToWrite, &nNumberOfBytesWritten, NULL); | |
30 | +} | |
31 | + | |
32 | +DWORD File::Seek(LONG lDistanceToMove, DWORD dwMoveMethod) | |
33 | +{ | |
34 | + return SetFilePointer(hFile_, lDistanceToMove, NULL, dwMoveMethod); | |
35 | +} | |
36 | + | |
37 | +DWORD File::Tell() const | |
38 | +{ | |
39 | + /* | |
40 | + -- Reference -- | |
41 | + http://nukz.net/reference/fileio/hh/winbase/filesio_3vhu.htm | |
42 | + */ | |
43 | + return SetFilePointer( | |
44 | + hFile_, // must have GENERIC_READ and/or GENERIC_WRITE | |
45 | + 0, // do not move pointer | |
46 | + NULL, // hFile is not large enough to need this pointer | |
47 | + FILE_CURRENT | |
48 | + ); // provides offset from current position | |
49 | +} | |
50 | + | |
51 | +DWORD File::Size() const | |
52 | +{ | |
53 | + return GetFileSize(hFile_, NULL); | |
54 | +} | |
55 | + | |
56 | +BOOL File::Flush() | |
57 | +{ | |
58 | + return FlushFileBuffers(hFile_); | |
59 | +} | |
60 | + |
@@ -0,0 +1,32 @@ | ||
1 | +#pragma once | |
2 | + | |
3 | +#include "IFile.h" | |
4 | + | |
5 | +#include <stdio.h> | |
6 | + | |
7 | +class File : public IFile | |
8 | +{ | |
9 | +public: | |
10 | + File(HANDLE hFile); | |
11 | + | |
12 | + File(FILE* pFile); | |
13 | + | |
14 | + BOOL Read(void* pBuffer, DWORD nNumberOfBytesToRead, DWORD& nNumberOfBytesRead); | |
15 | + | |
16 | + BOOL Write(const void* pBuffer, DWORD nNumberOfBytesToWrite, DWORD& nNumberOfBytesWritten); | |
17 | + | |
18 | + DWORD Seek(LONG lDistanceToMove, DWORD dwMoveMethod); | |
19 | + | |
20 | + DWORD Tell() const; | |
21 | + | |
22 | + DWORD Size() const; | |
23 | + | |
24 | + BOOL Flush(); | |
25 | + | |
26 | + bool HasBuffer() const { return false; } | |
27 | + virtual const void* GetBuffer() const { return 0; } | |
28 | + | |
29 | +private: | |
30 | + HANDLE hFile_; | |
31 | +}; | |
32 | + |
@@ -0,0 +1,35 @@ | ||
1 | +#pragma once | |
2 | + | |
3 | +#include <windows.h> | |
4 | + | |
5 | +/*! | |
6 | + | |
7 | +概要 | |
8 | + 画像の入出力用に仮想的にFileを扱えるようにする | |
9 | + | |
10 | +制限 | |
11 | + 非同期操作には対応しない | |
12 | + 2GBまで | |
13 | + | |
14 | +備考 | |
15 | + CxImage by Davide Pizzolato (http://www.xdp.it/cximage.htm) を参考にしました。 | |
16 | + | |
17 | + InterfaceをWindowsAPIのFile関数に似せています | |
18 | + | |
19 | +*/ | |
20 | + | |
21 | +class IFile | |
22 | +{ | |
23 | +public: | |
24 | + virtual BOOL Read(void* pBuffer, DWORD nNumberOfBytesToRead, DWORD& nNumberOfBytesRead) = 0; | |
25 | + virtual BOOL Write(const void* pBuffer, DWORD nNumberOfBytesToWrite, DWORD& nNumberOfBytesWritten) = 0; | |
26 | + virtual DWORD Seek(LONG lDistanceToMove, DWORD dwMoveMethod) = 0; | |
27 | + virtual DWORD Tell() const = 0; | |
28 | + virtual DWORD Size() const = 0; | |
29 | + virtual bool IsEof() const { return Tell() == Size(); } | |
30 | + virtual BOOL Flush() = 0; | |
31 | + | |
32 | + virtual bool HasBuffer() const = 0; | |
33 | + virtual const void* GetBuffer() const = 0; | |
34 | +}; | |
35 | + |
@@ -0,0 +1,59 @@ | ||
1 | +#include "stdafx.h" | |
2 | +#include "MemoryFile.h" | |
3 | + | |
4 | +MemoryFile::MemoryFile(void* pBuff, size_t buffSize) | |
5 | + : | |
6 | + pBuff_(pBuff), | |
7 | + buffSize_(buffSize) | |
8 | +{ | |
9 | +} | |
10 | + | |
11 | +BOOL MemoryFile::Read(void* pBuffer, DWORD nNumberOfBytesToRead, DWORD& nNumberOfBytesRead) | |
12 | +{ | |
13 | + DWORD fileSize = Size(); | |
14 | + nNumberOfBytesRead = (fileSize < curPos_+nNumberOfBytesToRead) ? (fileSize - curPos_) : nNumberOfBytesToRead; | |
15 | + memcpy(pBuffer, (BYTE*)pBuff_+curPos_, nNumberOfBytesRead); | |
16 | + Seek(nNumberOfBytesRead, FILE_CURRENT); | |
17 | + return TRUE; | |
18 | +} | |
19 | + | |
20 | +BOOL MemoryFile::Write(const void* pBuffer, DWORD nNumberOfBytesToWrite, DWORD& nNumberOfBytesWritten) | |
21 | +{ | |
22 | + memcpy((BYTE*)pBuff_+curPos_, pBuffer, nNumberOfBytesToWrite); | |
23 | + nNumberOfBytesWritten = nNumberOfBytesToWrite; | |
24 | + Seek(nNumberOfBytesToWrite, FILE_CURRENT); | |
25 | + return TRUE; | |
26 | +} | |
27 | + | |
28 | +DWORD MemoryFile::Seek(LONG lDistanceToMove, DWORD dwMoveMethod) | |
29 | +{ | |
30 | + switch (dwMoveMethod) { | |
31 | + case FILE_BEGIN: | |
32 | + curPos_ = lDistanceToMove; | |
33 | + break; | |
34 | + case FILE_CURRENT: | |
35 | + curPos_ += lDistanceToMove; | |
36 | + break; | |
37 | + case FILE_END: | |
38 | + curPos_ += buffSize_ + lDistanceToMove; | |
39 | + break; | |
40 | + } | |
41 | + assert(0 <= curPos_ && curPos_ <= buffSize_); | |
42 | + return curPos_; | |
43 | +} | |
44 | + | |
45 | +DWORD MemoryFile::Tell() const | |
46 | +{ | |
47 | + return curPos_; | |
48 | +} | |
49 | + | |
50 | +DWORD MemoryFile::Size() const | |
51 | +{ | |
52 | + return buffSize_; | |
53 | +} | |
54 | + | |
55 | +BOOL MemoryFile::Flush() | |
56 | +{ | |
57 | + return TRUE; | |
58 | +} | |
59 | + |
@@ -0,0 +1,31 @@ | ||
1 | +#pragma once | |
2 | + | |
3 | +#include "IFile.h" | |
4 | + | |
5 | +class MemoryFile : public IFile | |
6 | +{ | |
7 | +public: | |
8 | + MemoryFile(void* pBuff, size_t buffSize); | |
9 | + | |
10 | + BOOL Read(void* pBuffer, DWORD nNumberOfBytesToRead, DWORD& nNumberOfBytesRead); | |
11 | + | |
12 | + BOOL Write(const void* pBuffer, DWORD nNumberOfBytesToWrite, DWORD& nNumberOfBytesWritten); | |
13 | + | |
14 | + DWORD Seek(LONG lDistanceToMove, DWORD dwMoveMethod); | |
15 | + | |
16 | + DWORD Tell() const; | |
17 | + | |
18 | + DWORD Size() const; | |
19 | + | |
20 | + BOOL Flush(); | |
21 | + | |
22 | + bool HasBuffer() const { return true; } | |
23 | + virtual const void* GetBuffer() const { return pBuff_; } | |
24 | + | |
25 | +private: | |
26 | + void* pBuff_; | |
27 | + DWORD buffSize_; | |
28 | + | |
29 | + DWORD curPos_; | |
30 | +}; | |
31 | + |
@@ -25,15 +25,42 @@ enum ElementType { | ||
25 | 25 | ElementType_MaxKey = 0x7F, |
26 | 26 | }; |
27 | 27 | |
28 | +template <typename T> | |
29 | +struct Document | |
30 | +{ | |
31 | + Document(T& writer) | |
32 | + : | |
33 | + w_(writer) | |
34 | + { | |
35 | + w_.EnterDocument(); | |
36 | + } | |
37 | + ~Document() | |
38 | + { | |
39 | + w_.LeaveDocument(); | |
40 | + } | |
41 | +private: | |
42 | + T& w_; | |
43 | +}; | |
44 | + | |
28 | 45 | struct IReader { |
29 | - const char* GetName() const; | |
30 | - ElementType GetType() const; | |
31 | - const char* GetDataPtr() const; | |
32 | - const char** GetCurrentPath() const; | |
46 | + const char* ReadElementName() const; | |
47 | + ElementType ReadElementType() const; | |
48 | + const char* ReadElementDataPtr() const; | |
49 | + | |
50 | + void ReadElement_Double(double& val) const; | |
51 | + void ReadElement_String(char* str, uint32_t& strLen) const; | |
52 | + void ReadElement_Binary(uint32_t& nBytes, BinarySubType& subType, char* data) const; | |
53 | + void ReadElement_Boolean(bool& val); | |
54 | + void ReadElement_Regex(char* pattern, char* option); | |
55 | + void ReadElement_Int32(int32_t& val); | |
56 | + void ReadElement_Int64(int64_t& val); | |
57 | + | |
58 | + const char** CurrentPath() const; | |
33 | 59 | |
34 | - bool ReadNextElement(); | |
35 | - bool EnterDocument(); | |
36 | - bool LeaveDocument(); | |
60 | + bool MoveToNextElement(); | |
61 | + | |
62 | + virtual bool EnterDocument() = 0; | |
63 | + virtual bool LeaveDocument() = 0; | |
37 | 64 | }; |
38 | 65 | |
39 | 66 | enum BinarySubType { |
@@ -56,43 +83,13 @@ struct IWriter { | ||
56 | 83 | void WriteElement_Regex(const char* name, const char* pattern, const char* option) { WriteElementHead(ElementType_Regex, name); WriteCString(pattern); WriteCString(option); } |
57 | 84 | void WriteElement_Int32(const char* name, int32_t val) { WriteElementHead(ElementType_Int32, name); WriteInt32(val); } |
58 | 85 | void WriteElement_Int64(const char* name, int64_t val) { WriteElementHead(ElementType_Int64, name); WriteInt64(val); } |
59 | - void WriteElement(const char* name, ElementType type, const char* data) { | |
60 | - WriteElementHead(type, name); | |
61 | - size_t len; | |
62 | - switch (type) { | |
63 | - case ElementType_String: | |
64 | - case ElementType_JavaScriptCode: | |
65 | - case ElementType_Symbol: | |
66 | - break; | |
67 | - case ElementType_Document: | |
68 | - break; | |
69 | - case ElementType_Binary: | |
70 | - break; | |
71 | - case ElementType_Boolean: | |
72 | - len = 1; | |
73 | - break; | |
74 | - case ElementType_Null: | |
75 | - len = 0; | |
76 | - break; | |
77 | - case ElementType_Regex: | |
78 | - break; | |
79 | - case ElementType_Int32: | |
80 | - len = 4; | |
81 | - break; | |
82 | - case ElementType_Double: | |
83 | - case ElementType_UTCdatetime: | |
84 | - case ElementType_TimeStamp: | |
85 | - case ElementType_Int64: | |
86 | - len = 8; | |
87 | - break; | |
88 | - } | |
89 | - Write(data, len); | |
90 | - } | |
91 | - void EnterDocument(); // start writing document | |
92 | - void LeaveDocument(); | |
93 | 86 | |
87 | + virtual void EnterDocument() = 0; // start writing document | |
88 | + virtual void LeaveDocument() = 0; | |
89 | + | |
90 | +private: | |
94 | 91 | // device |
95 | - void Write(const char* data, uint32_t len); | |
92 | + virtual void Write(const char* data, uint32_t len) = 0; | |
96 | 93 | |
97 | 94 | // impl |
98 | 95 | void WriteElementHead(ElementType type, const char* name) { WriteInt8(type); WriteCString(name); } |
@@ -107,11 +104,5 @@ struct IWriter { | ||
107 | 104 | |
108 | 105 | }; |
109 | 106 | |
110 | -struct FileReader : IReader { | |
111 | - const char* name_; | |
112 | - ElementType type_; | |
113 | - std::vector<const char*> path_; | |
114 | -}; | |
115 | - | |
116 | 107 | |
117 | 108 | } // namespace BSON |