1 /*=============================================================================
2     Copyright (c) 2002 2004 2006 Joel de Guzman
3     Copyright (c) 2004 Eric Niebler
4     Copyright (c) 2005 Thomas Guest
5     Copyright (c) 2013, 2017 Daniel James
6 
7     Use, modification and distribution is subject to the Boost Software
8     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9     http://www.boost.org/LICENSE_1_0.txt)
10 =============================================================================*/
11 
12 #if !defined(BOOST_QUICKBOOK_DETAIL_PATH_HPP)
13 #define BOOST_QUICKBOOK_DETAIL_PATH_HPP
14 
15 #include <boost/filesystem/path.hpp>
16 #include "native_text.hpp"
17 
18 namespace quickbook
19 {
20     namespace fs = boost::filesystem;
21 
22     // The relative path from base to path
23     fs::path path_difference(
24         fs::path const& base, fs::path const& path, bool is_file = false);
25 
26     // Convert a Boost.Filesystem path to a URL.
27     std::string file_path_to_url(fs::path const&);
28     std::string dir_path_to_url(fs::path const&);
29 
30     namespace detail
31     {
32 // 'generic':   Paths in quickbook source and the generated boostbook.
33 //              Always UTF-8.
34 // 'command_line':
35 //              Paths (or other parameters) from the command line and
36 //              possibly other sources in the future. Wide strings on
37 //              normal windows, UTF-8 for cygwin and other platforms
38 //              (hopefully).
39 // 'path':      Stored as a boost::filesystem::path. Since
40 //              Boost.Filesystem doesn't support cygwin, this
41 //              is always wide on windows. UTF-8 on other
42 //              platforms (again, hopefully).
43 
44 #if QUICKBOOK_WIDE_PATHS
45         typedef std::wstring command_line_string;
46 #else
47         typedef std::string command_line_string;
48 #endif
49 
50         std::string command_line_to_utf8(command_line_string const&);
51         fs::path command_line_to_path(command_line_string const&);
52 
53         std::string path_to_generic(fs::path const&);
54         fs::path generic_to_path(quickbook::string_view);
55 
56         stream_string path_to_stream(fs::path const& path);
57 
58         // Command line parameters that might be a path, a url, or empty.
59         // Not very efficient, but won't be used much.
60         class path_or_url
61         {
62             int type_;
63             boost::filesystem::path path_;
64             std::string url_;
65 
66           public:
67             // Creates an empty path_or_url.
68             path_or_url();
69 
70             path_or_url(path_or_url const&);
71 
72             // Stores a parameter as either a path or a URL depending
73             // on whether it looks like an absolute URL (i.e. starts with
74             // 'scheme:')
75             explicit path_or_url(command_line_string const&);
76 
77             path_or_url& operator=(path_or_url const&);
78 
79             path_or_url& operator=(command_line_string const&);
80 
81             void swap(path_or_url&);
82 
83             // Explicity create a URL
84             static path_or_url url(string_view);
85 
86             // Explicitly create a path
87             static path_or_url path(boost::filesystem::path const&);
88 
89             // Returns true if this isn't empty.
90             operator bool() const;
91 
92             // Returns true if contains a path.
93             bool is_path() const;
94 
95             // Returns true is contains a URL.
96             bool is_url() const;
97 
98             // Returns the stored path.
99             // pre: is_path()
100             boost::filesystem::path const& get_path() const;
101 
102             // Returns the stored URL.
103             // pre: is_url()
104             std::string const& get_url() const;
105 
106             // Appends the value, either by path concatenation or URL
107             // concatenation.
108             // Note: a URL will strip text after the last '/', a path won't.
109             //       Maybe should only work when the path is known to be a
110             //       directory?
111             path_or_url operator/(string_view) const;
112         };
113     }
114 }
115 
116 #endif
117