~ubuntu-branches/ubuntu/raring/voxbo/raring

« back to all changes in this revision

Viewing changes to lib/endian.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-06-06 11:33:11 UTC
  • Revision ID: james.westby@ubuntu.com-20100606113311-v3c13imdkkd5n7ae
Tags: upstream-1.8.5~svn1172
ImportĀ upstreamĀ versionĀ 1.8.5~svn1172

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// endian.cpp
 
3
// byteorder-related functions for VoxBo
 
4
// Copyright (c) 1998-2001 by The VoxBo Development Team
 
5
 
 
6
// This file is part of VoxBo
 
7
// 
 
8
// VoxBo is free software: you can redistribute it and/or modify it
 
9
// under the terms of the GNU General Public License as published by
 
10
// the Free Software Foundation, either version 3 of the License, or
 
11
// (at your option) any later version.
 
12
// 
 
13
// VoxBo is distributed in the hope that it will be useful, but
 
14
// WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
// General Public License for more details.
 
17
// 
 
18
// You should have received a copy of the GNU General Public License
 
19
// along with VoxBo.  If not, see <http://www.gnu.org/licenses/>.
 
20
// 
 
21
// For general information on VoxBo, including the latest complete
 
22
// source code and binary distributions, manual, and associated files,
 
23
// see the VoxBo home page at: http://www.voxbo.org/
 
24
// 
 
25
// original version written by Dan Kimberg
 
26
 
 
27
using namespace std;
 
28
 
 
29
#include "vbutil.h"
 
30
#include <netinet/in.h>
 
31
 
 
32
void
 
33
swapn(unsigned char *uc,int dsize,int len)
 
34
{
 
35
  if (dsize==2)
 
36
    swap((uint16 *)uc,len);
 
37
  else if (dsize==4)
 
38
    swap((uint32 *)uc,len);
 
39
  else if (dsize==8)
 
40
    swap((double *)uc,len);
 
41
}
 
42
 
 
43
void
 
44
swap(uint16 *sh,int len)
 
45
{
 
46
  unsigned char *foo,tmp;
 
47
  for (int i=0; i<len; i++) {
 
48
    foo = (unsigned char *)(sh + i);
 
49
    tmp = foo[0];
 
50
    foo[0] = foo[1];
 
51
    foo[1] = tmp;
 
52
  }
 
53
}
 
54
 
 
55
void
 
56
swap(int16 *sh,int len)
 
57
{
 
58
  unsigned char *foo,tmp;
 
59
  for (int i=0; i<len; i++) {
 
60
    foo = (unsigned char *)(sh + i);
 
61
    tmp = foo[0];
 
62
    foo[0] = foo[1];
 
63
    foo[1] = tmp;
 
64
  }
 
65
}
 
66
 
 
67
void
 
68
swap(uint32 *lng,int len)
 
69
{
 
70
  unsigned char *foo,tmp;
 
71
  for (int i=0; i<len; i++) {
 
72
    foo = (unsigned char *)(lng + i);
 
73
    tmp = foo[0];
 
74
    foo[0] = foo[3];
 
75
    foo[3] = tmp;
 
76
    tmp = foo[1];
 
77
    foo[1] = foo[2];
 
78
    foo[2] = tmp;
 
79
  }
 
80
}
 
81
 
 
82
void
 
83
swap(int32 *lng,int len)
 
84
{
 
85
  unsigned char *foo,tmp;
 
86
  for (int i=0; i<len; i++) {
 
87
    foo = (unsigned char *)(lng + i);
 
88
    tmp = foo[0];
 
89
    foo[0] = foo[3];
 
90
    foo[3] = tmp;
 
91
    tmp = foo[1];
 
92
    foo[1] = foo[2];
 
93
    foo[2] = tmp;
 
94
  }
 
95
}
 
96
 
 
97
void
 
98
swap(float *flt,int len)
 
99
{
 
100
  unsigned char *foo,tmp;
 
101
  for (int i=0; i<len; i++) {
 
102
    foo = (unsigned char *)(flt + i);
 
103
    tmp = foo[0];
 
104
    foo[0] = foo[3];
 
105
    foo[3] = tmp;
 
106
    tmp = foo[1];
 
107
    foo[1] = foo[2];
 
108
    foo[2] = tmp;
 
109
  }
 
110
}
 
111
 
 
112
void
 
113
swap(double *dbl,int len)
 
114
{
 
115
  unsigned char *foo,tmp;
 
116
  for (int i=0; i<len; i++) {
 
117
    foo = (unsigned char *)(dbl + i);
 
118
    tmp = foo[0];
 
119
    foo[0] = foo[7];
 
120
    foo[7] = tmp;
 
121
    tmp = foo[1];
 
122
    foo[1] = foo[6];
 
123
    foo[6] = tmp;
 
124
    tmp = foo[2];
 
125
    foo[2] = foo[5];
 
126
    foo[5] = tmp;
 
127
    tmp = foo[3];
 
128
    foo[3] = foo[4];
 
129
    foo[4] = tmp;
 
130
  }
 
131
}
 
132
 
 
133
VB_byteorder
 
134
my_endian()
 
135
{
 
136
  if (ntohs(1)==1)
 
137
    return ENDIAN_BIG;
 
138
  else
 
139
    return ENDIAN_LITTLE;
 
140
}