Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: xfa/fgas/crt/fgas_stream.cpp

Issue 1998453002: Remove Release() from CFX_StreamImp, rename to IFX_StreamImp (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Implement suggestions. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "xfa/fgas/crt/fgas_stream.h" 7 #include "xfa/fgas/crt/fgas_stream.h"
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory>
10 11
11 #include "xfa/fgas/crt/fgas_codepage.h" 12 #include "xfa/fgas/crt/fgas_codepage.h"
12 #include "xfa/fgas/crt/fgas_system.h" 13 #include "xfa/fgas/crt/fgas_system.h"
13 14
14 namespace { 15 namespace {
15 16
16 class CFX_StreamImp { 17 class IFX_StreamImp {
17 public: 18 public:
18 virtual void Release() { delete this; } 19 virtual ~IFX_StreamImp() {}
19 virtual uint32_t GetAccessModes() const { return m_dwAccess; } 20
20 virtual int32_t GetLength() const = 0; 21 virtual int32_t GetLength() const = 0;
21 virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) = 0; 22 virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) = 0;
22 virtual int32_t GetPosition() = 0; 23 virtual int32_t GetPosition() = 0;
23 virtual FX_BOOL IsEOF() const = 0; 24 virtual FX_BOOL IsEOF() const = 0;
24 virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) = 0; 25 virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) = 0;
25 virtual int32_t ReadString(FX_WCHAR* pStr, 26 virtual int32_t ReadString(FX_WCHAR* pStr,
26 int32_t iMaxLength, 27 int32_t iMaxLength,
27 FX_BOOL& bEOS) = 0; 28 FX_BOOL& bEOS) = 0;
28 virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) = 0; 29 virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) = 0;
29 virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) = 0; 30 virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) = 0;
30 virtual void Flush() = 0; 31 virtual void Flush() = 0;
31 virtual FX_BOOL SetLength(int32_t iLength) = 0; 32 virtual FX_BOOL SetLength(int32_t iLength) = 0;
32 33
33 protected: 34 protected:
34 CFX_StreamImp(); 35 IFX_StreamImp();
Lei Zhang 2016/05/18 23:43:06 separator after ctor
Tom Sepez 2016/05/18 23:47:04 Done.
35 virtual ~CFX_StreamImp() {} 36 uint32_t GetAccessModes() const { return m_dwAccess; }
37 void SetAccessModes(uint32_t modes) { m_dwAccess = modes; }
38
39 private:
36 uint32_t m_dwAccess; 40 uint32_t m_dwAccess;
37 }; 41 };
38 42
39 class CFX_FileStreamImp : public CFX_StreamImp { 43 class CFX_FileStreamImp : public IFX_StreamImp {
40 public: 44 public:
41 CFX_FileStreamImp(); 45 CFX_FileStreamImp();
42 virtual ~CFX_FileStreamImp(); 46 ~CFX_FileStreamImp() override;
47
43 FX_BOOL LoadFile(const FX_WCHAR* pszSrcFileName, uint32_t dwAccess); 48 FX_BOOL LoadFile(const FX_WCHAR* pszSrcFileName, uint32_t dwAccess);
44 virtual int32_t GetLength() const; 49
45 virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset); 50 // IFX_StreamImp:
46 virtual int32_t GetPosition(); 51 int32_t GetLength() const override;
47 virtual FX_BOOL IsEOF() const; 52 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override;
48 virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize); 53 int32_t GetPosition() override;
49 virtual int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, FX_BOOL& bEOS); 54 FX_BOOL IsEOF() const override;
50 virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize); 55 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override;
51 virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength); 56 int32_t ReadString(FX_WCHAR* pStr,
52 virtual void Flush(); 57 int32_t iMaxLength,
53 virtual FX_BOOL SetLength(int32_t iLength); 58 FX_BOOL& bEOS) override;
59 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override;
60 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override;
61 void Flush() override;
62 FX_BOOL SetLength(int32_t iLength) override;
54 63
55 protected: 64 protected:
56 FXSYS_FILE* m_hFile; 65 FXSYS_FILE* m_hFile;
57 int32_t m_iLength; 66 int32_t m_iLength;
58 }; 67 };
59 68
60 class CFX_BufferStreamImp : public CFX_StreamImp { 69 class CFX_BufferStreamImp : public IFX_StreamImp {
61 public: 70 public:
62 CFX_BufferStreamImp(); 71 CFX_BufferStreamImp();
63 virtual ~CFX_BufferStreamImp() {} 72 ~CFX_BufferStreamImp() override {}
73
64 FX_BOOL LoadBuffer(uint8_t* pData, int32_t iTotalSize, uint32_t dwAccess); 74 FX_BOOL LoadBuffer(uint8_t* pData, int32_t iTotalSize, uint32_t dwAccess);
65 virtual int32_t GetLength() const; 75
66 virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset); 76 // IFX_StreamImp:
67 virtual int32_t GetPosition(); 77 int32_t GetLength() const override;
68 virtual FX_BOOL IsEOF() const; 78 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override;
69 virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize); 79 int32_t GetPosition() override;
70 virtual int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, FX_BOOL& bEOS); 80 FX_BOOL IsEOF() const override;
71 virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize); 81 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override;
72 virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength); 82 int32_t ReadString(FX_WCHAR* pStr,
73 virtual void Flush() {} 83 int32_t iMaxLength,
74 virtual FX_BOOL SetLength(int32_t iLength) { return FALSE; } 84 FX_BOOL& bEOS) override;
85 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override;
86 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override;
87 void Flush() override {}
88 FX_BOOL SetLength(int32_t iLength) override { return FALSE; }
75 89
76 protected: 90 protected:
77 uint8_t* m_pData; 91 uint8_t* m_pData;
78 int32_t m_iTotalSize; 92 int32_t m_iTotalSize;
79 int32_t m_iPosition; 93 int32_t m_iPosition;
80 int32_t m_iLength; 94 int32_t m_iLength;
81 }; 95 };
82 96
83 class CFX_FileReadStreamImp : public CFX_StreamImp { 97 class CFX_FileReadStreamImp : public IFX_StreamImp {
84 public: 98 public:
85 CFX_FileReadStreamImp(); 99 CFX_FileReadStreamImp();
86 virtual ~CFX_FileReadStreamImp() {} 100 ~CFX_FileReadStreamImp() override {}
101
87 FX_BOOL LoadFileRead(IFX_FileRead* pFileRead, uint32_t dwAccess); 102 FX_BOOL LoadFileRead(IFX_FileRead* pFileRead, uint32_t dwAccess);
88 virtual int32_t GetLength() const;
89 virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset);
90 virtual int32_t GetPosition() { return m_iPosition; }
91 virtual FX_BOOL IsEOF() const;
92 103
93 virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize); 104 // IFX_StreamImp:
94 virtual int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, FX_BOOL& bEOS); 105 int32_t GetLength() const override;
95 virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) { 106 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override;
107 int32_t GetPosition() override { return m_iPosition; }
108 FX_BOOL IsEOF() const override;
109 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override;
110 int32_t ReadString(FX_WCHAR* pStr,
111 int32_t iMaxLength,
112 FX_BOOL& bEOS) override;
113 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override {
96 return 0; 114 return 0;
97 } 115 }
98 virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) { 116 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override {
99 return 0; 117 return 0;
100 } 118 }
101 virtual void Flush() {} 119 void Flush() override {}
102 virtual FX_BOOL SetLength(int32_t iLength) { return FALSE; } 120 FX_BOOL SetLength(int32_t iLength) override { return FALSE; }
103 121
104 protected: 122 protected:
105 IFX_FileRead* m_pFileRead; 123 IFX_FileRead* m_pFileRead;
106 int32_t m_iPosition; 124 int32_t m_iPosition;
107 int32_t m_iLength; 125 int32_t m_iLength;
108 }; 126 };
109 127
110 class CFX_BufferReadStreamImp : public CFX_StreamImp { 128 class CFX_BufferReadStreamImp : public IFX_StreamImp {
111 public: 129 public:
112 CFX_BufferReadStreamImp(); 130 CFX_BufferReadStreamImp();
113 ~CFX_BufferReadStreamImp(); 131 ~CFX_BufferReadStreamImp() override;
132
114 FX_BOOL LoadBufferRead(IFX_BufferRead* pBufferRead, 133 FX_BOOL LoadBufferRead(IFX_BufferRead* pBufferRead,
115 int32_t iFileSize, 134 int32_t iFileSize,
116 uint32_t dwAccess, 135 uint32_t dwAccess,
117 FX_BOOL bReleaseBufferRead); 136 FX_BOOL bReleaseBufferRead);
118 137
119 virtual int32_t GetLength() const; 138 // IFX_StreamImp:
120 virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset); 139 int32_t GetLength() const override;
121 virtual int32_t GetPosition() { return m_iPosition; } 140 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override;
122 virtual FX_BOOL IsEOF() const; 141 int32_t GetPosition() override { return m_iPosition; }
123 142 FX_BOOL IsEOF() const override;
124 virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize); 143 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override;
125 virtual int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, FX_BOOL& bEOS); 144 int32_t ReadString(FX_WCHAR* pStr,
126 virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) { 145 int32_t iMaxLength,
146 FX_BOOL& bEOS) override;
147 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override {
127 return 0; 148 return 0;
128 } 149 }
129 virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) { 150 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override {
130 return 0; 151 return 0;
131 } 152 }
132 virtual void Flush() {} 153 void Flush() override {}
133 virtual FX_BOOL SetLength(int32_t iLength) { return FALSE; } 154 FX_BOOL SetLength(int32_t iLength) override { return FALSE; }
134 155
135 private: 156 private:
136 IFX_BufferRead* m_pBufferRead; 157 IFX_BufferRead* m_pBufferRead;
137 FX_BOOL m_bReleaseBufferRead; 158 FX_BOOL m_bReleaseBufferRead;
138 int32_t m_iPosition; 159 int32_t m_iPosition;
139 int32_t m_iBufferSize; 160 int32_t m_iBufferSize;
140 }; 161 };
141 162
142 class CFX_FileWriteStreamImp : public CFX_StreamImp { 163 class CFX_FileWriteStreamImp : public IFX_StreamImp {
143 public: 164 public:
144 CFX_FileWriteStreamImp(); 165 CFX_FileWriteStreamImp();
145 virtual ~CFX_FileWriteStreamImp() {} 166 ~CFX_FileWriteStreamImp() override {}
167
146 FX_BOOL LoadFileWrite(IFX_FileWrite* pFileWrite, uint32_t dwAccess); 168 FX_BOOL LoadFileWrite(IFX_FileWrite* pFileWrite, uint32_t dwAccess);
147 virtual int32_t GetLength() const; 169
148 virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset); 170 // IFX_StreamImp:
149 virtual int32_t GetPosition() { return m_iPosition; } 171 int32_t GetLength() const override;
150 virtual FX_BOOL IsEOF() const; 172 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override;
151 virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) { return 0; } 173 int32_t GetPosition() override { return m_iPosition; }
152 virtual int32_t ReadString(FX_WCHAR* pStr, 174 FX_BOOL IsEOF() const override;
153 int32_t iMaxLength, 175 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override { return 0; }
154 FX_BOOL& bEOS) { 176 int32_t ReadString(FX_WCHAR* pStr,
177 int32_t iMaxLength,
178 FX_BOOL& bEOS) override {
155 return 0; 179 return 0;
156 } 180 }
157 virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize); 181 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override;
158 virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength); 182 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override;
159 virtual void Flush(); 183 void Flush() override;
160 virtual FX_BOOL SetLength(int32_t iLength) { return FALSE; } 184 FX_BOOL SetLength(int32_t iLength) override { return FALSE; }
161 185
162 protected: 186 protected:
163 IFX_FileWrite* m_pFileWrite; 187 IFX_FileWrite* m_pFileWrite;
164 int32_t m_iPosition; 188 int32_t m_iPosition;
165 }; 189 };
166 190
167 enum FX_STREAMTYPE { 191 enum FX_STREAMTYPE {
168 FX_SREAMTYPE_Unknown = 0, 192 FX_SREAMTYPE_Unknown = 0,
169 FX_STREAMTYPE_File, 193 FX_STREAMTYPE_File,
170 FX_STREAMTYPE_Buffer, 194 FX_STREAMTYPE_Buffer,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 virtual FX_BOOL SetLength(int32_t iLength); 226 virtual FX_BOOL SetLength(int32_t iLength);
203 virtual int32_t GetBOM(uint8_t bom[4]) const; 227 virtual int32_t GetBOM(uint8_t bom[4]) const;
204 virtual uint16_t GetCodePage() const; 228 virtual uint16_t GetCodePage() const;
205 virtual uint16_t SetCodePage(uint16_t wCodePage); 229 virtual uint16_t SetCodePage(uint16_t wCodePage);
206 virtual IFX_Stream* CreateSharedStream(uint32_t dwAccess, 230 virtual IFX_Stream* CreateSharedStream(uint32_t dwAccess,
207 int32_t iOffset, 231 int32_t iOffset,
208 int32_t iLength); 232 int32_t iLength);
209 233
210 protected: 234 protected:
211 FX_STREAMTYPE m_eStreamType; 235 FX_STREAMTYPE m_eStreamType;
212 CFX_StreamImp* m_pStreamImp; 236 IFX_StreamImp* m_pStreamImp;
213 uint32_t m_dwAccess; 237 uint32_t m_dwAccess;
214 int32_t m_iTotalSize; 238 int32_t m_iTotalSize;
215 int32_t m_iPosition; 239 int32_t m_iPosition;
216 int32_t m_iStart; 240 int32_t m_iStart;
217 int32_t m_iLength; 241 int32_t m_iLength;
218 int32_t m_iRefCount; 242 int32_t m_iRefCount;
219 }; 243 };
220 244
221 class CFX_TextStream : public IFX_Stream { 245 class CFX_TextStream : public IFX_Stream {
222 public: 246 public:
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 if (!pSR->LoadBuffer(pData, length, dwAccess)) { 391 if (!pSR->LoadBuffer(pData, length, dwAccess)) {
368 pSR->Release(); 392 pSR->Release();
369 return NULL; 393 return NULL;
370 } 394 }
371 if (dwAccess & FX_STREAMACCESS_Text) { 395 if (dwAccess & FX_STREAMACCESS_Text) {
372 return new CFX_TextStream(pSR, TRUE); 396 return new CFX_TextStream(pSR, TRUE);
373 } 397 }
374 return pSR; 398 return pSR;
375 } 399 }
376 400
377 CFX_StreamImp::CFX_StreamImp() : m_dwAccess(0) {} 401 IFX_StreamImp::IFX_StreamImp() : m_dwAccess(0) {}
378 CFX_FileStreamImp::CFX_FileStreamImp() 402
379 : CFX_StreamImp(), m_hFile(NULL), m_iLength(0) {} 403 CFX_FileStreamImp::CFX_FileStreamImp() : m_hFile(nullptr), m_iLength(0) {}
404
380 CFX_FileStreamImp::~CFX_FileStreamImp() { 405 CFX_FileStreamImp::~CFX_FileStreamImp() {
381 if (m_hFile != NULL) { 406 if (m_hFile)
382 FXSYS_fclose(m_hFile); 407 FXSYS_fclose(m_hFile);
383 }
384 } 408 }
409
385 FX_BOOL CFX_FileStreamImp::LoadFile(const FX_WCHAR* pszSrcFileName, 410 FX_BOOL CFX_FileStreamImp::LoadFile(const FX_WCHAR* pszSrcFileName,
386 uint32_t dwAccess) { 411 uint32_t dwAccess) {
387 ASSERT(m_hFile == NULL); 412 ASSERT(m_hFile == NULL);
388 ASSERT(pszSrcFileName != NULL && FXSYS_wcslen(pszSrcFileName) > 0); 413 ASSERT(pszSrcFileName != NULL && FXSYS_wcslen(pszSrcFileName) > 0);
389 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \ 414 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \
390 _FX_OS_ == _FX_WIN64_ 415 _FX_OS_ == _FX_WIN64_
391 const FX_WCHAR* wsMode; 416 const FX_WCHAR* wsMode;
392 if (dwAccess & FX_STREAMACCESS_Write) { 417 if (dwAccess & FX_STREAMACCESS_Write) {
393 if (dwAccess & FX_STREAMACCESS_Append) { 418 if (dwAccess & FX_STREAMACCESS_Append) {
394 wsMode = L"a+b"; 419 wsMode = L"a+b";
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 } 469 }
445 if (dwAccess & FX_STREAMACCESS_Truncate) { 470 if (dwAccess & FX_STREAMACCESS_Truncate) {
446 FX_fsetsize(m_hFile, 0); 471 FX_fsetsize(m_hFile, 0);
447 } 472 }
448 } 473 }
449 } else { 474 } else {
450 return FALSE; 475 return FALSE;
451 } 476 }
452 } 477 }
453 #endif 478 #endif
454 m_dwAccess = dwAccess; 479 SetAccessModes(dwAccess);
455 if ((dwAccess & (FX_STREAMACCESS_Write | FX_STREAMACCESS_Truncate)) == 480 if ((dwAccess & (FX_STREAMACCESS_Write | FX_STREAMACCESS_Truncate)) ==
456 (FX_STREAMACCESS_Write | FX_STREAMACCESS_Truncate)) { 481 (FX_STREAMACCESS_Write | FX_STREAMACCESS_Truncate)) {
457 m_iLength = 0; 482 m_iLength = 0;
458 } else { 483 } else {
459 m_iLength = FX_filelength(m_hFile); 484 m_iLength = FX_filelength(m_hFile);
460 } 485 }
461 return TRUE; 486 return TRUE;
462 } 487 }
463 int32_t CFX_FileStreamImp::GetLength() const { 488 int32_t CFX_FileStreamImp::GetLength() const {
464 ASSERT(m_hFile != NULL); 489 ASSERT(m_hFile != NULL);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 } 527 }
503 iPosition += iCount * 2; 528 iPosition += iCount * 2;
504 if (FXSYS_ftell(m_hFile) != iPosition) { 529 if (FXSYS_ftell(m_hFile) != iPosition) {
505 FXSYS_fseek(m_hFile, iPosition, 0); 530 FXSYS_fseek(m_hFile, iPosition, 0);
506 } 531 }
507 bEOS = (iPosition >= m_iLength); 532 bEOS = (iPosition >= m_iLength);
508 return iCount; 533 return iCount;
509 } 534 }
510 int32_t CFX_FileStreamImp::WriteData(const uint8_t* pBuffer, 535 int32_t CFX_FileStreamImp::WriteData(const uint8_t* pBuffer,
511 int32_t iBufferSize) { 536 int32_t iBufferSize) {
512 ASSERT(m_hFile != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); 537 ASSERT(m_hFile && (GetAccessModes() & FX_STREAMACCESS_Write) != 0);
513 ASSERT(pBuffer != NULL && iBufferSize > 0); 538 ASSERT(pBuffer && iBufferSize > 0);
514 int32_t iRet = FXSYS_fwrite(pBuffer, 1, iBufferSize, m_hFile); 539 int32_t iRet = FXSYS_fwrite(pBuffer, 1, iBufferSize, m_hFile);
515 if (iRet != 0) { 540 if (iRet != 0) {
516 int32_t iPos = FXSYS_ftell(m_hFile); 541 int32_t iPos = FXSYS_ftell(m_hFile);
517 if (iPos > m_iLength) { 542 if (iPos > m_iLength) {
518 m_iLength = iPos; 543 m_iLength = iPos;
519 } 544 }
520 } 545 }
521 return iRet; 546 return iRet;
522 } 547 }
523 int32_t CFX_FileStreamImp::WriteString(const FX_WCHAR* pStr, int32_t iLength) { 548 int32_t CFX_FileStreamImp::WriteString(const FX_WCHAR* pStr, int32_t iLength) {
524 ASSERT(m_hFile != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); 549 ASSERT(m_hFile && (GetAccessModes() & FX_STREAMACCESS_Write) != 0);
525 ASSERT(pStr != NULL && iLength > 0); 550 ASSERT(pStr && iLength > 0);
526 int32_t iRet = FXSYS_fwrite(pStr, 2, iLength, m_hFile); 551 int32_t iRet = FXSYS_fwrite(pStr, 2, iLength, m_hFile);
527 if (iRet != 0) { 552 if (iRet != 0) {
528 int32_t iPos = FXSYS_ftell(m_hFile); 553 int32_t iPos = FXSYS_ftell(m_hFile);
529 if (iPos > m_iLength) { 554 if (iPos > m_iLength) {
530 m_iLength = iPos; 555 m_iLength = iPos;
531 } 556 }
532 } 557 }
533 return iRet; 558 return iRet;
534 } 559 }
535 void CFX_FileStreamImp::Flush() { 560 void CFX_FileStreamImp::Flush() {
536 ASSERT(m_hFile != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); 561 ASSERT(m_hFile && (GetAccessModes() & FX_STREAMACCESS_Write) != 0);
537 FXSYS_fflush(m_hFile); 562 FXSYS_fflush(m_hFile);
538 } 563 }
539 FX_BOOL CFX_FileStreamImp::SetLength(int32_t iLength) { 564 FX_BOOL CFX_FileStreamImp::SetLength(int32_t iLength) {
540 ASSERT(m_hFile != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); 565 ASSERT(m_hFile && (GetAccessModes() & FX_STREAMACCESS_Write) != 0);
541 FX_BOOL bRet = FX_fsetsize(m_hFile, iLength); 566 FX_BOOL bRet = FX_fsetsize(m_hFile, iLength);
542 m_iLength = FX_filelength(m_hFile); 567 m_iLength = FX_filelength(m_hFile);
543 return bRet; 568 return bRet;
544 } 569 }
545 CFX_FileReadStreamImp::CFX_FileReadStreamImp() 570 CFX_FileReadStreamImp::CFX_FileReadStreamImp()
546 : m_pFileRead(NULL), m_iPosition(0), m_iLength(0) {} 571 : m_pFileRead(NULL), m_iPosition(0), m_iLength(0) {}
547 FX_BOOL CFX_FileReadStreamImp::LoadFileRead(IFX_FileRead* pFileRead, 572 FX_BOOL CFX_FileReadStreamImp::LoadFileRead(IFX_FileRead* pFileRead,
548 uint32_t dwAccess) { 573 uint32_t dwAccess) {
549 ASSERT(m_pFileRead == NULL && pFileRead != NULL); 574 ASSERT(m_pFileRead == NULL && pFileRead != NULL);
550 if (dwAccess & FX_STREAMACCESS_Write) { 575 if (dwAccess & FX_STREAMACCESS_Write) {
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
793 int32_t CFX_FileWriteStreamImp::WriteString(const FX_WCHAR* pStr, 818 int32_t CFX_FileWriteStreamImp::WriteString(const FX_WCHAR* pStr,
794 int32_t iLength) { 819 int32_t iLength) {
795 return WriteData((const uint8_t*)pStr, iLength * sizeof(FX_WCHAR)); 820 return WriteData((const uint8_t*)pStr, iLength * sizeof(FX_WCHAR));
796 } 821 }
797 void CFX_FileWriteStreamImp::Flush() { 822 void CFX_FileWriteStreamImp::Flush() {
798 if (m_pFileWrite) { 823 if (m_pFileWrite) {
799 m_pFileWrite->Flush(); 824 m_pFileWrite->Flush();
800 } 825 }
801 } 826 }
802 CFX_BufferStreamImp::CFX_BufferStreamImp() 827 CFX_BufferStreamImp::CFX_BufferStreamImp()
803 : CFX_StreamImp(), 828 : m_pData(nullptr), m_iTotalSize(0), m_iPosition(0), m_iLength(0) {}
804 m_pData(NULL), 829
805 m_iTotalSize(0),
806 m_iPosition(0),
807 m_iLength(0) {}
808 FX_BOOL CFX_BufferStreamImp::LoadBuffer(uint8_t* pData, 830 FX_BOOL CFX_BufferStreamImp::LoadBuffer(uint8_t* pData,
809 int32_t iTotalSize, 831 int32_t iTotalSize,
810 uint32_t dwAccess) { 832 uint32_t dwAccess) {
811 ASSERT(m_pData == NULL); 833 ASSERT(m_pData && pData && iTotalSize > 0);
Lei Zhang 2016/05/18 23:43:06 !m_pData
Tom Sepez 2016/05/18 23:47:04 good catch. Done.
812 ASSERT(pData != NULL && iTotalSize > 0); 834 SetAccessModes(dwAccess);
813 m_dwAccess = dwAccess;
814 m_pData = pData; 835 m_pData = pData;
815 m_iTotalSize = iTotalSize; 836 m_iTotalSize = iTotalSize;
816 m_iPosition = 0; 837 m_iPosition = 0;
817 m_iLength = (dwAccess & FX_STREAMACCESS_Write) != 0 ? 0 : iTotalSize; 838 m_iLength = (dwAccess & FX_STREAMACCESS_Write) != 0 ? 0 : iTotalSize;
818 return TRUE; 839 return TRUE;
819 } 840 }
820 int32_t CFX_BufferStreamImp::GetLength() const { 841 int32_t CFX_BufferStreamImp::GetLength() const {
821 ASSERT(m_pData != NULL); 842 ASSERT(m_pData != NULL);
822 return m_iLength; 843 return m_iLength;
823 } 844 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 while (*pSrc && iCount < iLen) { 892 while (*pSrc && iCount < iLen) {
872 *pStr++ = *pSrc++; 893 *pStr++ = *pSrc++;
873 iCount++; 894 iCount++;
874 } 895 }
875 m_iPosition += iCount * 2; 896 m_iPosition += iCount * 2;
876 bEOS = (*pSrc == L'\0') || (m_iPosition >= m_iLength); 897 bEOS = (*pSrc == L'\0') || (m_iPosition >= m_iLength);
877 return iCount; 898 return iCount;
878 } 899 }
879 int32_t CFX_BufferStreamImp::WriteData(const uint8_t* pBuffer, 900 int32_t CFX_BufferStreamImp::WriteData(const uint8_t* pBuffer,
880 int32_t iBufferSize) { 901 int32_t iBufferSize) {
881 ASSERT(m_pData != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); 902 ASSERT(m_pData && (GetAccessModes() & FX_STREAMACCESS_Write) != 0);
882 ASSERT(pBuffer != NULL && iBufferSize > 0); 903 ASSERT(pBuffer && iBufferSize > 0);
883 int32_t iLen = std::min(m_iTotalSize - m_iPosition, iBufferSize); 904 int32_t iLen = std::min(m_iTotalSize - m_iPosition, iBufferSize);
884 if (iLen <= 0) { 905 if (iLen <= 0) {
885 return 0; 906 return 0;
886 } 907 }
887 FXSYS_memcpy(m_pData + m_iPosition, pBuffer, iLen); 908 FXSYS_memcpy(m_pData + m_iPosition, pBuffer, iLen);
888 m_iPosition += iLen; 909 m_iPosition += iLen;
889 if (m_iPosition > m_iLength) { 910 if (m_iPosition > m_iLength) {
890 m_iLength = m_iPosition; 911 m_iLength = m_iPosition;
891 } 912 }
892 return iLen; 913 return iLen;
893 } 914 }
894 int32_t CFX_BufferStreamImp::WriteString(const FX_WCHAR* pStr, 915 int32_t CFX_BufferStreamImp::WriteString(const FX_WCHAR* pStr,
895 int32_t iLength) { 916 int32_t iLength) {
896 ASSERT(m_pData != NULL && (m_dwAccess & FX_STREAMACCESS_Write) != 0); 917 ASSERT(m_pData && (GetAccessModes() & FX_STREAMACCESS_Write) != 0);
897 ASSERT(pStr != NULL && iLength > 0); 918 ASSERT(pStr && iLength > 0);
898 int32_t iLen = std::min((m_iTotalSize - m_iPosition) / 2, iLength); 919 int32_t iLen = std::min((m_iTotalSize - m_iPosition) / 2, iLength);
899 if (iLen <= 0) { 920 if (iLen <= 0) {
900 return 0; 921 return 0;
901 } 922 }
902 FXSYS_memcpy(m_pData + m_iPosition, pStr, iLen * 2); 923 FXSYS_memcpy(m_pData + m_iPosition, pStr, iLen * 2);
903 m_iPosition += iLen * 2; 924 m_iPosition += iLen * 2;
904 if (m_iPosition > m_iLength) { 925 if (m_iPosition > m_iLength) {
905 m_iLength = m_iPosition; 926 m_iLength = m_iPosition;
906 } 927 }
907 return iLen; 928 return iLen;
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
1117 } 1138 }
1118 CFX_Stream::CFX_Stream() 1139 CFX_Stream::CFX_Stream()
1119 : m_eStreamType(FX_SREAMTYPE_Unknown), 1140 : m_eStreamType(FX_SREAMTYPE_Unknown),
1120 m_pStreamImp(NULL), 1141 m_pStreamImp(NULL),
1121 m_dwAccess(0), 1142 m_dwAccess(0),
1122 m_iTotalSize(0), 1143 m_iTotalSize(0),
1123 m_iPosition(0), 1144 m_iPosition(0),
1124 m_iStart(0), 1145 m_iStart(0),
1125 m_iLength(0), 1146 m_iLength(0),
1126 m_iRefCount(1) {} 1147 m_iRefCount(1) {}
1148
1127 CFX_Stream::~CFX_Stream() { 1149 CFX_Stream::~CFX_Stream() {
1128 if (m_eStreamType != FX_STREAMTYPE_Stream && m_pStreamImp != NULL) { 1150 if (m_eStreamType != FX_STREAMTYPE_Stream)
1129 m_pStreamImp->Release(); 1151 delete m_pStreamImp;
1130 }
1131 } 1152 }
1153
1132 FX_BOOL CFX_Stream::LoadFile(const FX_WCHAR* pszSrcFileName, 1154 FX_BOOL CFX_Stream::LoadFile(const FX_WCHAR* pszSrcFileName,
1133 uint32_t dwAccess) { 1155 uint32_t dwAccess) {
1134 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { 1156 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp)
1135 return FALSE; 1157 return FALSE;
1136 } 1158
1137 if (pszSrcFileName == NULL || FXSYS_wcslen(pszSrcFileName) < 1) { 1159 if (!pszSrcFileName || FXSYS_wcslen(pszSrcFileName) < 1)
1138 return FALSE; 1160 return FALSE;
1139 } 1161
1140 m_pStreamImp = new CFX_FileStreamImp(); 1162 std::unique_ptr<CFX_FileStreamImp> pImp(new CFX_FileStreamImp());
1141 FX_BOOL bRet = 1163 if (!pImp->LoadFile(pszSrcFileName, dwAccess))
1142 ((CFX_FileStreamImp*)m_pStreamImp)->LoadFile(pszSrcFileName, dwAccess); 1164 return FALSE;
1143 if (!bRet) { 1165
1144 m_pStreamImp->Release(); 1166 m_pStreamImp = pImp.release();
1145 m_pStreamImp = NULL; 1167 m_eStreamType = FX_STREAMTYPE_File;
1146 } else { 1168 m_dwAccess = dwAccess;
1147 m_eStreamType = FX_STREAMTYPE_File; 1169 m_iLength = m_pStreamImp->GetLength();
1148 m_dwAccess = dwAccess; 1170 return TRUE;
1149 m_iLength = m_pStreamImp->GetLength();
1150 }
1151 return bRet;
1152 } 1171 }
1172
1153 FX_BOOL CFX_Stream::LoadFileRead(IFX_FileRead* pFileRead, uint32_t dwAccess) { 1173 FX_BOOL CFX_Stream::LoadFileRead(IFX_FileRead* pFileRead, uint32_t dwAccess) {
1154 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { 1174 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp)
1155 return FALSE; 1175 return FALSE;
1156 } 1176
1157 if (pFileRead == NULL) { 1177 if (!pFileRead)
1158 return FALSE; 1178 return FALSE;
1159 } 1179
1160 m_pStreamImp = new CFX_FileReadStreamImp(); 1180 std::unique_ptr<CFX_FileReadStreamImp> pImp(new CFX_FileReadStreamImp());
1161 FX_BOOL bRet = 1181 if (!pImp->LoadFileRead(pFileRead, dwAccess))
1162 ((CFX_FileReadStreamImp*)m_pStreamImp)->LoadFileRead(pFileRead, dwAccess); 1182 return FALSE;
1163 if (!bRet) { 1183
1164 m_pStreamImp->Release(); 1184 m_pStreamImp = pImp.release();
1165 m_pStreamImp = NULL; 1185 m_eStreamType = FX_STREAMTYPE_File;
1166 } else { 1186 m_dwAccess = dwAccess;
1167 m_eStreamType = FX_STREAMTYPE_File; 1187 m_iLength = m_pStreamImp->GetLength();
1168 m_dwAccess = dwAccess; 1188 return TRUE;
1169 m_iLength = m_pStreamImp->GetLength();
1170 }
1171 return bRet;
1172 } 1189 }
1190
1173 FX_BOOL CFX_Stream::LoadFileWrite(IFX_FileWrite* pFileWrite, 1191 FX_BOOL CFX_Stream::LoadFileWrite(IFX_FileWrite* pFileWrite,
1174 uint32_t dwAccess) { 1192 uint32_t dwAccess) {
1175 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { 1193 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp)
1176 return FALSE; 1194 return FALSE;
1177 } 1195
1178 if (pFileWrite == NULL) { 1196 if (!pFileWrite)
1179 return FALSE; 1197 return FALSE;
1180 } 1198
1181 m_pStreamImp = new CFX_FileWriteStreamImp(); 1199 std::unique_ptr<CFX_FileWriteStreamImp> pImp(new CFX_FileWriteStreamImp());
1182 FX_BOOL bRet = ((CFX_FileWriteStreamImp*)m_pStreamImp) 1200 if (!pImp->LoadFileWrite(pFileWrite, dwAccess))
1183 ->LoadFileWrite(pFileWrite, dwAccess); 1201 return FALSE;
1184 if (!bRet) { 1202
1185 m_pStreamImp->Release(); 1203 m_pStreamImp = pImp.release();
1186 m_pStreamImp = NULL; 1204 m_eStreamType = FX_STREAMTYPE_File;
1187 } else { 1205 m_dwAccess = dwAccess;
1188 m_eStreamType = FX_STREAMTYPE_File; 1206 m_iLength = m_pStreamImp->GetLength();
1189 m_dwAccess = dwAccess; 1207 return TRUE;
1190 m_iLength = m_pStreamImp->GetLength();
1191 }
1192 return bRet;
1193 } 1208 }
1209
1194 FX_BOOL CFX_Stream::LoadBuffer(uint8_t* pData, 1210 FX_BOOL CFX_Stream::LoadBuffer(uint8_t* pData,
1195 int32_t iTotalSize, 1211 int32_t iTotalSize,
1196 uint32_t dwAccess) { 1212 uint32_t dwAccess) {
1197 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { 1213 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp)
1198 return FALSE; 1214 return FALSE;
1199 } 1215
1200 if (pData == NULL || iTotalSize < 1) { 1216 if (!pData || iTotalSize < 1)
1201 return FALSE; 1217 return FALSE;
1202 } 1218
1203 m_pStreamImp = new CFX_BufferStreamImp(); 1219 std::unique_ptr<CFX_BufferStreamImp> pImp(new CFX_BufferStreamImp());
1204 FX_BOOL bRet = ((CFX_BufferStreamImp*)m_pStreamImp) 1220 if (!pImp->LoadBuffer(pData, iTotalSize, dwAccess))
1205 ->LoadBuffer(pData, iTotalSize, dwAccess); 1221 return FALSE;
1206 if (!bRet) { 1222
1207 m_pStreamImp->Release(); 1223 m_pStreamImp = pImp.release();
1208 m_pStreamImp = NULL; 1224 m_eStreamType = FX_STREAMTYPE_Buffer;
1209 } else { 1225 m_dwAccess = dwAccess;
1210 m_eStreamType = FX_STREAMTYPE_Buffer; 1226 m_iLength = m_pStreamImp->GetLength();
1211 m_dwAccess = dwAccess; 1227 return TRUE;
1212 m_iLength = m_pStreamImp->GetLength();
1213 }
1214 return bRet;
1215 } 1228 }
1229
1216 FX_BOOL CFX_Stream::LoadBufferRead(IFX_BufferRead* pBufferRead, 1230 FX_BOOL CFX_Stream::LoadBufferRead(IFX_BufferRead* pBufferRead,
1217 int32_t iFileSize, 1231 int32_t iFileSize,
1218 uint32_t dwAccess, 1232 uint32_t dwAccess,
1219 FX_BOOL bReleaseBufferRead) { 1233 FX_BOOL bReleaseBufferRead) {
1220 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { 1234 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp)
1221 return FALSE; 1235 return FALSE;
1222 } 1236
1223 if (!pBufferRead) { 1237 if (!pBufferRead)
1224 return FALSE; 1238 return FALSE;
1225 } 1239
1226 m_pStreamImp = new CFX_BufferReadStreamImp; 1240 std::unique_ptr<CFX_BufferReadStreamImp> pImp(new CFX_BufferReadStreamImp);
1227 FX_BOOL bRet = ((CFX_BufferReadStreamImp*)m_pStreamImp) 1241 if (!pImp->LoadBufferRead(pBufferRead, iFileSize, dwAccess,
1228 ->LoadBufferRead(pBufferRead, iFileSize, dwAccess, 1242 bReleaseBufferRead))
1229 bReleaseBufferRead); 1243 return FALSE;
1230 if (!bRet) { 1244
1231 m_pStreamImp->Release(); 1245 m_pStreamImp = pImp.release();
1232 m_pStreamImp = NULL; 1246 m_eStreamType = FX_STREAMTYPE_BufferRead;
1233 } else { 1247 m_dwAccess = dwAccess;
1234 m_eStreamType = FX_STREAMTYPE_BufferRead; 1248 m_iLength = m_pStreamImp->GetLength();
1235 m_dwAccess = dwAccess; 1249 return TRUE;
1236 m_iLength = m_pStreamImp->GetLength();
1237 }
1238 return bRet;
1239 } 1250 }
1251
1240 void CFX_Stream::Release() { 1252 void CFX_Stream::Release() {
1241 if (--m_iRefCount < 1) { 1253 if (--m_iRefCount < 1) {
1242 delete this; 1254 delete this;
1243 } 1255 }
1244 } 1256 }
1245 IFX_Stream* CFX_Stream::Retain() { 1257 IFX_Stream* CFX_Stream::Retain() {
1246 m_iRefCount++; 1258 m_iRefCount++;
1247 return this; 1259 return this;
1248 } 1260 }
1249 int32_t CFX_Stream::GetLength() const { 1261 int32_t CFX_Stream::GetLength() const {
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
1617 return m_pStream->WriteData((const uint8_t*)pData, (int32_t)size) == 1629 return m_pStream->WriteData((const uint8_t*)pData, (int32_t)size) ==
1618 (int32_t)size; 1630 (int32_t)size;
1619 } 1631 }
1620 FX_BOOL CFGAS_FileWrite::WriteBlock(const void* pData, 1632 FX_BOOL CFGAS_FileWrite::WriteBlock(const void* pData,
1621 FX_FILESIZE offset, 1633 FX_FILESIZE offset,
1622 size_t size) { 1634 size_t size) {
1623 m_pStream->Seek(FX_STREAMSEEK_Begin, offset); 1635 m_pStream->Seek(FX_STREAMSEEK_Begin, offset);
1624 int32_t iLen = m_pStream->WriteData((uint8_t*)pData, (int32_t)size); 1636 int32_t iLen = m_pStream->WriteData((uint8_t*)pData, (int32_t)size);
1625 return iLen == (int32_t)size; 1637 return iLen == (int32_t)size;
1626 } 1638 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698