TF++, Miscellaneous classes and modules in C++:
fortraniotest.cc
Go to the documentation of this file.
1 
42 #define FORTRANIOTEST_VERSION \
43  "FORTRANIOTEST V1.2 C++ part of the Fortran I/O test routines"
44 
45 #include <iostream>
46 #include <cassert>
47 #include <tfxx/commandline.h>
48 #include <tfxx/fortranio.h>
49 #include <tfxx/complexio.h>
50 
51 using std::cout;
52 using std::cerr;
53 using std::endl;
54 
59 /*----------------------------------------------------------------------*/
70 template<class T>
71 void test_swap()
72 {
74  T v1,v2;
75  const int nbytes=sizeof(T);
76  char charpool[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
77  char charresult[26];
78  assert(nbytes<26);
79  for (int i=0; i<nbytes; i++)
80  {
81  u1.bytes[i]=charpool[i];
82  charresult[i]=charpool[i];
83  }
84  charresult[nbytes]='\0';
85  cout << "in: ";
86  cout.width(10);
87  cout << charresult;
88  // take the long way (using these types explicitely)
89  v1=u1.value;
90  v2=tfxx::ioswap::swap(v1);
91  u2.value=v2;
92  for (int i=0; i<nbytes; i++)
93  {
94  charresult[i]=u2.bytes[i];
95  }
96  charresult[nbytes]='\0';
97  cout << " out: ";
98  cout.width(12);
99  cout << charresult << endl;
100 }
101 
107 #define SWAPIT( T ) \
108  cout.width(40); \
109  cout << " swap test with \'" #T "\': "; \
110  test_swap<T>();
111 
115 void cpu_type()
116 {
117  switch (tfxx::ioswap::cpu()) {
119  cout << "We are running on an Intel type CPU" << endl;
120  break;
122  cout << "We are running on a Motorola type CPU" << endl;
123  break;
125  cout << "The type of this CPU is unknown" << endl;
126  break;
127  default:
128  cerr << "ERROR: illegal return value from " <<
129  "tfxx::ioswap::cpu()" << endl;
130  abort();
131  }
132 }
133 
134 const char mymagic[]="ABCD";
135 
139 void write_data(const std::string& name, const bool& verbose=false)
140 {
141  if (verbose) cout << "write data to \'" << name << "\'" << endl;
142  std::ofstream os(name.c_str());
144 
146  const int nval=10;
147  fo << nval;
148  cout << "nval: " << nval << endl;
149  for (int i=0; i<nval; i++)
150  {
151  int val1=(i+1)*4;
152  int val2=100*val1;
153  fo << val1 << val2;
154  cout << " val1/2: " << val1 << ", " << val2 << endl;
155  }
156  fo.end_block();
157  for (int i=0; i<nval; i++)
158  {
159  double val=(i+1)*15.;
160  cout << " val: " << val << endl;
161  fo << val;
162  }
163  fo.end_block();
164  long long int llint=1551;
165  long int lint=2662;
166  std::complex<double> dcplx(4.5,5.4);
167  std::complex<float> scplx(14.5,15.4);
168  fo << llint << lint << dcplx << scplx;
169  cout << "extra: " << llint << ", " << lint << ", "
170  << dcplx << ", " << scplx << endl;;
171 }
172 
176 void read_data(const std::string& name, const bool& verbose=false)
177 {
178  if (verbose) cout << "read data from \'" << name << "\'" << endl;
179  std::ifstream is(name.c_str());
181  match=tfxx::ioswap::file_magic_test(is, mymagic, true);
182  if (verbose) {
183  switch(match) {
185  cout << "Bytesex matches" << endl;
186  break;
188  cout << "Magic number found, byte data has to be swapped" << endl;
189  break;
191  cout << "Magic number not found" << endl;
192  break;
193  default:
194  cerr << "ERROR: illegal return value from " <<
195  "tfxx::ioswap::file_magic_test()" << endl;
196  break;
197  }
198  }
199  if ((match==tfxx::ioswap::magic_match)
200  || (match==tfxx::ioswap::magic_swap))
201  {
203  int nval;
204  fi >> nval;
205  cout << "nval: " << nval << endl;
206  for (int i=0; i<nval; i++)
207  {
208  int val1,val2;
209  fi >> val1 >> val2;
210  cout << " val1/2: " << val1 << ", " << val2 << endl;
211  }
212  for (int i=0; i<nval; i++)
213  {
214  double val;
215  fi >> val;
216  cout << " val: " << val << endl;
217  }
218  long long int llint;
219  long int lint;
220  std::complex<double> dcplx;
221  std::complex<float> scplx;
222  fi >> llint >> lint >> dcplx >> scplx;
223  cout << "extra: " << llint << ", " << lint << ", "
224  << dcplx << ", " << scplx << endl;;
225  }
226 }
227 
228 /*----------------------------------------------------------------------*/
237 int main(int iargc, char* argv[])
238 {
239 
240  // define usage information
241  char usage_text[]=
242  {
244  "usage: fortraniotest [-v] [-c] [-s] [-C]" "\n"
245  " [-w [file]] [-r [file]]" "\n"
246  " or: fortraniotest --help|-h" "\n"
247  };
248 
249  // define full help text
250  char help_text[]=
251  {
252  " " "\n"
253  "-v be verbose" "\n"
254  "-c check byte size of data types" "\n"
255  "-s test byte swapping" "\n"
256  "-C tell us about the CPU model" "\n"
257  "-w [file] write to file" "\n"
258  "-r [file] read from file" "\n"
259  FORTRANIOTEST_CVSID
260  };
261 
262  // define commandline options
263  using namespace tfxx::cmdline;
264  static Declare options[]=
265  {
266  // 0: print help
267  {"help",arg_no,"-"},
268  // 1: verbose mode
269  {"v",arg_no,"-"},
270  // 2: check byte size
271  {"c",arg_no,"-"},
272  // 3: test byte swapping
273  {"s",arg_no,"-"},
274  // 4: tell about CPU
275  {"C",arg_no,"-"},
276  // 5: write to file
277  {"w",arg_opt,"junk"},
278  // 6: write to file
279  {"r",arg_opt,"junk"},
280  {NULL}
281  };
282 
283  // no arguments? print usage...
284  if (iargc<2)
285  {
286  cerr << usage_text << endl;
287  exit(0);
288  }
289 
290  // collect options from commandline
291  Commandline cmdline(iargc, argv, options);
292 
293  // help requested? print full help text...
294  if (cmdline.optset(0))
295  {
296  cerr << usage_text << endl;
297  cerr << help_text << endl;
298  exit(0);
299  }
300 
301  bool verbose=cmdline.optset(1);
302  bool check_size=cmdline.optset(2);
303  bool byte_swapping=cmdline.optset(3);
304  bool tell_CPU=cmdline.optset(4);
305  bool write_file=cmdline.optset(5);
306  std::string outfile=cmdline.string_arg(5);
307  bool read_file=cmdline.optset(6);
308  std::string infile=cmdline.string_arg(6);
309 
310  if (verbose) { cout << FORTRANIOTEST_VERSION << endl << endl; }
311  if (check_size)
312  {
313  cout << "NOTICE: check_assumed_size() no longer exists!"
314  << endl
315  << " and the even better news: there is no need for it"
316  << endl;
317  }
318  if (byte_swapping)
319  {
320  SWAPIT(char)
321  SWAPIT(int)
322  SWAPIT(long int)
323  SWAPIT(long long int)
324  SWAPIT(float)
325  SWAPIT(double)
326  }
327  if (tell_CPU) cpu_type();
328  if (write_file) write_data(outfile, verbose);
329  if (read_file) read_data(infile, verbose);
330 }
331 
335 /* ----- END OF fortraniotest.cc ----- */
write FORTRAN binary data ,This class writes FORTRAN binary data. FORTRAN binary data is always writt...
Definition: fortranio.h:200
void write_data(const std::string &name, const bool &verbose=false)
write test data
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 end_block()
finish data block
Definition: fortranio.cc:155
Motorola CPU.
Definition: bytesex.h:108
bool optset(const int &iopt) const
true if option # iopt was set on commandline
Definition: commandline.h:213
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 matches this machine.
Definition: bytesex.h:116
The bytesex of the file must be swapped to match this machine.
Definition: bytesex.h:118
Ecpu_type cpu()
Check CPU model. ,.
Definition: ioswap.cc:80
int main(int iargc, char *argv[])
Main program.
Emagic_type
Define bytesex indicator for magic number test.
Definition: bytesex.h:114
#define FORTRANIOTEST_VERSION
#define SWAPIT(T)
Macro function to preform easy to use swap check.
void test_swap()
Function template to test dry byte swapping.
Namespace containing all components of module commandline. ,.
Definition: commandline.cc:41
read FORTRAN binary data ,This class reads FORTRAN binary data. FORTRAN binary data is always written...
Definition: fortranio.h:137
Evaluates commandline by calling long_getopt. ,You may instantiate a Commandline object by passing th...
Definition: commandline.h:201
option may have an optional argument
Definition: commandline.h:104
The magic number does match the file.
Definition: bytesex.h:120
const char mymagic[]
struct to define options ,This struct is used to define a list of options. An example is: ...
Definition: commandline.h:136
void file_magic_write(std::ostream &os, const char *const cmagic, const bool &fortranmode=false)
Write magic number to file. ,.
Definition: ioswap.cc:135
option has no argument
Definition: commandline.h:100
std::string string_arg(const int &iopt) const
return argument of option # iopt as string value
Definition: commandline.h:216
void cpu_type()
Ask for CPU type and print result.
void read_data(const std::string &name, const bool &verbose=false)
read test data