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 | #include "webrtc/base/file.h" |
| 12 | |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame^] | 13 | #include <utility> |
| 14 | |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 15 | namespace rtc { |
| 16 | |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame^] | 17 | namespace { |
| 18 | |
| 19 | std::string NormalizePathname(Pathname&& path) { |
| 20 | path.Normalize(); |
| 21 | return path.pathname(); |
| 22 | } |
| 23 | |
| 24 | } // namespace |
| 25 | |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 26 | File::File(PlatformFile file) : file_(file) {} |
| 27 | |
Viktor Palmkvist | 1d477ea | 2016-10-03 13:16:02 +0200 | [diff] [blame] | 28 | File::File() : file_(kInvalidPlatformFileValue) {} |
| 29 | |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 30 | File::~File() { |
| 31 | Close(); |
| 32 | } |
| 33 | |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame^] | 34 | // static |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 35 | File File::Open(const std::string& path) { |
| 36 | return File(OpenPlatformFile(path)); |
| 37 | } |
| 38 | |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame^] | 39 | // static |
| 40 | File File::Open(Pathname&& path) { |
| 41 | return Open(NormalizePathname(std::move(path))); |
| 42 | } |
| 43 | |
| 44 | // static |
| 45 | File File::Open(const Pathname& path) { |
| 46 | return Open(Pathname(path)); |
| 47 | } |
| 48 | |
| 49 | // static |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 50 | File File::Create(const std::string& path) { |
| 51 | return File(CreatePlatformFile(path)); |
| 52 | } |
| 53 | |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame^] | 54 | // static |
| 55 | File File::Create(Pathname&& path) { |
| 56 | return Create(NormalizePathname(std::move(path))); |
| 57 | } |
| 58 | |
| 59 | // static |
| 60 | File File::Create(const Pathname& path) { |
| 61 | return Create(Pathname(path)); |
| 62 | } |
| 63 | |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 64 | File::File(File&& other) : file_(other.file_) { |
| 65 | other.file_ = kInvalidPlatformFileValue; |
| 66 | } |
| 67 | |
| 68 | File& File::operator=(File&& other) { |
| 69 | Close(); |
| 70 | file_ = other.file_; |
| 71 | other.file_ = kInvalidPlatformFileValue; |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | bool File::IsOpen() { |
| 76 | return file_ != kInvalidPlatformFileValue; |
| 77 | } |
| 78 | |
| 79 | } // namespace rtc |