File: file.h

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (129 lines) | stat: -rw-r--r-- 3,527 bytes parent folder | download | duplicates (2)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/***********************************************/
/**
* @file file.h
*
* read and write files.
*
* @author Torsten Mayer-Guerr
* @date 2017-12-06
*
*/
/***********************************************/

#ifndef __GROOPS_FILE__
#define __GROOPS_FILE__

#include "base/exception.h"
#include "inputOutput/fileName.h"

/** @addtogroup inputOutputGroup */
/// @{

/***** CLASS ***********************************/

// Internal class
class StreamBase : virtual public std::ios
{
protected:
  std::streambuf *buffer;
  FileName        fileName_;
  Bool            canSeek_;

public:
  StreamBase();
 ~StreamBase();

  StreamBase(const StreamBase &) = delete;
  StreamBase &operator=(const StreamBase &) = delete;

  void open(const FileName &fileName, std::ios::openmode openMode);
  void close();

  FileName fileName() const {return fileName_;}
  Bool     canSeek()  const {return canSeek_;}
};

/***** CLASS ***********************************/

class OutFile : public StreamBase, public std::ostream
{
public:
  OutFile() : std::ostream(nullptr) {}
  OutFile(const FileName &fileName, std::ios::openmode openMode=std::ios::out) : std::ostream(nullptr) {open(fileName, openMode);}
 ~OutFile() {}

  OutFile(const OutFile &) = delete;
  OutFile &operator=(const OutFile &) = delete;

  void open(const FileName &fileName, std::ios::openmode openMode=std::ios::out) {StreamBase::open(fileName, openMode);}
  void close() {StreamBase::close();}

  OutFile &operator<<(std::ostream  &(*pf)(std::ostream  &)) {pf(*this); return *this;}
  OutFile &operator<<(std::ios      &(*pf)(std::ios      &)) {pf(*this); return *this;}
  OutFile &operator<<(std::ios_base &(*pf)(std::ios_base &)) {pf(*this); return *this;}
  template<typename T> inline OutFile &operator<<(const T &x);
};

/***** CLASS ***********************************/

class InFile : public StreamBase, public std::istream
{
public:
  InFile() : std::istream(nullptr) {}
  InFile(const FileName &fileName, std::ios::openmode openMode=std::ios::in) : std::istream(nullptr) {open(fileName, openMode);}
 ~InFile() {}

  InFile(const InFile &) = delete;
  InFile &operator=(const InFile &) = delete;

  void open(const FileName &fileName, std::ios::openmode openMode=std::ios::in) {StreamBase::open(fileName, openMode);}
  void close() {StreamBase::close();}

  InFile &operator>>(std::istream  &(*pf)(std::istream  &)) {pf(*this); return *this;}
  InFile &operator>>(std::ios      &(*pf)(std::ios      &)) {pf(*this); return *this;}
  InFile &operator>>(std::ios_base &(*pf)(std::ios_base &)) {pf(*this); return *this;}
  template<typename T> inline InFile &operator>>(T &x);
};

/// @}

/***********************************************/
/***** INLINES *********************************/
/***********************************************/

template<typename T> inline OutFile &OutFile::operator<<(const T &x)
{
  try
  {
    if(fileName_.empty())
      throw(Exception("no file open"));
    static_cast<std::ostream&>(*this)<<x;
    return *this;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW_EXTRA("filename=<"+fileName_.str()+">", e)
  }
}

/***********************************************/

template<typename T>
inline InFile &InFile::operator>>(T &x)
{
  try
  {
    if(fileName_.empty())
      throw(Exception("no file open"));
    static_cast<std::istream&>(*this)>>x;
    return *this;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW_EXTRA("filename=<"+fileName_.str()+">", e)
  }
}

/***********************************************/

#endif /* __GROOPS__ */