path.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2008 The FLWOR Foundation.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef ZORBA_PATH_H
00018 #define ZORBA_PATH_H
00019 
00020 #include <string>
00021 #include <iostream>
00022 #include <zorba/config.h>
00023 
00024 namespace zorba {
00025 
00026 class ZORBA_DLL_PUBLIC filesystem_path {
00027 private:
00028   std::string path;
00029 
00030   void canonicalize ();
00031 
00032 protected:
00033   std::string
00034     getPathString() const;
00035 
00036 public:
00037   enum flags_t {
00038     CONVERT_SLASHES = 1,
00039     RESOLVE         = 2
00040   };
00041 
00042 public:
00043 
00044   /**
00045     * @brief Utility function to normalize a path as a system conforming
00046     *        path and optionally resolve it.
00047     *
00048     * This function takes a path (as system path, file uri) and normalizes it.
00049     * It converts file URIs to system paths and replaces '/' and '\' to the
00050     * platform specific directory separator ('\' on Windows, '/' on UNIX like
00051     * operating systems like Linux and Mac OS X).
00052     * If the parameter base is set, it also resolves the path.
00053     *
00054     * @param in The path to normalize.
00055     * @param base The base name to resolve a path (default = ""), if this is the
00056     *             empty string, it does not resolve anything, but only normalizes
00057     *             the path.
00058     * @return Returns a normalized and optionally a resolved path.
00059     */
00060   static std::string normalize_path(std::string const &in, std::string const &base = "");
00061 
00062   // from current dir
00063   filesystem_path ();
00064 
00065   filesystem_path (const std::string &path_, int flags = 0);
00066 
00067   filesystem_path (const filesystem_path &base, const filesystem_path &rel) {
00068     if (rel.is_complete ())
00069       *this = rel;
00070     else {
00071       *this = base.get_path () + get_directory_separator () + rel.get_path ();
00072       canonicalize ();
00073     }
00074   }
00075 
00076   filesystem_path &operator = (const std::string &p_)
00077   { path = p_; canonicalize (); return *this; }
00078 
00079   const std::string &get_path () const { return path; }
00080   const char *c_str () const { return path.c_str (); }
00081   operator const std::string & () const { return path; }
00082 
00083   bool is_complete () const;
00084   bool is_root () const;
00085   void resolve_relative ();
00086 
00087   filesystem_path branch_path () const;
00088 
00089   static const char *get_directory_separator ();
00090   static const char *get_path_separator ();
00091 };
00092 
00093 inline std::ostream &operator<< (std::ostream &os, const filesystem_path &p) {
00094   return os << p.get_path ();
00095 }
00096 
00097 
00098 } // namespace zorba
00099 #endif /* ZORBA_PATH_H */
00100 /*
00101 * Local variables:
00102 * mode: c++
00103 * End:
00104 */
00105 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus