]> https://gitweb.dealii.org/ - dealii.git/commitdiff
implement a mechanism for selecting file types at run time
authorGuido Kanschat <dr.guido.kanschat@gmail.com>
Mon, 28 Feb 2005 01:06:45 +0000 (01:06 +0000)
committerGuido Kanschat <dr.guido.kanschat@gmail.com>
Mon, 28 Feb 2005 01:06:45 +0000 (01:06 +0000)
git-svn-id: https://svn.dealii.org/trunk@9910 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/deal.II/include/grid/grid_in.h
deal.II/deal.II/source/grid/grid_in.cc

index b13588b715e8ff9fbd0ab4425227e7c7ca8ccc26..ca0c04f33a12b01b4dd41f12a34c0728ad893313 100644 (file)
@@ -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 <int dim>
 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<dim> &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 (<tt>`|'</tt>)
+                                     * 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<CellData<dim> > &cells,
                                   const std::vector<Point<dim> >    &vertices,
                                   std::ostream                               &out);
+    
+                                    /**
+                                     * Input format used by read() if
+                                     * no format is given.
+                                     */
+    Format default_format;
 };
 
 
index 4af96c5da7dd5c9a2162c80cdb33a5ae7d5e2ba1..5c2d81d17bd725e95607b9598b89a555de9ad8ac 100644 (file)
@@ -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
 
 #include <map>
 #include <algorithm>
-
+#include <fstream>
 
 
 template <int dim>
 GridIn<dim>::GridIn () :
-               tria(0) {}
+               tria(0), default_format(ucd)
+{}
 
 
 template <int dim>
@@ -989,6 +990,110 @@ GridIn<3>::debug_output_grid (const std::vector<CellData<3> > &cells,
 
 #endif
 
+template <int dim>
+void GridIn<dim>::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 <int dim>
+void GridIn<dim>::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 <int dim>
+std::string
+GridIn<dim>::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 <int dim>
+typename GridIn<dim>::Format
+GridIn<dim>::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 <int dim>
+std::string GridIn<dim>::get_format_names () 
+{
+  return "dbmesh|msh|ucd|xda";
+}
+
 
 
 //explicit instantiations

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.