summaryrefslogtreecommitdiff
path: root/src/lib/libsdw_internal.cpp
blob: e4e4c52cde2ad7ccf320d962616fe88ef4e6cffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* libsdw
 * Version: MPL 2.0 / LGPLv2.1+
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Major Contributor(s):
 * Copyright (C) 2002, 2005 William Lachance (william.lachance@sympatico.ca)
 * Copyright (C) 2002, 2004 Marc Maurer (uwog@uwog.net)
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU Lesser General Public License Version 2.1 or later
 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
 * applicable instead of those above.
 *
 * For further information visit http://libsdw.sourceforge.net
 */

#include "libsdw_internal.h"

uint8_t libsdw::readU8(WPXInputStream *input)
{
	if (!input || input->atEOS())
	{
		SDW_DEBUG_MSG(("Throwing EndOfStreamException\n"));
		throw EndOfStreamException();
	}
	unsigned long numBytesRead;
	uint8_t const *p = input->read(sizeof(uint8_t), numBytesRead);

	if (p && numBytesRead == sizeof(uint8_t))
		return *(uint8_t const *)(p);
	SDW_DEBUG_MSG(("Throwing EndOfStreamException\n"));
	throw EndOfStreamException();
}

int8_t libsdw::readS8(WPXInputStream *input)
{
	return (int8_t)readU8(input);
}

uint16_t libsdw::readU16(WPXInputStream *input)
{
	if (!input || input->atEOS())
	{
		SDW_DEBUG_MSG(("Throwing EndOfStreamException\n"));
		throw EndOfStreamException();
	}
	unsigned long numBytesRead;
	uint8_t const *p = input->read(sizeof(uint16_t), numBytesRead);

	if (p && numBytesRead == sizeof(uint16_t))
		return (uint16_t)((uint16_t)p[0]|((uint16_t)p[1]<<8));
	SDW_DEBUG_MSG(("Throwing EndOfStreamException\n"));
	throw EndOfStreamException();
}

int16_t libsdw::readS16(WPXInputStream *input)
{
	return (int16_t)readU16(input);
}

uint32_t libsdw::readU32(WPXInputStream *input)
{
	if (!input || input->atEOS())
	{
		SDW_DEBUG_MSG(("Throwing EndOfStreamException\n"));
		throw EndOfStreamException();
	}
	unsigned long numBytesRead;
	uint8_t const *p = input->read(sizeof(uint32_t), numBytesRead);

	if (p && numBytesRead == sizeof(uint32_t))
		return (uint32_t)p[0]|((uint32_t)p[1]<<8)|((uint32_t)p[2]<<16)|((uint32_t)p[3]<<24);
	SDW_DEBUG_MSG(("Throwing EndOfStreamException\n"));
	throw EndOfStreamException();
}

int32_t libsdw::readS32(WPXInputStream *input)
{
	return (int32_t)readU32(input);
}

WPXString libsdw::readString(WPXInputStream *input)
{
	WPXString text;
	uint8_t c = 0;
	while ( !input->atEOS() && (c = readU8(input)) )
		text.append((char)c);
	return text;
}

/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */