- adapting regex code
[platform/upstream/libzypp.git] / zypp / repo / RepoVariables.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9
10 #include <iostream>
11 #include <map>
12 #include <algorithm>
13 #include "zypp/base/String.h"
14 #include "zypp/repo/RepoException.h"
15 #include "zypp/ZConfig.h"
16 #include "RepoVariables.h"
17
18 using namespace std;
19
20 namespace zypp
21 {
22 namespace repo
23 {
24   
25 RepoVariablesStringReplacer::RepoVariablesStringReplacer()
26 {}
27
28 RepoVariablesStringReplacer::~RepoVariablesStringReplacer()
29 {}
30
31 std::string RepoVariablesStringReplacer::operator()( const std::string &value ) const
32 {
33   string newvalue(value);
34   
35   // $arch
36   newvalue = str::gsub( newvalue,
37                         "$arch",
38                         ZConfig::instance().systemArchitecture().asString() );
39   // $basearch
40   
41   Arch::CompatSet cset( Arch::compatSet( ZConfig::instance().systemArchitecture() ) );
42   Arch::CompatSet::const_iterator it = cset.end();
43   --it;
44   // now at noarch
45   --it;
46   
47   Arch basearch = *it;
48   if ( basearch == Arch_noarch )
49   {
50     basearch = ZConfig::instance().systemArchitecture();
51   }
52
53   newvalue = str::gsub( newvalue,
54                         "$basearch",
55                         basearch.asString() );
56   return newvalue;
57 }
58
59 //////////////////////////////////////////////////////////////////////
60
61 RepoVariablesUrlReplacer::RepoVariablesUrlReplacer()
62 {}
63
64 RepoVariablesUrlReplacer::~RepoVariablesUrlReplacer()
65 {}
66
67
68 Url RepoVariablesUrlReplacer::operator()( const Url &value ) const
69 {
70   RepoVariablesStringReplacer replacer;
71   string transformed = replacer(value.asString());
72   Url newurl;
73   try {
74     newurl = Url(transformed);
75   }
76   catch ( const Exception &e )
77   {
78     ZYPP_CAUGHT(e);
79     // just return what we got
80     return value;
81   }
82   return newurl;
83 }
84
85 } // ns repo
86 } // ns zypp
87
88 // vim: set ts=2 sts=2 sw=2 et ai: