~thopiekar/zypper/libzypp-manual-import

« back to all changes in this revision

Viewing changes to zypp/repo/RepoVariables.cc

  • Committer: Thomas-Karl Pietrowski
  • Date: 2014-01-29 22:44:28 UTC
  • Revision ID: thopiekar@googlemail.com-20140129224428-gpcqnsdakby362n8
firstĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
16
#include "zypp/repo/RepoVariables.h"
 
17
#include "zypp/base/NonCopyable.h"
 
18
 
 
19
///////////////////////////////////////////////////////////////////
 
20
namespace zypp
 
21
{
 
22
  ///////////////////////////////////////////////////////////////////
 
23
  namespace repo
 
24
  {
 
25
    ///////////////////////////////////////////////////////////////////
 
26
    namespace
 
27
    {
 
28
      /** \brief Provide lazy initialized repo variables
 
29
       */
 
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
 
 
66
      /** \brief Replace repo variables on demand
 
67
       *
 
68
       * Initialisation of repo variables is delayed until they actually occur in
 
69
       * a string.
 
70
       */
 
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
 
84
    ///////////////////////////////////////////////////////////////////
 
85
 
 
86
    std::string RepoVariablesStringReplacer::operator()( const std::string & value ) const
 
87
    {
 
88
      return replacer( value );
 
89
    }
 
90
 
 
91
    Url RepoVariablesUrlReplacer::operator()( const Url & value ) const
 
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
 
100
  ///////////////////////////////////////////////////////////////////
 
101
} // namespace zypp
 
102
///////////////////////////////////////////////////////////////////