/** * @brief The class for file path operations. * */ classfilepath { public: /** * @brief Construct a new filepath object * * @param path file path string */ filepath(const std::string &path);
/** * @brief Construct a new filepath object by copy * * @param other other filepath object */ filepath(const filepath &other);
/** * @brief Assign operator, assign a new filepath object by copy * * @param other other filepath object * @return filepath& the reference of the current filepath object */ filepath &operator=(const filepath &other);
public: /** * @brief Get the path separator of the current os platform. * * @return the path separator */ staticcharseparator();
/** * @brief Get the string of the filepath. * * @return the filepath */ std::string str()const;
/** * @brief Join the current filepath with a new filename. * * @param filename the filename to be joined * @return the new filepath object */ filepath join(const std::string &filename)const;
/** * @brief Get the parent directory of the filepath. * * @return parent directory path */ std::string dirname()const;
/** * @brief Get the filename or directory name of the filepath. * * @return filename or directory name */ std::string basename()const; /** * @brief Get the extension of the filepath. * * @return extension with dot */ std::string extension()const;
private: std::string filepath_; };
/** * @brief Define the output stream operator for filepath object. * * @param os the std::ostream object * @param fp the filepath object to be output * @return std::ostream& the reference of the std::ostream object after outputing the filepath object. */ std::ostream &operator<<(std::ostream &os, const filepath &fp);
/** * @brief Create a filepath object from a string. * * @param path file path string * @return filepath object */ filepath path(const std::string &path);
std::string filepath::dirname()const { auto index = filepath_.find_last_of(separator()); if (index == std::string::npos) { return""; } return filepath_.substr(0, index); }
std::string filepath::basename()const { auto index = filepath_.find_last_of(separator()); // auto len = filepath_.length() - index - 1; if (index == std::string::npos) { return filepath_; } return filepath_.substr(index + 1); }
std::string filepath::extension()const { auto pos = filepath_.find_last_of('.'); if (pos == std::string::npos) { return""; }