chiark / gitweb /
WIP island determination; pixmap handling in progress
[ypp-sc-tools.db-test.git] / pctb / common.c
1 /*
2  * Utility functions
3  */
4 /*
5  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  * 
10  *  This program is free software: you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation, either version 3 of the License, or
13  *  (at your option) any later version.
14  * 
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  * 
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  * 
23  *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24  *  are used without permission.  This program is not endorsed or
25  *  sponsored by Three Rings.
26  */
27
28 #include "common.h"
29
30 #define dbassert(x)                                                     \
31   ((x) ? (void)0 :                                                      \
32      fatal("Error in database.\n"                                       \
33            " Requirement not met: %s:%d: %s", __FILE__,__LINE__, #x))
34
35 void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
36   errno=0;
37   char *s= fgets(lbuf,lbufsz,f);
38   sysassert(!ferror(f));
39   dbassert(!feof(f));
40   assert(s);
41   int l= strlen(lbuf);
42   dbassert(l>0);  dbassert(lbuf[--l]='\n');
43   lbuf[l]= 0;
44 }
45
46 void *mmalloc(size_t sz) {
47   void *r;
48   if (!sz) return 0;
49   sysassert( r= malloc(sz) );
50   return r;
51 }
52 void *mrealloc(void *p, size_t sz) {
53   assert(sz);
54   void *r;
55   sysassert( r= realloc(p,sz) );
56   return r;
57 }