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