annotate common/url_breaker.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1 #include "url_breaker.h"
ferencd@0 2
ferencd@0 3 #include "log.h"
ferencd@0 4
ferencd@0 5 #include <boost/tokenizer.hpp>
ferencd@0 6 #include <vector>
ferencd@0 7
ferencd@0 8 url_breaker::url_breaker(const std::string &components, const std::string &url)
ferencd@0 9 {
ferencd@0 10 std::vector<std::string> tempcomps;
ferencd@0 11 boost::char_separator<char> sep("/");
ferencd@0 12
ferencd@0 13 // gather the mapping
ferencd@0 14 {
ferencd@0 15 boost::tokenizer<boost::char_separator<char>> tok(components, sep);
ferencd@0 16
ferencd@0 17 for(boost::tokenizer<boost::char_separator<char>>::iterator beg=tok.begin(); ; )
ferencd@0 18 {
ferencd@0 19 tempcomps.push_back(*beg);
ferencd@0 20 ++ beg;
ferencd@0 21 if(beg == tok.end())
ferencd@0 22 {
ferencd@0 23 break;
ferencd@0 24 }
ferencd@0 25 }
ferencd@0 26
ferencd@0 27 }
ferencd@0 28
ferencd@0 29 // gather the varios url components
ferencd@0 30 {
ferencd@0 31 boost::tokenizer<boost::char_separator<char>> tok(url, sep);
ferencd@0 32 auto vecit = tempcomps.begin();
ferencd@0 33 for(boost::tokenizer<boost::char_separator<char>>::iterator beg=tok.begin();; )
ferencd@0 34 {
ferencd@0 35 comps[*vecit] = *beg;
ferencd@0 36 ++vecit;
ferencd@0 37 ++beg;
ferencd@0 38
ferencd@0 39 if(vecit == tempcomps.end() && beg == tok.end())
ferencd@0 40 {
ferencd@0 41 break;
ferencd@0 42 }
ferencd@0 43
ferencd@0 44 if(vecit == tempcomps.end())
ferencd@0 45 {
ferencd@0 46 log_err() << "components map" << components << "contains less keys than" << url;
ferencd@0 47 break;
ferencd@0 48 }
ferencd@0 49
ferencd@0 50 if(beg == tok.end())
ferencd@0 51 {
ferencd@0 52 log_err() << "components map" << components << "contains more keys than" << url;
ferencd@0 53 break;
ferencd@0 54 }
ferencd@0 55 }
ferencd@0 56
ferencd@0 57 }
ferencd@0 58
ferencd@0 59 // debug part
ferencd@0 60 // for(auto iterator = comps.begin(); iterator != comps.end(); ++ iterator)
ferencd@0 61 // {
ferencd@0 62 // debug() << "COMP[" << iterator->first << "] = " << iterator->second;
ferencd@0 63 // }
ferencd@0 64 }
ferencd@0 65
ferencd@0 66 std::string url_breaker::operator [](const std::string &k)
ferencd@0 67 {
ferencd@0 68 if(comps.count(k))
ferencd@0 69 {
ferencd@0 70 return comps[k];
ferencd@0 71 }
ferencd@0 72 log_err() << "components map contains no value for " << k;
ferencd@0 73 return "";
ferencd@0 74 }