Isis 3 Programmer Reference
EndianSwapper.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "Endian.h"
8 #include "EndianSwapper.h"
9 #include "IException.h"
10 #include "Message.h"
11 #include <string>
12 
13 #include <iostream>
14 
15 using namespace std;
16 namespace Isis {
23  EndianSwapper::EndianSwapper(QString inputEndian) {
24 
25  if(inputEndian != "LSB" && inputEndian != "MSB") {
26  string message = "Invalid parameter-InputEndian must be LSB or MSB";
27  throw IException(IException::Programmer, message, _FILEINFO_);
28  }
29 
30  if((Isis::IsLsb() && inputEndian == "LSB") ||
31  (Isis::IsMsb() && inputEndian == "MSB")) {
32  p_needSwap = false;
33  p_swapDirection = 1;
34  }
35  else {
36  p_needSwap = true;
37  p_swapDirection = -1;
38  }
39 
40  }
41 
42 
46  EndianSwapper::~EndianSwapper() {
47  }
48 
49 
55  double EndianSwapper::Double(void *buf) {
56  double result = *(double *)buf;
57 
58  if(p_needSwap) {
59  char *ptr = (char *)buf + (sizeof(double) - 1) * p_needSwap;
60 
61  for(unsigned int i = 0; i < sizeof(double); i++) {
62  p_swapper.p_char[i] = *ptr;
63  ptr += p_swapDirection;
64  }
65 
66  result = p_swapper.p_double;
67  }
68 
69  return result;
70  }
71 
72 
78  float EndianSwapper::Float(void *buf) {
79  float result = *(float *)buf;
80 
81  if(p_needSwap) {
82  char *ptr = (char *)buf + (sizeof(float) - 1) * p_needSwap;
83 
84  for(unsigned int i = 0; i < sizeof(float); i++) {
85  p_swapper.p_char[i] = *ptr;
86  ptr += p_swapDirection;
87  }
88 
89  result = p_swapper.p_float;
90  }
91 
92  return result;
93  }
94 
95 
99  int EndianSwapper::ExportFloat(void *buf) {
100  return Int(buf);
101  }
102 
108  int EndianSwapper::Int(void *buf) {
109  int result = *(int *)buf;
110 
111  if(p_needSwap) {
112  char *ptr = (char *)buf + (sizeof(int) - 1) * p_needSwap;
113 
114  for(unsigned int i = 0; i < sizeof(int); i++) {
115  p_swapper.p_char[i] = *ptr;
116  ptr += p_swapDirection;
117  }
118 
119  result = p_swapper.p_int;
120  }
121 
122  return result;
123  }
124 
130  uint32_t EndianSwapper::Uint32_t(void *buf) {
131  uint32_t result = *(uint32_t *)buf;
132 
133  if(p_needSwap) {
134  char *ptr = (char *)buf + (sizeof(uint32_t) - 1) * p_needSwap;
135 
136  for(unsigned int i = 0; i < sizeof(uint32_t); i++) {
137  p_swapper.p_char[i] = *ptr;
138  ptr += p_swapDirection;
139  }
140 
141  result = p_swapper.p_uint32;
142  }
143 
144  return result;
145  }
146 
152  long long int EndianSwapper::LongLongInt(void *buf) {
153  long long int result = *(long long int *)buf;
154 
155  if(p_needSwap) {
156  char *ptr = (char *)buf + (sizeof(long long int) - 1) * p_needSwap;
157 
158  for(unsigned int i = 0; i < sizeof(long long int); i++) {
159  p_swapper.p_char[i] = *ptr;
160  ptr += p_swapDirection;
161  }
162 
163  result = p_swapper.p_longLongInt;
164  }
165 
166  return result;
167  }
168 
174  short int EndianSwapper::ShortInt(void *buf) {
175  short int result = *(short int *)buf;
176 
177  if(p_needSwap) {
178  char *ptr = (char *)buf + (sizeof(short int) - 1) * p_needSwap;
179 
180  for(unsigned int i = 0; i < sizeof(short int); i++) {
181  p_swapper.p_char[i] = *ptr;
182  ptr += p_swapDirection;
183  }
184 
185  result = p_swapper.p_shortInt;
186  }
187 
188  return result;
189  }
190 
191 
197  unsigned short int EndianSwapper::UnsignedShortInt(void *buf) {
198  unsigned short int result = *(unsigned short int *)buf;
199  if(p_needSwap) {
200  char *ptr = (char *)buf + (sizeof(unsigned short int) - 1) * p_needSwap;
201 
202  for(unsigned int i = 0; i < sizeof(unsigned short int); i++) {
203  p_swapper.p_char[i] = *ptr;
204  ptr += p_swapDirection;
205  }
206 
207  result = p_swapper.p_uShortInt;
208  }
209  return result;
210  }
211 }
Isis::IsLsb
bool IsLsb()
Return true if this host is an LSB first machine and false if it is not.
Definition: Endian.h:67
Isis::IsMsb
bool IsMsb()
Return true if this host is an MSB first machine and false if it is not.
Definition: Endian.h:83
Isis::IException
Isis exception class.
Definition: IException.h:91
std
Namespace for the standard library.
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16