1 /*-----------------------------------------------------------------------------
2 Name: StringStripper.h
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Helper to strip a std::string containing separators into a std::vector
6 Author: <Michele Laghi> laghi@swissinfo.org
7 -----------------------------------------------------------------------------*/
8
9 #ifndef _UTIL_STRINGSTRIPPER_H
10 #define _UTIL_STRINGSTRIPPER_H
11
12 #include <string>
13 #include <vector>
14 #include <util/XmlBCfg.h>
15
16
17
18 namespace org { namespace xmlBlaster {
19 namespace util {
20
21 /**
22 * Class StringStripper is used to strip a std::string with separators into a std::vector
23 * of (separated) std::strings. It is mostly used to strip a name to be used in a
24 * name server. An example could be to strip the following name:
25 *
26 * <pre>
27 * std::string name = "motor.electric.stepper.motor1";
28 * std::vector<std::string> vec = StringStripper(".").strip(name);
29 * </pre>
30 */
31 class Dll_Export StringStripper {
32
33 private:
34 std::string separator_;
35 int sepSize_;
36 public:
37
38 StringStripper(const std::string &separator) {
39 separator_ = separator;
40 sepSize_ = (int)separator_.length();
41 }
42
43 /**
44 * strip strips the std::string into a std::vector of std::strings. If the input
45 * std::string terminates with a separator, then the last std::string in the
46 * std::vector will be empty. No separator appears in the return std::strings.
47 */
48 std::vector<std::string> strip(std::string line) {
49 std::vector<std::string> ret;
50 std::string sub;
51 int pos;
52 while ((pos=(int)line.find(separator_)) >= 0) {
53 sub.assign(line,0, pos);
54 line = line.substr(pos+sepSize_);
55 ret.insert(ret.end(), sub);
56 }
57 ret.insert(ret.end(), line);
58 return ret;
59 }
60
61 };
62 }}} // namespace
63
64 #endif
syntax highlighted by Code2HTML, v. 0.9.1