From 974f731f9ba723c76f24ed150a6a7c127da46640 Mon Sep 17 00:00:00 2001 From: dockes Date: Fri, 10 Nov 2006 13:30:03 +0000 Subject: [PATCH] pcSubst() --- src/utils/smallut.cpp | 29 ++++++++++++++++++++++++++++- src/utils/smallut.h | 7 ++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/utils/smallut.cpp b/src/utils/smallut.cpp index cb6d829b..f367e66d 100644 --- a/src/utils/smallut.cpp +++ b/src/utils/smallut.cpp @@ -1,5 +1,5 @@ #ifndef lint -static char rcsid[] = "@(#$Id: smallut.cpp,v 1.17 2006-10-11 14:16:26 dockes Exp $ (C) 2004 J.F.Dockes"; +static char rcsid[] = "@(#$Id: smallut.cpp,v 1.18 2006-11-10 13:30:03 dockes Exp $ (C) 2004 J.F.Dockes"; #endif /* * This program is free software; you can redistribute it and/or modify @@ -403,6 +403,33 @@ string escapeHtml(const string &in) return out; } +// Small utility to substitute printf-like percents cmds in a string +bool pcSubst(const string& in, string& out, map& subs) +{ + string::const_iterator it; + for (it = in.begin(); it != in.end();it++) { + if (*it == '%') { + if (++it == in.end()) { + out += '%'; + break; + } + if (*it == '%') { + out += '%'; + continue; + } + map::iterator tr; + if ((tr = subs.find(*it)) != subs.end()) { + out += tr->second; + } else { + out += *it; + } + } else { + out += *it; + } + } + return true; +} + //////////////////// // Internal redefinition of system time interface to help with dependancies struct m_timespec { diff --git a/src/utils/smallut.h b/src/utils/smallut.h index dabea61b..ff4051da 100644 --- a/src/utils/smallut.h +++ b/src/utils/smallut.h @@ -16,13 +16,15 @@ */ #ifndef _SMALLUT_H_INCLUDED_ #define _SMALLUT_H_INCLUDED_ -/* @(#$Id: smallut.h,v 1.17 2006-10-11 14:16:26 dockes Exp $ (C) 2004 J.F.Dockes */ +/* @(#$Id: smallut.h,v 1.18 2006-11-10 13:30:03 dockes Exp $ (C) 2004 J.F.Dockes */ #include #include +#include #ifndef NO_NAMESPACES using std::string; using std::list; +using std::map; #endif /* NO_NAMESPACES */ extern int stringicmp(const string& s1, const string& s2); @@ -67,6 +69,9 @@ extern string neutchars(const string &str, string chars); * if reasonably possible. */ extern string truncate_to_word(string &input, string::size_type maxlen); +/** Small utility to substitute printf-like percents cmds in a string */ +bool pcSubst(const string& in, string& out, map& subs); + class Chrono { public: Chrono();