diff --git a/src/factory/printer.cpp b/src/factory/printer.cpp index 7c13f61..4afaee4 100644 --- a/src/factory/printer.cpp +++ b/src/factory/printer.cpp @@ -5,6 +5,7 @@ */ #include +#include #if defined(__linux__) || defined(__APPLE__) #include #define HAVE_ISATTY @@ -13,6 +14,7 @@ #include #include +#include #define epicsExportSharedSymbols #include @@ -436,4 +438,57 @@ std::ostream& operator<<(std::ostream& strm, const PVStructure::Formatter& forma return strm; } +static char hexdigit(char c) { + c &= 0xf; + if(c<9) + return '0'+c; + else + return 'A'+c-10; +} + +escape::~escape() {} + +std::string escape::str() const +{ + std::ostringstream strm; + strm<<(*this); + return strm.str(); +} + +epicsShareFunc +std::ostream& operator<<(std::ostream& strm, const escape& Q) +{ + for(size_t pos = 0, len = Q.orig.size(); pos < len; pos++) { + const char C = Q.orig[pos]; + char quote = '\\', next; + // compare me with epicsStrnEscapedFromRaw() + switch(C) { + case '\a': next = 'a'; break; + case '\b': next = 'b'; break; + case '\f': next = 'f'; break; + case '\n': next = 'n'; break; + case '\r': next = 'r'; break; + case '\t': next = 't'; break; + case '\v': next = 'v'; break; + case '\\': next = '\\'; break; + case '\'': next = '\''; break; + case '\"': next = '\"'; if(Q.S==escape::CSV) quote = '"'; break; + default: + if(!isprint(C)) { + // print three charator escape + strm<<"\\x"<>4)<::return_t print_cast(const T& v) { return detail::print_convolute::op(v); } +class escape; + +epicsShareFunc +std::ostream& operator<<(std::ostream& strm, const escape& Q); + +//! Helper to print a string with escaping +//! @code strm<<'"'< lines_t; namespace { +struct SB { + std::ostringstream strm; + operator std::string() { return strm.str(); } + template + SB& operator<<(const T& v) { + strm< esc(N+1, '\0'); // include space for trailing nil - epicsStrnEscapedFromRaw(&esc[0], esc.size(), inp.c_str(), inp.size()); - return std::string(&esc[0]); -} - std::string print(const pvd::PVStructure::Formatter& fmt) { std::ostringstream strm; @@ -74,7 +77,7 @@ testDiff(const std::string& expect, const std::string& actual, const std::string while(L