/* * This file is part of abc2ps, * Copyright (C) 1996,1997,1998 Michael Methfessel * See file abc2ps.c for details. */ #include #include #include #include "abc2ps.h" #include "util.h" /* low-level utilities */ /* ----- error warning ----- */ void wng (char msg[], char str[]) { printf ("+++ %s%s\n", msg, str); } /* ----- error exit ----- */ void rx (msg, str) char msg[],str[]; { printf ("\n+++ %s%s\n", msg, str); exit (1); } void rx1 (char msg[], char c) { printf ("\n+++ %s%c\n", msg, c); exit (1); } void rxi (msg, i) char msg[]; int i; { printf ("\n+++ %s%d\n", msg, i); exit (1); } /* ----- bug: print message for internal error and maybe stop ----- */ void bug (msg,fatal) char msg[]; int fatal; { printf ("\n\nThis cannot happen!"); if (strlen(msg)>0) printf ("\nInternal error: %s.\n", msg); if (fatal) { printf ("Emergency stop.\n\n"); exit (1); } else { printf ("Trying to continue...\n\n"); } } /* ----- ranf(x1,x2): return random float between x1 and x2 --- */ float ranf(float x1, float x2) { static int m=259200; /* generator constants */ static int a=421; static int c=54773; static int j=1; /* seed */ float r; j=(j*a+c)%m; r=x1+(x2-x1)*(double)j/(double)m; return r; } /* ----- getline ----- */ /* * Added by jc: * This routine reads a line from fp into buf, and trims away any * trailing whitespace. We are paranoid about whether isspace(c) * returns true for CR, so this routine should work even if the input * came from a DOS system. */ char * getline(buf,len,fp) char* buf; int len; FILE* fp; { char* rp; int c, l; if ((rp = fgets(buf,len,fp))) { l = strlen(buf); while ((l > 0) && (((c = buf[l-1]) && isspace(c)) || (c == '\r'))) buf[--l] = 0; } return rp; } /* ----- strip: remove leading and trailing blanks ----- */ void strip (str1,str) char str[],str1[]; { int l,i,i1,i2; l=strlen(str); i1=0; for (i=0; i=0; i--) if ((str[i]!=' ') && (str[i]!='\n')) { i2=i+1; break; } for (i=i1;i <%s>\n", l, i1, i2, str, str1);*/ } /* ----- nwords: count words in string ----- */ int nwords (str) char *str; { int w,k; char *c; c=str; w=0; for(k=0;k<=50;k++) { while (*c==' ') c++; if (*c=='\0') break; w++; while ((*c!=' ') && (*c!='\0')) c++; if (*c=='\0') break; } return w; } /* ----- getword: return n-th word from string ---- */ int getword (iw,str,str1) int iw; char *str,*str1; { int w,k; char *c,*cc; if (iw<0) { *str1='\0'; return 0;} c=str; w=0; for(k=0;k<=50;k++) { while (*c==' ') c++; if (*c=='\0') break; if (w==iw) { cc=str1; while ((*c!=' ')&&(*c!='\0')) { *cc=*c; c++; cc++; } *cc='\0'; return 1; } w++; while ((*c!=' ') && (*c!='\0')) c++; if (*c=='\0') break; } *str1='\0'; return 0; } /* ----- abbrev: check for valid abbreviation ----- */ int abbrev (str,ab,nchar) char str[],ab[]; int nchar; { int i,nc; if (strlen(str) > strlen(ab)) return 0; nc=strlen(str); if (nc0) q++; *q = 0; strcat(fid1,ext); } /* ----- cutext: cut off extension on a file identifier ----- */ void cutext (fid) char fid[]; { int i,l; l=strlen(fid); for (i=0;i') w=56.4; else if (c=='?') w=44.4; else if (c=='/') w=27.8; else if (c=='`') w=33.3; else if (c==' ') w=25.0; else w=50.0; return w/100.0; } /* ----- get_file_size ------- */ /* version using standard function stat */ #include int get_file_size (fname) char fname[]; { int m,rc; struct stat statbuf; rc = stat(fname,&statbuf); if (rc == -1) { printf ("Unsuccessful call to stat for file %s\n", fname); return -1; } m=statbuf.st_size; return m; } /* version which counts bytes by hand */ int get_file_size1 (fname) char fname[]; { int m,i; FILE *fp; if ((fp = fopen (fname,"r")) == NULL) { printf ("Cannot open file to determine size: %s", fname); return -1; } m=0; i=getc(fp); while (i != EOF) { m++; i=getc(fp); } fclose (fp); return m; }