summaryrefslogtreecommitdiff
path: root/src/shared/indenter/test/main.cpp
blob: 0df204044b26e92ec86f3bd440fb4c4d437d783f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
**
**************************************************************************/

#include "indenter.h"

#include <QtCore/QFile>
#include <QtCore/QStringList>
#include <QtCore/QDebug>
#include <QtCore/QTextStream>
#include <QtCore/QFileInfo>

#include <stdio.h>

typedef SharedTools::Indenter<QStringList::const_iterator> Indenter;

static QString fileContents(const QString &fileName)
{
    QFile f(fileName);
    if (!f.open(QIODevice::ReadOnly)) {
        const QString msg = QString(QLatin1String("error: Cannot open file '%1' for reading: %2")).
                            arg(fileName).arg(f.errorString());
        qWarning(msg.toLatin1().constData());
        return QString::null;
    }

    const QString contents = QString::fromUtf8(f.readAll());
    f.close();
    if ( contents.isEmpty() ) {
        const QString msg =  QString(QLatin1String("error: File '%1' is empty")).arg(fileName);
        qWarning(msg.toLatin1().constData());
        return QString::null;
    }
    return contents;
}

static void printUsage(char *a0)
{
    const QFileInfo fi(QString::fromUtf8(a0));
    const QString usage = QString(QLatin1String("Usage: %1 [-i indent-size] [-t tab-size] file.cpp")).
                          arg(fi.fileName());
    qWarning(usage.toUtf8().constData());
}

static int integerOptionArgument(char ** &aptr, char **end)
{
    if (++aptr == end)
        return -1;
    const QString arg = QString::fromUtf8(*aptr);
    bool ok;
    const int rc = arg.toInt (&ok);
    if (!ok)
        return -1;
    return rc;
}

static QStringList parseCommandLine(char **begin, char **end)
{
    char **aptr = begin;
    if (++aptr == end)
        return QStringList();

    QStringList  fileNames;
    for ( ; aptr != end; ++aptr) {
        const char *arg = *aptr;
        if (arg[0] == '-') {
            switch (arg[1]) {
            case 't': {
                const int tabSize = integerOptionArgument(aptr, end);
                if ( tabSize == -1)
                    return QStringList();
                Indenter::instance().setTabSize(tabSize);
            }
                break;
            case 'i': {
                const int indentSize =  integerOptionArgument(aptr, end);
                if (indentSize  == -1)
                    return QStringList();
                Indenter::instance().setIndentSize(indentSize);
            }
                break;
            default:
                return  QStringList();
            }
        } else {
            fileNames.push_back(QString::fromUtf8(arg));
        }
    }
    return  fileNames;
}

int format(const QString &fileName)
{
    const QString code = fileContents(fileName);
    if (code == QString::null)
        return 1;

    QStringList program = code.split(QLatin1Char('\n'), QString::KeepEmptyParts);
    while (!program.isEmpty()) {
        if (!program.back().trimmed().isEmpty())
            break;
        program.pop_back();
    }

    QStringList p;
    QString out;

    const QChar colon = QLatin1Char(':');
    const QChar blank = QLatin1Char(' ');
    const QChar newLine = QLatin1Char('\n');

    QStringList::const_iterator cend = program.constEnd();
    for (QStringList::const_iterator it = program.constBegin(); it != cend; ++it) {
        p.push_back(*it);
        QString &line = p.back();

        QChar typedIn = Indenter::instance().firstNonWhiteSpace(line);
        if (p.last().endsWith(colon))
            typedIn = colon;

        const int indent = Indenter::instance().indentForBottomLine(p.constBegin(), p.constEnd(), typedIn);

        const QString trimmed = line.trimmed();
        // Indent the line in the list so that the formatter code sees the indented line.
        if (!trimmed.isEmpty()) {
            line = QString(indent, blank);
            line += trimmed;
        }
        out += line;
        out += newLine ;
    }

    while (out.endsWith(newLine))
        out.truncate(out.length() - 1 );

    fputs(out.toUtf8().constData(), stdout);
    return 0;
}

int main(int argc, char **argv)
{
    const QStringList fileNames = parseCommandLine(argv, argv + argc);
    if (fileNames.empty()) {
        printUsage(argv[0]);
        return 1;
    }

    foreach (const QString &fileName, fileNames)
        if (const int rc = format(fileName))
            return rc;

    return 0;
}