blob: 2ab6875b96517090704fb0a08cd9866ea6058a79 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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.org5ae19de2011-12-13 22:59:33 +000014#include <stddef.h>
15
phoglund@webrtc.org740be442012-12-12 12:52:15 +000016#include "webrtc/common_types.h"
17#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19// Implementation of an InStream and OutStream that can read (exclusive) or
20// write from/to a file.
21
22namespace webrtc {
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000023
phoglund@webrtc.org740be442012-12-12 12:52:15 +000024class FileWrapper : public InStream, public OutStream {
25 public:
26 static const size_t kMaxFileNameSize = 1024;
niklase@google.com470e71d2011-07-07 08:21:25 +000027
phoglund@webrtc.org740be442012-12-12 12:52:15 +000028 // Factory method. Constructor disabled.
29 static FileWrapper* Create();
niklase@google.com470e71d2011-07-07 08:21:25 +000030
phoglund@webrtc.org740be442012-12-12 12:52:15 +000031 // Returns true if a file has been opened.
32 virtual bool Open() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000033
phoglund@webrtc.org740be442012-12-12 12:52:15 +000034 // 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.com470e71d2011-07-07 08:21:25 +000039
phoglund@webrtc.org740be442012-12-12 12:52:15 +000040 virtual int CloseFile() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000041
phoglund@webrtc.org740be442012-12-12 12:52:15 +000042 // 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.com470e71d2011-07-07 08:21:25 +000045
phoglund@webrtc.org740be442012-12-12 12:52:15 +000046 // Flush any pending writes.
47 virtual int Flush() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000048
phoglund@webrtc.org740be442012-12-12 12:52:15 +000049 // 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.com470e71d2011-07-07 08:21:25 +000054
phoglund@webrtc.org740be442012-12-12 12:52:15 +000055 // 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.com470e71d2011-07-07 08:21:25 +000059
phoglund@webrtc.org740be442012-12-12 12:52:15 +000060 // 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.com470e71d2011-07-07 08:21:25 +000064
phoglund@webrtc.org740be442012-12-12 12:52:15 +000065 // 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.com470e71d2011-07-07 08:21:25 +000069
phoglund@webrtc.org740be442012-12-12 12:52:15 +000070 // 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.com470e71d2011-07-07 08:21:25 +000074};
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000075
niklase@google.com470e71d2011-07-07 08:21:25 +000076} // namespace webrtc
77
phoglund@webrtc.org740be442012-12-12 12:52:15 +000078#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_