File: main.cpp

package info (click to toggle)
flightcrew 0.9.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 6,748 kB
  • sloc: cpp: 53,736; xml: 2,006; ansic: 275; python: 215; sh: 112; makefile: 8; exp: 8
file content (163 lines) | stat: -rw-r--r-- 4,552 bytes parent folder | download | duplicates (5)
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
/************************************************************************
**
**  Copyright (C) 2010  Strahinja Markovic
**
**  This file is part of FlightCrew.
**
**  FlightCrew is free software: you can redistribute it and/or modify
**  it under the terms of the GNU Lesser General Public License as published
**  by the Free Software Foundation, either version 3 of the License, or
**  (at your option) any later version.
**
**  FlightCrew 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 FlightCrew.  If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/

#include <iostream>
#include <flightcrew.h>
namespace fc = FlightCrew;

// We set the MSVC warning level down to 3
// for code that we have no control over
#if defined(_MSC_VER)
#   pragma warning( push, 3 )
#endif

#include <boost/foreach.hpp> 
// We're most definitely not going to use
// it as BOOST_FOREACH.
#define foreach BOOST_FOREACH

#include <boost/program_options.hpp>
namespace po = boost::program_options;

// ... and then we reset the warning level
// back to normal (warning level 4)
#if defined(_MSC_VER)
#   pragma warning( pop )
#endif

static const std::string FLIGHTCREW_VERSION = FLIGHTCREW_FULL_VERSION;

bool ValidateFiles( const std::vector< std::string > &files_to_validate )
{
    bool seen_problems = false;

    foreach( const std::string &file, files_to_validate )
    {
        std::vector< fc::Result > results;

        try
        {
            results = fc::ValidateEpub( file );
        }

        catch ( fc::FileDoesNotExistEx& )
        {
            std::cerr << "File " << file << " does not exist.\n\n";
            continue;
        }

        foreach( const fc::Result &result, results )
        {
            seen_problems = true;            
            std::cerr << result.GetFilepath();

            if ( result.GetErrorLine() > 0 )
                
                std::cerr << "(" << result.GetErrorLine() << ")";

            std::cerr << ": ";

            if ( result.GetResultType() == fc::ResultType_ERROR )

                std::cerr << "error ";

            else 

                std::cerr << "warning ";

            std::cerr << result.GetResultId() << ": ";
            std::cerr << result.GetMessage() << "\n";   
        }

        if ( !results.empty() )
        
            std::cerr << "\n";
    }

    return seen_problems;
}


int main( int argc, char *argv[] )
{
    try 
    {
        po::options_description options( "Allowed options" );
        options.add_options()
            ( "help", "produce help message" )
            ( "version", "show the program version")
            ( "input-file", po::value< std::vector< std::string > >(), "input file" );

        po::positional_options_description positionals;
        // -1 for count means unlimited
        positionals.add( "input-file", -1 );

        po::variables_map var_map;       
        store( po::command_line_parser( argc, argv ).
               options( options ).positional( positionals ).run(), var_map );
        po::notify( var_map );    

        if ( var_map.count( "help" ) )
        {
            std::cout << "Usage: flightcrew-cli [options] file...\n";
            std::cout << options << "\n";
            return 1;
        }

        if ( var_map.count( "version" ) )
        {
            std::cout << "flightcrew-cli version: " << FLIGHTCREW_VERSION <<  " " << "\n";
            return 1;
        }

        if ( var_map.count( "input-file" ) ) 
        {
            std::vector< std::string > files_to_validate =
                var_map[ "input-file" ].as< std::vector< std::string > >();

            bool problems_found = ValidateFiles( files_to_validate );
            if ( !problems_found )

                std::cout << "No problems found.\n";

            return problems_found;
        } 
        
        else
        {
            std::cout << "No input file specified. Use --help for usage information.\n";
        }
    }

    catch ( std::exception& exception )
    {
        std::cerr << "Error during run: " << exception.what() << "\n";
        return 1;
    }

    catch ( ... ) 
    {
        std::cerr << "Unknown exception!\n";
        return 1;
    }

    return 0;
}