X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.main.git;a=blobdiff_plain;f=pctb%2Fcommon.c;fp=pctb%2Fcommon.c;h=0000000000000000000000000000000000000000;hp=f95e585866657f8d8e29c19c7c31b5daeae9c977;hb=c68fb80a6bbf7acbcac4b2cb2143f5fea745cd2b;hpb=b9cce976550d000f15e5a8f2b690740bdae1e468 diff --git a/pctb/common.c b/pctb/common.c deleted file mode 100644 index f95e585..0000000 --- a/pctb/common.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Utility functions - */ -/* - * This is part of ypp-sc-tools, a set of third-party tools for assisting - * players of Yohoho Puzzle Pirates. - * - * Copyright (C) 2009 Ian Jackson - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Yohoho and Puzzle Pirates are probably trademarks of Three Rings and - * are used without permission. This program is not endorsed or - * sponsored by Three Rings. - */ - -#include "common.h" - -void *mmalloc(size_t sz) { - void *r; - if (!sz) return 0; - sysassert( r= malloc(sz) ); - return r; -} -void *mrealloc(void *p, size_t sz) { - assert(sz); - void *r; - sysassert( r= realloc(p,sz) ); - return r; -} - - -FILE *dbfile; -static const char *basepath; /* as passed in by caller */ -static pid_t dbzcat; - -int dbfile_gzopen(const char *basepath_spec) { - assert(!dbfile); - - basepath= basepath_spec; - - char *zpath= masprintf("%s.gz", basepath); - int e= gzopen(zpath, O_RDONLY, &dbfile, &dbzcat, 0); - free(zpath); - if (e) { errno=e; sysassert(errno==ENOENT); return 0; } - - return 1; -} - -int dbfile_open(const char *tpath) { - assert(!dbfile); - - basepath= tpath; - - dbzcat= -1; - dbfile= fopen(tpath,"r"); - if (!dbfile) { sysassert(errno==ENOENT); return 0; } - return 1; -} - -void dbfile_close(void) { - gzclose(&dbfile, &dbzcat, basepath); -} - -#define dbassertgl(x) ((x) ? (void)0 : dbfile_assertfail(file,line,#x)) - -void dbfile_getsline(char *lbuf, size_t lbufsz, const char *file, int line) { - errno=0; - char *s= fgets(lbuf,lbufsz,dbfile); - sysassert(!ferror(dbfile)); - dbassertgl(!feof(dbfile)); - assert(s); - int l= strlen(lbuf); - dbassertgl(l>0); dbassertgl(lbuf[--l]=='\n'); - lbuf[l]= 0; -} - -int dbfile_vscanf(const char *fmt, va_list al) { - int r= vfscanf(dbfile,fmt,al); - sysassert(!ferror(dbfile)); - return r; -} - -int dbfile_scanf(const char *fmt, ...) { - va_list al; - va_start(al,fmt); - int r= dbfile_vscanf(fmt,al); - va_end(al); - return r; -} - -void dbfile_assertfail(const char *file, int line, const char *m) { - if (dbzcat) - fatal("Error in dictionary file %s.gz:\n" - " Requirement not met at %s:%d:\n" - " %s", - basepath, file,line, m); - else if (dbfile) - fatal("Error in dictionary file %s at byte %ld:\n" - " Requirement not met at %s:%d:\n" - " %s", - basepath,(long)ftell(dbfile), file,line, m); - else - fatal("Semantic error in dictionaries:\n" - " Requirement not met at %s:%d:\n" - " %s", - file,line, m); -} - -int gzopen(const char *zpath, int oflags, FILE **f_r, pid_t *pid_r, - const char *gziplevel /* 0 for read; may be 0, or "-1" etc. */) { - - int zfd= open(zpath, oflags, 0666); - if (!zfd) return errno; - - int pipefds[2]; - sysassert(! pipe(pipefds) ); - - int oi,io; const char *cmd; const char *stdiomode; - switch ((oflags & O_ACCMODE)) { - case O_RDONLY: oi=0; io=1; cmd="gunzip"; stdiomode="r"; break; - case O_WRONLY: oi=1; io=0; cmd="gzip"; stdiomode="w"; break; - default: abort(); - } - - sysassert( (*pid_r=fork()) != -1 ); - if (!*pid_r) { - sysassert( dup2(zfd,oi)==oi ); - sysassert( dup2(pipefds[io],io)==io ); - sysassert(! close(zfd) ); - sysassert(! close(pipefds[0]) ); - sysassert(! close(pipefds[1]) ); - execlp(cmd,cmd,gziplevel,(char*)0); - sysassert(!"execlp gzip/gunzip"); - } - sysassert(! close(zfd) ); - sysassert(! close(pipefds[io]) ); - sysassert( *f_r= fdopen(pipefds[oi], stdiomode) ); - - return 0; -} - -void gzclose(FILE **f, pid_t *p, const char *what) { - if (!*f) return; - - sysassert(!ferror(*f)); - sysassert(!fclose(*f)); - - if (*p != -1) { - char *process= masprintf("%s (de)compressor",what); - waitpid_check_exitstatus(*p,process,1); - free(process); - *p= -1; - } - - *f= 0; -}