Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r76789 - branches/quickbook-dev/tools/quickbook/src
From: dnljms_at_[hidden]
Date: 2012-01-29 19:06:02


Author: danieljames
Date: 2012-01-29 19:05:59 EST (Sun, 29 Jan 2012)
New Revision: 76789
URL: http://svn.boost.org/trac/boost/changeset/76789

Log:
Quickbook: Use custom stream-like class for console output.

Deals correctly with paths and UTF-8 strings.
Text files modified:
   branches/quickbook-dev/tools/quickbook/src/actions.cpp | 22 +++++-----
   branches/quickbook-dev/tools/quickbook/src/code_snippet.cpp | 4 +-
   branches/quickbook-dev/tools/quickbook/src/doc_info_actions.cpp | 8 ++--
   branches/quickbook-dev/tools/quickbook/src/input_path.cpp | 79 +++++++++++++++++++++++++++++++++++----
   branches/quickbook-dev/tools/quickbook/src/input_path.hpp | 63 +++++++++++++++++--------------
   branches/quickbook-dev/tools/quickbook/src/quickbook.cpp | 15 +++----
   branches/quickbook-dev/tools/quickbook/src/syntax_highlight.cpp | 2
   7 files changed, 130 insertions(+), 63 deletions(-)

Modified: branches/quickbook-dev/tools/quickbook/src/actions.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/actions.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/actions.cpp 2012-01-29 19:05:59 EST (Sun, 29 Jan 2012)
@@ -231,7 +231,7 @@
             boost::lexical_cast<std::string>(pos.column));
 
         detail::outerr(state.current_file->path, pos.line)
- << detail::utf8(formatted_message) << std::endl;
+ << formatted_message << std::endl;
         ++state.error_count;
     }
 
@@ -596,7 +596,7 @@
                     detail::outerr(callout_body.get_file(), callout_body.get_position())
                         << "Expanding callout." << std::endl
                         << "------------------begin------------------" << std::endl
- << detail::utf8(callout_body.get_quickbook())
+ << callout_body.get_quickbook()
                         << std::endl
                         << "------------------end--------------------" << std::endl
                         ;
@@ -839,7 +839,7 @@
             {
                 detail::outwarn(name.get_file(), name.get_position())
                     << "Duplicate image attribute: "
- << detail::utf8(name.get_quickbook())
+ << name.get_quickbook()
                     << std::endl;
             }
         }
@@ -864,7 +864,7 @@
                 detail::outerr(attributes["fileref"].get_file(), attributes["fileref"].get_position()) :
                 detail::outwarn(attributes["fileref"].get_file(), attributes["fileref"].get_position()))
                 << "Image path isn't portable: '"
- << detail::utf8(fileref)
+ << fileref
                 << "'"
                 << std::endl;
             if (qbk_version_n >= 106u) ++state.error_count;
@@ -1044,7 +1044,7 @@
                 &state.templates.top_scope())))
         {
             detail::outwarn(body.get_file(), body.get_position())
- << "Template Redefinition: " << detail::utf8(identifier) << std::endl;
+ << "Template Redefinition: " << identifier << std::endl;
             ++state.error_count;
         }
     }
@@ -1289,10 +1289,10 @@
                 detail::outerr(state.current_file, first)
                     << "Expanding "
                     << (is_block ? "block" : "phrase")
- << " template: " << detail::utf8(symbol->identifier) << std::endl
+ << " template: " << symbol->identifier << std::endl
                     << std::endl
                     << "------------------begin------------------" << std::endl
- << detail::utf8(symbol->content.get_quickbook())
+ << symbol->content.get_quickbook()
                     << "------------------end--------------------" << std::endl
                     << std::endl;
                 ++state.error_count;
@@ -1303,7 +1303,7 @@
             {
                 detail::outerr(state.current_file, first)
                     << "Mismatched sections in template "
- << detail::utf8(symbol->identifier)
+ << symbol->identifier
                     << std::endl;
                 ++state.error_count;
                 return;
@@ -1793,7 +1793,7 @@
             }
 
             *err << "Path isn't portable: '"
