src/hbtools/docml2bin/main.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 11 Jun 2010 13:58:22 +0300
changeset 5 627c4a0fd0e7
parent 2 06ff229162e9
child 7 923ff622b8b9
permissions -rw-r--r--
Revision: 201021 Kit: 2010123

/****************************************************************************
**
** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (developer.feedback@nokia.com)
**
** This file is part of the HbTools module of the UI Extensions for Mobile.
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at developer.feedback@nokia.com.
**
****************************************************************************/

#include <QCoreApplication>
#include <QStringList>
#include <QFile>
#include <hbdocumentloader.h>
#include <assert.h>
#include <iostream>

#include <QDebug>


void showHelp() {
    std::cout << "docml2bin.exe usage:\n\n";

    std::cout << "docml2bin -s sourceFile [-t targetFile]\n\n";

    std::cout << "    Converts plain text docml file to a binary file.\n\n";

    std::cout << "    If target file name is not given creates file named\n";
    std::cout << "    <source file> + \".bin\".\n\n";

    std::cout << "options:\n\n";

    std::cout << " -s \t\tname of the source file (\"<filename.docml>\").\n";
    std::cout << " -t \t\tname of the targe file (\"<binaryfile.bin>\").\n\n";

    std::cout << "Example:\n";
    std::cout << "docml2bin.exe -s myfile.docml -t c:/resouces/bin/mybinary.docml\n\n";
}

int main(int argc, char *argv[])
{

    qDebug() << "docml2bin, 1";

    QCoreApplication app(argc, argv);

    if (argc <= 2) {
        showHelp();
    } else {
        qDebug() << "docml2bin, 2";
        //bool backup = false;
        QString source, target;
        //QStringList restore;
        QStringList args(app.arguments());

        for (int n = 0; n < args.count(); n++) {
            if (args[n].toLower() == "-s") {
                source = args[n+1];
                n++;
            } else if (args[n].toLower() == "-t") {
                target = args[n+1];
                n++;
            /*
            } else if (args[n].toLower() == "--backup-and-replace") {
                backup = true;
            } else if (args[n].toLower() == "-restore") {
                n++;
                while (n < args.count()) {
                    restore.append( args[n] );
                    n++;
                }
            */
            }
        }
        qDebug() << "docml2bin, 3";
        /*
        if (restore.count()) {
            for (int i = 0; i < restore.count(); i++) {
                QString orig = restore.at(i) + ".orig";
                if ( QFile::exists(orig) ) {
                    QFile::remove(restore.at(i));
                    QFile origFile(orig);
                    origFile.rename(restore.at(i));
                }
            }
            return 0;
        }
        */

        if (source.length() > 0) {
            /*
            if (backup) {
                QString orig = source + ".orig";
                if ( QFile::exists(orig) ) {
                    QFile::remove(orig);
                }
                QFile origFile(source);
                origFile.rename(orig);
                target = source;
                source = orig;
            }
            */
            qDebug() << "docml2bin, 4";
            if (!QFile::exists(source)) {
                std::cout << "Error: file " << source.toStdString() << " does not exist.\n";
            } else {
                qDebug() << "docml2bin, 5";
                // Open file and parse lines. Each line should have three value separated with:
                QFile sourceFile(source);
                if (sourceFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
                    qDebug() << "docml2bin, 6";
                    if (!target.length()) {
                        target = source + ".bin";
                    }
                    QFile targetFile(target);
                    if (targetFile.open(QIODevice::WriteOnly)) {
                        qDebug() << "docml2bin, 7";
                        HbDocumentLoader loader;
                        loader.createBinary( &sourceFile, &targetFile );
                        qDebug() << "docml2bin, 8";
                        targetFile.close();
                    }
                    sourceFile.close();
                }
            }
        }
    }
    qDebug() << "docml2bin, 9";

    return 0;
}