Commit c807aaee authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Merge branch 'feature/file_version' into 'master'

add file-version utility

See merge request !8
parents a670fab7 998e75a6
Pipeline #5408 passed with stage
in 22 minutes and 37 seconds
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <dune/common/exceptions.hh>
#include "string.hh"
namespace Dune
{
namespace Gmsh4
{
/// Return a version tuple identifying the .msh file format version
inline std::vector<int> fileVersion(std::string filename)
{
std::ifstream file(filename, std::ios_base::in);
std::string section;
file >> section;
if (section != "$MeshFormat")
DUNE_THROW(Dune::IOError, "Invalid header of msh file.");
std::string version;
int file_type = -1;
int data_size = -1;
file >> version >> file_type >> data_size;
if (std::stod(version) <= 0.0)
DUNE_THROW(Dune::IOError, "Invalid version number in msh file.");
if (file_type != 0 and file_type != 1)
DUNE_THROW(Dune::IOError, "Invalid file-type: 0 for ASCII mode, 1 for binary mode.");
if (data_size < 4 || data_size > 16)
DUNE_THROW(Dune::IOError, "Invalid data-size range: should be in {4, 16}");
std::vector<int> version_tuple;
split(version.begin(), version.end(), '.', [&](auto first, auto last) {
version_tuple.push_back(std::stoi(std::string{first,last}));
});
return version_tuple;
}
}
}
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <dune/gmsh4/gmsh4reader.hh> #include <dune/gmsh4/gmsh4reader.hh>
#include <dune/gmsh4/gridcreators/continuousgridcreator.hh> #include <dune/gmsh4/gridcreators/continuousgridcreator.hh>
#include <dune/gmsh4/gridcreators/discontinuousgridcreator.hh> #include <dune/gmsh4/gridcreators/discontinuousgridcreator.hh>
#include <dune/gmsh4/utility/version.hh>
#include <dune/alugrid/grid.hh> #include <dune/alugrid/grid.hh>
...@@ -34,6 +35,10 @@ int main(int argc, char** argv) ...@@ -34,6 +35,10 @@ int main(int argc, char** argv)
using GridType = Dune::ALUGrid<2,2,Dune::simplex,Dune::conforming>; using GridType = Dune::ALUGrid<2,2,Dune::simplex,Dune::conforming>;
using GridView = typename GridType::LeafGridView; using GridView = typename GridType::LeafGridView;
{ {
auto ver = Gmsh4::fileVersion(GRID_PATH "/square.msh");
if (ver[0] != 4)
DUNE_THROW(Dune::Exception, "Wrong .msh file version");
auto gridPtr = Gmsh4Reader<GridType>::createGridFromFile(GRID_PATH "/square.msh"); auto gridPtr = Gmsh4Reader<GridType>::createGridFromFile(GRID_PATH "/square.msh");
auto& grid = *gridPtr; auto& grid = *gridPtr;
...@@ -50,6 +55,10 @@ int main(int argc, char** argv) ...@@ -50,6 +55,10 @@ int main(int argc, char** argv)
} }
{ {
auto ver = Gmsh4::fileVersion(GRID_PATH "/square_part2.msh");
if (ver[0] != 4)
DUNE_THROW(Dune::Exception, "Wrong .msh file version");
auto gridPtr = Gmsh4Reader<GridType>::createGridFromFile(GRID_PATH "/square_part2.msh"); auto gridPtr = Gmsh4Reader<GridType>::createGridFromFile(GRID_PATH "/square_part2.msh");
auto& grid = *gridPtr; auto& grid = *gridPtr;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment