Imported Upstream version 1.51.0
[platform/upstream/boost.git] / tools / quickbook / src / input_path.hpp
1 /*=============================================================================
2     Copyright (c) 2009 Daniel James
3
4     Use, modification and distribution is subject to the Boost Software
5     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6     http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8
9 #if !defined(BOOST_QUICKBOOK_DETAIL_INPUT_PATH_HPP)
10 #define BOOST_QUICKBOOK_DETAIL_INPUT_PATH_HPP
11
12 #include <boost/config.hpp>
13 #include <boost/filesystem/path.hpp>
14 #include <string>
15 #include <stdexcept>
16 #include <iostream>
17 #include "fwd.hpp"
18
19 #if defined(__cygwin__) || defined(__CYGWIN__)
20 #   define QUICKBOOK_CYGWIN_PATHS 1
21 #elif defined(_WIN32)
22 #   define QUICKBOOK_WIDE_PATHS 1
23 #   if defined(BOOST_MSVC) && BOOST_MSVC >= 1400
24 #       define QUICKBOOK_WIDE_STREAMS 1
25 #   endif
26 #endif
27
28 #if !defined(QUICKBOOK_WIDE_PATHS)
29 #define QUICKBOOK_WIDE_PATHS 0
30 #endif
31
32 #if !defined(QUICKBOOK_WIDE_STREAMS)
33 #define QUICKBOOK_WIDE_STREAMS 0
34 #endif
35
36 #if !defined(QUICKBOOK_CYGWIN_PATHS)
37 #define QUICKBOOK_CYGWIN_PATHS 0
38 #endif
39
40 namespace quickbook
41 {
42     namespace fs = boost::filesystem;
43
44     namespace detail
45     {
46         struct conversion_error : std::runtime_error
47         {
48             conversion_error(char const* m) : std::runtime_error(m) {}
49         };
50
51         // 'generic':   Paths in quickbook source and the generated boostbook.
52         //              Always UTF-8.
53         // 'input':     Paths (or other parameters) from the command line and
54         //              possibly other sources in the future. Wide strings on
55         //              normal windows, UTF-8 for cygwin and other platforms
56         //              (hopefully).
57         // 'stream':    Strings to be written to a stream.
58         // 'path':      Stored as a boost::filesystem::path. Since
59         //              Boost.Filesystem doesn't support cygwin, this
60         //              is always wide on windows. UTF-8 on other
61         //              platforms (again, hopefully).
62     
63 #if QUICKBOOK_WIDE_PATHS
64         typedef std::wstring input_string;
65 #else
66         typedef std::string input_string;
67 #endif
68
69         // A light wrapper around C++'s streams that gets things right
70         // in the quickbook context.
71         //
72         // This is far from perfect but it fixes some issues.
73         struct ostream
74         {
75 #if QUICKBOOK_WIDE_STREAMS
76             typedef std::wostream base_ostream;
77             typedef std::wios base_ios;
78             typedef std::wstring string;
79 #else
80             typedef std::ostream base_ostream;
81             typedef std::ios base_ios;
82             typedef std::string string;
83 #endif
84             base_ostream& base;
85
86             explicit ostream(base_ostream& x) : base(x) {}
87
88             // C strings should always be ascii.
89             ostream& operator<<(char);
90             ostream& operator<<(char const*);
91
92             // std::string should be UTF-8 (what a mess!)
93             ostream& operator<<(std::string const&);
94
95             // Other value types.
96             ostream& operator<<(int x);
97             ostream& operator<<(unsigned int x);
98             ostream& operator<<(long x);
99             ostream& operator<<(unsigned long x);
100             ostream& operator<<(fs::path const&);
101
102             // Modifiers
103             ostream& operator<<(base_ostream& (*)(base_ostream&));
104             ostream& operator<<(base_ios& (*)(base_ios&));
105         };
106
107
108         std::string input_to_utf8(input_string const&);
109         fs::path input_to_path(input_string const&);
110     
111         std::string path_to_generic(fs::path const&);
112         fs::path generic_to_path(std::string const&);
113
114         void initialise_output();
115         
116         ostream& out();
117
118         // Preformats an error/warning message so that it can be parsed by
119         // common IDEs. Uses the ms_errors global to determine if VS format
120         // or GCC format. Returns the stream to continue ouput of the verbose
121         // error message.
122         ostream& outerr();
123         ostream& outerr(fs::path const& file, int line = -1);
124         ostream& outwarn(fs::path const& file, int line = -1);
125         ostream& outerr(file_ptr const&, string_iterator);
126         ostream& outwarn(file_ptr const&, string_iterator);
127     }
128 }
129
130 #endif