OpenZGY/C++ API and Internals (ALPHA)
Access seismic data stored in ZGY format.
structaccess.h
Go to the documentation of this file.
1 // Copyright 2017-2020, Schlumberger
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "../declspec.h"
18 
19 #include <array>
20 #include <sstream>
21 #include <iomanip>
22 #include <cstring>
23 #include <type_traits>
24 
30 namespace InternalZGY { // namespace StructAccess {
31 #if 0
32 }
33 #endif
34 
41 template<typename T, std::size_t N>
42 std::array<T, N>
43 ptr_to_array(const T* in)
44 {
45  std::array<T, N> result;
46  memcpy(result.data(), in, sizeof(result));
47  return result;
48 }
49 
61 template<typename T>
62 T align(const T& in)
63 {
64  T result;
65  memcpy(&result, &in, sizeof(T));
66  return result;
67 }
68 
74 template<typename T, std::size_t N>
75 std::string
76 array_to_string(const std::array<T, N>& a)
77 {
78  std::stringstream ss;
79  ss << "(";
80  for (std::size_t ii=0; ii<N; ++ii)
81  ss << std::to_string(a[ii]) << (ii == N-1 ? ")" : ", ");
82  return ss.str();
83 }
84 
88 template<typename T, std::size_t N>
89 std::string
90 array_to_hex(const std::array<T, N>& a)
91 {
92  std::stringstream ss;
93  ss << std::hex << std::setfill('0') << std::right;
94  for (std::size_t ii=0; ii<N; ++ii)
95  ss << std::setw(2*sizeof(T)) << int(a[ii]) << (ii==N-1 ? "" : ",");
96  return ss.str();
97 }
98 
103 template<typename T, std::size_t N>
104 std::string
105 ptr_to_string(const T* a)
106 {
107  return array_to_hex<T, N>(ptr_to_array<T, N>(a));
108 }
109 
114 template<typename T, std::size_t N>
115 std::string
116 ptr_to_hex(const T* a)
117 {
118  return array_to_hex<T, N>(ptr_to_array<T, N>(a));
119 }
120 
132 template<typename T>
133 static void
134 byteswapAlways(T *ptr, size_t n = 1)
135 {
136  char in[sizeof(T)], out[sizeof(T)];
137  for (size_t offset=0; offset<n; ++offset) {
138  memcpy(&in[0], ptr, sizeof(T));
139  for (size_t ii=0; ii<sizeof(T); ++ii)
140  out[sizeof(T)-ii-1] = in[ii];
141  memcpy(ptr, &out[0], sizeof(T));
142  ++ptr;
143  }
144 }
145 
150 template<typename T>
151 static void
152 byteswapT(T *ptr, size_t n = 1)
153 {
154 #if BIG_ENDIAN_ARCH
155  byteswapAlways(ptr, n);
156 #endif
157 }
158 
177 extern OPENZGY_TEST_API void byteswapV1Long(std::int64_t *ptr, size_t n = 1);
178 
179 extern OPENZGY_TEST_API void byteswapV1Long(std::uint64_t *ptr, size_t n = 1);
180 
181 template<typename T, typename U, int N>
182 std::array<T,N>
183 array_cast(const std::array<U,N>& in)
184 {
185  std::array<T,N> result;
186  for (int ii=0; ii<N; ++ii)
187  result[ii] = static_cast<T>(in[ii]);
188  return result;
189 }
190 
191 } // namespace
InternalZGY::array_to_string
std::string array_to_string(const std::array< T, N > &a)
Definition: structaccess.h:76
InternalZGY::array_to_hex
std::string array_to_hex(const std::array< T, N > &a)
Definition: structaccess.h:90
InternalZGY::byteswapV1Long
OPENZGY_TEST_API void byteswapV1Long(std::int64_t *ptr, size_t n=1)
Definition: structaccess.cpp:36
InternalZGY::ptr_to_array
std::array< T, N > ptr_to_array(const T *in)
Definition: structaccess.h:43
InternalZGY::ptr_to_string
std::string ptr_to_string(const T *a)
Definition: structaccess.h:105
InternalZGY::ptr_to_hex
std::string ptr_to_hex(const T *a)
Definition: structaccess.h:116
InternalZGY
Implementation not visible to clients.
InternalZGY::align
T align(const T &in)
Definition: structaccess.h:62