EMAN2
byteorder.h
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef eman__byteorder_h__
33#define eman__byteorder_h__ 1
34
35#include <cstddef>
36
37namespace EMAN
38{
59 {
60 public:
61 static bool is_host_big_endian();
62
67 static bool is_float_big_endian(float small_number);
68
69
76 template < class T > static bool is_data_big_endian(const T * small_num_addr)
77 {
78 if (!small_num_addr) {
79 return false;
80 }
81
82 bool data_big_endian = false;
83 size_t typesize = sizeof(T);
84 char *d = (char *) small_num_addr;
85
86 if (is_host_big_endian()) {
87 data_big_endian = false;
88 for (size_t i = typesize / 2; i < typesize; i++)
89 {
90 if (d[i] != 0) {
91 data_big_endian = true;
92 break;
93 }
94 }
95 }
96 else {
97 data_big_endian = true;
98 for (size_t j = 0; j < typesize / 2; j++) {
99 if (d[j] != 0) {
100 data_big_endian = false;
101 break;
102 }
103 }
104 }
105
106 return data_big_endian;
107 }
108
112 template < class T > static void become_big_endian(T * data, size_t n = 1)
113 {
114 if (!is_host_big_endian()) {
115 swap_bytes < T > (data, n);
116 }
117 }
118
122 template < class T > static void become_little_endian(T * data, size_t n = 1)
123 {
124 if (is_host_big_endian()) {
125 swap_bytes < T > (data, n);
126 }
127 }
128
131 template < class T > static void swap_bytes(T * data, size_t n = 1)
132 {
133 unsigned char s;
134 size_t p = sizeof(T);
135 char *d = (char *) data;
136
137 if (p > 1) {
138 for (size_t i = 0; i < n; i++, d += p) {
139 for (size_t j = 0; j < p / 2; j++) {
140 s = d[j];
141 d[j] = d[p - 1 - j];
142 d[p - 1 - j] = s;
143 }
144 }
145 }
146 }
147
148 private:
150 static bool host_big_endian;
151 };
152
153}
154
155#endif
ByteOrder defines functions to work on big/little endian byte orders.
Definition: byteorder.h:59
static bool is_host_big_endian()
Definition: byteorder.cpp:40
static bool is_host_endian_checked
Definition: byteorder.h:149
static bool host_big_endian
Definition: byteorder.h:150
static void swap_bytes(T *data, size_t n=1)
swap the byte order of data with 'n' T-type elements.
Definition: byteorder.h:131
static void become_little_endian(T *data, size_t n=1)
convert data from host byte order to little endian order.
Definition: byteorder.h:122
static bool is_data_big_endian(const T *small_num_addr)
given a pointer to a reasonable small integer number, return whether the number is big endian or not.
Definition: byteorder.h:76
static bool is_float_big_endian(float small_number)
given a small floating number, return whether the number is in big endian or not.
Definition: byteorder.cpp:59
static void become_big_endian(T *data, size_t n=1)
convert data from host byte order to big endian order.
Definition: byteorder.h:112
E2Exception class.
Definition: aligner.h:40