TF++, Miscellaneous classes and modules in C++:

◆ file_magic_test()

Emagic_type tfxx::ioswap::file_magic_test ( std::istream &  is,
const char *const  cmagic,
const bool &  fortranmode = false 
)

Check for magic number in file.

check magic number in file

,

Parameters
isinput stream to read from
cmagic4-byte character sequence representing magic number (most restrictive: pass a const pointer to a const char) is pointer to character array of size sizeof(int)
fortranmodeuse Fortran binary I/O if true
Returns
The return value tells whether the file matches

The function may have the following return values

  • magic_match The file has the requested magic number and the bytesex of the data matches that of the system
  • magic_swap The file has the requested magic number but byte swapping is required for input data
  • magic_nomatch The requested magic number was not found in the file
Examples:
tests/fortraniotest.cc.

Definition at line 112 of file ioswap.cc.

References tfxx::ioswap::IOUnion< T >::bytes, magic(), magic_match, magic_nomatch, magic_swap, swap(), and tfxx::ioswap::IOUnion< T >::value.

Referenced by tfxx::fortranio::FortranBinInput::match_magic(), and read_data().

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()
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
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
Emagic_type
Define bytesex indicator for magic number test.
Definition: bytesex.h:114
The magic number does match the file.
Definition: bytesex.h:120
Here is the call graph for this function:
Here is the caller graph for this function: