USGS

Isis 3.0 Object Programmers' Reference

Home

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  long long int EndianSwapper::LongLongInt(void *buf) {
147  long long int result = *(long long int *)buf;
148 
149  if(p_needSwap) {
150  char *ptr = (char *)buf + (sizeof(long long int) - 1) * p_needSwap;
151 
152  for(unsigned int i = 0; i < sizeof(long long int); i++) {
153  p_swapper.p_char[i] = *ptr;
154  ptr += p_swapDirection;
155  }
156 
157  result = p_swapper.p_longLongInt;
158  }
159 
160  return result;
161  }
162 
168  short int EndianSwapper::ShortInt(void *buf) {
169  short int result = *(short int *)buf;
170 
171  if(p_needSwap) {
172  char *ptr = (char *)buf + (sizeof(short int) - 1) * p_needSwap;
173 
174  for(unsigned int i = 0; i < sizeof(short int); i++) {
175  p_swapper.p_char[i] = *ptr;
176  ptr += p_swapDirection;
177  }
178 
179  result = p_swapper.p_shortInt;
180  }
181 
182  return result;
183  }
184 
185 
191  unsigned short int EndianSwapper::UnsignedShortInt(unsigned short int *buf) {
192  unsigned short int result = *(unsigned short int *)buf;
193 
194  if(p_needSwap) {
195  char *ptr = (char *)buf + (sizeof(unsigned short int) - 1) * p_needSwap;
196 
197  for(unsigned int i = 0; i < sizeof(unsigned short int); i++) {
198  p_swapper.p_char[i] = *ptr;
199  ptr += p_swapDirection;
200  }
201 
202  result = p_swapper.p_uShortInt;
203  }
204 
205  return result;
206  }
207 }
208