/* * This file is part of abc2ps, Copyright (C) 1996,1997 Michael Methfessel * See file abc2ps.c for details. */ #include #include #include #include #include #include #include "abc2ps.h" #include #ifdef _MSVC #include #endif /* low-level utilities */ /* case insensitive compare for unix */ #ifndef _MSVC int stricmp(const char *p1, const char *p2) { if (!(p1 || p2)) return 0; if (!p1 && p2) return 1; if (p1 && !p2) return -1; while (*p1 && *p2) { if (tolower(*p2) < tolower(*p1)) { return -1; } else if (tolower(*p2) > tolower(*p1)) { return 1; } p1++; p2++; } /* one or both strings has run out */ if (!(*p1 || *p2)) { return 0; } else if (!*p1) { return 1; } else { return -1; } } #endif /* string arena */ static struct str_a { char str[1000]; /* area */ char *p; /* pointer in area */ struct str_a *n; /* next area */ short r; /* remaining space in area */ } *str_r, *str_p; /* root and current area pointers */ /* ----- error warning ----- */ void wng(char msg[], char str[]) { printf("++++ %s%s\n", msg, str); } /* ----- error exit ----- */ void rx(char msg[], char str[]) { printf("\n++++ %s%s\n", msg, str); exit(1); } /* ----- bug: print message for internal error and maybe stop ----- */ void bug(char msg[], int fatal) { printf("\n\nThis cannot happen!"); if (msg[0] != '\0') printf("\nInternal error: %s.\n", msg); if (fatal) { printf("Emergency stop.\n\n"); exit(1); } printf("Trying to continue...\n\n"); } /* ----- clrarena: clear the string arena ----- */ void clrarena(void) { str_p = 0; } /* ----- getarena: get a string area ----- */ char *getarena(int len) { char *p; len++; /* room for EOS */ if (str_p == 0 || str_p->r < len) { if (str_p == 0) { if (str_r == 0) { str_r = calloc(1, sizeof *str_r); str_p = str_r; } else str_p = str_r; } else { if (str_p->n == 0) str_p->n = calloc(1, sizeof *str_r); str_p = str_p->n; } str_p->p = str_p->str; str_p->r = sizeof str_p->str; } p = str_p->p; str_p->p += len; str_p->r -= len; return p; } /* ----- 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 */ j = (j * a + c) % m; return (float)(x1 + (x2 - x1) * (double) j / (double) m); } /* ----- strip: allocate memory and remove leading and trailing blanks ----- */ char *strip(char *str) { int l,i,i1,i2; char *p; l=strlen(str); p = getarena(l); i1=0; for (i=0; i= 0; ) if (!isspace(str[i])) { i2=i+1; break; } strncpy(p, &str[i1], i2 - i1); p[i2-i1]=0; /* printf(" l=%d i1=%d i2=%d <%s> <%s>\n", l, i1, i2, str, str1);*/ return p; } /* ----- nwords: count words in string ----- */ int nwords(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(int iw, char *c, char *str1) { int w,k; char *cc; if (iw<0) { *str1='\0'; return 0; } 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++; *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(char str[], char ab[], int nchar) { int nc; nc = strlen(str); if (nc > (int)strlen(ab)) return 0; if (nc') w=(float)56.4; else if (c=='?') w=(float)44.4; else if (c=='/') w=(float)27.8; else if (c=='`') w=(float)33.3; else if (c==' ') w=(float)25.0; else w=(float)50.0; return w/(float)100.0; } /* ----- get_file_size ------- */ /* version using standard function stat */ int get_file_size(char fname[]) { struct stat statbuf; if (stat(fname,&statbuf) < 0) { printf("Unsuccessful call to stat for file %s\n", fname); return -1; } return statbuf.st_size; } #if 0 /* version which counts bytes by hand */ int get_file_size1(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; } #endif