Isis 3 Programmer Reference
EndianSwapper.cpp
Go to the documentation of this file.
1 
23 #include "Endian.h"
24 #include "EndianSwapper.h"
25 #include "IException.h"
26 #include "Message.h"
27 #include <string>
28 
29 #include <iostream>
30 
31 using namespace std;
32 namespace Isis {
39  EndianSwapper::EndianSwapper(QString inputEndian) {
40 
41  if(inputEndian != "LSB" && inputEndian != "MSB") {
42  string message = "Invalid parameter-InputEndian must be LSB or MSB";
43  throw IException(IException::Programmer, message, _FILEINFO_);
44  }
45 
46  if((Isis::IsLsb() && inputEndian == "LSB") ||
47  (Isis::IsMsb() && inputEndian == "MSB")) {
48  p_needSwap = false;
49  p_swapDirection = 1;
50  }
51  else {
52  p_needSwap = true;
53  p_swapDirection = -1;
54  }
55 
56  }
57 
58 
62  EndianSwapper::~EndianSwapper() {
63  }
64 
65 
71  double EndianSwapper::Double(void *buf) {
72  double result = *(double *)buf;
73 
74  if(p_needSwap) {
75  char *ptr = (char *)buf + (sizeof(double) - 1) * p_needSwap;
76 
77  for(unsigned int i = 0; i < sizeof(double); i++) {
78  p_swapper.p_char[i] = *ptr;
79  ptr += p_swapDirection;
80  }
81 
82  result = p_swapper.p_double;
83  }
84 
85  return result;
86  }
87 
88 
94  float EndianSwapper::Float(void *buf) {
95  float result = *(float *)buf;
96 
97  if(p_needSwap) {
98  char *ptr = (char *)buf + (sizeof(float) - 1) * p_needSwap;
99 
100  for(unsigned int i = 0; i < sizeof(float); i++) {
101  p_swapper.p_char[i] = *ptr;
102  ptr += p_swapDirection;
103  }
104 
105  result = p_swapper.p_float;
106  }
107 
108  return result;
109  }
110 
111 
115  int EndianSwapper::ExportFloat(void *buf) {
116  return Int(buf);
117  }
118 
124  int EndianSwapper::Int(void *buf) {
125  int result = *(int *)buf;
126 
127  if(p_needSwap) {
128  char *ptr = (char *)buf + (sizeof(int) - 1) * p_needSwap;
129 
130  for(unsigned int i = 0; i < sizeof(int); i++) {
131  p_swapper.p_char[i] = *ptr;
132  ptr += p_swapDirection;
133  }
134 
135  result = p_swapper.p_int;
136  }
137 
138  return result;
139  }
140 
146  uint32_t EndianSwapper::Uint32_t(void *buf) {
147  uint32_t result = *(uint32_t *)buf;
148 
149  if(p_needSwap) {
150  char *ptr = (char *)buf + (sizeof(uint32_t) - 1) * p_needSwap;
151 
152  for(unsigned int i = 0; i < sizeof(uint32_t); i++) {
153  p_swapper.p_char[i] = *ptr;
154  ptr += p_swapDirection;
155  }
156 
157  result = p_swapper.p_uint32;
158  }
159 
160  return result;
161  }
162 
168  long long int EndianSwapper::LongLongInt(void *buf) {
169  long long int result = *(long long int *)buf;
170 
171  if(p_needSwap) {
172  char *ptr = (char *)buf + (sizeof(long long int) - 1) * p_needSwap;
173 
174  for(unsigned int i = 0; i < sizeof(long long int); i++) {
175  p_swapper.p_char[i] = *ptr;
176  ptr += p_swapDirection;
177  }
178 
179  result = p_swapper.p_longLongInt;
180  }
181 
182  return result;
183  }
184 
190  short int EndianSwapper::ShortInt(void *buf) {
191  short int result = *(short int *)buf;
192 
193  if(p_needSwap) {
194  char *ptr = (char *)buf + (sizeof(short int) - 1) * p_needSwap;
195 
196  for(unsigned int i = 0; i < sizeof(short int); i++) {
197  p_swapper.p_char[i] = *ptr;
198  ptr += p_swapDirection;
199  }
200 
201  result = p_swapper.p_shortInt;
202  }
203 
204  return result;
205  }
206 
207 
213  unsigned short int EndianSwapper::UnsignedShortInt(void *buf) {
214  unsigned short int result = *(unsigned short int *)buf;
215  if(p_needSwap) {
216  char *ptr = (char *)buf + (sizeof(unsigned short int) - 1) * p_needSwap;
217 
218  for(unsigned int i = 0; i < sizeof(unsigned short int); i++) {
219  p_swapper.p_char[i] = *ptr;
220  ptr += p_swapDirection;
221  }
222 
223  result = p_swapper.p_uShortInt;
224  }
225  return result;
226  }
227 }
Namespace for the standard library.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
bool IsLsb()
Return true if this host is an LSB first machine and false if it is not.
Definition: Endian.h:84
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
bool IsMsb()
Return true if this host is an MSB first machine and false if it is not.
Definition: Endian.h:100