aboutsummaryrefslogtreecommitdiff
path: root/src/inspstring.cpp
diff options
context:
space:
mode:
authorGravatar Daniel De Graaf2010-08-27 22:12:25 -0400
committerGravatar Daniel De Graaf2010-08-27 22:43:59 -0400
commit60a649d4c71dfd02db589f95ed4b04b3b40641b1 (patch)
tree4e2a200ee4be50fdef590f7ebfec5316313251af /src/inspstring.cpp
parentPromote PopulateInfoMap to core, it is moderately useful (diff)
Add FormatSubstitute utility class
Diffstat (limited to 'src/inspstring.cpp')
-rw-r--r--src/inspstring.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/inspstring.cpp b/src/inspstring.cpp
index bcdcbb3c1..3a345bd55 100644
--- a/src/inspstring.cpp
+++ b/src/inspstring.cpp
@@ -224,3 +224,42 @@ std::string Base64ToBin(const std::string& data_str, const char* table)
}
return rv;
}
+
+std::string FormatSubstitute::format(const std::string& src)
+{
+ std::string result;
+ result.reserve(MAXBUF);
+ for (unsigned int i = 0; i < src.length(); i++)
+ {
+ char c = src[i];
+ if (c == '$')
+ {
+ std::string word;
+ i++;
+ if (src[i] == '{')
+ {
+ // processing something like "${foo}bar"
+ while (src[i] && src[i] != '}')
+ word.push_back(src[i++]);
+ }
+ else
+ {
+ while (isalnum(src[i]))
+ word.push_back(src[i++]);
+ i--;
+ }
+ result.append(lookup(word));
+ }
+ else
+ result.push_back(c);
+ }
+ return result;
+}
+
+std::string MapFormatSubstitute::lookup(const std::string& word)
+{
+ SubstMap::const_iterator value = map.find(word);
+ if (value == map.end())
+ return "";
+ return value->second;
+}