niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ |
| 13 | |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 16 | #include "webrtc/common_types.h" |
| 17 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
| 19 | // Implementation of an InStream and OutStream that can read (exclusive) or |
| 20 | // write from/to a file. |
| 21 | |
| 22 | namespace webrtc { |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 23 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 24 | class FileWrapper : public InStream, public OutStream { |
| 25 | public: |
| 26 | static const size_t kMaxFileNameSize = 1024; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 28 | // Factory method. Constructor disabled. |
| 29 | static FileWrapper* Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 31 | // Returns true if a file has been opened. |
| 32 | virtual bool Open() const = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 34 | // Opens a file in read or write mode, decided by the read_only parameter. |
| 35 | virtual int OpenFile(const char* file_name_utf8, |
| 36 | bool read_only, |
| 37 | bool loop = false, |
| 38 | bool text = false) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 40 | virtual int CloseFile() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 42 | // Limits the file size to |bytes|. Writing will fail after the cap |
| 43 | // is hit. Pass zero to use an unlimited size. |
| 44 | virtual int SetMaxFileSize(size_t bytes) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 46 | // Flush any pending writes. |
| 47 | virtual int Flush() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 49 | // Returns the opened file's name in |file_name_utf8|. Provide the size of |
| 50 | // the buffer in bytes in |size|. The name will be truncated if |size| is |
| 51 | // too small. |
| 52 | virtual int FileName(char* file_name_utf8, |
| 53 | size_t size) const = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 55 | // Write |format| to the opened file. Arguments are taken in the same manner |
| 56 | // as printf. That is, supply a format string containing text and |
| 57 | // specifiers. Returns the number of characters written or -1 on error. |
| 58 | virtual int WriteText(const char* format, ...) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 60 | // Inherited from Instream. |
| 61 | // Reads |length| bytes from file to |buf|. Returns the number of bytes read |
| 62 | // or -1 on error. |
| 63 | virtual int Read(void* buf, int length) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 65 | // Inherited from OutStream. |
| 66 | // Writes |length| bytes from |buf| to file. The actual writing may happen |
| 67 | // some time later. Call Flush() to force a write. |
| 68 | virtual bool Write(const void* buf, int length) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 70 | // Inherited from both Instream and OutStream. |
| 71 | // Rewinds the file to the start. Only available when OpenFile() has been |
| 72 | // called with |loop| == true or |readOnly| == true. |
| 73 | virtual int Rewind() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | }; |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 75 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 76 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 78 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ |