OpenStructure
Loading...
Searching...
No Matches
swap_util.hh
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// This file is part of the OpenStructure project <www.openstructure.org>
3//
4// Copyright (C) 2003-2010 by the IPLT authors
5// Copyright (C) 2008-2020 by the OpenStructure authors
6//
7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License as published by the Free
9// Software Foundation; either version 3.0 of the License, or (at your option)
10// any later version.
11// This library is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14// details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with this library; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//------------------------------------------------------------------------------
20#ifndef OST_IO_SWAP_UTIL_HH
21#define OST_IO_SWAP_UTIL_HH
22
23
24/*
25 byte swapping utils, originally copied from IPLT
26
27 Author: Ansgar Philippsen
28*/
29
30#include <cmath>
31
32namespace ost { namespace io {
33
34// byte swapping stuff
35
36template <typename T> void swap_buf(T* p, int n);
37
38inline
39void swap_short(short int *p, short int n) {swap_buf<short int>(p,n);}
40inline
41void swap_ushort(unsigned short int *p, short int n) {swap_buf<unsigned short int>(p,n);}
42inline
43void swap_int(int *p, int n) {swap_buf<int>(p,n);}
44inline
45void swap_uint(uint *p, int n) {swap_buf<uint>(p,n);}
46inline
47void swap_float(float *p, int n) {swap_buf<float>(p,n);}
48inline
49void swap_double(double *p, int n) {swap_buf<double>(p,n);}
50inline
51void swap_long(long *p, int n) {swap_buf<long>(p,n);}
52
53//definitions
54template <typename T>
55void swap_buf(T* p, int n)
56{
57 unsigned char *a,tmp;
58
59 for(int k=0;k<n;k++) {
60 a=reinterpret_cast<unsigned char *>(&p[k]);
61
62 if(sizeof(T)==2) {
63 tmp=a[0];
64 a[0]=a[1];
65 a[1]=tmp;
66 } else if(sizeof(T)==4) {
67 tmp=a[0];
68 a[0]=a[3];
69 a[3]=tmp;
70 tmp=a[1];
71 a[1]=a[2];
72 a[2]=tmp;
73 } else if(sizeof(T)==8) {
74 tmp = a[0];
75 a[0] = a[7];
76 a[7] = tmp;
77 tmp = a[1];
78 a[1] = a[6];
79 a[6] = tmp;
80 tmp = a[2];
81 a[2] = a[5];
82 a[5] =tmp;
83 tmp = a[3];
84 a[3] = a[4];
85 a[4] = tmp;
86 }
87 }
88}
89
90}} // namespace
91
92#endif
93
94
unsigned int uint
Definition base.hh:29
void swap_ushort(unsigned short int *p, short int n)
Definition swap_util.hh:41
void swap_float(float *p, int n)
Definition swap_util.hh:47
void swap_long(long *p, int n)
Definition swap_util.hh:51
void swap_int(int *p, int n)
Definition swap_util.hh:43
void swap_double(double *p, int n)
Definition swap_util.hh:49
void swap_buf(T *p, int n)
Definition swap_util.hh:55
void swap_short(short int *p, short int n)
Definition swap_util.hh:39
void swap_uint(uint *p, int n)
Definition swap_util.hh:45
Definition base.dox:1