Initial commit.

This commit is contained in:
Jonas Kvinge
2018-02-27 18:06:05 +01:00
parent 85d9664df7
commit b2b1ba7abe
1393 changed files with 177311 additions and 1 deletions

View File

@@ -0,0 +1,4 @@
Makefile
*.o
*.moc
cmdline_tester

View File

@@ -0,0 +1,35 @@
##### Probably don't want to edit below this line #####
IF (WIN32 AND Qt5Core_FOUND)
FIND_PACKAGE( Qt5Widgets REQUIRED )
INCLUDE_DIRECTORIES(${Qt5Widgets_INCLUDE_DIRS})
ADD_DEFINITIONS(${Qt5Widgets_DEFINITIONS})
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
ENDIF()
IF (NOT Qt5Core_FOUND)
# Use it
INCLUDE( ${QT_USE_FILE} )
ENDIF()
INCLUDE(AddFileDependencies)
# Include the library include directories, and the current build directory (moc)
INCLUDE_DIRECTORIES(
../../include
${CMAKE_CURRENT_BINARY_DIR}
)
ADD_EXECUTABLE(
cmdline_tester
cmdline_tester.cpp
cmdlineparser.cpp
)
TARGET_LINK_LIBRARIES(
cmdline_tester
${QT_LIBRARIES}
${Qt5Widgets_LIBRARIES}
qjson${QJSON_SUFFIX}
)

View File

