]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew-cli/main.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew-cli / main.cpp
1 /************************************************************************
2 **
3 **  Copyright (C) 2010  Strahinja Markovic
4 **
5 **  This file is part of FlightCrew.
6 **
7 **  FlightCrew is free software: you can redistribute it and/or modify
8 **  it under the terms of the GNU Lesser General Public License as published
9 **  by the Free Software Foundation, either version 3 of the License, or
10 **  (at your option) any later version.
11 **
12 **  FlightCrew is distributed in the hope that it will be useful,
13 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 **  GNU Lesser General Public License for more details.
16 **
17 **  You should have received a copy of the GNU Lesser General Public License
18 **  along with FlightCrew.  If not, see <http://www.gnu.org/licenses/>.
19 **
20 *************************************************************************/
21
22 #include <iostream>
23 #include <flightcrew.h>
24 namespace fc = FlightCrew;
25
26 // We set the MSVC warning level down to 3
27 // for code that we have no control over
28 #if defined(_MSC_VER)
29 #   pragma warning( push, 3 )
30 #endif
31
32 #include <boost/foreach.hpp> 
33 // We're most definitely not going to use
34 // it as BOOST_FOREACH.
35 #define foreach BOOST_FOREACH
36
37 #include <boost/program_options.hpp>
38 namespace po = boost::program_options;
39
40 // ... and then we reset the warning level
41 // back to normal (warning level 4)
42 #if defined(_MSC_VER)
43 #   pragma warning( pop )
44 #endif
45
46 static const std::string FLIGHTCREW_VERSION = FLIGHTCREW_FULL_VERSION;
47
48 bool ValidateFiles( const std::vector< std::string > &files_to_validate )
49 {
50     bool seen_problems = false;
51
52     foreach( const std::string &file, files_to_validate )
53     {
54         std::vector< fc::Result > results;
55
56         try
57         {
58             results = fc::ValidateEpub( file );
59         }
60
61         catch ( fc::FileDoesNotExistEx& )
62         {
63             std::cerr << "File " << file << " does not exist.\n\n";
64             continue;
65         }
66
67         foreach( const fc::Result &result, results )
68         {
69             seen_problems = true;            
70             std::cerr << result.GetFilepath();
71
72             if ( result.GetErrorLine() > 0 )
73                 
74                 std::cerr << "(" << result.GetErrorLine() << ")";
75
76             std::cerr << ": ";
77
78             if ( result.GetResultType() == fc::ResultType_ERROR )
79
80                 std::cerr << "error ";
81
82             else 
83
84                 std::cerr << "warning ";
85
86             std::cerr << result.GetResultId() << ": ";
87             std::cerr << result.GetMessage() << "\n";   
88         }
89
90         if ( !results.empty() )
91         
92             std::cerr << "\n";
93     }
94
95     return seen_problems;
96 }
97
98
99 int main( int argc, char *argv[] )
100 {
101     try 
102     {
103         po::options_description options( "Allowed options" );
104         options.add_options()
105             ( "help", "produce help message" )
106             ( "version", "show the program version")
107             ( "input-file", po::value< std::vector< std::string > >(), "input file" );
108
109         po::positional_options_description positionals;
110         // -1 for count means unlimited
111         positionals.add( "input-file", -1 );
112
113         po::variables_map var_map;       
114         store( po::command_line_parser( argc, argv ).
115                options( options ).positional( positionals ).run(), var_map );
116         po::notify( var_map );    
117
118         if ( var_map.count( "help" ) )
119         {
120             std::cout << "Usage: flightcrew-cli [options] file...\n";
121             std::cout << options << "\n";
122             return 1;
123         }
124
125         if ( var_map.count( "version" ) )
126         {
127             std::cout << "flightcrew-cli version: " << FLIGHTCREW_VERSION <<  " " << "\n";
128             return 1;
129         }
130
131         if ( var_map.count( "input-file" ) ) 
132         {
133             std::vector< std::string > files_to_validate =
134                 var_map[ "input-file" ].as< std::vector< std::string > >();
135
136             bool problems_found = ValidateFiles( files_to_validate );
137             if ( !problems_found )
138
139                 std::cout << "No problems found.\n";
140
141             return problems_found;
142         } 
143         
144         else
145         {
146             std::cout << "No input file specified. Use --help for usage information.\n";
147         }
148     }
149
150     catch ( std::exception& exception )
151     {
152         std::cerr << "Error during run: " << exception.what() << "\n";
153         return 1;
154     }
155
156     catch ( ... ) 
157     {
158         std::cerr << "Unknown exception!\n";
159         return 1;
160     }
161
162     return 0;
163 }