1 | /*============================================================================= |
---|
2 | Copyright (c) 2002 2004 Joel de Guzman |
---|
3 | Copyright (c) 2004 Eric Niebler |
---|
4 | http://spirit.sourceforge.net/ |
---|
5 | |
---|
6 | Use, modification and distribution is subject to the Boost Software |
---|
7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
8 | http://www.boost.org/LICENSE_1_0.txt) |
---|
9 | =============================================================================*/ |
---|
10 | #if !defined(BOOST_SPIRIT_QUICKBOOK_UTILS_HPP) |
---|
11 | #define BOOST_SPIRIT_QUICKBOOK_UTILS_HPP |
---|
12 | |
---|
13 | #include <string> |
---|
14 | #include <iostream> |
---|
15 | #include <boost/ref.hpp> |
---|
16 | #include <boost/assert.hpp> |
---|
17 | |
---|
18 | namespace quickbook { namespace detail |
---|
19 | { |
---|
20 | template <typename Char> |
---|
21 | inline void |
---|
22 | print_char(Char ch, std::ostream& out) |
---|
23 | { |
---|
24 | switch (ch) |
---|
25 | { |
---|
26 | case '<': out << "<"; break; |
---|
27 | case '>': out << ">"; break; |
---|
28 | case '&': out << "&"; break; |
---|
29 | case '"': out << """; break; |
---|
30 | default: out << ch; break; |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | template <typename Char> |
---|
35 | inline void |
---|
36 | print_space(Char ch, std::ostream& out) |
---|
37 | { |
---|
38 | switch (ch) |
---|
39 | { |
---|
40 | case ' ': out << " "; break; |
---|
41 | default: out << ch; break; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | template <typename Char> |
---|
46 | inline Char |
---|
47 | filter_identifier_char(Char ch) |
---|
48 | { |
---|
49 | if (!std::isalnum(ch)) |
---|
50 | ch = '_'; |
---|
51 | return std::tolower(ch); |
---|
52 | } |
---|
53 | |
---|
54 | template <typename Iterator> |
---|
55 | inline std::string |
---|
56 | make_identifier(Iterator const& first, Iterator const& last) |
---|
57 | { |
---|
58 | std::string out_name; |
---|
59 | for (Iterator i = first; i != last; ++i) |
---|
60 | out_name += filter_identifier_char(*i); |
---|
61 | return out_name; |
---|
62 | } |
---|
63 | |
---|
64 | template <typename T> |
---|
65 | struct var_wrapper |
---|
66 | : public ::boost::reference_wrapper<T> |
---|
67 | { |
---|
68 | typedef ::boost::reference_wrapper<T> parent; |
---|
69 | |
---|
70 | explicit inline var_wrapper(T& t) : parent(t) {} |
---|
71 | |
---|
72 | inline T& operator()() const { return parent::get(); } |
---|
73 | }; |
---|
74 | |
---|
75 | template <typename T> |
---|
76 | inline var_wrapper<T> |
---|
77 | var(T& t) |
---|
78 | { |
---|
79 | return var_wrapper<T>(t); |
---|
80 | } |
---|
81 | |
---|
82 | // un-indent a code segment |
---|
83 | void unindent( std::string& program ) |
---|
84 | { |
---|
85 | std::string::size_type const n = program.find_first_not_of(" \t"); |
---|
86 | BOOST_ASSERT( std::string::npos != n ); |
---|
87 | program.erase( 0, n ); |
---|
88 | |
---|
89 | std::string::size_type pos = 0; |
---|
90 | while( std::string::npos != ( pos = program.find( '\n', pos ) ) ) |
---|
91 | { |
---|
92 | if( std::string::npos == ( pos = program.find_first_not_of('\n', pos) ) ) |
---|
93 | { |
---|
94 | break; |
---|
95 | } |
---|
96 | |
---|
97 | program.erase( pos, n ); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | // remove the extension from a filename |
---|
102 | std::string |
---|
103 | remove_extension(std::string const& filename) |
---|
104 | { |
---|
105 | std::string::size_type const n = filename.find_last_of('.'); |
---|
106 | return std::string(filename.begin(), filename.begin()+n); |
---|
107 | } |
---|
108 | }} |
---|
109 | |
---|
110 | #endif // BOOST_SPIRIT_QUICKBOOK_UTILS_HPP |
---|
111 | |
---|