1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/* name: utility.c */
/* author: J. Michael Word */
/* date written: 2/26/96 */
/* purpose: utility functions */
/*****************************************************************/
/* NOTICE: This is free software and the source code is freely */
/* available. You are free to redistribute or modify under the */
/* conditions that (1) this notice is not removed or modified */
/* in any way and (2) any modified versions of the program are */
/* also available for free. */
/* ** Absolutely no Warranty ** */
/* Copyright (C) 1999 J. Michael Word */
/*****************************************************************/
#include "utility.h"
#include <stdlib.h> /*060902 needs this for exit() */
#include <stdio.h>
#include <string.h> /*060902 needs this for strlen() */
#include <ctype.h>
#ifdef NEEDSTRCASECMP
int strncasecmp(const char *buf, const char *pat, int sz) {
int rc = 0;
int i = 0;
for(; i < sz; i++) {
if (tolower(buf[i]) != tolower(pat[i])) { rc = 1; break; }
else if (buf[i] == '\0') { break; }
}
return rc;
}
int strcasecmp(const char *buf, const char *pat) {
int rc = 0;
int i = 0;
for(; buf[i] && pat[i]; i++) {
if (tolower(buf[i]) != tolower(pat[i])) { rc = 1; break; }
}
return rc;
}
#endif
void note(char *message) {
fprintf(stderr, "%s\n", message);
}
void warn(char *message) {
fprintf(stderr, "WARNING: %s\n", message);
}
void errmsg(char *message) {
fprintf(stderr, "ERROR: %s\n", message);
}
void halt(char *message) {
fprintf(stderr, "ERROR: %s\n", message);
exit(1);
}
int parseInteger(char *str, int start, int len) {
register int value = 0;
register char ch;
int neg = 0, inside = 0;
if (!str || start < 0) { return 0; }
str += start;
while((len-- > 0) && *str) {
ch = *str++;
if ((ch >='0') && (ch <= '9')) {
value = (10*value) + (ch - '0');
inside = 1;
}
else if (ch == '+' && !inside) {
inside = 1;
}
else if (ch == '-' && !inside) {
neg = 1;
inside = 1;
}
else if (isspace(ch) && !inside) { /* nothing */ }
else break; /* end of integer */
}
return (neg?-value:value);
}
float parseReal(char *str, int start, int len) {
double value = 0.0, scale = 1.0;
register char ch;
int inside = 0, infract = 0;
if (!str || start < 0) { return 0; }
str += start;
while((len-- > 0) && *str) {
ch = *str++;
if ((ch >='0') && (ch <= '9')) {
value = (10.0*value) + (ch - '0');
if (infract) { scale *= 0.1; }
inside = 1;
}
else if (ch == '+' && !inside) {
inside = 1;
}
else if (ch == '-' && !inside) {
scale = -1.0;
inside = 1;
}
else if (ch == '.' && !infract) {
infract = 1;
}
else if (isspace(ch) && !inside) { /* nothing */ }
else break; /* end of real */
}
return value*scale;
}
void copyChars(char *to, char *from, int n) {
int i;
for(i=0; i<n; i++) { to[i] = from[i]; }
}
int compArgStr(char *str, char *arg, int min) {
int i, max;
char s, a;
if (!str || !arg) return 0;
max = strlen(arg);
for(i=0; i<max; i++) {
s = toupper(str[i]);
a = toupper(arg[i]);
if (i >= min && (s == '\0' || s == '.'
|| s == '+' || s == '-' || isdigit(s))) {
break; /* good ending point */
}
else if (s != a) {
i = 0; /* failed to match */
break;
}
}
return i;
}
int nonblankstr(char *str) {
for(; *str; str++) {
if (! isspace(*str)) { return 1; }
}
return 0;
}
int nonblankrange(char *str, int start, int len) {
register int i;
for(i = 0; i < start; i++) {
if (*str++ == '\0') { return 0; }
}
for(i = 0; i < len; i++) {
if (*str == '\0') { break; }
if (! isspace(*str)) { return 1; }
str++;
}
return 0;
}
|