Path.h revision 37b74a387bb3993387029859c2d9d051c41c724e
15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- Path.h -------------------------------------------------------------===//
25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//                     The MCLinker Project
45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source
65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details.
75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
9f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines// This file declares the mcld::sys::fs::Path. It follows TR2/boost
105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// filesystem (v3), but modified to remove exception handling and the
115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// path class.
125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#ifndef MCLD_SUPPORT_PATH_H_
1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_SUPPORT_PATH_H_
1537b74a387bb3993387029859c2d9d051c41c724eStephen Hines
1637b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Config/Config.h"
175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <llvm/Support/raw_ostream.h>
19affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
20affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <iosfwd>
215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <functional>
225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <string>
23f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines#include <locale>
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace mcld {
2637b74a387bb3993387029859c2d9d051c41c724eStephen Hinesnamespace sys {
2737b74a387bb3993387029859c2d9d051c41c724eStephen Hinesnamespace fs {
285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
29affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#if defined(MCLD_ON_WIN32)
30f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char preferred_separator = '/';
31f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char separator = '/';
325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#else
33f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char preferred_separator = '/';
34f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char separator = '/';
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
37f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char colon = ':';
38f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char dot = '.';
39f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class Path
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  \brief Path provides an abstraction for the path to a file or directory in
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *   the operating system's filesystem.
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */
4437b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass Path {
4537b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
4637b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef char ValueType;
4737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef std::string StringType;
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4937b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path();
5137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  explicit Path(const ValueType* s);
5237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  explicit Path(const StringType& s);
535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path(const Path& pCopy);
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  virtual ~Path();
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // -----  assignments  ----- //
575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  template <class InputIterator>
585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path& assign(InputIterator begin, InputIterator end);
5937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  Path& assign(const StringType& s);
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path& assign(const ValueType* s, unsigned int length);
615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  //  -----  appends  ----- //
635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  template <class InputIterator>
645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path& append(InputIterator begin, InputIterator end);
655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path& append(const Path& pPath);
6637b74a387bb3993387029859c2d9d051c41c724eStephen Hines  Path& append(const StringType& pPath);
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  //  -----  observers  ----- //
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool empty() const;
705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool isFromRoot() const;
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool isFromPWD() const;
735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
74f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  const StringType& native() const { return m_PathName; }
7537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  StringType& native() { return m_PathName; }
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
7737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  const ValueType* c_str() const { return m_PathName.c_str(); }
785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // -----  decomposition  ----- //
80cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao  Path parent_path() const;
8122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Path filename() const;
825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path stem() const;
835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path extension() const;
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // -----  generic form observers  ----- //
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  StringType generic_string() const;
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool canonicalize();
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
8937b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  StringType::size_type m_append_separator_if_needed();
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void m_erase_redundant_separator(StringType::size_type sep_pos);
925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
9337b74a387bb3993387029859c2d9d051c41c724eStephen Hines protected:
945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  StringType m_PathName;
955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
9737b74a387bb3993387029859c2d9d051c41c724eStephen Hinesbool operator==(const Path& pLHS, const Path& pRHS);
9837b74a387bb3993387029859c2d9d051c41c724eStephen Hinesbool operator!=(const Path& pLHS, const Path& pRHS);
9967e37f1be98c926645219cfb47fab9e90d8c725cShih-wei LiaoPath operator+(const Path& pLHS, const Path& pRHS);
1005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
101f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
102f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines// Non-member Functions
103f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
10437b74a387bb3993387029859c2d9d051c41c724eStephen Hinesbool exists(const Path& pPath);
1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
10637b74a387bb3993387029859c2d9d051c41c724eStephen Hinesbool is_directory(const Path& pPath);
1075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
108f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinestemplate <class Char, class Traits>
10937b74a387bb3993387029859c2d9d051c41c724eStephen Hinesinline std::basic_ostream<Char, Traits>& operator<<(
11037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    std::basic_ostream<Char, Traits>& pOS,
11137b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const Path& pPath) {
112f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  return pOS << pPath.native();
113f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines}
1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
115f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinestemplate <class Char, class Traits>
11637b74a387bb3993387029859c2d9d051c41c724eStephen Hinesinline std::basic_istream<Char, Traits>& operator>>(
11737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    std::basic_istream<Char, Traits>& pOS,
11837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    Path& pPath) {
119f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  return pOS >> pPath.native();
120f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines}
1215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
12237b74a387bb3993387029859c2d9d051c41c724eStephen Hinesinline llvm::raw_ostream& operator<<(llvm::raw_ostream& pOS,
12337b74a387bb3993387029859c2d9d051c41c724eStephen Hines                                     const Path& pPath) {
124f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  return pOS << pPath.native();
125f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines}
1265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
127f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
128f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines// class path member template implementation
129f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
1305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaotemplate <class InputIterator>
13137b74a387bb3993387029859c2d9d051c41c724eStephen HinesPath& Path::assign(InputIterator begin, InputIterator end) {
1325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  m_PathName.clear();
1335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if (begin != end)
1345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_PathName.append<InputIterator>(begin, end);
1355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  return *this;
1365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaotemplate <class InputIterator>
13937b74a387bb3993387029859c2d9d051c41c724eStephen HinesPath& Path::append(InputIterator begin, InputIterator end) {
1405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if (begin == end)
1415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    return *this;
1425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  StringType::size_type sep_pos(m_append_separator_if_needed());
1435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  m_PathName.append<InputIterator>(begin, end);
1445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if (sep_pos)
1455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_erase_redundant_separator(sep_pos);
1465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  return *this;
1475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
14937b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace fs
15037b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace sys
15137b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace mcld
1525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
153f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
154f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines// STL compatible functions
155f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
1565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace std {
1575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
15837b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <>
15937b74a387bb3993387029859c2d9d051c41c724eStephen Hinesstruct less<mcld::sys::fs::Path>
16037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    : public binary_function<mcld::sys::fs::Path, mcld::sys::fs::Path, bool> {
16137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  bool operator()(const mcld::sys::fs::Path& pX,
16237b74a387bb3993387029859c2d9d051c41c724eStephen Hines                  const mcld::sys::fs::Path& pY) const {
1635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pX.generic_string().size() < pY.generic_string().size())
1645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return true;
1655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    return (pX.generic_string() < pY.generic_string());
1665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
1675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
1685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
16937b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace std
1705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
17137b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif  // MCLD_SUPPORT_PATH_H_
172