TF++, Miscellaneous classes and modules in C++:
fortranio.h
Go to the documentation of this file.
1 
45 // include guard
46 #ifndef TF_FORTRANIO_H_VERSION
47 
48 #define TF_FORTRANIO_H_VERSION \
49  "TF_ V1.5 "
50 
51 #include<tfxx/bytesex.h>
52 #include<fstream>
53 #include<sstream>
54 
55 namespace tfxx {
56 
112 namespace fortranio {
113 
138  public:
140  FortranBinInput(std::istream& is, const bool& swap=false):
141  Mistream(is), Mswap(swap), Mnbytes(0), Mnremain(0) { }
145  bool more() const
146  { return((Mnbytes>0) || Mistream.good()); }
154  template<typename T> void get(T& value);
156  bool match_magic(const char* cmagic);
157 
158  private:
160  char extract_next_char();
162  void extract_chars(char* buf, const int& n);
164  void read_block_size();
166  void finish_block();
167 
169  std::istream& Mistream;
171  bool Mswap;
173  long int Mnbytes;
175  long int Mnremain;
176 };
177 
201  public:
203  FortranBinOutput(std::ostream& os):
204  Mostream(os), Mbuffer(std::string()) { }
208  template<typename T>
209  void put(const T& value);
211  void end_block();
213  void write_magic(const char* cmagic);
214 
215  private:
217  void put_char(const char& c);
219  void put_chars(const char* buf, const int& n);
220 
222  std::ostream& Mostream;
224  std::ostringstream Mbuffer;
225 };
226 
250 /*----------------------------------------------------------------------*/
251 // definition of member function templates
252 template<typename T>
253  void FortranBinOutput::put(const T& value)
254 {
256  buf.value=value;
257  put_chars(buf.bytes, sizeof(T));
258 }
259 
260 template<typename T>
261  void FortranBinInput::get(T& value)
262 {
264  extract_chars(buf.bytes, sizeof(T));
265  value=buf.value;
266  if (Mswap) { value=tfxx::ioswap::swap(buf.value); }
267 }
268 
269 /*----------------------------------------------------------------------*/
270 
282 template<typename T>
285 {
286  fo.put(value);
287  return(fo);
288 }
289 
301 template<typename T>
304 {
305  fi.get(value);
306  return(fi);
307 }
308 
309 } // namespace fortranio
310 
311 } // namespace tfxx
312 
313 #endif // TF_FORTRANIO_H_VERSION (includeguard)
314 
315 /* ----- END OF fortranio.h ----- */
write FORTRAN binary data ,This class writes FORTRAN binary data. FORTRAN binary data is always writt...
Definition: fortranio.h:200
char extract_next_char()
return next element from input stream
Definition: fortranio.cc:114
std::istream & Mistream
The input stream to read from.
Definition: fortranio.h:169
std::ostream & Mostream
The output stream to write to.
Definition: fortranio.h:222
void put_chars(const char *buf, const int &n)
put some chars to the output
Definition: fortranio.cc:172
long int Mnremain
Number of bytes still to read from current block.
Definition: fortranio.h:175
void end_block()
finish data block
Definition: fortranio.cc:155
char bytes[sizeof(T)]
Definition: bytesex.h:100
STL namespace.
T swap(const T &value)
How to swap any generic type.
Definition: bytesex.h:138
bool more() const
are there more data expected?
Definition: fortranio.h:145
tfxx::fortranio::FortranBinOutput & operator<<(tfxx::fortranio::FortranBinOutput &fo, const blitz::Array< T, N > &a)
Output operator template for class FortranBinOutput and blitz.
tfxx::fortranio::FortranBinInput & operator>>(tfxx::fortranio::FortranBinInput &fi, blitz::Array< T, N > &a)
Input operator template for class FortranBinInput and blitz.
FortranBinOutput(std::ostream &os)
start writing to output stream os
Definition: fortranio.h:203
FortranBinInput(std::istream &is, const bool &swap=false)
start reading from input stream is
Definition: fortranio.h:140
~FortranBinInput()
needs a cleaning destructor
Definition: fortranio.h:143
void extract_chars(char *buf, const int &n)
extract next set of characters
Definition: fortranio.cc:128
void put_char(const char &c)
put the next char to the output
Definition: fortranio.cc:178
read FORTRAN binary data ,This class reads FORTRAN binary data. FORTRAN binary data is always written...
Definition: fortranio.h:137
void read_block_size()
read next block size
Definition: fortranio.cc:66
std::ostringstream Mbuffer
The buffering is done by a stringstream.
Definition: fortranio.h:224
void finish_block()
finish open block
Definition: fortranio.cc:93
void write_magic(const char *cmagic)
write magic number (to separate block)
Definition: fortranio.cc:184
bool match_magic(const char *cmagic)
expect magic number and adjust swapping flag
Definition: fortranio.cc:137
bool Mswap
Has byte swapping to be performed.
Definition: fortranio.h:171
long int Mnbytes
Number of bytes expected in current block.
Definition: fortranio.h:173
void put(const T &value)
put a value to the output
Definition: fortranio.h:253
~FortranBinOutput()
we need a flushing destructor
Definition: fortranio.h:206
Namespace containing all code of library libtfxx.