QJson project page QJson home page

/work/a/ports/devel/qjson/work/qjson/src/parser.cpp
00001 /* This file is part of QJson
00002  *
00003  * Copyright (C) 2008 Flavio Castelli <flavio.castelli@gmail.com>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "parser.h"
00022 #include "parser_p.h"
00023 #include "json_parser.hh"
00024 #include "json_scanner.h"
00025 
00026 #include <QtCore/QBuffer>
00027 #include <QtCore/QStringList>
00028 #include <QtCore/QTextStream>
00029 #include <QtCore/QDebug>
00030 
00031 using namespace QJson;
00032 
00033 ParserPrivate::ParserPrivate() :
00034     m_scanner(0)
00035   , m_negate(false)
00036   , m_error(false)
00037 {
00038 }
00039 
00040 ParserPrivate::~ParserPrivate()
00041 {
00042   delete m_scanner;
00043 }
00044 
00045 void ParserPrivate::setError(QString errorMsg, int errorLine) {
00046   m_error = true;
00047   m_errorMsg = errorMsg;
00048   m_errorLine = errorLine;
00049 }
00050 
00051 Parser::Parser() :
00052     d(new ParserPrivate)
00053 {
00054 }
00055 
00056 Parser::~Parser()
00057 {
00058   delete d;
00059 }
00060 
00061 QVariant Parser::parse (QIODevice* io, bool* ok)
00062 {
00063   d->m_errorMsg.clear();
00064   delete d->m_scanner;
00065   d->m_scanner = 0;
00066 
00067   if (!io->isOpen()) {
00068     if (!io->open(QIODevice::ReadOnly)) {
00069       if (ok != 0)
00070         *ok = false;
00071       qCritical ("Error opening device");
00072       return QVariant();
00073     }
00074   }
00075 
00076   if (!io->isReadable()) {
00077     if (ok != 0)
00078       *ok = false;
00079     qCritical ("Device is not readable");
00080     io->close();
00081     return QVariant();
00082   }
00083 
00084   d->m_scanner = new JSonScanner (io);
00085   yy::json_parser parser(d);
00086   parser.parse();
00087 
00088   delete d->m_scanner;
00089   d->m_scanner = 0;
00090 
00091   if (ok != 0)
00092     *ok = !d->m_error;
00093 
00094   io->close();
00095   return d->m_result;
00096 }
00097 
00098 QVariant Parser::parse(const QByteArray& jsonString, bool* ok) {
00099   QBuffer buffer;
00100   buffer.open(QBuffer::ReadWrite);
00101   buffer.write(jsonString);
00102   buffer.seek(0);
00103   return parse (&buffer, ok);
00104 }
00105 
00106 QString Parser::errorString() const
00107 {
00108   return d->m_errorMsg;
00109 }
00110 
00111 int Parser::errorLine() const
00112 {
00113   return d->m_errorLine;
00114 }

SourceForge Logo hosts this site. Send comments to:
QJson Developers