Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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_BASE_FILE_H_ |
| 12 | #define WEBRTC_BASE_FILE_H_ |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include <string> |
| 17 | |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 18 | #include "webrtc/base/constructormagic.h" |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 19 | #include "webrtc/base/pathutils.h" |
| 20 | #include "webrtc/base/platform_file.h" |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | // This class wraps the platform specific APIs for simple file interactions. |
| 25 | // |
| 26 | // The various read and write methods are best effort, i.e. if an underlying |
| 27 | // call does not manage to read/write all the data more calls will be performed, |
| 28 | // until an error is detected or all data is read/written. |
| 29 | class File { |
| 30 | public: |
| 31 | // Wraps the given PlatformFile. This class is then responsible for closing |
| 32 | // the file, which will be done in the destructor if Close is never called. |
| 33 | explicit File(PlatformFile); |
Viktor Palmkvist | 1d477ea | 2016-10-03 13:16:02 +0200 | [diff] [blame] | 34 | // The default constructor produces a closed file. |
| 35 | File(); |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 36 | ~File(); |
| 37 | |
| 38 | File(File&& other); |
| 39 | File& operator=(File&& other); |
| 40 | |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 41 | // Open and Create give files with both reading and writing enabled. |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 42 | static File Open(const std::string& path); |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 43 | static File Open(Pathname&& path); |
| 44 | static File Open(const Pathname& path); |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 45 | // If the file already exists it will be overwritten. |
| 46 | static File Create(const std::string& path); |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 47 | static File Create(Pathname&& path); |
| 48 | static File Create(const Pathname& path); |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 49 | |
zijiehe | 2769ec6 | 2016-12-14 15:03:03 -0800 | [diff] [blame] | 50 | // Remove a file in the file system. |
| 51 | static bool Remove(const std::string& path); |
| 52 | static bool Remove(Pathname&& path); |
| 53 | static bool Remove(const Pathname& path); |
| 54 | |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 55 | size_t Write(const uint8_t* data, size_t length); |
| 56 | size_t Read(uint8_t* buffer, size_t length); |
| 57 | |
| 58 | // The current position in the file after a call to these methods is platform |
| 59 | // dependent (MSVC gives position offset+length, most other |
| 60 | // compilers/platforms do not alter the position), i.e. do not depend on it, |
| 61 | // do a Seek before any subsequent Read/Write. |
| 62 | size_t WriteAt(const uint8_t* data, size_t length, size_t offset); |
| 63 | size_t ReadAt(uint8_t* buffer, size_t length, size_t offset); |
| 64 | |
| 65 | // Attempt to position the file at the given offset from the start. |
| 66 | // Returns true if successful, false otherwise. |
| 67 | bool Seek(size_t offset); |
| 68 | |
| 69 | // Attempt to close the file. Returns true if successful, false otherwise, |
| 70 | // most notably when the file is already closed. |
| 71 | bool Close(); |
| 72 | |
| 73 | bool IsOpen(); |
| 74 | |
| 75 | private: |
| 76 | PlatformFile file_; |
| 77 | RTC_DISALLOW_COPY_AND_ASSIGN(File); |
| 78 | }; |
| 79 | |
| 80 | } // namespace rtc |
| 81 | |
| 82 | #endif // WEBRTC_BASE_FILE_H_ |