home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
cbase.zip
/
CBASE10B.ZIP
/
CBASE.ZIP
/
CBIMP.C
< prev
next >
Wrap
Text File
|
1989-11-08
|
9KB
|
507 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "cbimp.c 1.2 - 89/11/08" */
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
/*#include <stddef.h>*/
/*#include <string.h>*/
#include "cbase_.h"
#ifndef isodigit
#define isodigit(c) (((c >= '0') && (c <= '7')) ? 1 : 0)
#endif
/*man---------------------------------------------------------------------------
NAME
cbimp - import cbase data
SYNOPSIS
DESCRIPTION
SEE ALSO
cbcmp, cbexp.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise,
a value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
/* array data type import macro */
#define vimp(FP, P, N, TYPE, IMPFCT) { \
int i = 0; \
TYPE *cp = (TYPE *)(P); \
\
for (i = 0; i < ((N) / sizeof(*cp) - 1); i++) { \
if ((IMPFCT)((FP), cp, (N)) == -1) return -1; \
cp++; \
} \
}
#define MAXFLDLEN (1024)
static char s[MAXFLDLEN + 1];
static int getfld(fp)
FILE *fp;
{
int i = 0;
int c = 0;
memset(s, 0, sizeof(s));
for (i = 0; ; i++) {
c = fgetc(fp);
if (ferror(fp)) {
return -1;
}
if (feof(fp)) {
break;
}
if ((c == EXPFLDDLM) || (c == EXPRECDLM)) {
break;
}
if (i >= sizeof(s) - 1) {
return -1;
}
s[i] = c;
}
s[i] = '\0';
return 0;
}
#define charimp (ucharimp)
#define charvimp (ucharvimp)
static int ucharimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
int c = 0;
if (getfld(fp) == -1) {
return -1;
}
c = (unsigned char)s[0];
if (c == EXPESC) {
switch ((unsigned char)s[1]) {
case '\0': /* premature end of field */
return -1;
break; /* case '\0': */
#if __STDC__ == 1
case 'a': /* audible alert */
c = '\a';
break; /* case 'a': */
#endif
case 'b': /* backspace */
c = '\b';
break; /* case 'b': */
case 'f': /* form feed */
c = '\f';
break; /* case 'f': */
case 'n': /* newline */
c = '\n';
break; /* case 'n': */
case 'r': /* carriage return */
c = '\r';
break; /* case 'r': */
case 't': /* horizontal tab */
c = '\t';
break; /* case 't': */
case 'v': /* vertical tab */
c = '\v';
break; /* case 'v': */
default: /* */
if (isodigit(s[1])) {
if (!isodigit(s[2]) || !isodigit(s[3])) return -1;
if (sscanf(s, "%3o", &c) != 1) return -1;
} else {
c = (unsigned char)s[1];
}
break; /* default: */
}
}
*(unsigned char *)p = c;
return 0;
}
static int ucharvimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, unsigned char, ucharimp);
return 0;
}
static int shortimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%hd", (short *)p) != 1) return -1;
return 0;
}
static int shortvimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, SIGNED short, shortimp);
return 0;
}
static int ushortimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%hu", (unsigned short *)p) != 1) return -1;
return 0;
}
static int ushortvimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, unsigned short, ushortimp);
return 0;
}
static int intimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%d", (int *)p) != 1) return -1;
return 0;
}
static int intvimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, SIGNED int, intimp);
return 0;
}
static int uintimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%u", (unsigned int *)p) != 1) return -1;
return 0;
}
static int uintvimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, unsigned int, uintimp);
return 0;
}
static int longimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%ld", (long *)p) != 1) return -1;
return 0;
}
static int longvimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, SIGNED long, longimp);
return 0;
}
static int ulongimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%lu", (unsigned long *)p) != 1) return -1;
return 0;
}
static int ulongvimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, unsigned long, ulongimp);
return 0;
}
static int floatimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%g", (float *)p) != 1) return -1;
return 0;
}
static int floatvimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, float, floatimp);
return 0;
}
static int doubleimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%lg", (double *)p) != 1) return -1;
return 0;
}
static int doublevimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, double, doubleimp);
return 0;
}
static int ldoubleimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%Lg", (LDOUBLE *)p) != 1) return -1;
return 0;
}
static int ldoublevimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, LDOUBLE, ldoubleimp);
return 0;
}
static int pointerimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
if (getfld(fp) == -1) {
return -1;
}
if (sscanf(s, "%p", (void **)p) != 1) return -1;
return 0;
}
static int pointervimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vimp(fp, p, n, void *, pointerimp);
return 0;
}
static int stringimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
unsigned char *si = NULL;
int i = 0;
int c = 0;
if (getfld(fp) == -1) {
return -1;
}
/* initialize return */
memset(p, 0, n);
/* convert */ /* string will be truncated if too long */
si = (unsigned char *)s;
for (i = 0; i < n - 1; i++) {
c = *si++;
if (c == '\0') {
*((char *)p + i) = '\0';
break;
}
if (c == EXPESC) {
switch (*si) {
case '\0': /* premature end of field */
return -1;
break; /* case '\0': */
#if __STDC__ == 1
case 'a': /* audible alert */
c = '\a';
break; /* case 'a': */
#endif
case 'b': /* backspace */
c = '\b';
break; /* case 'b': */
case 'f': /* form feed */
c = '\f';
break; /* case 'f': */
case 'n': /* newline */
c = '\n';
break; /* case 'n': */
case 'r': /* carriage return */
c = '\r';
break; /* case 'r': */
case 't': /* horizontal tab */
c = '\t';
break; /* case 't': */
case 'v': /* vertical tab */
c = '\v';
break; /* case 'v': */
default: /* */
if (isodigit(*si)) {
if (!isodigit(*(si + 1)) || !isodigit(*(si + 2))) return -1;
if (sscanf(si, "%3o", &c) != 1) return -1;
si += 2;
} else {
c = *si;
}
break; /* default: */
}
si++;
}
*((char *)p + i) = c;
}
*((char *)p + n - 1) = '\0';
return 0;
}
static int binaryimp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
/* calculate number of hex digits for each char
# bits in char == CHAR_BIT, # bits in hex digit == 4 */
CONST int hexdigits = (CHAR_BIT + (4 - 1)) / 4;
unsigned char *si = (unsigned char *)s;
unsigned char *pi = (unsigned char *)p;
char fmt[24]; /* printf format string */
int c = 0;
sprintf(fmt, "%%%dX", hexdigits);
if (getfld(fp) == -1) {
return -1;
}
/* initialize return */
memset(p, 0, n);
while (1) {
if (si - (unsigned char *)s >= sizeof(s) - hexdigits) break;
if (pi - (unsigned char *)p >= n) break;
if (sscanf(si, fmt, &c) != 1) return -1;
si += hexdigits;
*pi++ = (unsigned char)c;
}
return 0;
}
/* import function table definition */
CONST cbimp_t cbimpv[] = {
charimp, /* t_char = 0 */
charvimp, /* t_charv = 1 */
ucharimp, /* t_uchar = 2 */
ucharvimp, /* t_ucharv = 3 */
shortimp, /* t_short = 4 */
shortvimp, /* t_shortv = 5 */
ushortimp, /* t_ushort = 6 */
ushortvimp, /* t_ushortv = 7 */
intimp, /* t_int = 8 */
intvimp, /* t_intv = 9 */
uintimp, /* t_uint = 10 */
uintvimp, /* t_uintv = 11 */
longimp, /* t_long = 12 */
longvimp, /* t_longv = 13 */
ulongimp, /* t_ulong = 14 */
ulongvimp, /* t_ulongv = 15 */
floatimp, /* t_float = 16 */
floatvimp, /* t_floatv = 17 */
doubleimp, /* t_double = 18 */
doublevimp, /* t_doublev = 19 */
ldoubleimp, /* t_ldouble = 20 */
ldoublevimp, /* t_ldoublev = 21 */
pointerimp, /* t_pointer = 22 */
pointervimp, /* t_pointerv = 23 */
stringimp, /* t_string = 24 */
binaryimp /* t_binary = 25 */
};