Solid

predicateparse.cpp
1/*
2 SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7extern "C" {
8#include "predicateparse.h"
9
10void PredicateParse_mainParse(const char *_code);
11}
12
13#include "predicate.h"
14#include "soliddefs_p.h"
15
16#include <stdlib.h>
17
18#include <QStringList>
19#include <QThreadStorage>
20
21namespace Solid
22{
23namespace PredicateParse
24{
25struct ParsingData {
26 ParsingData()
27 : result(nullptr)
28 {
29 }
30
31 Solid::Predicate *result;
32 QByteArray buffer;
33};
34
35}
36}
37
38Q_GLOBAL_STATIC(QThreadStorage<Solid::PredicateParse::ParsingData *>, s_parsingData)
39
40Solid::Predicate Solid::Predicate::fromString(const QString &predicate)
41{
42 Solid::PredicateParse::ParsingData *data = new Solid::PredicateParse::ParsingData();
43 s_parsingData->setLocalData(data);
44 data->buffer = predicate.toLatin1();
45 PredicateParse_mainParse(data->buffer.constData());
46 Predicate result;
47 if (data->result) {
48 result = Predicate(*data->result);
49 delete data->result;
50 }
51 s_parsingData->setLocalData(nullptr);
52 return result;
53}
54
55void PredicateParse_setResult(void *result)
56{
57 Solid::PredicateParse::ParsingData *data = s_parsingData->localData();
58 data->result = (Solid::Predicate *)result;
59}
60
61void PredicateParse_errorDetected(const char *s)
62{
63 qWarning("ERROR from solid predicate parser: %s", s);
64 s_parsingData->localData()->result = nullptr;
65}
66
67void PredicateParse_destroy(void *pred)
68{
69 Solid::PredicateParse::ParsingData *data = s_parsingData->localData();
71 if (p != data->result) {
72 delete p;
73 }
74}
75
76void *PredicateParse_newAtom(char *interface, char *property, void *value)
77{
78 QString iface(interface);
79 QString prop(property);
80 QVariant *val = (QVariant *)value;
81
82 Solid::Predicate *result = new Solid::Predicate(iface, prop, *val);
83
84 delete val;
85 free(interface);
86 free(property);
87
88 return result;
89}
90
91void *PredicateParse_newMaskAtom(char *interface, char *property, void *value)
92{
93 QString iface(interface);
94 QString prop(property);
95 QVariant *val = (QVariant *)value;
96
97 Solid::Predicate *result = new Solid::Predicate(iface, prop, *val, Solid::Predicate::Mask);
98
99 delete val;
100 free(interface);
101 free(property);
102
103 return result;
104}
105
106void *PredicateParse_newIsAtom(char *interface)
107{
108 QString iface(interface);
109
110 Solid::Predicate *result = new Solid::Predicate(iface);
111
112 free(interface);
113
114 return result;
115}
116
117void *PredicateParse_newAnd(void *pred1, void *pred2)
118{
119 Solid::Predicate *result = new Solid::Predicate();
120
121 Solid::PredicateParse::ParsingData *data = s_parsingData->localData();
122 Solid::Predicate *p1 = (Solid::Predicate *)pred1;
123 Solid::Predicate *p2 = (Solid::Predicate *)pred2;
124
125 if (p1 == data->result || p2 == data->result) {
126 data->result = nullptr;
127 }
128
129 *result = *p1 & *p2;
130
131 delete p1;
132 delete p2;
133
134 return result;
135}
136
137void *PredicateParse_newOr(void *pred1, void *pred2)
138{
139 Solid::Predicate *result = new Solid::Predicate();
140
141 Solid::PredicateParse::ParsingData *data = s_parsingData->localData();
142 Solid::Predicate *p1 = (Solid::Predicate *)pred1;
143 Solid::Predicate *p2 = (Solid::Predicate *)pred2;
144
145 if (p1 == data->result || p2 == data->result) {
146 data->result = nullptr;
147 }
148
149 *result = *p1 | *p2;
150
151 delete p1;
152 delete p2;
153
154 return result;
155}
156
157void *PredicateParse_newStringValue(char *val)
158{
159 QString s(val);
160
161 free(val);
162
163 return new QVariant(s);
164}
165
166void *PredicateParse_newBoolValue(int val)
167{
168 bool b = (val != 0);
169 return new QVariant(b);
170}
171
172void *PredicateParse_newNumValue(int val)
173{
174 return new QVariant(val);
175}
176
177void *PredicateParse_newDoubleValue(double val)
178{
179 return new QVariant(val);
180}
181
182void *PredicateParse_newEmptyStringListValue()
183{
184 return new QVariant(QStringList());
185}
186
187void *PredicateParse_newStringListValue(char *name)
188{
190 list << QString(name);
191
192 free(name);
193
194 return new QVariant(list);
195}
196
197void *PredicateParse_appendStringListValue(char *name, void *list)
198{
199 QVariant *variant = (QVariant *)list;
200
201 QStringList new_list = variant->toStringList();
202
203 new_list << QString(name);
204
205 delete variant;
206 free(name);
207
208 return new QVariant(new_list);
209}
210
211void PredicateLexer_unknownToken(const char *text)
212{
213 qWarning("ERROR from solid predicate parser: unrecognized token '%s' in predicate '%s'\n", text, s_parsingData->localData()->buffer.constData());
214}
This class implements predicates for devices.
Definition predicate.h:67
KIOCORE_EXPORT QStringList list(const QString &fileClass)
The single responsibility of this class is to create arguments valid for logind Inhibit call.
Definition fakebattery.h:16
const char * constData() const const
QStringList toStringList() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:56:28 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.