1 /** 2 * Copyright 2019 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_PATH_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_PATH_H_ 18 19 #include <dirent.h> 20 #include <memory> 21 #include <string> 22 23 #include "minddata/dataset/util/status.h" 24 25 namespace mindspore { 26 namespace dataset { 27 class Path { 28 public: 29 class DirIterator { 30 public: 31 static std::shared_ptr<DirIterator> OpenDirectory(Path *f); 32 33 ~DirIterator(); 34 35 bool HasNext(); 36 37 Path Next(); 38 39 private: 40 explicit DirIterator(Path *f); 41 42 Path *dir_ = nullptr; 43 DIR *dp_ = nullptr; 44 struct dirent *entry_ = nullptr; 45 }; 46 47 explicit Path(const std::string &); 48 49 explicit Path(const char *); 50 51 ~Path() = default; 52 53 Path(const Path &); 54 55 Path &operator=(const Path &); 56 57 Path(Path &&) noexcept; 58 59 Path &operator=(Path &&) noexcept; 60 ToString()61 std::string ToString() const { return path_; } 62 63 Path operator+(const Path &); 64 65 Path operator+(const std::string &); 66 67 Path operator+(const char *); 68 69 Path &operator+=(const Path &rhs); 70 71 Path &operator+=(const std::string &); 72 73 Path &operator+=(const char *); 74 75 Path operator/(const Path &); 76 77 Path operator/(const std::string &); 78 79 Path operator/(const char *); 80 81 bool operator==(const Path &rhs) const { return (path_ == rhs.path_); } 82 83 bool operator!=(const Path &rhs) const { return (path_ != rhs.path_); } 84 85 bool operator<(const Path &rhs) const { return (path_ < rhs.path_); } 86 87 bool operator>(const Path &rhs) const { return (path_ > rhs.path_); } 88 89 bool operator<=(const Path &rhs) const { return (path_ <= rhs.path_); } 90 91 bool operator>=(const Path &rhs) const { return (path_ >= rhs.path_); } 92 93 bool Exists(); 94 95 bool IsDirectory(); 96 97 Status CreateDirectory(bool is_common_dir = false); 98 99 Status CreateDirectories(bool is_common_dir = false); 100 101 Status CreateCommonDirectories(); 102 103 std::string Extension() const; 104 105 std::string ParentPath(); 106 107 Status Remove(); 108 109 Status CreateFile(int *fd); 110 111 Status OpenFile(int *fd, bool create = false); 112 113 Status CloseFile(int fd) const; 114 115 Status TruncateFile(int fd) const; 116 117 std::string Basename(); 118 119 static Status RealPath(const std::string &path, std::string &realpath_str); // NOLINT 120 121 friend std::ostream &operator<<(std::ostream &os, const Path &s); 122 123 private: 124 static char separator_; 125 std::string path_; 126 }; 127 } // namespace dataset 128 } // namespace mindspore 129 130 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_PATH_H_ 131