Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/tools/quickbook/detail/utils.cpp @ 50

Last change on this file since 50 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.7 KB
RevLine 
[29]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#include "./utils.hpp"
11#include <cctype>
12#include <boost/spirit/core.hpp>
13
14namespace quickbook {
15    extern bool ms_errors;
16}
17
18namespace quickbook { namespace detail
19{
20    void
21    print_char(char ch, std::ostream& out)
22    {
23        switch (ch)
24        {
25            case '<': out << "&lt;";    break;
26            case '>': out << "&gt;";    break;
27            case '&': out << "&amp;";   break;
28            case '"': out << "&quot;";  break;
29            default:  out << ch;        break;
30            // note &apos; is not included. see the curse of apos:
31            // http://fishbowl.pastiche.org/2003/07/01/the_curse_of_apos
32        }
33    }
34
35    void
36    print_string(std::basic_string<char> const& str, std::ostream& out)
37    {
38        for (std::string::const_iterator cur = str.begin();
39            cur != str.end(); ++cur)
40        {
41            print_char(*cur, out);
42        }
43    }
44
45    void
46    print_space(char ch, std::ostream& out)
47    {
48        out << ch;
49    }
50   
51    namespace
52    {
53        bool 
54        find_empty_content_pattern(
55            std::basic_string<char> const& str
56          , std::string::size_type& pos
57          , std::string::size_type& len)
58        {
59            using namespace boost::spirit;
60            typedef std::basic_string<char>::const_iterator iter;
61            for (iter i = str.begin(); i!=str.end(); ++i)
62            {
63                parse_info<iter> r = parse(i, str.end(), '>' >> +blank_p >> '<');
64                if (r.hit)
65                {
66                    pos = i-str.begin();
67                    len = r.length;
68                    return true;
69                }
70            }
71
72            return false;
73        }
74    }
75
76    void
77    convert_nbsp(std::basic_string<char>& str)
78    {
79        std::string::size_type pos;
80        std::string::size_type len;
81        while (find_empty_content_pattern(str, pos, len))
82            str.replace(pos, len, ">&nbsp;<");
83    }
84
85    char
86    filter_identifier_char(char ch)
87    {
88        if (!std::isalnum(static_cast<unsigned char>(ch)))
89            ch = '_';
90        return static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
91    }
92
93    // un-indent a code segment
94    void unindent(std::string& program)
95    {
96        std::string::size_type const start = program.find_first_not_of("\r\n");
97        program.erase(0, start); // erase leading newlines
98
99        std::string::size_type const n = program.find_first_not_of(" \t");
100        BOOST_ASSERT(std::string::npos != n);
101        program.erase(0, n);
102
103        std::string::size_type pos = 0;
104        while (std::string::npos != (pos = program.find_first_of("\r\n", pos)))
105        {
106            if (std::string::npos == (pos = program.find_first_not_of("\r\n", pos)))
107            {
108                break;
109            }
110
111            program.erase(pos, n);
112        }
113    }
114
115    // remove the extension from a filename
116    std::string
117    remove_extension(std::string const& filename)
118    {
119        std::string::size_type const n = filename.find_last_of('.');
120        if(std::string::npos == n)
121        {
122            return filename;
123        }
124        else
125        {
126            return std::string(filename.begin(), filename.begin()+n);
127        }
128    }
129
130    std::string escape_uri(std::string uri)
131    {
132        for (std::string::size_type n = 0; n < uri.size(); ++n)
133        {
134            static char const mark[] = "-_.!~*'()?\\/";
135            if((!std::isalnum(static_cast<unsigned char>(uri[n])) || 127 < static_cast<unsigned char>(uri[n]))
136              && 0 == std::strchr(mark, uri[n]))
137            {
138                static char const hex[] = "0123456789abcdef";
139                char escape[] = { hex[uri[n] / 16], hex[uri[n] % 16] };
140                uri.insert(n + 1, escape, 2);
141                uri[n] = '%';
142                n += 2;
143            }
144        }
145        return uri;
146    }
147   
148    std::ostream & outerr(const std::string & file, int line)
149    {
150        if (ms_errors)
151            return std::clog << file << "(" << line << "): error: ";
152        else
153            return std::clog << file << ":" << line << ": error: ";
154    }
155   
156    std::ostream & outwarn(const std::string & file, int line)
157    {
158        if (ms_errors)
159            return std::clog << file << "(" << line << "): warning: ";
160        else
161            return std::clog << file << ":" << line << ": warning: ";
162    }
163}}
164
165
Note: See TracBrowser for help on using the repository browser.