TF++, Miscellaneous classes and modules in C++:
fortranio.cc
Go to the documentation of this file.
1 
46 #define TF_FORTRANIO_CC_VERSION \
47  "TF_FORTRANIO_CC V1.7 "
48 
49 #include <iostream>
50 #include <string>
51 #include <tfxx/fortranio.h>
52 #include <tfxx/error.h>
53 
54 using std::cerr;
55 using std::endl;
56 
57 namespace tfxx {
58 namespace fortranio {
59 
60 /*======================================================================*/
61 //
62 // FortranBinInput
63 // ===============
64 
67  {
69  // in case we are already in the file, we have to read and check the
70  // byte-count at the end of the block
71  if (Mnbytes != 0)
72  {
73  TFXX_assert((Mnremain==0),
74  "FotranBinInput: internal error - remain count is not zero!");
75  Mistream.read(buf.bytes, sizeof(int));
76  int endcount=buf.value;
77  if (Mswap) { endcount=tfxx::ioswap::swap(buf.value); }
78  TFXX_assert((Mnbytes == endcount),
79  "FortranBinInput: bount count at end of block does not match!");
80  }
81  // read byte-count at beginning of next block
82  Mistream.read(buf.bytes, sizeof(int));
83  Mnremain=buf.value;
84  if (Mswap) { Mnremain=tfxx::ioswap::swap(buf.value); }
86  TFXX_assert((Mistream.good()),
87  "FortranBinInput: stream is not good after reading block size");
88  }
89 
90 /*----------------------------------------------------------------------*/
91 
94  {
95  // do we have to read any more from current block?
96  while (Mnremain>0) { this->extract_next_char(); }
97  // had this block finite size? Then read end count and compare
98  if (Mnbytes>0) {
100  Mistream.read(buf.bytes, sizeof(int));
101  int endcount=buf.value;
102  if (Mswap) { endcount=tfxx::ioswap::swap(buf.value); }
103  TFXX_assert((Mnbytes == endcount),
104  "FortranBinInput (finish_block): "
105  "bount count at end of block does not match!");
106  }
107  // we do not open a new block - indicate by zero blocksize
108  Mnbytes=0;
109  }
110 
111 /*----------------------------------------------------------------------*/
112 
115  {
116  char result;
117  TFXX_assert((Mistream.good()),
118  "FortranBinInput: stream is not good upon reading next char");
119  if (Mnremain < 1) { read_block_size(); }
120  Mistream.read(&result, 1);
121  Mnremain--;
122  return(result);
123  }
124 
125 /*----------------------------------------------------------------------*/
126 
128  void FortranBinInput::extract_chars(char* buf, const int& n)
129  {
130  for (int i=0; ((i<n) && more()); i++)
131  { buf[i]=extract_next_char(); }
132  }
133 
134 /*----------------------------------------------------------------------*/
135 
137  bool FortranBinInput::match_magic(const char* cmagic)
138  {
139  // skip rest of previous block
140  this->finish_block();
142  =tfxx::ioswap::file_magic_test(Mistream, cmagic, true);
143  if (result==tfxx::ioswap::magic_nomatch) return(false);
144  if (result==tfxx::ioswap::magic_swap) { Mswap=true; }
145  else { Mswap=false; }
146  return(true);
147  }
148 
149 /*======================================================================*/
150 //
151 // FortranBinOutput
152 // ================
153 
156  {
157  std::string buffer=Mbuffer.str();
159  count.value=buffer.size();
160  if (count.value>0)
161  {
162  Mostream.write(count.bytes, sizeof(int));
163  Mostream.write(buffer.c_str(), count.value);
164  Mostream.write(count.bytes, sizeof(int));
165  Mbuffer.str(std::string());
166  }
167  }
168 
169 /*----------------------------------------------------------------------*/
170 
172  void FortranBinOutput::put_chars(const char* buf, const int& n)
173  { Mbuffer.write(buf, n); }
174 
175 /*----------------------------------------------------------------------*/
176 
178  void FortranBinOutput::put_char(const char& c)
179  { Mbuffer.write(&c, 1); }
180 
181 /*----------------------------------------------------------------------*/
182 
184  void FortranBinOutput::write_magic(const char* cmagic)
185  {
186  this->end_block();
187  this->put(tfxx::ioswap::magic(cmagic));
188  this->end_block();
189  }
190 
191 } // namespace fortranio
192 } // namespace tfxx
193 
194 /* ----- END OF fortranio.cc ----- */
#define TFXX_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:175
int magic(const char *const cmagic)
Create a magic number from a character string. ,If represents the input character sequence cmagic an...
Definition: ioswap.cc:59
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
Emagic_type file_magic_test(std::istream &is, const char *const cmagic, const bool &fortranmode=false)
Check for magic number in file.
Definition: ioswap.cc:112
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
T swap(const T &value)
How to swap any generic type.
Definition: bytesex.h:138
The bytesex of the file must be swapped to match this machine.
Definition: bytesex.h:118
bool more() const
are there more data expected?
Definition: fortranio.h:145
Emagic_type
Define bytesex indicator for magic number test.
Definition: bytesex.h:114
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
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
The magic number does match the file.
Definition: bytesex.h:120
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
Namespace containing all code of library libtfxx.