Fortran SFF API to data I/O streams in C++
fileunit.cc
Go to the documentation of this file.
1 
37 #define TF_FILEUNIT_CC_VERSION \
38  "TF_FILEUNIT_CC V1.2"
39 
40 #include<fstream>
41 #include <fapidxx/fileunit.h>
42 #include <fapidxx/error.h>
43 #include <fapidxx/helper.h>
44 
45 namespace fapidxx {
46 
49 
52 
54  datrw::Eformat selectedformat=datrw::Fsff;
55 
56  /*======================================================================*/
57 
59  : Mformat(datrw::anyID(datrw::Fsff))
60  {
61  Mstreammap.clear();
62  } // IFileUnits::IFileUnits()
63 
64  /*----------------------------------------------------------------------*/
65 
67  {
68  while (Mstreammap.size()>0)
69  {
70  Tstreammap::iterator first=Mstreammap.begin();
71  this->close(first->first);
72  }
73  Mstreammap.clear();
74  } // IFileUnits::~IFileUnits()
75 
76  /*----------------------------------------------------------------------*/
77  datrw::ianystream& IFileUnits::open(const int& unit,
78  const std::string& filename)
79  {
80  FAPIDXX_fuassert((!this->isopen(unit)), unit,
81  "IFileUnits::open: file is already open");
82  IstreamCompound &compound=Mstreammap[unit];
83  compound.Mistream=new std::ifstream(filename.c_str(),
84  datrw::ianystream::openmode(Mformat));
85  compound.Mianystream=new datrw::ianystream(*compound.Mistream, Mformat);
86  return (this->getstream(unit));
87  } // datrw::ianystream& IFileUnits::open
88 
89  /*----------------------------------------------------------------------*/
90 
91  void IFileUnits::close(const int& unit)
92  {
93  FAPIDXX_fuassert((this->isopen(unit)), unit,
94  "IFileUnits::close: file is not open");
95  IstreamCompound &compound=Mstreammap[unit];
96  delete compound.Mianystream;
97  delete compound.Mistream;
98  Mstreammap.erase(unit);
99  } // void IFileUnits::close(const int& unit)
100 
101  /*----------------------------------------------------------------------*/
102 
103  bool IFileUnits::isopen(const int& unit) const
104  {
105  bool retval=false;
106  retval = (Mstreammap.count(unit)>0);
107  return(retval);
108  } // bool IFileUnits::isopen(const int& unit) const
109 
110  /*----------------------------------------------------------------------*/
111 
112  datrw::ianystream& IFileUnits::getstream(const int& unit)
113  {
114  FAPIDXX_fuassert((this->isopen(unit)), unit,
115  "IFileUnits::getstream: file is not open");
116  return (*Mstreammap[unit].Mianystream);
117  } // datrw::ianystream& IFileUnits::getstream(const int& unit)
118 
119  /*----------------------------------------------------------------------*/
120 
121  void IFileUnits::setformat(const std::string& format)
122  {
123  Mformat=format;
124  } // void IFileUnits::setformat(const std::string& format)
125 
126  /*======================================================================*/
127 
129  : Mformat(datrw::anyID(datrw::Fsff))
130  {
131  Mstreammap.clear();
132  } // OFileUnits::OFileUnits()
133 
134  /*----------------------------------------------------------------------*/
135 
137  {
138  while (Mstreammap.size()>0)
139  {
140  Tstreammap::iterator first=Mstreammap.begin();
141  this->close(first->first);
142  }
143  Mstreammap.clear();
144  } // OFileUnits::~OFileUnits()
145 
146  /*----------------------------------------------------------------------*/
147  datrw::oanystream& OFileUnits::open(const int& unit,
148  const std::string& filename)
149  {
150  FAPIDXX_fuassert((!this->isopen(unit)), unit,
151  "OFileUnits::open: file is already open");
152  OstreamCompound &compound=Mstreammap[unit];
153  datrw::abort_if_exists(filename);
154  compound.Mostream=new std::ofstream(filename.c_str(),
155  datrw::oanystream::openmode(Mformat));
156  compound.Moanystream=new datrw::oanystream(*compound.Mostream, Mformat);
157  return (this->getstream(unit));
158  } // datrw::oanystream& OFileUnits::open
159 
160  /*----------------------------------------------------------------------*/
161 
162  void OFileUnits::close(const int& unit)
163  {
164  FAPIDXX_fuassert((this->isopen(unit)), unit,
165  "OFileUnits::close: file is not open");
166  OstreamCompound &compound=Mstreammap[unit];
167  delete compound.Moanystream;
168  delete compound.Mostream;
169  Mstreammap.erase(unit);
170  } // void OFileUnits::close(const int& unit)
171 
172  /*----------------------------------------------------------------------*/
173 
174  bool OFileUnits::isopen(const int& unit) const
175  {
176  bool retval=false;
177  retval = (Mstreammap.count(unit)>0);
178  return(retval);
179  } // bool OFileUnits::isopen(const int& unit) const
180 
181  /*----------------------------------------------------------------------*/
182 
183  datrw::oanystream& OFileUnits::getstream(const int& unit)
184  {
185  FAPIDXX_fuassert((this->isopen(unit)), unit,
186  "OFileUnits::getstream: file is not open");
187  return (*Mstreammap[unit].Moanystream);
188  } // datrw::oanystream& OFileUnits::getstream(const int& unit)
189 
190  /*----------------------------------------------------------------------*/
191 
192  void OFileUnits::setformat(const std::string& format)
193  {
194  Mformat=format;
195  } // void OFileUnits::setformat(const std::string& format)
196 
197 } // namespace fapidxx
198 
210 /* ----- END OF fileunit.cc ----- */
Definition: error.cc:44
datrw::oanystream * Moanystream
Definition: fileunit.h:121
bool isopen(const int &unit) const
check whether a file is opened for this file unit
Definition: fileunit.cc:103
datrw::ianystream * Mianystream
Definition: fileunit.h:79
void setformat(const std::string &format)
set file format to be used upon file open
Definition: fileunit.cc:121
datrw::oanystream & open(const int &unit, const std::string &filename)
open a new file
Definition: fileunit.cc:147
exceptions and error handling macros (prototypes)
#define FAPIDXX_fuassert(C, U, M)
Check an assertion and report by throwing an exception.
Definition: error.h:205
Tstreammap Mstreammap
place to hold my oanystream objects
Definition: fileunit.h:132
~OFileUnits()
destructor has to close all open files
Definition: fileunit.cc:136
void setformat(const std::string &format)
set file format to be used upon file open
Definition: fileunit.cc:192
::fapidxx::OFileUnits ostreammanager
the global ostream manager
Definition: fileunit.cc:51
datrw::ianystream & open(const int &unit, const std::string &filename)
open a new file
Definition: fileunit.cc:77
Tstreammap Mstreammap
place to hold my ianystream objects
Definition: fileunit.h:90
OFileUnits()
default contructor to initialize member data
Definition: fileunit.cc:128
bool isopen(const int &unit) const
check whether a file is opened for this file unit
Definition: fileunit.cc:174
IFileUnits()
default contructor to initialize member data
Definition: fileunit.cc:58
std::string Mformat
file type to be used
Definition: fileunit.h:88
a file unit interface to libdatrwxx (prototypes)
A C++ stream has to be handled together with an ianystream.
Definition: fileunit.h:77
datrw::ianystream & getstream(const int &unit)
return a stream associated with a file unit
Definition: fileunit.cc:112
~IFileUnits()
destructor has to close all open files
Definition: fileunit.cc:66
datrw::Eformat selectedformat
file format to be used
Definition: fileunit.cc:54
datrw::oanystream & getstream(const int &unit)
return a stream associated with a file unit
Definition: fileunit.cc:183
void close(const int &unit)
close the file associated with this file unit
Definition: fileunit.cc:162
std::string Mformat
file type to be used
Definition: fileunit.h:130
::fapidxx::IFileUnits istreammanager
the global istream manager
Definition: fileunit.cc:48
A C++ stream has to be handled together with an oanystream.
Definition: fileunit.h:119
some helper functions (prototypes)
void close(const int &unit)
close the file associated with this file unit
Definition: fileunit.cc:91