/*============================================================================= Copyright (c) 2002 2004 Joel de Guzman Copyright (c) 2004 Eric Niebler http://spirit.sourceforge.net/ Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #if !defined(BOOST_SPIRIT_QUICKBOOK_UTILS_HPP) #define BOOST_SPIRIT_QUICKBOOK_UTILS_HPP #include #include #include #include namespace quickbook { namespace detail { template inline void print_char(Char ch, std::ostream& out) { switch (ch) { case '<': out << "<"; break; case '>': out << ">"; break; case '&': out << "&"; break; case '"': out << """; break; default: out << ch; break; } } template inline void print_space(Char ch, std::ostream& out) { switch (ch) { case ' ': out << " "; break; default: out << ch; break; } } template inline Char filter_identifier_char(Char ch) { if (!std::isalnum(ch)) ch = '_'; return std::tolower(ch); } template inline std::string make_identifier(Iterator const& first, Iterator const& last) { std::string out_name; for (Iterator i = first; i != last; ++i) out_name += filter_identifier_char(*i); return out_name; } template struct var_wrapper : public ::boost::reference_wrapper { typedef ::boost::reference_wrapper parent; explicit inline var_wrapper(T& t) : parent(t) {} inline T& operator()() const { return parent::get(); } }; template inline var_wrapper var(T& t) { return var_wrapper(t); } // un-indent a code segment void unindent( std::string& program ) { std::string::size_type const n = program.find_first_not_of(" \t"); BOOST_ASSERT( std::string::npos != n ); program.erase( 0, n ); std::string::size_type pos = 0; while( std::string::npos != ( pos = program.find( '\n', pos ) ) ) { if( std::string::npos == ( pos = program.find_first_not_of('\n', pos) ) ) { break; } program.erase( pos, n ); } } // remove the extension from a filename std::string remove_extension(std::string const& filename) { std::string::size_type const n = filename.find_last_of('.'); return std::string(filename.begin(), filename.begin()+n); } }} #endif // BOOST_SPIRIT_QUICKBOOK_UTILS_HPP