1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
// *****************************************************************************
/*!
  \file      src/IO/MeshDetect.cpp
  \copyright 2012-2015 J. Bakosi,
             2016-2018 Los Alamos National Security, LLC.,
             2019-2021 Triad National Security, LLC.,
             2022-2025 J. Bakosi
             All rights reserved. See the LICENSE file for details.
  \brief     Unstructured mesh file format detection
  \details   Unstructured mesh file format detection functions.
*/
// *****************************************************************************

#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdexcept><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "MeshDetect.hpp"
#include "Exception.hpp"
#include "Reader.hpp"

namespace tk {

MeshReaderType
detectInput( const std::string& filename )
// *****************************************************************************
//  Detect input mesh file type
//! \param[in] filename File to open and detect its type
//! \return enum specifying the mesh reader type
// *****************************************************************************
{
  std::ifstream inFile;

  // Check if file exists, throw exception if it does not
  inFile.open( filename, std::ifstream::in );
  ErrChk( inFile.good(), "Failed to open file: " + filename );

  inFile.get();
  ErrChk( !inFile.fail(), "Failed to read from file: " + filename );

  // Close it
  inFile.close();
  ErrChk( !inFile.fail(), "Failed to close file: " + filename );

  // Get first three letters from input file
  std::string s( Reader( filename ).firstline().substr(0,4) );

  if ( s.find("$Me") != std::string::npos ) {
    return MeshReaderType::GMSH;
  } else if ( s.find("CDF") != std::string::npos ||
              s.find("HDF") != std::string::npos ) {
    return MeshReaderType::EXODUSII;
  } else if ( s.find("*nd") != std::string::npos ) {
    return MeshReaderType::ASC;
  } else if ( s.find("   ") != std::string::npos ) {
    return MeshReaderType::UGRID;
  } else if ( s.find(" npo") != std::string::npos ) {
    return MeshReaderType::RDGFLO;
  } else if ( s.find("Mes") != std::string::npos ) {
    return MeshReaderType::MEDIT;
  } else {
    try {
      // cppcheck-suppress ignoredReturnValue
      std::stoi(s);    // try to convert to an integer
    } catch ( const std::invalid_argument& ) {
      Throw( "Input mesh file type could not be determined from header: " +
             filename );
    }
    // could also catch std::out_of_range, the other exception potentially
    // thrown by std::stoi(), but a three-digit integer will always fit into int

    // if we got here, the above string-to-integer conversion succeeded
    return MeshReaderType::NETGEN;
  }
}

MeshWriterType
pickOutput( const std::string& filename )
// *****************************************************************************
//  Determine output mesh file type
//! \param[in] filename Filename to pick its type based on extension given
//! \return enum specifying the mesh writer type
// *****************************************************************************
{
  // Get extension of input file name
  std::string fn = filename;
  std::string ext( fn.substr(fn.find_last_of(".") + 1) );

  if ( ext == "msh" ) {
    return MeshWriterType::GMSH;
  } else if ( ext == "exo" || ext == "h5" ) {
    return MeshWriterType::EXODUSII;
  } else if ( ext == "mesh" ) {
    return MeshWriterType::NETGEN;
  } else {
    Throw( "Output mesh file type could not be determined from extension of "
           "filename '" + filename + "'; valid extensions are: "
           "'msh' for Gmsh, 'exo' or 'h5' for ExodusII, 'mesh' for Netgen's "
           "neutral" );
  }
}

} // tk::