- << detail::utf8(path_text)
+ << path_text
                 << "'"
                 << std::endl;
 
@@ -2056,7 +2056,7 @@
                 if (!state.templates.add(ts))
                 {
                     detail::outerr(ts.content.get_file(), ts.content.get_position())
- << "Template Redefinition: " << detail::utf8(tname) << std::endl;
+ << "Template Redefinition: " << tname << std::endl;
                     ++state.error_count;
                 }
             }
@@ -2130,7 +2130,7 @@
                     << "Loading file "
                     << paths.filename
                     << ": "
- << detail::utf8(e.what())
+ << e.what()
                     << std::endl;
             }
         }

Modified: branches/quickbook-dev/tools/quickbook/src/code_snippet.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/code_snippet.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/code_snippet.cpp 2012-01-29 19:05:59 EST (Sun, 29 Jan 2012)
@@ -484,7 +484,7 @@
             if (qbk_version_n >= 106u) {
                 detail::outerr(source_file->path)
                     << "Unclosed snippet '"
- << detail::utf8(snippet_stack->id)
+ << snippet_stack->id
                     << "'"
                     << std::endl;
                 ++error_count;
@@ -492,7 +492,7 @@
             else {
                 detail::outwarn(source_file->path)
                     << "Unclosed snippet '"
- << detail::utf8(snippet_stack->id)
+ << snippet_stack->id
                     << "'"
                     << std::endl;
             }

Modified: branches/quickbook-dev/tools/quickbook/src/doc_info_actions.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/doc_info_actions.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/doc_info_actions.cpp 2012-01-29 19:05:59 EST (Sun, 29 Jan 2012)
@@ -192,7 +192,7 @@
             detail::outwarn(state.current_file->path)
                 << (duplicates.size() > 1 ?
                     "Duplicate attributes" : "Duplicate attribute")
- << ":" << detail::utf8(boost::algorithm::join(duplicates, ", "))
+ << ":" << boost::algorithm::join(duplicates, ", ")
                 << "\n"
                 ;
         }
@@ -268,7 +268,7 @@
             {
                 detail::outerr(xmlbase.get_file(), xmlbase.get_position())
                     << "xmlbase \""
- << detail::utf8(xmlbase.get_quickbook())
+ << xmlbase.get_quickbook()
                     << "\" isn't a directory."
                     << std::endl;
 
@@ -301,8 +301,8 @@
                 detail::outwarn(state.current_file->path)
                     << (invalid_attributes.size() > 1 ?
                         "Invalid attributes" : "Invalid attribute")
- << " for '" << detail::utf8(doc_type) << " document info': "
- << detail::utf8(boost::algorithm::join(invalid_attributes, ", "))
+ << " for '" << doc_type << " document info': "
+ << boost::algorithm::join(invalid_attributes, ", ")
                     << "\n"
                     ;
             }

Modified: branches/quickbook-dev/tools/quickbook/src/input_path.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/input_path.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/input_path.cpp 2012-01-29 19:05:59 EST (Sun, 29 Jan 2012)
@@ -122,7 +122,7 @@
         return fs::path(static_cast<wchar_t*>(ptr));
     }
     
- stream_string path_to_stream(fs::path const& path)
+ ostream::string path_to_stream(fs::path const& path)
     {
         cygwin_conv_path_t flags = CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
 
@@ -145,12 +145,12 @@
     }
 
 #if QUICKBOOK_WIDE_PATHS && !QUICKBOOK_WIDE_STREAMS
- stream_string path_to_stream(fs::path const& path)
+ ostream::string path_to_stream(fs::path const& path)
     {
         return path.string();
     }
 #else
- stream_string path_to_stream(fs::path const& path)
+ ostream::string path_to_stream(fs::path const& path)
     {
         return path.native();
     }
