diff options
author | Chris Johns <chrisj@rtems.org> | 2014-08-04 09:19:55 +1000 |
---|---|---|
committer | Chris Johns <chrisj@rtems.org> | 2014-08-04 09:19:55 +1000 |
commit | 28884db485eba2b580d8da526e190f903b2e6174 (patch) | |
tree | 6cd24717823743f18041b6113351ff6a3468d3b4 /rld.h | |
parent | 482ca38073a878814640089086c0ba262e80e4cd (diff) |
rtems-tld: Add trace configuration support.
Extend the configuration support to provide the needed configuration
required to generate the C stub support.
Diffstat (limited to 'rld.h')
-rw-r--r-- | rld.h | 116 |
1 files changed, 105 insertions, 11 deletions
@@ -25,7 +25,11 @@ #if !defined (_RLD_H_) #define _RLD_H_ +#include <algorithm> +#include <cctype> +#include <functional> #include <iostream> +#include <locale> #include <sstream> #include <string> @@ -77,17 +81,6 @@ namespace rld namespace rld { /** - * Convert a supported type to a string. - */ - template <class T> - std::string to_string (T t, std::ios_base & (*f)(std::ios_base&) = std::dec) - { - std::ostringstream oss; - oss << f << t; - return oss.str(); - } - - /** * General error. */ struct error @@ -111,6 +104,107 @@ namespace rld rld::error (_what, std::string (__FILE__) + ":" + to_string (__LINE__)) /** + * Convert a supported type to a string. + */ + template <class T> + std::string to_string (T t, std::ios_base & (*f)(std::ios_base&) = std::dec) + { + std::ostringstream oss; + oss << f << t; + return oss.str(); + } + + /** + * A container of strings. + */ + typedef std::vector < std::string > strings; + + /** + * Trim from start. + */ + inline std::string& ltrim (std::string &s) + { + s.erase (s.begin (), + std::find_if (s.begin (), s.end (), + std::not1 (std::ptr_fun < int, int > (std::isspace)))); + return s; + } + + /** + * Trim from end. + */ + inline std::string& rtrim (std::string &s) + { + s.erase (std::find_if (s.rbegin (), s.rend (), + std::not1 (std::ptr_fun < int, int > (std::isspace))).base(), + s.end()); + return s; + } + + /** + * Trim from both ends. + */ + inline std::string& trim (std::string &s) + { + return ltrim (rtrim (s)); + } + + /** + * Split the string in a contain of strings based on the the + * delimiter. Optionally trim any white space or include empty string. + * + * @todo The split should optionally honour string quoting. + */ + inline strings& split (strings& se, + const std::string& s, + char delimiter, + bool strip_quotes = true, + bool strip_whitespace = true, + bool empty = false) + { + std::stringstream ss(s); + std::string e; + se.clear (); + while (std::getline (ss, e, delimiter)) + { + if (strip_whitespace) + trim (e); + if (strip_quotes) + { + if ((e.front () == '"') || (e.front () == '\'')) + { + if (e.front () != e.back ()) + throw rld::error ("invalid quoting", "string: " + s); + e = e.substr (1, e.length () - 1); + } + } + if (empty || !e.empty ()) + { + se.push_back (e); + } + } + return se; + } + + /** + * Join the strings together with the separator. + */ + inline std::string& join (const strings& ss, + const std::string& separator, + std::string& s) + { + for (strings::const_iterator ssi = ss.begin (); + ssi != ss.end (); + ++ssi) + { + s += *ssi; + if ((ssi != ss.begin ()) && (ssi != ss.end ())) + s += separator; + } + return s; + } + + /** * Increment the verbose level. */ void verbose_inc (); |