File: binReader.cpp

package info (click to toggle)
lierolibre 0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,604 kB
  • ctags: 5,694
  • sloc: cpp: 36,414; sh: 11,663; makefile: 813; ansic: 268
file content (133 lines) | stat: -rw-r--r-- 3,670 bytes parent folder | download | duplicates (4)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (c) 2007-2010, Erik Lindroos <gliptic@gmail.com>
 * Copyright (c) 2012, Martin Erik Werner <martinerikwerner@gmail.com>
 * This software is released under the The BSD-2-Clause License:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

// Compile via: g++ -c -lSDL binReader.cpp

#include <stdexcept>

// Needed for cross-platform *int32 definitions
#include <SDL/SDL.h>

#include "binReader.hpp"

using namespace std;

void checkedFread(void* ptr, size_t size, size_t count, FILE* f)
{
	if(fread(ptr, size, count, f) != count)
		throw runtime_error("fread failed to read fully");
}

string readPascalString(FILE* f)
{
	unsigned char length;
	checkedFread(&length, 1, 1, f);
	char txt[256];
	checkedFread(txt, 1, length, f);
	return string(txt, length);
}

string readPascalString(FILE* f, unsigned char fieldLen)
{
	char txt[256];
	checkedFread(txt, 1, fieldLen, f);
	unsigned char length = static_cast<unsigned char>(txt[0]);
	return string(txt + 1, length);
}

void writePascalString(FILE* f, string const& str, unsigned char fieldLen)
{
	int len = int(str.size() < fieldLen ? str.size() : fieldLen - 1);
	size_t zeroes = fieldLen - 1 - len;
	fputc(len, f);
	fwrite(str.data(), 1, len, f);
	for(size_t i = 0; i < zeroes; ++i)
		fputc(0, f);
}

string readPascalStringAt(FILE* f, int location)
{
	unsigned char length;
	fseek(f, location, SEEK_SET);
	checkedFread(&length, 1, 1, f);
	char txt[256];
	checkedFread(txt, 1, length, f);
	return string(txt, length);
}

Uint32 readUint8(FILE* f)
{
	unsigned char temp[1];
	checkedFread(temp, 1, 1, f);
	return temp[0];
}

void writeUint8(FILE* f, Uint32 v)
{
	fputc(v & 0xff, f);
}

Sint32 readSint8(FILE* f)
{
	char temp[1];
	checkedFread(temp, 1, 1, f);
	return temp[0];
}

Uint32 readUint16(FILE* f)
{
	unsigned char temp[2];
	checkedFread(temp, 1, 2, f);
	return temp[0] + (temp[1] << 8);
}

void writeUint16(FILE* f, Uint32 v)
{
	fputc(v & 0xff, f);
	fputc((v >> 8) & 0xff, f);
}

Sint32 readSint16(FILE* f)
{
	unsigned char temp[2];
	checkedFread(temp, 1, 2, f);
	return temp[0] + (static_cast<char>(temp[1]) << 8);
}

Uint32 readUint32(FILE* f)
{
	unsigned char temp[4];
	checkedFread(temp, 1, 4, f);
	return temp[0] + (temp[1] << 8) + (temp[2] << 16) + (temp[3] << 24);
}

Sint32 readSint32(FILE* f)
{
	unsigned char temp[4];
	checkedFread(temp, 1, 4, f);
	return temp[0] + (temp[1] << 8) + (temp[2] << 16) + (static_cast<char>(temp[3]) << 24);
}