@@ -166,21 +166,23 @@
         if (_isatty(_fileno(stderr))) _setmode(_fileno(stderr), _O_U16TEXT);
     }
 
- void write_utf8(ostream& out, std::string const& x)
+ void write_utf8(ostream::base_ostream& out, std::string const& x)
     {
         out << from_utf8(x);
     }
 
     ostream& out()
     {
- return std::wcout;
+ static ostream x(std::wcout);
+ return x;
     }
 
     namespace
     {
         inline ostream& error_stream()
         {
- return std::wcerr;
+ static ostream x(std::wcerr);
+ return x;
         }
     }
 
@@ -190,21 +192,23 @@
     {
     }
 
- void write_utf8(ostream& out, std::string const& x)
+ void write_utf8(ostream::base_ostream& out, std::string const& x)
     {
         out << x;
     }
 
     ostream& out()
     {
- return std::cout;
+ static ostream x(std::cout);
+ return x;
     }
 
     namespace
     {
         inline ostream& error_stream()
         {
- return std::clog;
+ static ostream x(std::clog);
+ return x;
         }
     }
 
@@ -254,4 +258,61 @@
     {
         return outwarn(f->path, f->position_of(pos).line);
     }
+
+ ostream& ostream::operator<<(char c) {
+ assert(c > 0 && c <= 127);
+ base << c;
+ return *this;
+ }
+
+ inline bool check_ascii(char const* x) {
+ for(;*x;++x) if(*x <= 0 || *x > 127) return false;
+ return true;
+ }
+
+ ostream& ostream::operator<<(char const* x) {
+ assert(check_ascii(x));
+ base << x;
+ return *this;
+ }
+
+ ostream& ostream::operator<<(std::string const& x) {
+ write_utf8(base, x);
+ return *this;
+ }
+
+ ostream& ostream::operator<<(int x) {
+ base << x;
+ return *this;
+ }
+
+ ostream& ostream::operator<<(unsigned int x) {
+ base << x;
+ return *this;
+ }
+
+ ostream& ostream::operator<<(long x) {
+ base << x;
+ return *this;
+ }
+
+ ostream& ostream::operator<<(unsigned long x) {
+ base << x;
+ return *this;
+ }
+
+ ostream& ostream::operator<<(fs::path const& x) {
+ base << path_to_stream(x);
+ return *this;
+ }
+
+ ostream& ostream::operator<<(base_ostream& (*x)(base_ostream&)) {
+ base << x;
+ return *this;
+ }
+
+ ostream& ostream::operator<<(base_ios& (*x)(base_ios&)) {
+ base << x;
+ return *this;
+ }
 }}

Modified: branches/quickbook-dev/tools/quickbook/src/input_path.hpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/input_path.hpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/input_path.hpp 2012-01-29 19:05:59 EST (Sun, 29 Jan 2012)
@@ -66,17 +66,47 @@
         typedef std::string input_string;
 #endif
 
+ // A light wrapper around C++'s streams that gets things right
+ // in the quickbook context.
+ //
+ // This is far from perfect but it fixes some issues.
+ struct ostream
+ {
 #if QUICKBOOK_WIDE_STREAMS
- typedef std::wostream ostream;
- typedef std::wstring stream_string;
+ typedef std::wostream base_ostream;
+ typedef std::wios base_ios;
+ typedef std::wstring string;
 #else
- typedef std::ostream ostream;
- typedef std::string stream_string;
+ typedef std::ostream base_ostream;
+ typedef std::ios base_ios;
+ typedef std::string string;
 #endif
+ base_ostream& base;
+
+ explicit ostream(base_ostream& x) : base(x) {}
+
+ // C strings should always be ascii.
+ ostream& operator<<(char);
+ ostream& operator<<(char const*);
+
+ // std::string should be UTF-8 (what a mess!)
+ ostream& operator<<(std::string const&);
+
+ // Other value types.
+ ostream& operator<<(int x);
+ ostream& operator<<(unsigned int x);
+ ostream& operator<<(long x);
+ ostream& operator<<(unsigned long x);
+ ostream& operator<<(fs::path const&);
+
+ // Modifiers
+ ostream& operator<<(base_ostream& (*)(base_ostream&));
+ ostream& operator<<(base_ios& (*)(base_ios&));
+ };
+
 
         std::string input_to_utf8(input_string const&);
         fs::path input_to_path(input_string const&);
