libzypp
10.5.0
|
00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 00005 | / /__ | | | | | | | 00006 | /_____||_| |_| |_| | 00007 | | 00008 \---------------------------------------------------------------------*/ 00012 #include <iostream> 00013 #include <map> 00014 00015 #include "zypp/base/Exception.h" 00016 00017 #include "zypp/Rel.h" 00018 00020 namespace zypp 00021 { 00022 00023 namespace 00024 { 00025 std::map<std::string,Rel::for_use_in_switch> _table; 00026 00027 std::map<std::string,Rel::for_use_in_switch>::const_iterator findStr( const std::string & strval_r ) 00028 { 00029 if ( _table.empty() ) 00030 { 00031 // initialize it 00032 _table["EQ"] = _table["eq"] = _table["=="] = _table["="] = Rel::EQ_e; 00033 _table["NE"] = _table["ne"] = _table["!="] = Rel::NE_e; 00034 _table["LT"] = _table["lt"] = _table["<"] = Rel::LT_e; 00035 _table["LE"] = _table["le"] = _table["lte"] = _table["<="] = Rel::LE_e; 00036 _table["GT"] = _table["gt"] = _table[">"] = Rel::GT_e; 00037 _table["GE"] = _table["ge"] = _table["gte"] = _table[">="] = Rel::GE_e; 00038 _table["ANY"] = _table["any"] = _table["(any)"] = _table[""] = Rel::ANY_e; 00039 _table["NONE"] = _table["none"] = Rel::NONE_e; 00040 } 00041 00042 return _table.find( strval_r ); 00043 } 00044 00045 Rel::for_use_in_switch parse( const std::string & strval_r ) 00046 { 00047 std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r ); 00048 if ( it == _table.end() ) 00049 { 00050 ZYPP_THROW( Exception("Rel parse: illegal string value '"+strval_r+"'") ); 00051 } 00052 return it->second; 00053 } 00054 00055 Rel::for_use_in_switch parse( const std::string & strval_r, const Rel & default_r ) 00056 { 00057 std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r ); 00058 if ( it == _table.end() ) 00059 { 00060 return default_r.inSwitch(); 00061 } 00062 return it->second; 00063 } 00064 } 00066 00067 const Rel Rel::EQ( Rel::EQ_e ); 00068 const Rel Rel::NE( Rel::NE_e ); 00069 const Rel Rel::LT( Rel::LT_e ); 00070 const Rel Rel::LE( Rel::LE_e ); 00071 const Rel Rel::GT( Rel::GT_e ); 00072 const Rel Rel::GE( Rel::GE_e ); 00073 const Rel Rel::ANY( Rel::ANY_e ); 00074 const Rel Rel::NONE( Rel::NONE_e ); 00075 00077 // 00078 // METHOD NAME : Rel::Rel 00079 // METHOD TYPE : Constructor 00080 // 00081 Rel::Rel( const std::string & strval_r ) 00082 : _op( parse( strval_r ) ) 00083 {} 00084 00085 Rel::Rel( const std::string & strval_r, const Rel & default_r ) 00086 : _op( parse( strval_r, default_r ) ) 00087 {} 00088 00089 bool Rel::parseFrom( const std::string & strval_r ) 00090 { 00091 std::map<std::string,Rel::for_use_in_switch>::const_iterator it = findStr( strval_r ); 00092 if ( it == _table.end() ) 00093 { 00094 return false; 00095 } 00096 _op = it->second; 00097 return true; 00098 } 00099 00101 // 00102 // METHOD NAME : Rel::asString 00103 // METHOD TYPE : const std::string & 00104 // 00105 const std::string & Rel::asString() const 00106 { 00107 static std::map<for_use_in_switch,std::string> _table; 00108 if ( _table.empty() ) 00109 { 00110 // initialize it 00111 _table[EQ_e] = "=="; 00112 _table[NE_e] = "!="; 00113 _table[LT_e] = "<"; 00114 _table[LE_e] = "<="; 00115 _table[GT_e] = ">"; 00116 _table[GE_e] = ">="; 00117 _table[ANY_e] = "ANY"; 00118 _table[NONE_e] = "NONE"; 00119 } 00120 return _table[_op]; 00121 } 00122 00124 } // namespace zypp