util/StringStripper2.h

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      StringStripper2.h
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Helper to strip a std::string containing two kinds of separators into a 
00006            std::vector of pairs of std::strings.
00007 Author:    <Michele Laghi> laghi@swissinfo.org
00008 -----------------------------------------------------------------------------*/
00009 
00010 #ifndef _UTIL_STRINGSTRIPPER2_H
00011 #define _UTIL_STRINGSTRIPPER2_H
00012 
00013 // #if defined(_WIN32) || defined(_WINDOWS)
00014 // #  include <_pair.h>
00015 // #endif
00016 #include <util/StringStripper.h>
00017 
00018 
00036 namespace org { namespace xmlBlaster {
00037 namespace util {
00038    
00039    class Dll_Export StringStripper2 {
00040       
00041    private:
00042       StringStripper mainStrip_, minorStrip_;
00043       
00044    public:
00045 
00046       StringStripper2(const std::string &mainSeparator="/", 
00047                       const std::string &minorSeparator=".") 
00048          : mainStrip_(mainSeparator), minorStrip_(minorSeparator) {
00049       }
00050 
00051 
00052       std::vector<std::pair<std::string,std::string> > strip(const std::string &line) {
00053 
00054          std::vector<std::string>               mainVector = mainStrip_.strip(line);
00055          std::string::size_type            vectorSize;
00056          std::pair<std::string,std::string>          namePair;
00057          std::vector<std::pair<std::string,std::string> > ret;
00058          
00059          for (std::string::size_type i=0; i < mainVector.size(); i++) {
00060             std::vector<std::string> minorVector = minorStrip_.strip(mainVector[i]);
00061 
00062             if ( (vectorSize = minorVector.size()) > 1) {
00063                std::string name = "";
00064                for (std::string::size_type j=0; j<(vectorSize-1); j++) name += minorVector[j];
00065                namePair = std::pair<std::string,std::string>(name,minorVector[vectorSize-1]);
00066             }
00067 
00068             else {
00069                if (vectorSize == 1) 
00070                   namePair = std::pair<std::string,std::string>(minorVector[0],"");
00071                else 
00072                   namePair = std::pair<std::string,std::string>("","");
00073             }
00074 
00075             ret.insert(ret.end(), namePair);
00076 
00077          }
00078 
00079          return ret; 
00080 
00081       }
00082 
00083    };
00084 }}} // namespace
00085 
00086 #endif