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 = QString::fromLatin1(interface, -1);
79 QString prop = QString::fromLatin1(property, -1);
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 = QString::fromLatin1(interface, -1);
94 QString prop = QString::fromLatin1(property, -1);
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 = QString::fromLatin1(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{
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
191 free(name);
192
193 return new QVariant(list);
194}
195
196void *PredicateParse_appendStringListValue(char *name, void *list)
197{
198 QVariant *variant = (QVariant *)list;
199
200 QStringList new_list = variant->toStringList();
201
202 new_list << QString::fromLatin1(name);
203
204 delete variant;
205 free(name);
206
207 return new QVariant(new_list);
208}
209
210void PredicateLexer_unknownToken(const char *text)
211{
212 qWarning("ERROR from solid predicate parser: unrecognized token '%s' in predicate '%s'\n", text, s_parsingData->localData()->buffer.constData());
213}
This class implements predicates for devices.
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
QString fromLatin1(QByteArrayView str)
QStringList toStringList() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:08:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.