From a9022b5972dc49d86f617a27940fafe9c4d0e7e7 Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 9 Jun 2011 13:46:28 +0000 Subject: Initial import of (very old) vidoom sources. --- util.c | 253 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 util.c (limited to 'util.c') diff --git a/util.c b/util.c new file mode 100644 index 0000000..f8837c4 --- /dev/null +++ b/util.c @@ -0,0 +1,253 @@ +/* + + viDOOM - level editor for DOOM + + Copyright (C) 2000 Ian Cowburn (ianc@noddybox.demon.co.uk) + + 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 2 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, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + ------------------------------------------------------------------------- + + Utility functions + +*/ +static const char rcs_id[]="$Id$"; + +#include "config.h" + +#include +#include +#include "mem.h" +#include "util.h" + +#define TRIM_NO 10 +#define TRIM_MAX 80 + + +char *TrimStr(char *p, int n) +{ + static int i=0; + static char trim[TRIM_NO][TRIM_MAX+1]; + int l; + + i=(i+1)%TRIM_NO; + n=MIN(n,TRIM_MAX); + l=strlen(p); + + if (n>l) + strcpy(trim[i],p); + else + { + strncpy(trim[i],p,n); + trim[i][n-2]='.'; + trim[i][n-1]='.'; + trim[i][n]=0; + } + + return(trim[i]); +} + + +char *UnMSDOS(char *p) +{ + char *op; + + op=p; + + while(p && *p) + { + if ((*p=='\r')&&(*(p+1)=='\n')) + { + char *s1,*s2; + + s1=p; + s2=p+1; + + while((*s1++=*s2++)); + } + + p++; + } + + return(op); +} + + +char *ApplyMSDOS(char *p, int rel) +{ + char *ret; + char *t,*o; + int l; + + t=o=p; + + l=0; + + while(*t) + { + if (*t++=='\n') + l++; + + l++; + } + + ret=Grab(l+1); + t=ret; + + while(*p) + { + if (*p=='\n') + *t++='\r'; + + *t++=*p++; + } + + if (rel) + Release(o); + + return(ret); +} + + +void UCase(char *p) +{ + while(*p) + { + if (islower(*p)) + *p=toupper(*p); + p++; + } +} + + +void LCase(char *p) +{ + while(*p) + { + if (isupper(*p)) + *p=tolower(*p); + p++; + } +} + + +Byte GetByte(FILE *fp) +{ + return ((Byte)fgetc(fp)); +} + + +Word GetWord(FILE *fp) +{ + return ((Word)fgetc(fp)|((Word)fgetc(fp))<<8); +} + + +Long GetLong(FILE *fp) +{ + return ((Long)fgetc(fp)| + ((Long)fgetc(fp))<<8| + ((Long)fgetc(fp))<<16| + ((Long)fgetc(fp))<<24); +} + + +void PutByte(FILE *fp,Byte b) +{ + fputc(b,fp); +} + + +void PutShort(FILE *fp,Short s) +{ + fputc(s&0xff,fp); + fputc((s>>8)&0xff,fp); +} + + +void PutUShort(FILE *fp,UShort s) +{ + fputc(s&0xff,fp); + fputc((s>>8)&0xff,fp); +} + + +void PutLong(FILE *fp,Long l) +{ + fputc(l&0xff,fp); + fputc((l>>8)&0xff,fp); + fputc((l>>16)&0xff,fp); + fputc((l>>24)&0xff,fp); +} + + +int FRead(FILE *fp, void *buff, int size) +{ + char *p; + int tot,rd; + + tot=0; + p=buff; + + while(tot