OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium 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 #ifndef OTS_FILE_STREAM_H_ | 5 #ifndef OTS_FILE_STREAM_H_ |
6 #define OTS_FILE_STREAM_H_ | 6 #define OTS_FILE_STREAM_H_ |
7 | 7 |
8 #include "opentype-sanitiser.h" | 8 #include "opentype-sanitiser.h" |
9 | 9 |
10 namespace ots { | 10 namespace ots { |
11 | 11 |
12 // An OTSStream implementation for testing. | 12 // An OTSStream implementation for testing. |
13 class FILEStream : public OTSStream { | 13 class FILEStream : public OTSStream { |
14 public: | 14 public: |
15 explicit FILEStream(FILE *stream) | 15 explicit FILEStream(FILE *stream) |
16 : file_(stream), position_(0) { | 16 : file_(stream), position_(0) { |
17 } | 17 } |
18 | 18 |
19 ~FILEStream() { | 19 ~FILEStream() { |
| 20 if (file_) |
| 21 fclose(file_); |
20 } | 22 } |
21 | 23 |
22 bool WriteRaw(const void *data, size_t length) { | 24 bool WriteRaw(const void *data, size_t length) { |
23 if (::fwrite(data, length, 1, file_) == 1) { | 25 if (!file_ || ::fwrite(data, length, 1, file_) == 1) { |
24 position_ += length; | 26 position_ += length; |
25 return true; | 27 return true; |
26 } | 28 } |
27 return false; | 29 return false; |
28 } | 30 } |
29 | 31 |
30 bool Seek(off_t position) { | 32 bool Seek(off_t position) { |
31 #if defined(_WIN32) | 33 #if defined(_WIN32) |
32 if (!::_fseeki64(file_, position, SEEK_SET)) { | 34 if (!file_ || !::_fseeki64(file_, position, SEEK_SET)) { |
33 position_ = position; | 35 position_ = position; |
34 return true; | 36 return true; |
35 } | 37 } |
36 #else | 38 #else |
37 if (!::fseeko(file_, position, SEEK_SET)) { | 39 if (!file_ || !::fseeko(file_, position, SEEK_SET)) { |
38 position_ = position; | 40 position_ = position; |
39 return true; | 41 return true; |
40 } | 42 } |
41 #endif // defined(_WIN32) | 43 #endif // defined(_WIN32) |
42 return false; | 44 return false; |
43 } | 45 } |
44 | 46 |
45 off_t Tell() const { | 47 off_t Tell() const { |
46 return position_; | 48 return position_; |
47 } | 49 } |
48 | 50 |
49 private: | 51 private: |
50 FILE * const file_; | 52 FILE * const file_; |
51 off_t position_; | 53 off_t position_; |
52 }; | 54 }; |
53 | 55 |
54 } // namespace ots | 56 } // namespace ots |
55 | 57 |
56 #endif // OTS_FILE_STREAM_H_ | 58 #endif // OTS_FILE_STREAM_H_ |
OLD | NEW |