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 // For handling native strings and streams.
10 
11 #if !defined(BOOST_QUICKBOOK_DETAIL_NATIVE_TEXT_HPP)
12 #define BOOST_QUICKBOOK_DETAIL_NATIVE_TEXT_HPP
13 
14 #include <boost/config.hpp>
15 #include <boost/filesystem/path.hpp>
16 #include <boost/utility/string_ref.hpp>
17 #include <string>
18 #include <stdexcept>
19 #include <iostream>
20 #include "fwd.hpp"
21 
22 #if defined(__cygwin__) || defined(__CYGWIN__)
23 #   define QUICKBOOK_CYGWIN_PATHS 1
24 #elif defined(_WIN32)
25 #   define QUICKBOOK_WIDE_PATHS 1
26 #   if defined(BOOST_MSVC) && BOOST_MSVC >= 1400
27 #       define QUICKBOOK_WIDE_STREAMS 1
28 #   endif
29 #endif
30 
31 #if !defined(QUICKBOOK_WIDE_PATHS)
32 #define QUICKBOOK_WIDE_PATHS 0
33 #endif
34 
35 #if !defined(QUICKBOOK_WIDE_STREAMS)
36 #define QUICKBOOK_WIDE_STREAMS 0
37 #endif
38 
39 #if !defined(QUICKBOOK_CYGWIN_PATHS)
40 #define QUICKBOOK_CYGWIN_PATHS 0
41 #endif
42 
43 namespace quickbook
44 {
45     namespace fs = boost::filesystem;
46 
47     namespace detail
48     {
49         struct conversion_error : std::runtime_error
50         {
conversion_errorquickbook::detail::conversion_error51             conversion_error(char const* m) : std::runtime_error(m) {}
52         };
53 
54         // 'generic':   Paths in quickbook source and the generated boostbook.
55         //              Always UTF-8.
56         // 'command_line':
57         //              Paths (or other parameters) from the command line and
58         //              possibly other sources in the future. Wide strings on
59         //              normal windows, UTF-8 for cygwin and other platforms
60         //              (hopefully).
61         // 'path':      Stored as a boost::filesystem::path. Since
62         //              Boost.Filesystem doesn't support cygwin, this
63         //              is always wide on windows. UTF-8 on other
64         //              platforms (again, hopefully).
65 
66 #if QUICKBOOK_WIDE_PATHS
67         typedef std::wstring command_line_string;
68         typedef boost::wstring_ref command_line_string_ref;
69 #else
70         typedef std::string command_line_string;
71         typedef boost::string_ref command_line_string_ref;
72 #endif
73 
74         // A light wrapper around C++'s streams that gets things right
75         // in the quickbook context.
76         //
77         // This is far from perfect but it fixes some issues.
78         struct ostream
79         {
80 #if QUICKBOOK_WIDE_STREAMS
81             typedef std::wostream base_ostream;
82             typedef std::wios base_ios;
83             typedef std::wstring string;
84             typedef boost::wstring_ref string_ref;
85 #else
86             typedef std::ostream base_ostream;
87             typedef std::ios base_ios;
88             typedef std::string string;
89             typedef boost::string_ref string_ref;
90 #endif
91             base_ostream& base;
92 
ostreamquickbook::detail::ostream93             explicit ostream(base_ostream& x) : base(x) {}
94 
95             // C strings should always be ascii.
96             ostream& operator<<(char);
97             ostream& operator<<(char const*);
98 
99             // std::string should be UTF-8 (what a mess!)
100             ostream& operator<<(std::string const&);
101             ostream& operator<<(boost::string_ref);
102 
103             // Other value types.
104             ostream& operator<<(int x);
105             ostream& operator<<(unsigned int x);
106             ostream& operator<<(long x);
107             ostream& operator<<(unsigned long x);
108 
109 #if !defined(BOOST_NO_LONG_LONG)
110             ostream& operator<<(long long x);
111             ostream& operator<<(unsigned long long x);
112 #endif
113 
114             ostream& operator<<(fs::path const&);
115 
116             // Modifiers
117             ostream& operator<<(base_ostream& (*)(base_ostream&));
118             ostream& operator<<(base_ios& (*)(base_ios&));
119         };
120 
121 
122         std::string command_line_to_utf8(command_line_string const&);
123         fs::path command_line_to_path(command_line_string const&);
124 
125         std::string path_to_generic(fs::path const&);
126         fs::path generic_to_path(boost::string_ref);
127 
128         void initialise_output();
129 
130         ostream& out();
131 
132         // Preformats an error/warning message so that it can be parsed by
133         // common IDEs. Uses the ms_errors global to determine if VS format
134         // or GCC format. Returns the stream to continue ouput of the verbose
135         // error message.
136         ostream& outerr();
137         ostream& outerr(fs::path const& file, int line = -1);
138         ostream& outwarn(fs::path const& file, int line = -1);
139         ostream& outerr(file_ptr const&, string_iterator);
140         ostream& outwarn(file_ptr const&, string_iterator);
141     }
142 }
143 
144 #endif
145