root/include/io_dof.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. err_open_file
  2. err_file_to_small
  3. file_to_dof
  4. file_to_dof
  5. dof_to_file

   1 /*! \file io_dof.h
   2 
   3     \brief <Program Name>
   4 
   5      Revision history:
   6 
   7           Revised by Christian Power dd.mm.yyyy
   8           Originally written by Christian Power
   9                (power22c@gmail.com) 21. Februar 2016
  10 
  11      Routine to read and write dof of finite element functions to files.
  12 
  13      Created by Christian Power on 21.02.2016
  14      Copyright (c) 2016 Christian Power.  All rights reserved.
  15  */
  16 
  17 #ifndef IO_DOF_H
  18 #define IO_DOF_H 
  19 
  20 #include <fstream>
  21 #include "esfem_fwd.h"
  22 
  23 namespace Esfem{
  24   namespace Io{
  25     template<typename It>
  26     void file_to_dof(It first, It last,
  27                      const std::string& filename);
  28     // Bad style above.  Output algorithm only have on iterator not two.
  29     template<typename It>
  30     void file_to_dof(It first, const std::string& filename, std::size_t dof_no);
  31     template<typename It>
  32     void dof_to_file(It first, It last,
  33                      const std::string& filename);
  34 
  35     // ----------------------------------------------------------------------
  36     // Template implementation
  37 
  38     // ------------------------------------------------------------
  39     // Error helper functions
  40     
  41     inline void err_open_file(const std::string& fname){
  42       throw std::logic_error {"Could not open file " + fname};
  43     }
  44     inline void err_file_to_small(const std::string& fname){
  45       throw std::runtime_error {"File \"" + fname + "\" not big enough."};
  46     }
  47     
  48     template<typename It>
  49     void file_to_dof(It first, It last,
  50                      const std::string& filename) try{      
  51       std::ifstream ifs {filename};
  52       if(!ifs) err_open_file(filename);
  53       while(first != last){
  54         double value {0.};
  55         if(ifs >> value) *first = value;
  56         else err_file_to_small(filename);
  57         ++first;
  58       }
  59     }
  60     catch(const std::exception&){
  61       std::throw_with_nested(std::runtime_error
  62                              {"Error in file_to_dof()."});
  63     }
  64     template<typename It>
  65     void file_to_dof(It first, const std::string& filename,
  66                      std::size_t dof_no) try{      
  67       std::ifstream ifs {filename};
  68       if(!ifs) err_open_file(filename);
  69       for( ; dof_no > 0; --dof_no){
  70         double value {0.};
  71         if(ifs >> value) *first = value;
  72         else err_file_to_small(filename);
  73         ++first;
  74       }
  75       if(!(ifs >> std::ws).eof()) throw std::runtime_error
  76         {"Stuff left in file \"" + filename + "\"."};
  77     }
  78     catch(const std::exception&){
  79       std::throw_with_nested(std::runtime_error
  80                              {"Error in file_to_dof()."});
  81     }
  82     template<typename It>
  83     void dof_to_file(It first, It last,
  84                      const std::string& filename) try{
  85       std::ofstream ofs {filename};
  86       if(!ofs) err_open_file(filename);
  87       while(first != last){
  88         if( !(ofs << *first << std::endl) )
  89           err_file_to_small(filename);
  90         ++first;
  91       }
  92     }
  93     catch(const std::exception&){
  94       std::throw_with_nested(std::runtime_error
  95                              {"Error in dof_to_file()."});
  96     }
  97   }
  98 }
  99 
 100 
 101 #endif // IO_DOF_H
 102 
 103 /*! Log:
 104  */

/* [<][>][^][v][top][bottom][index][help] */