Debian Codegraph @

src/Base/Swap.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2005 Imetric 3D GmbH *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include "Swap.h"
26
27 unsigned short Base::SwapOrder()
28 {
29 unsigned short usDummy = 1;
30 return *((char*)&usDummy) == 1 ? LOW_ENDIAN : HIGH_ENDIAN;
31 }
32
33 void Base::SwapVar(char& /*unused*/)
34 {}
35
36 void Base::SwapVar(unsigned char& /*unused*/)
37 {}
38
39 void Base::SwapVar(short& s)
40 {
41 short sTmp = s;
42 int i = 0;
43
44 for (i = 0; i < (int)sizeof(short); i++) {
45 *(((char*)&sTmp) + i) = *(((char*)&s) + sizeof(short) - i - 1);
46 }
47 s = sTmp;
48 }
49
50 void Base::SwapVar(unsigned short& s)
51 {
52 short sTmp = s;
53 int i = 0;
54
55 for (i = 0; i < (int)sizeof(short); i++) {
56 *(((char*)&sTmp) + i) = *(((char*)&s) + sizeof(short) - i - 1);
57 }
58 s = sTmp;
59 }
60
61 void Base::SwapVar(long& l)
62 {
63 long lTmp = l;
64 int i = 0;
65
66 for (i = 0; i < (int)sizeof(long); i++) {
67 *(((char*)&lTmp) + i) = *(((char*)&l) + sizeof(long) - i - 1);
68 }
69 l = lTmp;
70 }
71
72 void Base::SwapVar(unsigned long& l)
73 {
74 long lTmp = l;
75 int i = 0;
76
77 for (i = 0; i < (int)sizeof(long); i++) {
78 *(((char*)&lTmp) + i) = *(((char*)&l) + sizeof(long) - i - 1);
79 }
80 l = lTmp;
81 }
82
83 void Base::SwapVar(float& f)
84 {
85 float fTmp = f;
86 int i = 0;
87
88 for (i = 0; i < (int)sizeof(float); i++) {
89 *(((char*)&fTmp) + i) = *(((char*)&f) + sizeof(float) - i - 1);
90 }
91 f = fTmp;
92 }
93
94 void Base::SwapVar(double& d)
95 {
96 double dTmp = d;
97 int i = 0;
98
99 for (i = 0; i < (int)sizeof(double); i++) {
100 *(((char*)&dTmp) + i) = *(((char*)&d) + sizeof(double) - i - 1);
101 }
102 d = dTmp;
103 }