00001 00029 #include <cstdlib> 00030 #include <string> 00031 #include <iostream> 00032 00033 // Settings 00034 #include "config.h" 00035 // Init functions 00036 #include "init.h" 00037 // Notifications 00038 #include "notifier.h" 00039 // Params parsing 00040 #include "params.h" 00041 // Processing 00042 #include "processor.h" 00043 // Output writing 00044 #include "writer.h" 00045 00048 void exit_failure(void) { 00049 ace::notifier::progress.info("\nProgram terminated due to fatal error(s). See stderr output for more info."); 00050 exit(EXIT_FAILURE); 00051 } 00052 00058 int main(int argc, char** argv) { 00059 00060 // 0. Parse params if any. 00061 if ( argc == 1 ) { 00062 // Let the user know the right way of riding our piece of software :) 00063 ace::print_usage(); 00064 return EXIT_SUCCESS; 00065 } 00066 ace::parse_params(argc, argv); 00067 if ( !ace::settings::ready() ) { 00068 ace::notifier::error.fatal_error("Some of the mandatory program parameters are missing!"); 00069 // The user definitely needs to know the right way of riding our piece of software :) 00070 ace::print_usage(); 00071 exit_failure(); 00072 } else if ( !ace::settings::correct() ) { 00073 ace::notifier::error.fatal_error("Some of the program parameters are in incorrect format!"); 00074 // Again, let the user know the right way of riding our piece of software :) 00075 ace::print_usage(); 00076 exit_failure(); 00077 } 00078 00079 // 00080 ace::notifier::progress.info("Performing start-up checks."); 00081 00082 // 1. Startup init. 00083 if ( !ace::startup_init() ) { 00084 // Some error occurred - notification has been already printed out, so... 00085 exit_failure(); 00086 } 00087 00088 // Files to be processed. 00089 ace::filenames_t files_to_process; 00090 size_t files_total_size = 0; 00091 00092 // 2. Open input file and gather data filenames. 00093 if ( !ace::read_and_check_input_datafiles(files_to_process, files_total_size) ) { 00094 // Some error occurred - notification has been already printed out, so... 00095 exit_failure(); 00096 } 00097 00098 // 3. Open output file (and keep it open!) 00099 std::ofstream output_file(ace::settings::output_file); 00100 if ( !output_file ) { 00101 ace::notifier::error.fatal_error("Cannot open output file: " + std::string(ace::settings::output_file) + "!"); 00102 exit_failure(); 00103 } 00104 00105 // 4. Open context output file (if context tracing is set on) 00106 std::ofstream context_output_file; 00107 00108 if ( ace::settings::context_tracing_on() ) { 00109 // Open context output file (and keep it open!) 00110 context_output_file.open(ace::settings::context_output_file); 00111 if ( !context_output_file ) { 00112 ace::notifier::error.fatal_error("Cannot open context output file: " + std::string(ace::settings::context_output_file) + "!"); 00113 exit_failure(); 00114 } 00115 } 00116 00117 // 5. Open stats output file (if given:) 00118 std::ofstream stats_output_file; 00119 00120 if ( ace::settings::stats_file ) { 00121 // Open and keep it open. 00122 stats_output_file.open(ace::settings::stats_file); 00123 if ( !stats_output_file ) { 00124 ace::notifier::error.fatal_error("Cannot open file to save statistics: " + std::string(ace::settings::stats_file) + "!"); 00125 exit_failure(); 00126 } 00127 } 00128 00129 // 5.5 Open morphologic filter stats file (if given:) 00130 std::ofstream morphologic_filter_stats_output_file; 00131 00132 if ( ace::settings::morphologic_filter_stats_file ) { 00133 // Open and keep it open. 00134 morphologic_filter_stats_output_file.open(ace::settings::morphologic_filter_stats_file); 00135 if ( !morphologic_filter_stats_output_file ) { 00136 ace::notifier::error.fatal_error("Cannot open file to save morphologic filter statistics: " + std::string(ace::settings::morphologic_filter_stats_file) + "!"); 00137 exit_failure(); 00138 } 00139 } 00140 00141 // 00142 ace::notifier::progress.elapsed("Finished performing start-up checks."); 00143 00144 // 00145 try { 00146 // 6. Process input files. 00147 ace::process(files_to_process, files_total_size); 00148 } catch ( std::exception& e ) { 00149 ace::notifier::error.fatal_error(e.what()); 00150 exit_failure(); 00151 } 00152 00153 // 7.6 Output stats. 00154 if ( stats_output_file.is_open() ) { 00155 // Inform & perform (TM). 00156 ace::notifier::progress.info("Writing statistics file."); 00157 ace::write_stats(stats_output_file); 00158 stats_output_file.close(); 00159 ace::notifier::progress.elapsed("Finished writing statistics file."); 00160 } 00161 00162 // 7.7 Output morphologic filter stats. 00163 if ( morphologic_filter_stats_output_file.is_open() ) { 00164 // Inform & perform (TM). 00165 ace::notifier::progress.info("Writing morphologic filter statistics file."); 00166 ace::write_morphologic_filter_stats(morphologic_filter_stats_output_file); 00167 morphologic_filter_stats_output_file.close(); 00168 ace::notifier::progress.elapsed("Finished writing morphologic filter statistics file."); 00169 } 00170 00171 // 00172 ace::notifier::progress.info("Writing output datafile."); 00173 00174 // 7. Print the results to the file. 00175 ace::write(output_file); 00176 output_file.close(); 00177 00178 // Elapsed: 00179 ace::notifier::progress.elapsed("Finished writing output datafile."); 00180 00181 // 7.5 Output contexts. 00182 if ( context_output_file.is_open() ) { 00183 // Inform & perform (TM). 00184 ace::notifier::progress.info("Writing contexts to file."); 00185 ace::write_contexts(context_output_file); 00186 context_output_file.close(); 00187 ace::notifier::progress.elapsed("Finished writing contexts to file."); 00188 } 00189 00190 // 8. Nara, papa!:) 00191 ace::notifier::progress.info("Bye!"); 00192 00193 return EXIT_SUCCESS; 00194 }
1.5.6