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