@@ -0,0 +1,99 @@
/* This file is part of QJson
*
* Copyright (C) 2009 Flavio Castelli <flavio.castelli@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include <QtCore/QTextCodec>
#include <QtCore/QTime>
#include <QJson/Parser>
#include <QJson/Serializer>
#include "cmdlineparser.h"
using namespace QJson;
int main(int argc, char *argv[]) {
QCoreApplication app (argc, argv);
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForCStrings(codec);
#endif
QTime time;
int duration;
CmdLineParser cmd (app.arguments());
CmdLineParser::Result res = cmd.parse();
if (res == CmdLineParser::Help)
return 0;
else if (res == CmdLineParser::Error)
return -1;
QString filename = cmd.file();
if (!QFile::exists ( filename )) {
qCritical ("The file you specified doesn't exist!");
exit (1);
}
Parser parser;
bool ok;
QFile file (filename);
time.start();
QVariant data = parser.parse (&file, &ok);
duration = time.elapsed();
if (!ok) {
qCritical("%s:%i - Error: %s", filename.toLatin1().data(), parser.errorLine(), qPrintable(parser.errorString()));
exit (1);
}
else {
qDebug() << "Parsing of" << filename << "took" << duration << "ms";
if (!cmd.quiet())
qDebug() << data;
}
if (cmd.serialize()) {
// serializer tests
qDebug() << "Serializing... ";
QJson::Serializer serializer;
serializer.setIndentMode(cmd.indentationMode());
time.start();
QByteArray b = serializer.serialize(data, &ok);
if (!ok) {
qCritical() << "Serialization failed:" << serializer.errorMessage();
exit(1);
} else {
duration = time.elapsed();
qDebug() << "Serialization took:" << duration << "ms";
if (!cmd.quiet())
qDebug() << b;
}
}
qDebug() << "JOB DONE, BYE";
return 0;
}

View File

@@ -0,0 +1,170 @@
/* This file is part of qjson
*
* Copyright (C) 2010 Flavio Castelli <flavio@castelli.name>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <QtCore/QStringBuilder>
#ifdef Q_OS_WIN
//using Qt5
#ifdef QT_WIDGETS_LIB
#include <QtWidgets/QMessageBox>
#else
//using Qt4
#include <QtGui/QMessageBox>
#endif
#endif
#include "cmdlineparser.h"
using namespace QJson;
const QString CmdLineParser::m_helpMessage = QLatin1String(
"Usage: cmdline_tester [options] file\n\n"
"This program converts the json data read from 'file' to a QVariant object.\n"
"--quiet Do not print output generated by parser and serializer.\n"
"--serialize Parses the QVariant object back to json.\n"
"--indent Sets the indentation level used by the 'serialize' option.\n"
" Allowed values:\n"
" - none [default]\n"
" - compact\n"
" - minimum\n"
" - medium\n"
" - full\n"
"--help Displays this help.\n"
);
CmdLineParser::CmdLineParser(const QStringList &arguments)
: m_pos(0),
m_indentationMode(IndentNone),
m_serialize(false),
m_quiet(false)
{
for (int i = 1; i < arguments.count(); ++i) {
const QString &arg = arguments.at(i);
m_arguments.append(arg);
}
}
CmdLineParser::Result CmdLineParser::parse()
{
bool showHelp = false;
while (m_error.isEmpty() && hasMoreArgs()) {
const QString &arg = nextArg();
if (arg.toLower() == QLatin1String("--indent"))
handleSetIndentationMode();
else if (arg.toLower() == QLatin1String("--help"))
showHelp = true;
else if (arg.toLower() == QLatin1String("--serialize"))
m_serialize = true;
else if (arg.toLower() == QLatin1String("--quiet"))
m_quiet = true;
else if (!arg.startsWith(QLatin1String("--")))
m_file = arg;
else
m_error = QString(QLatin1String("Unknown option: %1")).arg(arg);
}
if (m_file.isEmpty()) {
m_error = QLatin1String("You have to specify the file containing the json data.");
}
if (!m_error.isEmpty()) {
showMessage(m_error + QLatin1String("\n\n\n") + m_helpMessage, true);
return Error;
} else if (showHelp) {
showMessage(m_helpMessage, false);
return Help;
}
return Ok;
}
bool CmdLineParser::hasMoreArgs() const
{
return m_pos < m_arguments.count();
}
const QString &CmdLineParser::nextArg()
{
Q_ASSERT(hasMoreArgs());
return m_arguments.at(m_pos++);
}
void CmdLineParser::handleSetIndentationMode()
{
if (hasMoreArgs()) {
const QString &indentationMode = nextArg();
if (indentationMode.compare(QLatin1String("none"), Qt::CaseInsensitive) == 0)
m_indentationMode = IndentNone;
else if (indentationMode.compare(QLatin1String("compact"), Qt::CaseInsensitive) == 0)
m_indentationMode = IndentCompact;
else if (indentationMode.compare(QLatin1String("minimum"), Qt::CaseInsensitive) == 0)
m_indentationMode = IndentMinimum;
else if (indentationMode.compare(QLatin1String("medium"), Qt::CaseInsensitive) == 0)
m_indentationMode = IndentMedium;
else if (indentationMode.compare(QLatin1String("full"), Qt::CaseInsensitive) == 0)
m_indentationMode = IndentFull;
else
m_error = QString(QLatin1String("Unknown indentation mode '%1'.")).
arg(indentationMode);
} else {
m_error = QLatin1String("Missing indentation level.");
}
}
void CmdLineParser::showMessage(const QString &msg, bool error)
{
#ifdef Q_OS_WIN
QString message = QLatin1String("<pre>") % msg % QLatin1String("</pre>");
if (error)
QMessageBox::critical(0, QLatin1String("Error"), message);
else
QMessageBox::information(0, QLatin1String("Notice"), message);
#else
fprintf(error ? stderr : stdout, "%s\n", qPrintable(msg));
#endif
}
void CmdLineParser::setIndentationMode(const IndentMode &mode)
{
m_indentationMode = mode;
}
IndentMode CmdLineParser::indentationMode() const
{
return m_indentationMode;
}
QString CmdLineParser::file() const
{
return m_file;
}
bool CmdLineParser::serialize()
{
return m_serialize;
}
bool CmdLineParser::quiet()
{
return m_quiet;
}

View File

@@ -0,0 +1,64 @@
/* This file is part of qjson
*
* Copyright (C) 2010 Flavio Castelli <flavio@castelli.name>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef CMDLINEPARSER_H
#define CMDLINEPARSER_H
#include <QtCore/QCoreApplication>
#include <QtCore/QStringList>
#include <QJson/Serializer>
namespace QJson {
class CmdLineParser
{
public:
enum Result {Ok, Help, Error};
CmdLineParser(const QStringList &arguments);
Result parse();
void setIndentationMode(const IndentMode &mode);
IndentMode indentationMode() const;
QString helpFile() const;
QString file() const;
bool serialize();
bool quiet();
void showMessage(const QString &msg, bool error);
private:
bool hasMoreArgs() const;
const QString &nextArg();
void handleSetIndentationMode();
QStringList m_arguments;
int m_pos;
IndentMode m_indentationMode;
QString m_file;
bool m_serialize;
bool m_quiet;
static const QString m_helpMessage;
QString m_error;
};
}
#endif

View File

@@ -0,0 +1,22 @@
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}