TF++, Miscellaneous classes and modules in C++:
ioswap.cc
Go to the documentation of this file.
1 
43 #define TF_IOSWAP_CC_VERSION \
44  "TF_IOSWAP_CC V1.3"
45 
46 #include <tfxx/bytesex.h>
47 #include <tfxx/error.h>
48 #include <iostream>
49 
50 using std::cout;
51 using std::endl;
52 
53 namespace tfxx {
54 
55 namespace ioswap {
56 
57 //----------------------------------------------------------------------
59 int magic(const char* const cmagic)
60 {
61  union {
62  unsigned int uival;
63  int ival;
64  } out;
65  union {
66  char chr[sizeof(int)];
67  unsigned char uchr[sizeof(int)];
68  } in;
69  const int& intsize=sizeof(int);
70  for (int i=0; i<intsize; i++)
71  { in.chr[i]=cmagic[i]; }
72  out.uival=0;
73  for (int i=0; i<intsize; i++)
74  { out.uival=out.uival*256+in.uchr[i]; }
75  return(out.ival);
76 } // magic()
77 
78 /*----------------------------------------------------------------------*/
81 {
82  Ecpu_type result=cpu_unknown;
83  IOUnion<int> u1,u2;
84  TFXX_assert((sizeof(int) == 4),
85  "The integer memory size on this CPU differs from the"
86  "required value of 4");
87  const int& intsize=sizeof(int);
88  char test_seq[]="ABCD";
89  // prepare sequence and reverse sequence
90  for (int i=0; i<intsize; i++)
91  {
92  u1.bytes[i]=test_seq[i];
93  u2.bytes[i]=test_seq[intsize-i-1];
94  }
95  // request magic number for sequence
96  int magnum=magic(u1.bytes);
97  // test against byte representation of sequence and reverse sequence
98  if (magnum == u1.value)
99  {
100  result=cpu_Motorola;
101  }
102  else if (magnum == u2.value)
103  {
104  result=cpu_Intel;
105  }
106  return(result);
107 } // cpu()
108 
109 /*----------------------------------------------------------------------*/
110 
112 Emagic_type file_magic_test(std::istream& is, const char* const cmagic,
113  const bool& fortranmode)
114 {
115  Emagic_type result=magic_nomatch;
116  IOUnion<int> req_magic, in_magic;
117  // create requested magic number
118  req_magic.value=magic(cmagic);
119  // skip Fortran block size
120  if (fortranmode) is.read(in_magic.bytes, sizeof(int));
121  // read magic number and compare
122  is.read(in_magic.bytes, sizeof(int));
123  if (in_magic.value == req_magic.value)
124  { result=magic_match; }
125  else if (in_magic.value == swap(req_magic.value))
126  { result=magic_swap; }
127  // skip Fortran block size
128  if (fortranmode) is.read(in_magic.bytes, sizeof(int));
129  return(result);
130 } // file_magic_test()
131 
132 /*----------------------------------------------------------------------*/
133 
135 void file_magic_write(std::ostream& os, const char* const cmagic,
136  const bool& fortranmode)
137 {
138  IOUnion<int> ifour, imagic;
139  ifour.value=sizeof(int);
140  imagic.value=magic(cmagic);
141  if (fortranmode) os.write(ifour.bytes, sizeof(int));
142  os.write(imagic.bytes, sizeof(int));
143  if (fortranmode) os.write(ifour.bytes, sizeof(int));
144 } // file_magic_write()
145 
146 } // namespace ioswap
147 
148 } // namespace tfxx
149 
150 /* ----- END OF ioswap.cc ----- */
#define TFXX_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:175
Ecpu_type
Define different CPU type that are recognized.
Definition: bytesex.h:104
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
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
Motorola CPU.
Definition: bytesex.h:108
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
Emagic_type
Define bytesex indicator for magic number test.
Definition: bytesex.h:114
The magic number does match the file.
Definition: bytesex.h:120
void file_magic_write(std::ostream &os, const char *const cmagic, const bool &fortranmode=false)
Write magic number to file. ,.
Definition: ioswap.cc:135
Namespace containing all code of library libtfxx.