- highlight goals updated less than 5 minutes ago
[buliscores] / qjson / src / parser.cpp
1 /* This file is part of QJson
2  *
3  * Copyright (C) 2008 Flavio Castelli <flavio.castelli@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "parser.h"
22 #include "parser_p.h"
23 #include "json_parser.hh"
24 #include "json_scanner.h"
25
26 #include <QtCore/QBuffer>
27 #include <QtCore/QStringList>
28 #include <QtCore/QTextStream>
29 #include <QtCore/QDebug>
30
31 using namespace QJson;
32
33 ParserPrivate::ParserPrivate() :
34     m_scanner(0)
35   , m_negate(false)
36   , m_error(false)
37   , m_specialNumbersAllowed(false)
38 {
39 }
40
41 ParserPrivate::~ParserPrivate()
42 {
43   delete m_scanner;
44 }
45
46 void ParserPrivate::setError(QString errorMsg, int errorLine) {
47   m_error = true;
48   m_errorMsg = errorMsg;
49   m_errorLine = errorLine;
50 }
51
52 Parser::Parser() :
53     d(new ParserPrivate)
54 {
55 }
56
57 Parser::~Parser()
58 {
59   delete d;
60 }
61
62 QVariant Parser::parse (QIODevice* io, bool* ok)
63 {
64   d->m_errorMsg.clear();
65   delete d->m_scanner;
66   d->m_scanner = 0;
67
68   if (!io->isOpen()) {
69     if (!io->open(QIODevice::ReadOnly)) {
70       if (ok != 0)
71         *ok = false;
72       qCritical ("Error opening device");
73       return QVariant();
74     }
75   }
76
77   if (!io->isReadable()) {
78     if (ok != 0)
79       *ok = false;
80     qCritical ("Device is not readable");
81     io->close();
82     return QVariant();
83   }
84
85   d->m_scanner = new JSonScanner (io);
86   d->m_scanner->allowSpecialNumbers(d->m_specialNumbersAllowed);
87   yy::json_parser parser(d);
88   parser.parse();
89
90   delete d->m_scanner;
91   d->m_scanner = 0;
92
93   if (ok != 0)
94     *ok = !d->m_error;
95
96   io->close();
97   return d->m_result;
98 }
99
100 QVariant Parser::parse(const QByteArray& jsonString, bool* ok) {
101   QBuffer buffer;
102   buffer.open(QBuffer::ReadWrite);
103   buffer.write(jsonString);
104   buffer.seek(0);
105   return parse (&buffer, ok);
106 }
107
108 QString Parser::errorString() const
109 {
110   return d->m_errorMsg;
111 }
112
113 int Parser::errorLine() const
114 {
115   return d->m_errorLine;
116 }
117
118 void QJson::Parser::allowSpecialNumbers(bool allowSpecialNumbers) {
119   d->m_specialNumbersAllowed = allowSpecialNumbers;
120 }
121
122 bool Parser::specialNumbersAllowed() const {
123   return d->m_specialNumbersAllowed;
124 }