path.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
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 
17 #ifndef ZORBA_PATH_H
18 #define ZORBA_PATH_H
19 
20 #include <string>
21 #include <iostream>
22 #include <zorba/config.h>
23 
24 namespace zorba {
25 
26 class ZORBA_DLL_PUBLIC filesystem_path {
27 private:
28  std::string path;
29 
30  void canonicalize ();
31 
32 protected:
33  std::string
34  getPathString() const;
35 
36 public:
37  enum flags_t {
38  CONVERT_SLASHES = 1,
39  RESOLVE = 2
40  };
41 
42 public:
43 
44  /**
45  * @brief Utility function to normalize a path as a system conforming
46  * path and optionally resolve it.
47  *
48  * This function takes a path (as system path, file uri) and normalizes it.
49  * It converts file URIs to system paths and replaces '/' and '\' to the
50  * platform specific directory separator ('\' on Windows, '/' on UNIX like
51  * operating systems like Linux and Mac OS X).
52  * If the parameter base is set, it also resolves the path.
53  *
54  * @param in The path to normalize.
55  * @param base The base name to resolve a path (default = ""), if this is the
56  * empty string, it does not resolve anything, but only normalizes
57  * the path.
58  * @return Returns a normalized and optionally a resolved path.
59  */
60  static std::string normalize_path(std::string const &in, std::string const &base = "");
61 
62  // from current dir
63  filesystem_path ();
64 
65  filesystem_path (const std::string &path_, int flags = 0);
66 
67  filesystem_path (const filesystem_path &base, const filesystem_path &rel) {
68  if (rel.is_complete ())
69  *this = rel;
70  else {
71  *this = base.get_path () + get_directory_separator () + rel.get_path ();
72  canonicalize ();
73  }
74  }
75 
76  filesystem_path &operator = (const std::string &p_)
77  { path = p_; canonicalize (); return *this; }
78 
79  const std::string &get_path () const { return path; }
80  const char *c_str () const { return path.c_str (); }
81  operator const std::string & () const { return path; }
82 
83  bool is_complete () const;
84  bool is_root () const;
85  void resolve_relative ();
86 
87  filesystem_path branch_path () const;
88 
89  static const char *get_directory_separator ();
90  static const char *get_path_separator ();
91 };
92 
93 inline std::ostream &operator<< (std::ostream &os, const filesystem_path &p) {
94  return os << p.get_path ();
95 }
96 
97 
98 } // namespace zorba
99 #endif /* ZORBA_PATH_H */
100 /*
101 * Local variables:
102 * mode: c++
103 * End:
104 */
105 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus