From 4181c946f4b9210185271263de92b596b437a317 Mon Sep 17 00:00:00 2001 From: wolf Date: Fri, 1 Jul 2005 21:44:24 +0000 Subject: [PATCH] A number of changes into the right direction. git-svn-id: https://svn.dealii.org/trunk@11050 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/examples/step-19/step-19.cc | 297 +++++++++++++++++----------- 1 file changed, 182 insertions(+), 115 deletions(-) diff --git a/deal.II/examples/step-19/step-19.cc b/deal.II/examples/step-19/step-19.cc index c245ab3ff8..1c0ef0476e 100644 --- a/deal.II/examples/step-19/step-19.cc +++ b/deal.II/examples/step-19/step-19.cc @@ -11,32 +11,188 @@ /* to the file deal.II/doc/license.html for the text and */ /* further information on this license. */ -// Base libraries + + // As usual, we start with include + // files. This program is content with really + // few of these -- we only need two files + // from the library (one for input and output + // of graphical data, one for parameter + // handling), and a few C++ standard headers: #include +#include -// C++ libraries +#include #include #include + // Before we start with the actual program, + // let us declare a few global variables that + // will be used to hold the parameters this + // program is going to use. Usually, global + // variables are frowned upon for a good + // reason, but since we have such a short + // program here that does only a single + // thing, we may stray from our usual line + // and make these variables global, rather + // than passing them around to all functions + // or encapsulating them into a class. + // + // The variables we have are: first, an + // object that will hold parameters of + // operation, such as output format (unless + // given on the command line); second, the + // names of input and output files; third, + // the format in which the output is to be + // written; and fourth the name of a + // parameter file: +ParameterHandler prm; +std::vector input_file_names; +std::string output_file; +std::string output_format; +std::string parameter_file; + + + // All the stuff this program does can be + // done from here on. As described in the + // introduction, what we have to do is + // declare what values the parameter file can + // have, parse the command line, read the + // input files, then write the output. We + // will do this in this order of operation, + // but before that let us declare a function + // that prints a message about how this + // program is to be used; the function first + // prints a general message, and then goes on + // to list the parameters that are allowed in + // the parameter file (the + // ``ParameterHandler'' class has a function + // to do exactly this): +void +print_usage_message () +{ + static const char* message + = + "\n" + "Converter from deal.II intermediate format to other graphics formats.\n" + "\n" + "Usage:\n" + " ./step-19 [-p parameter_file] list_of_input_files \n" + " [-x output_format] [-o output_file]\n" + "\n" + "Parameter sequences in brackets can be omitted a parameter file is\n" + "specified on the command line and if it provides values for these\n" + "missing parameters.\n" + "\n" + "The parameter file has the following format and allows the following\n" + "values (you can cut and paste this and use it for your own parameter\n" + "file):\n" + "\n"; + std::cout << message; + + prm.print_parameters (std::cout, ParameterHandler::Text); +} + + +void declare_parameters () +{ + prm.declare_entry ("Output file", "", + Patterns::Anything(), + "The name of the output file to be generated"); + + DataOutInterface<1,1>::declare_parameters (prm); +} + + + +void +parse_command_line (const int argc, + char *const* argv) +{ + if (argc < 2) + { + print_usage_message (); + exit (1); + } + + std::list args; + for (int i=1; i -void do_convert (const std::vector &input_files, - const std::string &output_file, - const std::string &/*parameter_file*/, - const std::string &output_format_extension) +void do_convert () { - std::ifstream intermediate_stream (input_files[0].c_str()); + std::ifstream intermediate_stream (input_file_names[0].c_str()); AssertThrow (intermediate_stream, ExcIO()); DataOutReader intermediate_data; intermediate_data.read (intermediate_stream); // for all following input files - for (unsigned int i=1; i additional_data; @@ -47,20 +203,20 @@ void do_convert (const std::vector &input_files, std::ofstream output_stream (output_file.c_str()); AssertThrow (output_stream, ExcIO()); - const typename DataOutInterface::OutputFormat - output_format - = intermediate_data.parse_output_format (output_format_extension); + const DataOutBase::OutputFormat format + = intermediate_data.parse_output_format (output_format); - intermediate_data.write(output_stream, output_format); + intermediate_data.write(output_stream, format); } -void convert (const std::vector &input_files, - const std::string &output_file, - const std::string ¶meter_file, - const std::string &output_format_extension) + +void convert () { - std::ifstream input(input_files[0].c_str()); + AssertThrow (input_file_names.size() > 0, + ExcMessage ("No input files specified.")); + + std::ifstream input(input_file_names[0].c_str()); AssertThrow (input, ExcIO()); const std::pair @@ -72,17 +228,11 @@ void convert (const std::vector &input_files, switch (dimensions.second) { case 1: - do_convert <1,1> (input_files, - output_file, - parameter_file, - output_format_extension); + do_convert <1,1> (); return; case 2: - do_convert <1,2> (input_files, - output_file, - parameter_file, - output_format_extension); + do_convert <1,2> (); return; } AssertThrow (false, ExcNotImplemented()); @@ -91,17 +241,11 @@ void convert (const std::vector &input_files, switch (dimensions.second) { case 2: - do_convert <2,2> (input_files, - output_file, - parameter_file, - output_format_extension); + do_convert <2,2> (); return; case 3: - do_convert <2,3> (input_files, - output_file, - parameter_file, - output_format_extension); + do_convert <2,3> (); return; } AssertThrow (false, ExcNotImplemented()); @@ -110,10 +254,7 @@ void convert (const std::vector &input_files, switch (dimensions.second) { case 3: - do_convert <3,3> (input_files, - output_file, - parameter_file, - output_format_extension); + do_convert <3,3> (); return; } AssertThrow (false, ExcNotImplemented()); @@ -128,84 +269,10 @@ int main (int argc, char ** argv) { try { - if (argc == 1) - { - std::cout << std::endl - << "Converter from deal.II intermediate format to " - << "other graphics formats." - << std::endl << std::endl - << "Usage: ./step-19 [-p parameter_file] " - << "list_of_input_files [-x output_format] " - << "output_file" - << std::endl - << std::endl; - exit (1); - } - - std::string parameter_file; - std::string output_format_extension; - std::vector file_names; - - for (int i=1; i input_files (file_names.begin(), - file_names.end()-1); - const std::string output_file (file_names.back()); - - if (output_format_extension.size() == 0) - { - output_format_extension - = std::string(output_file.begin() + output_file.rfind ('.') + 1, - output_file.end()); - - if (output_format_extension.size() == 0) - { - std::cerr << "Error: could not determine output file format " - << "from name of last file name." - << std::endl; - exit (1); - } - } + declare_parameters (); + parse_command_line (argc, argv); - convert (input_files, - output_file, - parameter_file, - output_format_extension); + convert (); } catch (std::exception &exc) { -- 2.39.5