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 <memory> 20 #include <string> 21 #include "utils/os.h" 22 #include "minddata/dataset/util/status.h" 23 24 namespace mindspore { 25 namespace dataset { 26 class Path { 27 public: 28 class DirIterator { 29 public: 30 static std::shared_ptr<DirIterator> OpenDirectory(Path *f); 31 32 ~DirIterator(); 33 34 bool HasNext(); 35 36 Path Next(); 37 38 private: 39 explicit DirIterator(Path *f); 40 41 Path *dir_ = nullptr; 42 DIR *dp_ = nullptr; 43 struct dirent *entry_ = nullptr; 44 }; 45 46 explicit Path(const std::string &); 47 48 explicit Path(const char *); 49 50 ~Path() = default; 51 52 Path(const Path &); 53 54 Path &operator=(const Path &); 55 56 Path(Path &&) noexcept; 57 58 Path &operator=(Path &&) noexcept; 59 ToString()60 std::string ToString() const { return path_; } 61 62 Path operator+(const Path &); 63 64 Path operator+(const std::string &); 65 66 Path operator+(const char *); 67 68 Path &operator+=(const Path &rhs); 69 70 Path &operator+=(const std::string &); 71 72 Path &operator+=(const char *); 73 74 Path operator/(const Path &); 75 76 Path operator/(const std::string &); 77 78 Path operator/(const char *); 79 80 bool operator==(const Path &rhs) const { return (path_ == rhs.path_); } 81 82 bool operator!=(const Path &rhs) const { return (path_ != rhs.path_); } 83 84 bool operator<(const Path &rhs) const { return (path_ < rhs.path_); } 85 86 bool operator>(const Path &rhs) const { return (path_ > rhs.path_); } 87 88 bool operator<=(const Path &rhs) const { return (path_ <= rhs.path_); } 89 90 bool operator>=(const Path &rhs) const { return (path_ >= rhs.path_); } 91 92 bool Exists(); 93 94 bool IsDirectory(); 95 96 bool IsFile(); 97 98 Status CreateDirectory(bool is_common_dir = false); 99 100 Status CreateDirectories(bool is_common_dir = false); 101 102 Status CreateCommonDirectories(); 103 104 std::string Extension() const; 105 106 std::string ParentPath(); 107 108 Status Remove(); 109 110 Status CreateFile(int *fd); 111 112 Status OpenFile(int *fd, bool create = false); 113 114 Status CloseFile(int fd) const; 115 116 Status TruncateFile(int fd) const; 117 118 std::string Basename(); 119 120 static Status RealPath(const std::string &path, std::string &realpath_str); // NOLINT 121 122 friend std::ostream &operator<<(std::ostream &os, const Path &s); 123 124 private: 125 static char separator_; 126 std::string path_; 127 }; 128 } // namespace dataset 129 } // namespace mindspore 130 131 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_PATH_H_ 132