DATRW++ library: seismic data I/O with multiple formats
bytesex.cc
Go to the documentation of this file.
1 /*! \file bytesex.cc
2  * \brief a copy of libtfxx ioswap.cc (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 29/06/2007
8  *
9  * a copy of libtfxx ioswap.cc (implementation)
10  * just to make libdatrw more independent
11  *
12  * ----
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26  * ----
27  *
28  * Copyright (c) 2007 by Thomas Forbriger (BFO Schiltach)
29  *
30  * REVISIONS and CHANGES
31  * - 29/06/2007 V1.0 Thomas Forbriger
32  * - 30/04/2010 V1.1 added magic number writing and reading
33  *
34  * ============================================================================
35  */
36 #define DATRW_BYTESEX_CC_VERSION \
37  "DATRW_BYTESEX_CC V1.1"
38 
39 #include <datrwxx/bytesex.h>
40 #include <datrwxx/error.h>
41 #include <iostream>
42 
43 using std::cout;
44 using std::endl;
45 
46 namespace datrw {
47 
48  namespace util {
49 
50  //----------------------------------------------------------------------
51  //! function to create the magic number
52  int magic(const char* const cmagic)
53  {
54  union {
55  unsigned int uival;
56  int ival;
57  } out;
58  union {
59  char chr[sizeof(int)];
60  unsigned char uchr[sizeof(int)];
61  } in;
62  const int& intsize=sizeof(int);
63  for (int i=0; i<intsize; i++)
64  { in.chr[i]=cmagic[i]; }
65  out.uival=0;
66  for (int i=0; i<intsize; i++)
67  { out.uival=out.uival*256+in.uchr[i]; }
68  return(out.ival);
69  } // magic()
70 
71  /*----------------------------------------------------------------------*/
72  //! check for my CPU model
74  {
75  Ecpu_type result=cpu_unknown;
76  IOUnion<int> u1,u2;
77  DATRW_assert((sizeof(int) == 4),
78  "The integer memory size on this CPU differs from the"
79  "required value of 4");
80  const int& intsize=sizeof(int);
81  char test_seq[]="ABCD";
82  // prepare sequence and reverse sequence
83  for (int i=0; i<intsize; i++)
84  {
85  u1.bytes[i]=test_seq[i];
86  u2.bytes[i]=test_seq[intsize-i-1];
87  }
88  // request magic number for sequence
89  int magnum=magic(u1.bytes);
90  // test against byte representation of sequence and reverse sequence
91  if (magnum == u1.value)
92  {
93  result=cpu_Motorola;
94  }
95  else if (magnum == u2.value)
96  {
97  result=cpu_Intel;
98  }
99  return(result);
100  } // cpu()
101 
102  /*----------------------------------------------------------------------*/
103 
104  //! check magic number in file
105  Emagic_type file_magic_test(std::istream& is, const char* const cmagic,
106  const bool& fortranmode)
107  {
108  Emagic_type result=magic_nomatch;
109  IOUnion<int> req_magic, in_magic;
110  // create requested magic number
111  req_magic.value=magic(cmagic);
112  // skip Fortran block size
113  if (fortranmode) is.read(in_magic.bytes, sizeof(int));
114  // read magic number and compare
115  is.read(in_magic.bytes, sizeof(int));
116  if (in_magic.value == req_magic.value)
117  { result=magic_match; }
118  else if (in_magic.value == swap(req_magic.value))
119  { result=magic_swap; }
120  // skip Fortran block size
121  if (fortranmode) is.read(in_magic.bytes, sizeof(int));
122  return(result);
123  } // file_magic_test()
124 
125  /*----------------------------------------------------------------------*/
126 
127  //! write magic number to file
128  void file_magic_write(std::ostream& os, const char* const cmagic,
129  const bool& fortranmode)
130  {
131  IOUnion<int> ifour, imagic;
132  ifour.value=sizeof(int);
133  imagic.value=magic(cmagic);
134  if (fortranmode) os.write(ifour.bytes, sizeof(int));
135  os.write(imagic.bytes, sizeof(int));
136  if (fortranmode) os.write(ifour.bytes, sizeof(int));
137  } // file_magic_write()
138 
139  } // namespace util
140 
141 } // namespace datrw
142 
143 /* ----- END OF bytesex.cc ----- */
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
The bytesex of the file matches this machine.
Definition: bytesex.h:100
Ecpu_type cpu()
check for my CPU model
Definition: bytesex.cc:73
Emagic_type file_magic_test(std::istream &is, const char *const cmagic, const bool &fortranmode)
check magic number in file
Definition: bytesex.cc:105
Ecpu_type
Define different CPU type that are recognized.
Definition: bytesex.h:88
The magic number does match the file.
Definition: bytesex.h:104
exception class declaration for libdatrwxx (prototypes)
T swap(const T &value)
How to swap any generic type.
Definition: bytesex.h:118
A copy of bytesex.h from libtfxx (prototypes)
unknown CPU
Definition: bytesex.h:94
Root namespace of library.
Definition: aalibdatrwxx.cc:16
Motorola CPU.
Definition: bytesex.h:92
The bytesex of the file must be swapped to match this machine.
Definition: bytesex.h:102
char bytes[sizeof(T)]
Definition: bytesex.h:84
int magic(const char *const cmagic)
function to create the magic number
Definition: bytesex.cc:52
Emagic_type
Define bytesex indicator for magic number test.
Definition: bytesex.h:98
void file_magic_write(std::ostream &os, const char *const cmagic, const bool &fortranmode)
write magic number to file
Definition: bytesex.cc:128
Intel CPU.
Definition: bytesex.h:90