source: calamares/trunk/fuentes/src/modules/test_conf.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 18 months ago

sync with github

File size: 2.8 KB
RevLine 
[7538]1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2017, Adriaan de Groot <groot@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares 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
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * This is a test-application that just checks the YAML config-file
21 * shipped with each module for correctness -- well, for parseability.
22 */
23
24#include <unistd.h>
25#include <stdlib.h>
26
27#include <iostream>
28
29#include <yaml-cpp/yaml.h>
30
31#include <QFile>
32#include <QByteArray>
33
34using std::cerr;
35
36static const char usage[] = "Usage: test_conf [-v] [-b] <file> ...\n";
37
38int main(int argc, char** argv)
39{
40    bool verbose = false;
41    bool bytes = false;
42
43    int opt;
44    while ((opt = getopt(argc, argv, "vb")) != -1) {
45        switch (opt) {
46        case 'v':
47            verbose = true;
48            break;
49        case 'b':
50            bytes = true;
51            break;
52        default: /* '?' */
53            cerr << usage;
54            return 1;
55        }
56    }
57
58    if ( optind >= argc )
59    {
60        cerr << usage;
61        return 1;
62    }
63
64    const char* filename = argv[optind];
65    try
66    {
67        YAML::Node doc;
68        if ( bytes )
69        {
70            QFile f( filename );
71            if ( f.open( QFile::ReadOnly | QFile::Text ) )
72                doc = YAML::Load( f.readAll().constData() );
73        }
74        else
75            doc = YAML::LoadFile( filename );
76
77        if ( doc.IsNull() )
78        {
79            // Special case: empty config files are valid,
80            // but aren't a map. For the example configs,
81            // this is still an error.
82            cerr << "WARNING:" << filename << '\n';
83            cerr << "WARNING: empty YAML\n";
84            return 1;
85        }
86
87        if ( !doc.IsMap() )
88        {
89            cerr << "WARNING:" << filename << '\n';
90            cerr << "WARNING: not-a-YAML-map\n";
91            return 1;
92        }
93
94        if ( verbose )
95        {
96            cerr << "Keys:\n";
97            for ( auto i = doc.begin(); i != doc.end(); ++i )
98                cerr << i->first.as<std::string>() << '\n';
99        }
100    }
101    catch ( YAML::Exception& e )
102    {
103        cerr << "WARNING:" << filename  << '\n';
104        cerr << "WARNING: YAML parser error " << e.what() << '\n';
105        return 1;
106    }
107
108    return 0;
109}
Note: See TracBrowser for help on using the repository browser.