You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

path.h 3.0 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <dirent.h>
  19. #include <memory>
  20. #include <string>
  21. #include "minddata/dataset/util/status.h"
  22. namespace mindspore {
  23. namespace dataset {
  24. class Path {
  25. public:
  26. class DirIterator {
  27. public:
  28. static std::shared_ptr<DirIterator> OpenDirectory(Path *f);
  29. ~DirIterator();
  30. bool HasNext();
  31. Path Next();
  32. private:
  33. explicit DirIterator(Path *f);
  34. Path *dir_ = nullptr;
  35. DIR *dp_ = nullptr;
  36. struct dirent *entry_ = nullptr;
  37. };
  38. explicit Path(const std::string &);
  39. explicit Path(const char *);
  40. ~Path() = default;
  41. Path(const Path &);
  42. Path &operator=(const Path &);
  43. Path(Path &&) noexcept;
  44. Path &operator=(Path &&) noexcept;
  45. std::string ToString() const { return path_; }
  46. Path operator+(const Path &);
  47. Path operator+(const std::string &);
  48. Path operator+(const char *);
  49. Path &operator+=(const Path &rhs);
  50. Path &operator+=(const std::string &);
  51. Path &operator+=(const char *);
  52. Path operator/(const Path &);
  53. Path operator/(const std::string &);
  54. Path operator/(const char *);
  55. bool operator==(const Path &rhs) const { return (path_ == rhs.path_); }
  56. bool operator!=(const Path &rhs) const { return (path_ != rhs.path_); }
  57. bool operator<(const Path &rhs) const { return (path_ < rhs.path_); }
  58. bool operator>(const Path &rhs) const { return (path_ > rhs.path_); }
  59. bool operator<=(const Path &rhs) const { return (path_ <= rhs.path_); }
  60. bool operator>=(const Path &rhs) const { return (path_ >= rhs.path_); }
  61. bool Exists();
  62. bool IsDirectory();
  63. Status CreateDirectory(bool is_common_dir = false);
  64. Status CreateDirectories(bool is_common_dir = false);
  65. Status CreateCommonDirectories();
  66. std::string Extension() const;
  67. std::string ParentPath();
  68. Status Remove();
  69. Status CreateFile(int *fd);
  70. Status OpenFile(int *fd, bool create = false);
  71. Status CloseFile(int fd) const;
  72. Status TruncateFile(int fd) const;
  73. std::string Basename();
  74. static Status RealPath(const std::string &path, std::string &realpath_str); // NOLINT
  75. friend std::ostream &operator<<(std::ostream &os, const Path &s);
  76. private:
  77. static char separator_;
  78. std::string path_;
  79. };
  80. } // namespace dataset
  81. } // namespace mindspore
  82. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_PATH_H_