OSDN Git Service

イニシャルコミット。
[marathon/ShapeFusion.git] / LittleEndianBuffer.cpp
1 /*
2  * This file is part of ShapeFusion (Copyright 2000 Tito Dal Canton)
3  *
4  * ShapeFusion is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * ShapeFusion is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with ShapeFusion; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 #include <iostream>
19 #include "LittleEndianBuffer.h"
20
21 LittleEndianBuffer::LittleEndianBuffer(unsigned int _size):
22                 GenericEndianBuffer(_size)
23 {
24
25 }
26
27 LittleEndianBuffer::LittleEndianBuffer(unsigned char *_data, unsigned int _size):
28                 GenericEndianBuffer(_data, _size)
29 {
30         
31 }
32
33 LittleEndianBuffer::~LittleEndianBuffer(void)
34 {
35
36 }
37
38 short LittleEndianBuffer::ReadShort(void)
39 {
40         if ((unsigned int)(mPosition - mData + 1) < mSize) {
41                 unsigned char   lo = *mPosition++,
42                                                 hi = *mPosition++;
43
44                 return (short)((hi << 8) | lo);
45         } else {
46                 std::cerr << "LittleEndianBuffer: attempted read beyond buffer limits\n";
47                 return 0;
48         }
49 }
50
51 unsigned short LittleEndianBuffer::ReadUShort(void)
52 {
53         if ((unsigned int)(mPosition - mData + 1) < mSize) {
54                 unsigned char   lo = *mPosition++,
55                                                 hi = *mPosition++;
56                 
57                 return (unsigned short)((hi << 8) | lo);
58         } else {
59                 std::cerr << "LittleEndianBuffer: attempted read beyond buffer limits\n";
60                 return 0;
61         }
62 }
63
64 long LittleEndianBuffer::ReadLong(void)
65 {
66         if ((unsigned int)(mPosition - mData + 3) < mSize) {
67                 unsigned char   a = *mPosition++,
68                                                 b = *mPosition++,
69                                                 c = *mPosition++,
70                                                 d = *mPosition++;
71                 
72                 return (long)((d << 24) | (c << 16) | (b << 8) | a);
73         } else {
74                 std::cerr << "LittleEndianBuffer: attempted read beyond buffer limits\n";
75                 return 0;
76         }
77 }
78
79 unsigned long LittleEndianBuffer::ReadULong(void)
80 {
81         if ((unsigned int)(mPosition - mData + 3) < mSize) {
82                 unsigned char   a = *mPosition++,
83                                                 b = *mPosition++,
84                                                 c = *mPosition++,
85                                                 d = *mPosition++;
86                 
87                 return (unsigned long)((d << 24) | (c << 16) | (b << 8) | a);
88         } else {
89                 std::cerr << "LittleEndianBuffer: attempted read beyond buffer limits\n";
90                 return 0;
91         }
92 }
93
94 void LittleEndianBuffer::WriteShort(short v)
95 {
96         if ((unsigned int)(mPosition - mData + 1) < mSize) {
97                 *mPosition++ = v & 0xff;
98                 *mPosition++ = (v >> 8) & 0xff;
99         } else {
100                 std::cerr << "LittleEndianBuffer: attempted write beyond buffer limits\n";
101         }
102 }
103
104 void LittleEndianBuffer::WriteUShort(unsigned short v)
105 {
106         if ((unsigned int)(mPosition - mData + 1) < mSize) {
107                 *mPosition++ = v & 0xff;
108                 *mPosition++ = (v >> 8) & 0xff;
109         } else {
110                 std::cerr << "LittleEndianBuffer: attempted write beyond buffer limits\n";
111         }
112 }
113
114 void LittleEndianBuffer::WriteLong(long v)
115 {
116         if ((unsigned int)(mPosition - mData + 3) < mSize) {
117                 *mPosition++ = v & 0xff;
118                 *mPosition++ = (v >> 8) & 0xff;
119                 *mPosition++ = (v >> 16) & 0xff;
120                 *mPosition++ = (v >> 24) & 0xff;
121         } else {
122                 std::cerr << "LittleEndianBuffer: attempted write beyond buffer limits\n";
123         }
124 }
125
126 void LittleEndianBuffer::WriteULong(unsigned long v)
127 {
128         if ((unsigned int)(mPosition - mData + 3) < mSize) {
129                 *mPosition++ = v & 0xff;
130                 *mPosition++ = (v >> 8) & 0xff;
131                 *mPosition++ = (v >> 16) & 0xff;
132                 *mPosition++ = (v >> 24) & 0xff;
133         } else {
134                 std::cerr << "LittleEndianBuffer: attempted write beyond buffer limits\n";
135         }
136 }
137