KUserFeedback

surveytargetexpressionparser.cpp
1/*
2 SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#include <config-userfeedback.h>
8
9#include "surveytargetexpressionparser.h"
10#include "surveytargetexpression.h"
11
12#ifdef HAVE_SURVEY_TARGET_EXPRESSIONS
13#include "surveytargetexpressionparser_p.h"
14#include "surveytargetexpressionscanner.h"
15#endif
16
17#include <QString>
18
19#include <cstring>
20
21char* unquoteString(const char *str)
22{
23 const auto size = strlen(str) - 2;
24 if (size <= 0)
25 return nullptr;
26 auto out = (char*)malloc(size + 1);
27 memset(out, 0, size + 1);
28 auto outIt = out;
29 for (auto it = str + 1; it < str + size + 1; ++it, ++outIt) {
30 if (*it == '\\') {
31 ++it;
32 switch (*it) {
33 case '\\':
34 case '"':
35 *outIt = *it; break;
36 case 'n':
37 *outIt = '\n'; break;
38 case 't':
39 *outIt = '\t'; break;
40 default:
41 *outIt++ = '\\';
42 *outIt = *it;
43 }
44 } else {
45 *outIt = *it;
46 }
47 }
48 return out;
49}
50
51using namespace KUserFeedback;
52
53SurveyTargetExpressionParser::SurveyTargetExpressionParser()
54 : m_expression(nullptr)
55{
56}
57
58SurveyTargetExpressionParser::~SurveyTargetExpressionParser()
59{
60 delete m_expression;
61}
62
63bool SurveyTargetExpressionParser::parse(const QString &s)
64{
65 delete m_expression;
66 m_expression = nullptr;
67
68#ifdef HAVE_SURVEY_TARGET_EXPRESSIONS
69 yyscan_t scanner;
70 YY_BUFFER_STATE state;
71 if (yylex_init(&scanner))
72 return false;
73
74 QByteArray b = s.toUtf8();
75 state = yy_scan_string(b.constData(), scanner);
76 if (yyparse(&m_expression, scanner)) {
77 delete m_expression;
78 m_expression = nullptr;
79 }
80
81 yy_delete_buffer(state, scanner);
82 yylex_destroy(scanner);
83#endif
84 return m_expression;
85}
86
87SurveyTargetExpression* SurveyTargetExpressionParser::expression() const
88{
89 return m_expression;
90}
Classes for integrating telemetry collection, survey targeting, and contribution encouragenemt and co...
const char * constData() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.