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//===----------------------------------------------------------------------===//
1387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#ifndef MCLD_SUPPORT_PATH_H
1487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#define MCLD_SUPPORT_PATH_H
155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <llvm/Support/raw_ostream.h>
17affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Config/Config.h>
18affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
19affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <iosfwd>
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <functional>
215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <string>
22f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines#include <locale>
235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace mcld {
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace sys  {
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace fs   {
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
28affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#if defined(MCLD_ON_WIN32)
29f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char preferred_separator = '/';
30f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char separator = '/';
315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#else
32f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char preferred_separator = '/';
33f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char separator = '/';
345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
36f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char colon = ':';
37f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesconst char dot = '.';
38f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines
395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class Path
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  \brief Path provides an abstraction for the path to a file or directory in
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *   the operating system's filesystem.
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass Path
445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef char                               ValueType;
47f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  typedef std::string                        StringType;
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path();
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Path(const ValueType* s);
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  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);
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  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);
665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  //  -----  observers  ----- //
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool empty() const;
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool isFromRoot() const;
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool isFromPWD() const;
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
73f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  const StringType& native() const { return m_PathName; }
74f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  StringType&       native()       { return m_PathName; }
755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  const ValueType* c_str() const
775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  { 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
895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
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
935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprotected:
945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  StringType m_PathName;
955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaobool operator==(const Path& pLHS,const Path& pRHS);
985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaobool 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//===----------------------------------------------------------------------===//
1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaobool exists(const Path &pPath);
1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaobool is_directory(const Path &pPath);
1075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
108f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinestemplate <class Char, class Traits>
109f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesinline std::basic_ostream<Char, Traits>&
110f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesoperator<<(std::basic_ostream<Char, Traits>& pOS, const Path& pPath)
111f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines{
112f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  return pOS << pPath.native();
113f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines}
1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
115f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinestemplate <class Char, class Traits>
116f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesinline std::basic_istream<Char, Traits>&
117f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesoperator>>(std::basic_istream<Char, Traits>& pOS, Path& pPath)
118f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines{
119f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  return pOS >> pPath.native();
120f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines}
1215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
122f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesinline llvm::raw_ostream&
123f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hinesoperator<<(llvm::raw_ostream& pOS, const Path& pPath)
124f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines{
125f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  return pOS << pPath.native();
126f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines}
1275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
128f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
129f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines// class path member template implementation
130f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
1315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaotemplate <class InputIterator>
1325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoPath& Path::assign(InputIterator begin, InputIterator end)
1335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
1345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  m_PathName.clear();
1355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if (begin != end)
1365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_PathName.append<InputIterator>(begin, end);
1375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  return *this;
1385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaotemplate <class InputIterator>
1415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoPath& Path::append(InputIterator begin, InputIterator end)
1425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
1435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if (begin == end)
1445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    return *this;
1455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  StringType::size_type sep_pos(m_append_separator_if_needed());
1465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  m_PathName.append<InputIterator>(begin, end);
1475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  if (sep_pos)
1485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    m_erase_redundant_separator(sep_pos);
1495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  return *this;
1505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of fs
1535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of sys
1545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of mcld
1555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
156f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
157f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines// STL compatible functions
158f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines//===----------------------------------------------------------------------===//
1595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace std {
1605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaotemplate<>
1625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct less<mcld::sys::fs::Path> : public binary_function<mcld::sys::fs::Path,
1635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                                         mcld::sys::fs::Path,
1645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                                         bool>
1655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
1665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  bool operator() (const mcld::sys::fs::Path& pX,const mcld::sys::fs::Path& pY) const {
1675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pX.generic_string().size() < pY.generic_string().size())
1685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return true;
1695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    return (pX.generic_string() < pY.generic_string());
1705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
1715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
1725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of std
1745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
1765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
177