⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cbimp.cpp

📁 CBASE v1.01 采用Borland公司TC++编写的数据库管理源程序库
💻 CPP
字号:
/*	Copyright (c) 1989 Citadel	*/
/*	   All Rights Reserved    	*/

/* #ident	"@(#)cbimp.c	1.4 - 90/06/20" */

/* ansi headers */
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
/*#include <stddef.h>*/
/*#include <string.h>*/

/* local headers */
#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(IMPFCT) {							\
	int i = 0;							\
	int nelems = n / sizeof(*cp);					\
									\
	for (i = 0; i < nelems; ++i) {					\
		if (IMPFCT(fp, cp, sizeof(*cp)) == -1) return -1;	\
		++cp;							\
	}								\
	return 0;							\
}

#define MAXFLDLEN	(1024)
static char s[MAXFLDLEN + 1];

/* getfld:  get next field from import file */
static int getfld(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] = NUL;

	return 0;
}

/* t_char -> use t_uchar import function */
#define charimp		ucharimp

/* t_charv -> use t_ucharv import function */
#define charvimp	ucharvimp

/* ucharimp:  t_uchar import function */
static int ucharimp(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 NUL:	/* premature end of field */
			return -1;
			break;	/* case NUL: */
#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;
}

/* ucharvimp:  t_ucharv import function */
static int ucharvimp(FILE *fp, const void *p, size_t n)
{
	unsigned char *cp = (unsigned char *)p;

	vimp(ucharimp);
}

/* shortimp:  t_short import function */
static int shortimp(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;
}

/* shortvimp:  t_shortv import function */
static int shortvimp(FILE *fp, const void *p, size_t n)
{
	signed short *cp = (signed short *)p;

	vimp(shortimp);
}

/* ushortimp:  t_ushort import function */
static int ushortimp(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;
}

/* ushortvimp:  t_ushortv import function */
static int ushortvimp(FILE *fp, const void *p, size_t n)
{
	unsigned short *cp = (unsigned short *)p;

	vimp(ushortimp);
}

/* intimp:  t_int import function */
static int intimp(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;
}

/* intvimp:  t_intv import function */
static int intvimp(FILE *fp, const void *p, size_t n)
{
	signed int *cp = (signed int *)p;

	vimp(intimp);
}

/* uintimp:  t_uint import function */
static int uintimp(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;
}

/* uintvimp:  t_uintv import function */
static int uintvimp(FILE *fp, const void *p, size_t n)
{
	unsigned int *cp = (unsigned int *)p;

	vimp(uintimp);
}

/* longimp:  t_long import function */
static int longimp(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;
}

/* longvimp:  t_longv import function */
static int longvimp(FILE *fp, const void *p, size_t n)
{
	signed long *cp = (signed long *)p;

	vimp(longimp);
}

/* ulongimp:  t_ulong import function */
static int ulongimp(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;
}

/* ulongvimp:  t_ulongv import function */
static int ulongvimp(FILE *fp, const void *p, size_t n)
{
	unsigned long *cp = (unsigned long *)p;

	vimp(ulongimp);
}

/* floatimp:  t_float import function */
static int floatimp(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;
}

/* floatvimp:  t_floatv import function */
static int floatvimp(FILE *fp, const void *p, size_t n)
{
	float *cp = (float *)p;

	vimp(floatimp);
}

/* dblimp:  t_ldouble import function */
static int dblimp(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;
}

/* dblvimp:  t_doublev import function */
static int dblvimp(FILE *fp, const void *p, size_t n)
{
	double *cp = (double *)p;

	vimp(dblimp);
}

/* ldblimp:  t_ldouble import function */
static int ldblimp(FILE *fp, const void *p, size_t n)
{
	if (getfld(fp) == -1) {
		return -1;
	}
#ifdef AC_LDOUBLE
	if (sscanf(s, "%Lg", (long double *)p) != 1) return -1;
#endif
	return 0;
}

/* ldblimp:  t_ldouble import function */
static int ldblvimp(FILE *fp, const void *p, size_t n)
{
#ifdef AC_LDOUBLE
	long double *cp = (long double *)p;
#else
	double *cp = (double *)p;
#endif
	vimp(ldblimp);
}

/* ptrimp:  t_pointer import function */
static int ptrimp(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;
}

/* ptrvimp:  t_pointerv import function */
static int ptrvimp(FILE *fp, const void *p, size_t n)
{
	void **cp = (void **)p;

	vimp(ptrimp);
}

/* strimp:  t_string import function */
static int strimp(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 == NUL) {
			*((char *)p + i) = NUL;
			break;
		}
		if (c == EXPESC) {
			switch (*si) {
			case NUL:	/* premature end of field */
				return -1;
				break;	/* case NUL: */
#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((char *)si, "%3o", &c) != 1) return -1;
					si += 2;
				} else {
					c = *si;
				}
				break;	/* default: */
			}
			++si;
		}
		*((char *)p + i) = c;
	}
	*((char *)p + n - 1) = NUL;

	return 0;
}

/* t_cistring -> use t_string import function */
#define cistrimp	strimp

/* binimp:  t_binary import function */
static int binimp(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((char *)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 */
	dblimp,			/* t_double	= 18 */
	dblvimp,		/* t_doublev	= 19 */
	ldblimp,		/* t_ldouble	= 20 */
	ldblvimp,		/* t_ldoublev	= 21 */
	ptrimp,			/* t_pointer	= 22 */
	ptrvimp,		/* t_pointerv	= 23 */
	strimp,			/* t_string	= 24 */
	cistrimp,		/* t_cistring	= 25 */
	binimp			/* t_binary	= 26 */
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -