LCOV - code coverage report
Current view: top level - utilities/include/mcrl2/utilities - file_utility.h (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 30 34 88.2 %
Date: 2024-04-26 03:18:02 Functions: 9 11 81.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Author(s): Sjoerd Cranen
       2             : // Copyright: see the accompanying file COPYING or copy at
       3             : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING
       4             : //
       5             : // Distributed under the Boost Software License, Version 1.0.
       6             : // (See accompanying file LICENSE_1_0.txt or copy at
       7             : // http://www.boost.org/LICENSE_1_0.txt)
       8             : //
       9             : /// \file mcrl2/utilities/file_utility.h
      10             : /// \brief add your file description here.
      11             : 
      12             : #ifndef MCRL2_UTILITIES_FILE_UTILITY_H
      13             : #define MCRL2_UTILITIES_FILE_UTILITY_H
      14             : 
      15             : #include "mcrl2/utilities/exception.h"
      16             : #include "mcrl2/utilities/logger.h"
      17             : #include <fstream>
      18             : 
      19             : namespace mcrl2
      20             : {
      21             : 
      22             : namespace utilities
      23             : {
      24             : 
      25             : /// \brief Returns true if the given filename has the extension ext. The extension does not start with a dot. 
      26             : inline
      27          30 : bool has_extension(const std::string& filename, const std::string& extension)
      28             : {
      29          30 :   assert(extension.size()>=1 && extension[0]!='.');  // The extension should not start with a dot and consist out of at least one character.
      30          30 :   std::string dotted_extension="."+extension;
      31          30 :   if (filename.size()<dotted_extension.size())
      32             :   {
      33           1 :     return false;
      34             :   }
      35          29 :   const std::string filename_extension = filename.substr(filename.size()-dotted_extension.size());
      36          29 :   return filename_extension==dotted_extension;
      37          30 : } 
      38             : 
      39             : class file_format
      40             : {
      41             :   protected:
      42             :     std::string m_shortname;
      43             :     std::string m_description;
      44             :     bool m_text_format;
      45             :     std::vector<std::string> m_extensions;
      46             :   
      47             :   public:
      48          32 :     file_format()
      49          32 :      : file_format("unknown", "Unknown format", false)
      50             :     {
      51          32 :     }
      52             : 
      53          40 :     file_format(const std::string& shortname, const std::string& description, bool is_text_format)
      54          40 :       : m_shortname(shortname), m_description(description), m_text_format(is_text_format)
      55          40 :     { }
      56             :   
      57          10 :     void add_extension(const std::string& ext)
      58             :     {
      59          10 :       m_extensions.push_back(ext);
      60          10 :     }
      61             :   
      62           3 :     const std::string& shortname() const { return m_shortname; }
      63             :   
      64           0 :     const std::string& description() const { return m_description; }
      65             :    
      66          11 :     bool text_format() const { return m_text_format; }
      67             :   
      68          19 :     bool matches(const std::string& filename) const
      69             :     {
      70          39 :       for (const std::string& ext: m_extensions)
      71             :       {
      72          23 :         if (has_extension(filename,ext))
      73             :         {
      74           3 :           return true;
      75             :         }
      76             :       }
      77          16 :       return false;
      78             :     }
      79             :   
      80          36 :     bool operator==(const file_format& other) const
      81             :     {
      82          36 :       return m_shortname == other.m_shortname;
      83             :     }
      84             : 
      85           0 :     bool operator<(const file_format& other) const
      86             :     {
      87           0 :       return m_shortname < other.m_shortname;
      88             :     }
      89             : };
      90             : 
      91             : inline
      92             : std::ostream& operator<<(std::ostream& stream, const file_format& format)
      93             : {
      94             :   return stream << format.shortname();
      95             : }
      96             : 
      97             : inline
      98          20 : bool file_exists(const std::string& filename)
      99             : {
     100          20 :   if (FILE * file = fopen(filename.c_str(), "r"))
     101             :   {
     102          20 :     fclose(file);
     103          20 :     return true;
     104             :   }
     105           0 :   return false;
     106             : } 
     107             : 
     108             : } // namespace utilities
     109             : 
     110             : } // namespace mcrl2
     111             : 
     112             : #endif // MCRL2_UTILITIES_FILE_UTILITY_H

Generated by: LCOV version 1.14