TF++, Miscellaneous classes and modules in C++:
bytesex.h
Go to the documentation of this file.
1 
41 // include guard
42 #ifndef TF_BYTESEX_H_VERSION
43 
44 #define TF_BYTESEX_H_VERSION \
45  "TF_BYTESEX_H V1.0"
46 
47 // we include fstream, because all function are closely related to file I/O
48 // and file_magic definitely requires fstream
49 #include<fstream>
50 
51 namespace tfxx {
52 
66 namespace ioswap {
67 
88 //----------------------------------------------------------------------
89 //
90 // we start with a set of useful types and enums
91 
97 template<typename T>
98 union IOUnion {
99  T value;
100  char bytes[sizeof(T)];
101 };
102 
104 enum Ecpu_type {
111 };
112 
121 };
122 
127 //----------------------------------------------------------------------
128 //
129 // some function templates
130 
137 template<class T>
138 T swap(const T& value)
139 {
140  IOUnion<T> in, out;
141  in.value=value;
142  out=swap(in);
143  return(out.value);
144 }
145 
152 template<class T>
154 {
155  IOUnion<T> result;
156  for (unsigned int i=0; i<sizeof(T); i++)
157  { result.bytes[i]=value.bytes[sizeof(T)-i-1]; }
158  return(result);
159 }
160 
161 //----------------------------------------------------------------------
162 //
163 // some binary function
164 
184 int magic(const char* const cmagic);
185 
190 Ecpu_type cpu();
191 
210 Emagic_type file_magic_test(std::istream& is, const char* const cmagic,
211  const bool& fortranmode=false);
212 
222 void file_magic_write(std::ostream& os, const char* const cmagic,
223  const bool& fortranmode=false);
224 
225 } // namespace ioswap
226 
227 } // namespace tfxx
228 
229 #endif // TF_BYTESEX_H_VERSION (includeguard)
230 
231 /* ----- END OF bytesex.h ----- */
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.