1 /*-----------------------------------------------------------------------------
2 Name: StringStripper2.h
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Helper to strip a std::string containing two kinds of separators into a
6 std::vector of pairs of std::strings.
7 Author: <Michele Laghi> laghi@swissinfo.org
8 -----------------------------------------------------------------------------*/
9
10 #ifndef _UTIL_STRINGSTRIPPER2_H
11 #define _UTIL_STRINGSTRIPPER2_H
12
13 // #if defined(_WIN32) || defined(_WINDOWS)
14 // # include <_pair.h>
15 // #endif
16 #include <util/StringStripper.h>
17
18
19 /**
20 * StringStripper2 is a class used to strip a std::string into a std::vector of pairs
21 * of std::strings. It needs two separators, a major one and a minor one.
22 *
23 * Lets explain it with an example:
24 * * if you have set the (default) separators: major sep: "/" and a minor
25 * sep: "." and a std::string to strip: "ti.che/ta.tacat/i.tac/tacum.i.tac"
26 * then the external strip would divide the std::string into a std::vector containing
27 * the following four std::strings: "ti.che", "ta.tacat", "i.tac", "tacumi.tac".
28 * The internal strip (the minor strip) would further divide all these std::strings
29 * into pairs of std::strings. In the last std::string there are two separators. The
30 * std::string will be divided so that the part of the std::string before the last
31 * separator will be the first element in the pair. The result will be:
32 * <"ti","che">,<"ta","tacat">,<"i","tac">,<"tacumi","tac">
33 */
34
35
36 namespace org { namespace xmlBlaster {
37 namespace util {
38
39 class Dll_Export StringStripper2 {
40
41 private:
42 StringStripper mainStrip_, minorStrip_;
43
44 public:
45
46 StringStripper2(const std::string &mainSeparator="/",
47 const std::string &minorSeparator=".")
48 : mainStrip_(mainSeparator), minorStrip_(minorSeparator) {
49 }
50
51
52 std::vector<std::pair<std::string,std::string> > strip(const std::string &line) {
53
54 std::vector<std::string> mainVector = mainStrip_.strip(line);
55 std::string::size_type vectorSize;
56 std::pair<std::string,std::string> namePair;
57 std::vector<std::pair<std::string,std::string> > ret;
58
59 for (std::string::size_type i=0; i < mainVector.size(); i++) {
60 std::vector<std::string> minorVector = minorStrip_.strip(mainVector[i]);
61
62 if ( (vectorSize = minorVector.size()) > 1) {
63 std::string name = "";
64 for (std::string::size_type j=0; j<(vectorSize-1); j++) name += minorVector[j];
65 namePair = std::pair<std::string,std::string>(name,minorVector[vectorSize-1]);
66 }
67
68 else {
69 if (vectorSize == 1)
70 namePair = std::pair<std::string,std::string>(minorVector[0],"");
71 else
72 namePair = std::pair<std::string,std::string>("","");
73 }
74
75 ret.insert(ret.end(), namePair);
76
77 }
78
79 return ret;
80
81 }
82
83 };
84 }}} // namespace
85
86 #endif
syntax highlighted by Code2HTML, v. 0.9.1