add maybeQuote()

Something for the *NIX gurus
to light their pipes with.
This commit is contained in:
Michael Davidsaver
2020-06-16 16:21:59 -07:00
parent 79b02254c4
commit f17d2bbca1
4 changed files with 62 additions and 14 deletions

View File

@@ -171,7 +171,7 @@ bool printEnumT(std::ostream& strm, const PVStructure& top, bool fromtop)
if(I>=ch.size()) {
strm<<" <undefined>";
} else {
strm<<' '<<escape(ch[I]);
strm<<' '<<maybeQuote(ch[I]);
}
return true;
}
@@ -180,7 +180,7 @@ void csvEscape(std::string& S)
{
// concise, not particularly efficient...
std::string temp(escape(S).style(escape::CSV).str());
if(S.find_first_of(" ,\\")!=S.npos) {// only quote if necessary (stupid Excel)
if(S.find_first_of("\" ,\\")!=S.npos) {// only quote if necessary (stupid Excel)
std::string temp2;
temp2.reserve(temp.size()+2);
temp2.push_back('\"');
@@ -188,7 +188,7 @@ void csvEscape(std::string& S)
temp2.push_back('\"');
temp2.swap(temp);
}
S = temp;
S.swap(temp);
}
bool printTable(std::ostream& strm, const PVStructure& top)
@@ -514,4 +514,38 @@ std::ostream& operator<<(std::ostream& strm, const escape& Q)
return strm;
}
std::ostream& operator<<(std::ostream& strm, const maybeQuote& q)
{
bool esc = false;
for(size_t i=0, N=q.s.size(); i<N && !esc; i++) {
switch(q.s[i]) {
case '\a':
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
case ' ':
case '\v':
case '\\':
case '\'':
case '\"':
esc = true;
break;
default:
if(!isprint(q.s[i])) {
esc = true;
}
break;
}
}
if(esc) {
strm<<'"'<<escape(q.s)<<'"';
} else {
strm<<q.s;
}
return strm;
}
}} //epics::pvData