- stream_string path_to_stream(fs::path const&);
     
         std::string path_to_generic(fs::path const&);
         fs::path generic_to_path(std::string const&);
@@ -94,29 +124,6 @@
         ostream& outwarn(fs::path const& file, int line = -1);
         ostream& outerr(file_ptr const&, string_iterator);
         ostream& outwarn(file_ptr const&, string_iterator);
-
- struct utf8_proxy
- {
- std::string value;
-
- explicit utf8_proxy(std::string const& v) : value(v) {}
- };
-
- void write_utf8(ostream& out, std::string const&);
-
- inline ostream& operator<<(ostream& out, utf8_proxy const& p) {
- write_utf8(out, p.value);
- return out;
- }
-
- inline utf8_proxy utf8(std::string const& value) {
- return utf8_proxy(value);
- }
-
- template <typename It>
- inline utf8_proxy utf8(It begin, It end) {
- return utf8_proxy(std::string(begin, end));
- }
     }
 }
 

Modified: branches/quickbook-dev/tools/quickbook/src/quickbook.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/quickbook.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/quickbook.cpp 2012-01-29 19:05:59 EST (Sun, 29 Jan 2012)
@@ -69,7 +69,7 @@
             if (!info.full) {
                 detail::outerr()
                     << "Error parsing command line definition: '"
- << detail::utf8(*it)
+ << *it
                     << "'"
                     << std::endl;
                 ++state.error_count;
@@ -141,7 +141,7 @@
             result = state.error_count ? 1 : 0;
         }
         catch (load_error& e) {
- detail::outerr(filein_) << detail::utf8(e.what()) << std::endl;
+ detail::outerr(filein_) << e.what() << std::endl;
             result = 1;
         }
 
@@ -307,8 +307,7 @@
             std::ostringstream description_text;
             description_text << desc;
 
- quickbook::detail::out()
- << quickbook::detail::utf8(description_text.str()) << "\n";
+ quickbook::detail::out() << description_text.str() << "\n";
 
             return 0;
         }
@@ -321,7 +320,7 @@
             quickbook::detail::out()
                 << QUICKBOOK_VERSION
                 << " (Boost "
- << quickbook::detail::utf8(boost_version)
+ << boost_version
                 << ")"
                 << std::endl;
             return 0;
@@ -434,7 +433,7 @@
             }
 
             quickbook::detail::out() << "Generating Output File: "
- << quickbook::detail::path_to_stream(fileout)
+ << fileout
                 << std::endl;
 
             if (!error_count)
@@ -456,14 +455,14 @@
             description_text << desc;
         
             quickbook::detail::outerr() << "No filename given\n\n"
- << quickbook::detail::utf8(description_text.str()) << std::endl;
+ << description_text.str() << std::endl;
             return 1;
         }
     }
 
     catch(std::exception& e)
     {
- quickbook::detail::outerr() << quickbook::detail::utf8(e.what()) << "\n";
+ quickbook::detail::outerr() << e.what() << "\n";
         return 1;
     }
 

Modified: branches/quickbook-dev/tools/quickbook/src/syntax_highlight.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/syntax_highlight.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/syntax_highlight.cpp 2012-01-29 19:05:59 EST (Sun, 29 Jan 2012)
@@ -148,7 +148,7 @@
 
         detail::outwarn(state.current_file->path, pos.line)
             << "in column:" << pos.column
- << ", unexpected character: " << detail::utf8(first, last)
+ << ", unexpected character: " << std::string(first.base(), last.base())
             << "\n";
 
         // print out an unexpected character


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk