/* A bunch of utility functions and such, most of which are probably * already part of the GNU C library. Created for my own personal * enlightenment, and distributed under the terms of the GNU GPL. * While I hope that this software will prove useful, it is distributed * without _any_ warranty whatsoever. Happy hacking. * * -=ninjadroid=- */ #include #include "utils.h" #define ALLOC 50000 #define MAXLEN 1000 #define CHBUFLEN 100 char chbuf[CHBUFLEN] = "\0"; int chbufi = 0; /* returns 0 if s==t, <0 if s0 if s>t */ int strcom(char *s, char *t) { for( ; *s == *t; s++, t++) if (*s == '\0') return 0; return *s - *t; } /* strcmp() */ /* returns 0 if s==t, -1 if st */ int numcom(char *s, char *t) { double ds, dt; ds = atof(s); dt = atof(t); if (ds == dt) return 0; return (ds > dt) ? 1 : -1; } /* numcom() */ /* converts a character-string into a floating point number */ double atof(char *s) { double val; int power = 1; char sign, i; sign = 1; if (*s == '-') { sign = -1; s++; } for (val = 0; isdigit(*s); s++) val = 10 * val + (*s - '0'); if (*s == '.') { s++; for (power = 1; isdigit(*s); s++) { val = 10 * val + (*s - '0'); power *= 10; } } val = val / power * sign; if (*s == 'e' || *s == 'E') { s++; if (*s == '-') { s++; for ( ; isdigit(*s); s++) { for (i = 1; isdigit(*s - i); i++) val /= 10; } } else { for ( ; isdigit(*s); s++) { for (i = 1; isdigit(*s - i); i++) val *= 10; } } } return val; } /* atof() */ /* returns 1 if c is a digit, else 0 */ int isdigit(char c) { return (c >= '0' && c <= '9') ? 1 : 0; } /* isdigit() */ /* swap two pointers */ void pswap(void **v, void **w) { void *p; p = *v; *v = *w; *w = p; } /* pswap() */ /* puts line, max length len, into s, returning length */ int getline(char *s, int len) { char c; int i; for (i = 0; i < len-1 && (c = getchar()) != EOF && c != '\n'; i++) s[i] = c; if (c == '\n') s[i++] = '\n'; s[i++] = '\0'; return i; } /* getline() */ /* a brain dead version of malloc() */ char *alloc(int len) { static char allocbuf[ALLOC]; static int index = 0; if (index + len < ALLOC) { index += len; return &allocbuf[index - len]; } return NULL; } /* alloc() */ /* fetch lines into lineptr, return number of lines */ int readlines(char *lineptr[], int maxlines) { int len, nlines; char *p, line[MAXLEN]; nlines = 0; while ((len = getline(line, MAXLEN)) > 1) { if (nlines >= maxlines || (p = alloc(len)) == NULL) return -1; else { strcopy(p, line); lineptr[nlines++] = p; } } return nlines; } /* readlines() */ /* print lines to screen */ void writelines(char *lineptr[], int maxlines) { int i; for (i = 0; i < maxlines; i++) printf("%s", lineptr[i]); } /* writelines() */ /* copys string t into string s */ void strcopy(char *s, char *t) { while ((*s++ = *t++) != '\0'); } /* strcpy() */ /* return c converted to upper case */ int toupper(char c) { if (c >= 'a' && c <= 'z') return c + ('A' - 'a'); /* otherwise... */ return c; } /* toupper() */ int isnum(char c) { if (c >= '0' && c <= '9') return 1; return 0; } /* toupper() */ int getch() { int val; if (chbuf[chbufi] != '\0') { val = chbuf[chbufi]; chbuf[chbufi] = '\0'; chbufi -= (chbufi == 0) ? 0 : 1; } else { val = getchar(); } return val; } /* getch() */ int ungetch(char c) { if (chbufi == CHBUFLEN - 1) return -1; else { chbuf[++chbufi] = c; return 0; } } int isalpha(char c) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return 1; return 0; } /* isalpha() */ int isalnum(char c) { if (isalpha(c) || isnum(c)) return 1; return 0; } /* isalnum() */ int isspace(char c) { if (c == '\t' || c == ' ' || c == '\n') return 1; return 0; } /* isspace() */ char *strcat(char *s, char *ct) { char *t = s; /* get to the end of s */ while (*s) s++; /* concatenate ct to the end of s */ while ((*s++ = *ct++)); /* return a pointer to s */ return t; } /* returns 1 if string t occurs at the end of s, zero otherwise */ int strend(char *s, char *t) { char *tp = t; /* you know the devil's in my soul */ while (*s) { if (*s == *tp) { for (t = tp; *s == *t && *t != '\0'; s++, t++) ; if (*t == '\0' && *s == '\0') return 1; } else s++; } return 0; } void strncpy(char *s, char *t, int n) { /* copy n chars of t into s */ while (t < (t+n) && *t != '\0') *s++ = *t++; } char *strncat(char *s, char *t, int n) { char *sp = s; /* find end of s */ while (*s) s++; /* copy n chars of t into s */ while (t < (t+n) && *t != '\0') *s++ = *t++; /* return a pointer to s */ return sp; } int strcmp(char *s, char *t, int n) { /* compare first n chars of s and t */ while (*s == *t && s < (s+n)) { if (*s == '\0') return 0; s++; t++; } /* return difference between s and t */ return *s - *t; }