libzypp: zypp/ng/sat/cap2str.cc Source File

libzypp 17.38.7

cap2str.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------*/
9#include "cap2str.h"
10#include <zypp/ng/idstring.h>
11#include <zypp/ng/reskind.h>
12#include <zypp/ng/sat/capability.h>
13
14 namespace zyppng::sat::detail {
15
16 // https://rpm-software-management.github.io/rpm/manual/boolean_dependencies.html
17 namespace
18 {
19
21 template <unsigned TLen = 5>
22 struct TempStrings
23 {
25 std::string & getNext()
26 { unsigned c = _next; _next = (_next+1) % TLen; _buf[c].clear(); return _buf[c]; }
27
28 private:
29 unsigned _next = 0;
30 std::string _buf[TLen];
31 };
32
33 inline bool isBoolOp( int op_r )
34 {
35 switch ( op_r ) {
36 case REL_AND:
37 case REL_OR:
38 case REL_COND:
39 case REL_UNLESS:
40 case REL_ELSE:
41 case REL_WITH:
42 case REL_WITHOUT:
43 return true;
44 }
45 return false;
46 }
47
48 inline bool needsBrace( int op_r, int parop_r )
49 {
50 return ( isBoolOp( parop_r ) || parop_r == 0 ) && isBoolOp( op_r )
51 && ( parop_r != op_r || op_r == REL_COND || op_r == REL_UNLESS || op_r == REL_ELSE )
52 && not ( ( parop_r == REL_COND || parop_r == REL_UNLESS ) && op_r == REL_ELSE );
53 }
54 }
55
56 void cap2str( std::string & outs_r, CPool * pool_r, IdType id_r, int parop_r )
57 {
58 if ( ISRELDEP(id_r) ) {
59 ::Reldep * rd = GETRELDEP( pool_r, id_r );
60 int op = rd->flags;
61
62 if ( op == CapDetail::CAP_ARCH ) {
63 if ( rd->evr == ARCH_SRC || rd->evr == ARCH_NOSRC ) {
64 // map arch .{src,nosrc} to kind srcpackage
65 outs_r += ResKind::srcpackage.c_str();
66 outs_r += ":";
67 outs_r += IdString(rd->name).c_str();
68 return;
69 }
70 cap2str( outs_r, pool_r, rd->name, op );
71 outs_r += ".";
72 cap2str( outs_r, pool_r, rd->evr, op );
73 return;
74 }
75
76 if ( op == CapDetail::CAP_NAMESPACE ) {
77 cap2str( outs_r, pool_r, rd->name, op );
78 outs_r += "(";
79 cap2str( outs_r, pool_r, rd->evr, op );
80 outs_r += ")";
81 return;
82 }
83
84 if ( op == REL_FILECONFLICT )
85 {
86 cap2str( outs_r, pool_r, rd->name, op );
87 return;
88 }
89
90 if ( needsBrace( op, parop_r ) ) {
91 outs_r += "(";
92 cap2str( outs_r, pool_r, rd->name, op );
93 outs_r += capRel2Str( op );
94 cap2str( outs_r, pool_r, rd->evr, op );
95 outs_r += ")";
96 return;
97 }
98
99 cap2str( outs_r, pool_r, rd->name, op );
100 outs_r += capRel2Str( op );
101 cap2str( outs_r, pool_r, rd->evr, op );
102 }
103 else
104 outs_r += IdString(id_r).c_str();
105 }
106
107 const char *cap2str( CPool *pool_r, IdType id_r, int parop_r )
108 {
109 if ( not id_r ) return "";
110 if ( not ISRELDEP( id_r ) ) return IdString( id_r ).c_str();
111
112 static TempStrings<5> tempstrs; // Round Robin buffer to prolong the lifetime of the returned char*
113
114 std::string & outs { tempstrs.getNext() };
115 cap2str( outs, pool_r, id_r, 0 );
116 return outs.c_str();
117 }
118
119 const char * capRel2Str( int op_r )
120 {
121 switch ( op_r )
122 {
123 case REL_GT: return " > ";
124 case REL_EQ: return " = ";
125 case REL_LT: return " < ";
126 case REL_GT|REL_EQ: return " >= ";
127 case REL_LT|REL_EQ: return " <= ";
128 case REL_GT|REL_LT: return " != ";
129 case REL_GT|REL_LT|REL_EQ: return " <=> ";
130 case REL_AND: return " and ";
131 case REL_OR: return " or ";
132 case REL_COND: return " if ";
133 case REL_UNLESS: return " unless ";
134 case REL_ELSE: return " else ";
135 case REL_WITH: return " with ";
136 case REL_WITHOUT: return " without ";
137 }
138 return "UNKNOWNCAPREL";
139 }
140
141
142} // namespace zyppng::sat::detail
cap2str.h
zypp::IdString::c_str
const char * c_str() const
Conversion to const char *.
Definition IdString.cc:51
zyppng::ResKind::srcpackage
static const ResKind srcpackage
Definition ResKind.h:44
zyppng::sat::CapDetail::CAP_ARCH
@ CAP_ARCH
Used internally.
Definition capability.h:366
zyppng::sat::CapDetail::CAP_NAMESPACE
@ CAP_NAMESPACE
Definition capability.h:365
zyppng::sat::detail
CLASS NAME : detail::DIWrap.
Definition cap2str.cc:14
zyppng::sat::detail::CPool
zypp::sat::detail::CPool CPool
Definition poolconstants.h:36
zyppng::sat::detail::capRel2Str
const char * capRel2Str(int op_r)
Convert a relation operator to its string representation.
Definition cap2str.cc:119
zyppng::sat::detail::IdType
zypp::sat::detail::IdType IdType
Definition poolconstants.h:43
zyppng::sat::detail::cap2str
void cap2str(std::string &outs_r, CPool *pool_r, IdType id_r, int parop_r)
Shared logic to convert a sat ID into a human readable string.
Definition cap2str.cc:56
zyppng::IdString
zypp::IdString IdString
Definition idstring.h:16
reskind.h
capability.h
idstring.h