From: Guido Kanschat Date: Mon, 28 Feb 2005 01:06:45 +0000 (+0000) Subject: implement a mechanism for selecting file types at run time X-Git-Tag: v8.0.0~14581 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=26a4a1455f36533a9bdef7f5e3dc5263eff7ba94;p=dealii.git implement a mechanism for selecting file types at run time git-svn-id: https://svn.dealii.org/trunk@9910 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/deal.II/include/grid/grid_in.h b/deal.II/deal.II/include/grid/grid_in.h index b13588b715..ca0c04f33a 100644 --- a/deal.II/deal.II/include/grid/grid_in.h +++ b/deal.II/deal.II/include/grid/grid_in.h @@ -2,7 +2,7 @@ // $Id$ // Version: $Name$ // -// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 by the deal.II authors +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -159,6 +159,28 @@ template class GridIn { public: + /** + * List of possible mesh input + * formats. These values are used + * when calling the function + * read() in order to determine + * the actual reader to be + * called. + */ + enum Format + { + /// Use #default_format stored in this object + Default, + /// Use read_ucd() + ucd, + /// Use read_dbmesh() + dbmesh, + /// Use read_xda() + xda, + /// Use read_msh() + msh + }; + /** * Constructor. */ @@ -170,6 +192,21 @@ class GridIn */ void attach_triangulation (Triangulation &tria); + /** + * Read from the given stream. If + * no format is given, the + * #default_format variable is + * used. + */ + void read (std::istream &, Format format=Default); + + /** + * Open the file given by the + * string and call the previous + * function read(). + */ + void read (const std::string&, Format format=Default); + /** * Read grid data from an ucd file. * Numerical data is ignored. @@ -194,6 +231,27 @@ class GridIn * Read grid data from an msh file. */ void read_msh (std::istream &); + + /** + * Returns the standard suffix + * for a file in this format. + */ + static std::string default_suffix (const Format format); + + /** + * Return the enum Format for the + * format name. + */ + static Format parse_format (const std::string &format_name); + + /** + * Return a list of implemented input + * formats. The different names are + * separated by vertical bar signs (`|') + * as used by the ParameterHandler + * classes. + */ + static std::string get_format_names (); /** * Exception @@ -360,6 +418,12 @@ class GridIn static void debug_output_grid (const std::vector > &cells, const std::vector > &vertices, std::ostream &out); + + /** + * Input format used by read() if + * no format is given. + */ + Format default_format; }; diff --git a/deal.II/deal.II/source/grid/grid_in.cc b/deal.II/deal.II/source/grid/grid_in.cc index 4af96c5da7..5c2d81d17b 100644 --- a/deal.II/deal.II/source/grid/grid_in.cc +++ b/deal.II/deal.II/source/grid/grid_in.cc @@ -2,7 +2,7 @@ // $Id$ // Version: $Name$ // -// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 by the deal.II authors +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -18,12 +18,13 @@ #include #include - +#include template GridIn::GridIn () : - tria(0) {} + tria(0), default_format(ucd) +{} template @@ -989,6 +990,110 @@ GridIn<3>::debug_output_grid (const std::vector > &cells, #endif +template +void GridIn::read (const std::string& filename, + Format format) +{ + std::ifstream in(filename.c_str()); + + Assert (in.is_open(), ExcFileNotOpen(filename.c_str())); + + if (!in) + { + std::ios_base::iostate state = in.rdstate(); + std::cerr << "File open, but error " << state << std::endl; + exit(1); + } + + read(in, format); + in.close(); +} + + +template +void GridIn::read (std::istream& in, + Format format) +{ + if (format == Default) + format = default_format; + + switch (format) + { + case dbmesh: + read_dbmesh (in); + return; + + case msh: + read_msh (in); + return; + + case ucd: + read_ucd (in); + return; + + case xda: + read_xda (in); + return; + + case Default: + break; + } + Assert (false, ExcInternalError()); +} + + + +template +std::string +GridIn::default_suffix (const Format format) +{ + switch (format) + { + case dbmesh: + return ".dbmesh"; + case msh: + return ".msh"; + case ucd: + return ".inp"; + case xda: + return ".xda"; + default: + Assert (false, ExcNotImplemented()); + return ".unknown_format"; + } +} + + + +template +typename GridIn::Format +GridIn::parse_format (const std::string &format_name) +{ + if (format_name == "dbmesh") + return dbmesh; + + if (format_name == "msh") + return msh; + + if (format_name == "ucd") + return ucd; + + if (format_name == "xda") + return xda; + + AssertThrow (false, ExcInvalidState ()); + // return something weird + return Format(Default); +} + + + +template +std::string GridIn::get_format_names () +{ + return "dbmesh|msh|ucd|xda"; +} + //explicit instantiations