libzypp  11.13.5
RepoVariables.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 
10 #include "zypp/base/LogTools.h"
11 #include "zypp/base/String.h"
12 
13 #include "zypp/ZConfig.h"
14 #include "zypp/Target.h"
15 #include "zypp/Arch.h"
17 #include "zypp/base/NonCopyable.h"
18 
20 namespace zypp
21 {
23  namespace repo
24  {
26  namespace
27  {
30  struct ReplacerData : private zypp::base::NonCopyable
31  {
32  const std::string & sysarch() const
33  {
34  if ( _sysarch.empty() )
35  initArchStr();
36  return _sysarch;
37  }
38 
39  const std::string & basearch() const
40  {
41  if ( _basearch.empty() )
42  initArchStr();
43  return _basearch;
44  }
45 
46  const std::string & releasever() const
47  {
48  if( _releasever.empty() )
49  _releasever = Target::distributionVersion( Pathname()/*guess*/ );
50  return _releasever;
51  }
52 
53  private:
54  void initArchStr() const
55  {
56  Arch arch( ZConfig::instance().systemArchitecture() );
57  _sysarch = arch.asString();
58  _basearch = arch.baseArch().asString();
59  }
60  private:
61  mutable std::string _sysarch;
62  mutable std::string _basearch;
63  mutable std::string _releasever;
64  };
65 
71  std::string replacer( const std::string & value_r )
72  {
73  static ReplacerData _data;
74 
75  std::string ret( value_r );
76  // Don't need to capture static (non automatic) _data in lambda
77  ret = str::replaceAllFun( ret, "$arch", []()-> std::string { return _data.sysarch(); } );
78  ret = str::replaceAllFun( ret, "$basearch", []()-> std::string { return _data.basearch(); } );
79  ret = str::replaceAllFun( ret, "$releasever", []()-> std::string { return _data.releasever(); } );
80  return ret;
81  }
82 
83  } // namespace
85 
86  std::string RepoVariablesStringReplacer::operator()( const std::string & value ) const
87  {
88  return replacer( value );
89  }
90 
92  {
93  Url newurl( value );
94  newurl.setPathData( replacer( value.getPathData() ) );
95  newurl.setQueryString( replacer( value.getQueryString() ) );
96  return newurl;
97  }
98 
99  } // namespace repo
101 } // namespace zypp