dune-fem  2.8-git
iolock.hh
Go to the documentation of this file.
1 // (c) Robert Kloefkorn
2 #ifndef DUNE_FEM_INPUTOUPUTLOCK_HH
3 #define DUNE_FEM_INPUTOUPUTLOCK_HH
4 
5 //- system includes
6 #include <cstdio>
7 #include <cstdlib>
8 
9 #include <iostream>
10 #include <fstream>
11 #include <string>
12 
13 namespace Dune
14 {
15 
16  namespace Fem
17  {
18 
20  class FileIOLock
21  {
22  std::string filename_;
23  FileIOLock ( const FileIOLock & );
24  public :
26  FileIOLock(const std::string& fn);
28  ~FileIOLock() ;
29 
31  static const char * suffix() { return "lock"; }
32  };
33 
36  {
38  public :
40  FileIOCheckError(const std::string& fn)
41  {
42  std::string lockfile(fn);
43  lockfile += ".";
44  lockfile += FileIOLock::suffix();
45 
46  std::ifstream file ( lockfile.c_str () );
47  if( file.is_open() )
48  {
49  std::cerr << "ERROR: data set `"<<fn<<"' not complete, lock file exists! " << std::endl;
50  abort();
51  }
52  }
53  };
54 
56  //
57  // INLINE
58  //
60 
61  // create lock file
62  inline FileIOLock :: FileIOLock (const std::string& fn)
63  : filename_(fn)
64  {
65  if( filename_ == "" )
66  {
67  filename_ = suffix();
68  }
69  else {
70  filename_ += ".";
71  filename_ += suffix();
72  }
73 
74  std::ofstream file ( filename_.c_str() );
75  if( !file )
76  {
77  std::cerr << "WARNING: Couldn't open lock file `"<<filename_<<"' in: ";
78  std::cerr << __FILE__<< " line: "<< __LINE__ << std::endl;
79  }
80  else
81  {
82  file.close();
83  }
84  return ;
85  }
86 
87  // remove lock file
89  {
90  if (filename_ != "")
91  {
92  int test = remove (filename_.c_str()) ;
93  if (test != 0)
94  {
95  std::cerr << "WARNING: Couldn't remove lock file `"<<filename_<<"' in: ";
96  std::cerr <<__FILE__<<" line: " <<__LINE__ << std::endl ;
97  }
98  }
99  return ;
100  }
101 
102  } // namespace Fem
103 
104 } // namespace Dune
105 #endif // #ifndef DUNE_FEM_INPUTOUPUTLOCK_HH
Definition: bindguard.hh:11
creates and removes lock file during backup process
Definition: iolock.hh:21
~FileIOLock()
removes lock file
Definition: iolock.hh:88
static const char * suffix()
suffix that is appended to lock files
Definition: iolock.hh:31
check if lock file exists and aborts if true
Definition: iolock.hh:36
FileIOCheckError(const std::string &fn)
creates lock file
Definition: iolock.hh:40