fixup Fix to build with libxml 2.12.x (fixes #505)
[platform/upstream/libzypp.git] / zypp / Rel.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Rel.cc
10  *
11 */
12 #include <iostream>
13 #include <map>
14
15 #include "zypp/base/Exception.h"
16
17 #include "zypp/Rel.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   namespace
24   {
25     std::map<std::string,Rel::for_use_in_switch> _table;
26
27     std::map<std::string,Rel::for_use_in_switch>::const_iterator findStr( const std::string & strval_r )
28     {
29       if ( _table.empty() )
30       {
31         // initialize it
32         _table["EQ"]   = _table["eq"]  = _table["=="]    = _table["="] = Rel::EQ_e;
33         _table["NE"]   = _table["ne"]  = _table["!="]                  = Rel::NE_e;
34         _table["LT"]   = _table["lt"]  = _table["<"]                   = Rel::LT_e;
35         _table["LE"]   = _table["le"]  = _table["lte"]  = _table["<="] = Rel::LE_e;
36         _table["GT"]   = _table["gt"]  = _table[">"]                   = Rel::GT_e;
37         _table["GE"]   = _table["ge"]  = _table["gte"]  = _table[">="] = Rel::GE_e;
38         _table["ANY"]  = _table["any"] = _table["(any)"] = _table[""]  = Rel::ANY_e;
39         _table["NONE"] = _table["none"]                                = Rel::NONE_e;
40       }
41
42       return _table.find( strval_r );
43     }
44
45     Rel::for_use_in_switch parse( const std::string & strval_r )
46     {
47       std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r );
48       if ( it == _table.end() )
49       {
50         ZYPP_THROW( Exception("Rel parse: illegal string value '"+strval_r+"'") );
51       }
52       return it->second;
53     }
54
55     Rel::for_use_in_switch parse( const std::string & strval_r, const Rel & default_r )
56     {
57       std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r );
58       if ( it == _table.end() )
59       {
60         return default_r.inSwitch();
61       }
62       return it->second;
63     }
64   }
65   ///////////////////////////////////////////////////////////////////
66
67   const Rel Rel::EQ( Rel::EQ_e );
68   const Rel Rel::NE( Rel::NE_e );
69   const Rel Rel::LT( Rel::LT_e );
70   const Rel Rel::LE( Rel::LE_e );
71   const Rel Rel::GT( Rel::GT_e );
72   const Rel Rel::GE( Rel::GE_e );
73   const Rel Rel::ANY( Rel::ANY_e );
74   const Rel Rel::NONE( Rel::NONE_e );
75
76   ///////////////////////////////////////////////////////////////////
77   //
78   //    METHOD NAME : Rel::Rel
79   //    METHOD TYPE : Constructor
80   //
81   Rel::Rel( const std::string & strval_r )
82   : _op( parse( strval_r ) )
83   {}
84
85   Rel::Rel( const std::string & strval_r, const Rel & default_r )
86   : _op( parse( strval_r, default_r ) )
87   {}
88
89   bool Rel::parseFrom( const std::string & strval_r )
90   {
91     std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r );
92     if ( it == _table.end() )
93     {
94       return false;
95     }
96     _op = it->second;
97     return true;
98   }
99
100   ///////////////////////////////////////////////////////////////////
101   //
102   //    METHOD NAME : Rel::asString
103   //    METHOD TYPE : const std::string &
104   //
105   const std::string & Rel::asString() const
106   {
107     static std::map<for_use_in_switch,std::string> _table;
108     if ( _table.empty() )
109       {
110         // initialize it
111         _table[EQ_e]   = "==";
112         _table[NE_e]   = "!=";
113         _table[LT_e]   = "<";
114         _table[LE_e]   = "<=";
115         _table[GT_e]   = ">";
116         _table[GE_e]   = ">=";
117         _table[ANY_e]  = "ANY";
118         _table[NONE_e] = "NONE";
119       }
120     return _table[_op];
121   }
122
123   /////////////////////////////////////////////////////////////////
124 } // namespace zypp
125 ///////////////////////////////////////////////////////////////////