home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
cbase.zip
/
CBASE10B.ZIP
/
CBASE.ZIP
/
CBEXP.C
< prev
next >
Wrap
Text File
|
1989-11-08
|
8KB
|
418 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "cbexp.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"
/*man---------------------------------------------------------------------------
NAME
cbexp - cbase export functions
SYNOPSIS
DESCRIPTION
SEE ALSO
cbcmp, cbimp.
DIAGNOSTICS
WARNINGS
Pointer format is implementation defined; it could possibly
contain the field delimiter ('|') or escape ('\\\\') chars. If
so, the field delimiter or escape character will have to be
changed.
------------------------------------------------------------------------------*/
/* array data type export macro */
#define vexp(FP, P, N, TYPE, EXPFCT) { \
int i = 0; \
TYPE *cp = (TYPE *)(P); \
\
for (i = 0; ; i++) { \
if ((EXPFCT)((FP), cp, (N)) == -1) return -1; \
if (i >= ((N) / sizeof(*cp) - 1)) break; \
if (fputc(EXPFLDDLM, (FP)) == EOF) return -1; \
cp++; \
} \
}
#define charexp (ucharexp)
#define charvexp (ucharvexp)
static int ucharexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
unsigned char c = *(unsigned char *)p;
switch (c) {
case EXPFLDDLM: /* export file field delimiter */
if (fputc(EXPESC, fp) == EOF) return -1;
if (fputc('F', fp) == EOF) return -1;
break; /* case EXPFLDDLM: */
case EXPESC: /* export file field escape character */
if (fputc(EXPESC, fp) == EOF) return -1;
if (fputc(EXPESC, fp) == EOF) return -1;
break; /* case EXPESC: */
#if __STDC__ == 1
case '\a': /* audible alert */
if (fputc(EXPESC, fp) == EOF) return -1;
if (fputc('a', fp) == EOF) return -1;
break; /* case '\a': */
#endif
case '\b': /* backspace */
if (fputc(EXPESC, fp) == EOF) return -1;
if (fputc('b', fp) == EOF) return -1;
break; /* case '\b': */
case '\f': /* form feed */
if (fputc(EXPESC, fp) == EOF) return -1;
if (fputc('f', fp) == EOF) return -1;
break; /* case '\f': */
case '\n': /* newline */
if (fputc(EXPESC, fp) == EOF) return -1;
if (fputc('n', fp) == EOF) return -1;
break; /* case '\n': */
case '\r': /* carriage return */
if (fputc(EXPESC, fp) == EOF) return -1;
if (fputc('r', fp) == EOF) return -1;
break; /* case '\r': */
case '\t': /* horizontal tab */
if (fputc(EXPESC, fp) == EOF) return -1;
if (fputc('t', fp) == EOF) return -1;
break; /* case '\t': */
case '\v': /* vertical tab */
if (fputc(EXPESC, fp) == EOF) return -1;
if (fputc('v', fp) == EOF) return -1;
break; /* case '\v': */
default:
if (isprint(c)) {
if (fputc(c, fp) == EOF) return -1;
} else {
if (fprintf(fp, "%c%.3o", (int)EXPESC, (int)c) < 0) {
return -1;
}
}
}
return 0;
}
static int ucharvexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, unsigned char, ucharexp);
return 0;
}
static int shortexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
SIGNED short x = 0;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%hd", (int)x) < 0) return -1;
return 0;
}
static int shortvexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, SIGNED short, shortexp);
return 0;
}
static int ushortexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
unsigned short x = 0;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%hu", (unsigned int)x) < 0) return -1;
return 0;
}
static int ushortvexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, unsigned short, ushortexp);
return 0;
}
static int intexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
SIGNED int x = 0;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%d", x) < 0) return -1;
return 0;
}
static int intvexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, SIGNED int, intexp);
return 0;
}
static int uintexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
unsigned int x = 0;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%u", x) < 0) return -1;
return 0;
}
static int uintvexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, unsigned int, uintexp);
return 0;
}
static int longexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
SIGNED long x = 0;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%ld", x) < 0) return -1;
return 0;
}
static int longvexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, SIGNED long, longexp);
return 0;
}
static int ulongexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
unsigned long x = 0;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%lu", x) < 0) return -1;
return 0;
}
static int ulongvexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, unsigned long, ulongexp);
return 0;
}
static int floatexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
float x = 0;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%G", (double)x) < 0) return -1;
return 0;
}
static int floatvexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, float, floatexp);
return 0;
}
static int doubleexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
double x = 0;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%G", x) < 0) return -1;
return 0;
}
static int doublevexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, double, doubleexp);
return 0;
}
static int ldoubleexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
LDOUBLE x = 0;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%lG", x) < 0) return -1;
return 0;
}
static int ldoublevexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, LDOUBLE, ldoubleexp);
return 0;
}
static int pointerexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
void *x = NULL;
memcpy(&x, p, sizeof(x));
if (fprintf(fp, "%p", x) < 0) return -1;
return 0;
}
static int pointervexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
vexp(fp, p, n, void *, pointerexp);
return 0;
}
static int stringexp(fp, p, n)
FILE *fp;
CONST void *p;
size_t n;
{
char *s = (char *)p;
int i = 0;
for (i = 0; i < n; i++) {
if (s[i] == '\0') {
break;
}
if (ucharexp(fp, &s[i], sizeof(*s)) == -1) {
return -1;
}
}
return 0;
}
static int binaryexp(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 *pi = (unsigned char *)p;
char fmt[24]; /* printf format string */
sprintf(fmt, "%%.%dX", hexdigits);
while (pi < ((unsigned char *)p + n)) {
if (fprintf(fp, fmt, (unsigned int)*pi++) < 0) return -1;
}
return 0;
}
/* export function table definition */
CONST cbexp_t cbexpv[] = {
charexp, /* t_char = 0 */
charvexp, /* t_charv = 1 */
ucharexp, /* t_uchar = 2 */
ucharvexp, /* t_ucharv = 3 */
shortexp, /* t_short = 4 */
shortvexp, /* t_shortv = 5 */
ushortexp, /* t_ushort = 6 */
ushortvexp, /* t_ushortv = 7 */
intexp, /* t_int = 8 */
intvexp, /* t_intv = 9 */
uintexp, /* t_uint = 10 */
uintvexp, /* t_uintv = 11 */
longexp, /* t_long = 12 */
longvexp, /* t_longv = 13 */
ulongexp, /* t_ulong = 14 */
ulongvexp, /* t_ulongv = 15 */
floatexp, /* t_float = 16 */
floatvexp, /* t_floatv = 17 */
doubleexp, /* t_double = 18 */
doublevexp, /* t_doublev = 19 */
ldoubleexp, /* t_ldouble = 20 */
ldoublevexp, /* t_ldoublev = 21 */
pointerexp, /* t_pointer = 22 */
pointervexp, /* t_pointerv = 23 */
stringexp, /* t_string = 24 */
binaryexp /* t_binary = 25 */
};