summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2011-06-09 13:46:28 +0000
committerIan C <ianc@noddybox.co.uk>2011-06-09 13:46:28 +0000
commita9022b5972dc49d86f617a27940fafe9c4d0e7e7 (patch)
tree61405aa4ade91ed1057f863ddf118ceb38e14f8e /util.c
Initial import of (very old) vidoom sources.
Diffstat (limited to 'util.c')
-rw-r--r--util.c253
1 files changed, 253 insertions, 0 deletions
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 <string.h>
+#include <ctype.h>
+#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<size)
+ {
+ rd=fread(p+tot,1,size-tot,fp);
+
+ if (rd<1)
+ return(-1);
+
+ tot+=rd;
+ }
+
+ return(tot);
+}
+
+
+int FWrite(FILE *fp, void *buff, int size)
+{
+ char *p;
+ int tot,wr;
+
+ tot=0;
+ p=buff;
+
+ while(tot<size)
+ {
+ wr=fwrite(p+tot,1,size-tot,fp);
+
+ if (wr<1)
+ return(-1);
+
+ tot+=wr;
+ }
+
+ return(tot);
+}
+
+
+long FLen(FILE *fp)
+{
+ long orig,off;
+
+ orig=ftell(fp);
+ fseek(fp,0,SEEK_END);
+ off=ftell(fp);
+ fseek(fp,orig,SEEK_SET);
+
+ return(off);
+}
+
+
+/* END OF FILE */