home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / path.h < prev    next >
C/C++ Source or Header  |  2008-03-17  |  7KB  |  201 lines

  1. #ifndef K3DSDK_PATH_H
  2. #define K3DSDK_PATH_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2007, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.         \author Tim Shead (tshead@k-3d.com)
  25. */
  26.  
  27. #include "types.h"
  28. #include "ustring.h"
  29. #include <boost/iterator/iterator_facade.hpp>
  30. #include <vector>
  31.  
  32. namespace k3d
  33. {
  34.  
  35. namespace filesystem
  36. {
  37.  
  38. /// Portable filesystem path that closely follows the boost::filesystem::path interface, with some important exceptions
  39. /** \note Never throws an exception */
  40. /** \note Stores paths in UTF-8 encoding.  All input and output is in UTF-8 encoding. */
  41. class path
  42. {
  43. public:
  44.     /// Constructs an empty path
  45.     path();
  46.     /// Constructs a path from a string containing the generic grammar
  47.     friend path generic_path(const ustring& GenericPath);
  48.     /// Constructs a path from a string containing the generic grammar (the string is assumed to contain UTF-8 data, no character set conversion is applied)
  49.     friend path generic_path(const string_t& GenericPath);
  50.     /// Constructs a path from a native file string, i.e. any valid Posix or Win32 path (doesn't matter which platform we were compiled on)
  51.     friend path native_path(const ustring& NativePath);
  52.  
  53.     /// Path concatenation
  54.     path& operator/=(const path& rhs);
  55.     /// Path concatenation
  56.     path operator/(const path& rhs) const;
  57.  
  58.     /// Appending text (useful for file extensions)
  59.     path operator+(const ustring& rhs) const;
  60.     /// Appending text (useful for file extensions)
  61.     path operator+(const string_t& rhs) const;
  62.  
  63.     /// Returns the generic form of the path as a UTF-8 encoded string
  64.     ustring generic_utf8_string() const;
  65.     /// Returns the native form of the path as a UTF-8 encoded string
  66.     ustring native_utf8_string() const;
  67.     /// Returns the native form of the path as a string suitable for console output
  68.     string_t native_console_string() const;
  69.     /// Returns the native form of the path as a string suitable for use with the underlying OS filesystem
  70.     string_t native_filesystem_string() const;
  71.  
  72.     // modification functions:
  73. //    path& normalize();
  74.  
  75.     // decomposition functions:
  76.     path root_path() const;
  77.     ustring root_name() const;
  78.     ustring root_directory() const;
  79. //    path relative_path() const;
  80.     ustring leaf() const;
  81.     path branch_path() const;
  82.  
  83.     // query functions:
  84.     bool_t empty() const;
  85.     bool_t is_complete() const;
  86. /*
  87.     bool_t has_root_path() const;
  88.     bool_t has_root_name() const;
  89.     bool_t has_root_directory() const;
  90.     bool_t has_relative_path() const;
  91.     bool_t has_leaf() const;
  92.     bool_t has_branch_path() const;
  93. */
  94.  
  95.     // iteration:
  96.     class iterator
  97.     {
  98.     public:
  99.         typedef std::forward_iterator_tag iterator_category;
  100.         typedef ustring value_type;
  101.         typedef ustring& reference;
  102.         typedef ustring* pointer;
  103.         typedef ptrdiff_t difference_type;
  104.  
  105.         iterator();
  106.         iterator(const ustring& Storage);
  107.         ~iterator();
  108.  
  109.         value_type operator*();
  110.  
  111.         bool_t operator==(const iterator& RHS);
  112.         bool_t operator!=(const iterator& RHS);
  113.         iterator& operator++();
  114.  
  115.     private:
  116.         class implementation;
  117.         implementation* const m_implementation;
  118.     };
  119.  
  120.     iterator begin() const;
  121.     iterator end() const;
  122.  
  123.     // relational operators:
  124.     bool_t operator==(const path& that) const;
  125.     bool_t operator!=(const path& that) const;
  126.     bool_t operator<(const path& that) const;
  127.     bool_t operator<=(const path& that) const;
  128.     bool_t operator>(const path& that) const;
  129.     bool_t operator>=(const path& that) const;
  130.  
  131. private:
  132.     path(const ustring& GenericPath);
  133.     ustring storage;
  134. };
  135.  
  136. /// Constructs a path from a string containing the generic grammar
  137. path generic_path(const ustring& GenericPath);
  138. /// Constructs a path from a string containing the generic grammar (the string is assumed to contain UTF-8 data, no character set conversion is applied)
  139. path generic_path(const string_t& GenericPath);
  140. /// Constructs a path from a native file string, i.e. any valid Posix or Win32 path (doesn't matter which platform we were compiled on)
  141. path native_path(const ustring& NativePath);
  142.  
  143. /// Defines a collection of paths
  144. typedef std::vector<path> path_list;
  145.  
  146. /// Returns true if the operating system reports that the given file exists
  147. bool_t exists(const path& Path);
  148. /// Converts the given absolute path into a relative path, relative to the given reference path
  149. const path make_relative_path(const path& AbsolutePath, const path& ReferencePath);
  150. /// If Path.leaf() contains a dot, returns the substring of Path.leaf() starting from the last dot.  Otherwise, returns empty string
  151. const ustring extension(const path& Path);
  152. /// If Path.leaf() contains a dot, replaces the substring of Path.leaf() starting from the last dot with the given text.  Otherwise, returns empty string.
  153. const path replace_extension(const path& Path, const string_t& NewExtension);
  154. /// Creates the given directory
  155. bool_t create_directory(const path& Path);
  156. /// Creates the given directory, including any missing parent directories
  157. bool_t create_directories(const path& Path);
  158. /// Returns true if the given path is a directory
  159. bool_t is_directory(const path& Path);
  160. /// Removes a file
  161. bool_t remove(const path& Path);
  162. /// Renames a file
  163. bool_t rename(const path& Source, const path& Target);
  164. /// Copies a file
  165. bool_t copy_file(const path& Source, const path& Target);
  166.  
  167. /// Returns true iff the Target exists and is newer than the Source
  168. const bool_t up_to_date(const path& Source, const path& Target);
  169.  
  170. /// Splits a string containing delimited paths (such as the PATH environment variable) into a collection of individual paths
  171. /** \note: Splits the string using the filesystem delimiter for the native system (the system we were compiled on). */
  172. const path_list split_native_paths(const ustring& Paths);
  173.  
  174. /// Provides iteration over the contents of a directory
  175. class directory_iterator :
  176.     public boost::iterator_facade<directory_iterator, const path, boost::single_pass_traversal_tag>
  177. {
  178. public:
  179.     /// Creates the "begin" iterator for the contents of the given directory
  180.     explicit directory_iterator(const path& Path);
  181.     /// Creates an "end" iterator
  182.     directory_iterator();
  183.     ~directory_iterator();
  184.  
  185. private:
  186.     class implementation;
  187.     implementation* m_implementation;
  188.  
  189.     friend class boost::iterator_core_access;
  190.     reference dereference() const;
  191.     void increment();
  192.     bool_t equal(const directory_iterator& rhs) const;
  193. };
  194.  
  195. } // namespace filesystem
  196.  
  197. } // namespace k3d
  198.  
  199. #endif // !K3DSDK_PATH_H
  200.  
  201.