2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
34 void report_verror(const char* format
, va_list ap
){
38 void report_error(const char* format
, ...){
41 report_verror(format
, ap
);
45 void fatal_error(const char* format
, ...){
48 report_verror(format
, ap
);
53 void out_of_memory(const char* prefix
){
54 report_error("%s: *out of memory*\n", prefix
);
58 /* 'safe' functions */
59 void* xmalloc(size_t size
){
60 void* v
= malloc(size
);
62 /* On Linux malloc by default does not return NULL
63 even if there is no more memory. So this function
64 isn't really 'safe' on that system. */
65 out_of_memory("xmalloc");
70 char* strxcpy(const char* str
){
71 char* cpy
= xmalloc(strlen(str
)+1);
76 void strmcat(char* dst
, const char* src
, size_t n
){
77 strncat(dst
, src
, n
- strlen(src
) - 1);
84 decodes utf8 encoded string str and places the
86 returns the number of bytes of str consumed.
88 int unicode_getc(char* str
, utf32
* u
){
89 /*unsigned char b[4] = {str[0], str[1], str[2], str[3]};*/
91 unsigned char a
[4] = {0,0,0,0};
101 if((b
[0] & 0x80) == 0){ /*one byte sequence*/
107 ((b
[0]&0xe0)==0xc0) &&
110 /*two byte sequence*/
111 a
[3] = ((b
[0]&0x03)<<6)|(b
[1]&0x3f);
112 a
[2] = (b
[0]&0x1c)>>2;
117 ((b
[0]&0xf0)==0xe0) &&
118 ((b
[1]&0xc0)==0x80) &&
121 /*three byte sequence*/
122 a
[3] = ((b
[1]&0x03)<<6)|(b
[2]&0x3f);
123 a
[2] = ((b
[0]&0x0f)<<4)|((b
[1]&0x3c)>>2);
128 ((b
[0]&0xf8)==0xf0) &&
129 ((b
[1]&0xc0)==0x80) &&
130 ((b
[2]&0xc0)==0x80) &&
133 /*four byte sequence*/
134 a
[3] = ((b
[2]&0x03)<<6)|(b
[3]&0x3f);
135 a
[2] = ((b
[1]&0x0f)<<4)|((b
[2]&0x3c)>>2);
136 a
[1] = ((b
[0]&0x03)<<2)|((b
[1]&0x30)>>4);
142 N
= 4;/*FIXME find next valid byte*/
145 *u
= (a
[0]<<24) | (a
[1]<<16) | (a
[2]<<8) | a
[3];
152 struct treenode
* root
,
153 int (*compare
)(void* k1
, void* k2
),
154 void* key
, void* value
)
157 struct treenode
* node
= xmalloc(sizeof(struct treenode
));
158 struct treenode
* ptr
;
166 if( compare(ptr
->key
, key
) < 0 ){
167 if(ptr
->l
){ptr
= ptr
->l
;}
168 else{ptr
->l
= node
; break;}
170 else if( compare(ptr
->key
, key
) > 0){
171 if(ptr
->r
){ptr
= ptr
->r
;}
172 else{ptr
->r
= node
; break;}
174 else{ /* key already exists */
175 report_error("tree_insert: key already exists\n");
182 struct treenode
* root
,
183 int (*compare
)(void* k1
, void* k2
),
189 else if(compare(root
->key
, key
)>0){
190 return tree_search(root
->r
, compare
, key
);
192 else if(compare(root
->key
, key
)<0){
193 return tree_search(root
->l
, compare
, key
);
202 /* rng utility functions */
203 void rand_reset(unsigned s
){
207 int randi(int a
, int b
){
225 unsigned x
= zrand();
230 /* Copyright 300BC Euclid */
231 int gcd(int a
, int b
){