home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
cbase.zip
/
CBASE10B.ZIP
/
ROLODECK.ZIP
/
CVTSS.C
next >
Wrap
Text File
|
1989-10-31
|
6KB
|
249 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "cvtss.c 1.2 - 89/10/31" */
#if __STDC__ == 1
#include <ctype.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CONST const
#else
#include <ctype.h>
#include <errno.h>
void *calloc(); /* stdlib.h */
void free();
#include <stdio.h>
#include <string.h>
#include <sys/types.h> /* size_t typedef */
#define CONST
#endif
/* m macros */
#define CVT_0 (0x0001) /* trim the parity bit */
#define CVT_X_SP (0x0002) /* discard all spaces and tabs */
#define CVT_X_CTL (0x0004) /* discard all control characters */
#define CVT_X_LEADSP (0x0008) /* discard leading spaces and tabs */
#define CVT_ONE_SP (0x0010) /* reduce spaces, tabs to one space */
#define CVT_UPPER (0x0020) /* convert lowercase to uppercase */
#define CVT_6 (0x0040) /* convert [ to ( and ] to ) */
#define CVT_X_TRAILSP (0x0080) /* discard trailing spaces and tabs */
#define CVT_QUOTES (0x0100) /* do not alter chars inside quotes */
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
/* macro to preserve characters within quotes */
#define QUOTES { \
if (quotes && (*ps == '\"')) { \
*pt++ = *ps++; \
while ((*ps != '\0') && (pt < (tt + n - 1))) { \
*pt++ = *ps; \
if (*ps++ == '\"') break; \
} \
continue; \
} \
}
/*man---------------------------------------------------------------------------
NAME
cvtss - convert string
SYNOPSIS
int cvtss(t, s, m, n)
char *t;
const char *s;
int m;
int n;
DESCRIPTION
The cvtss function takes the source string pointed to by s,
performs the conversions indicated by m, and places the result in
the target string pointed to by t. n is the size of the target
string. At most n - 1 characters are placed in t, terminated by
a '\0'.
Values for m are constructed by bitwise OR-ing flags from the
following list.
CVT_X_SP 0x0002 Discard all spaces and tabs.
CVT_X_CTL 0x0004 Discard all control characters.
CVT_X_LEADSP 0x0008 Discard leading spaces and tabs.
CVT_ONE_SP 0x0010 Reduce spaces and tabs to one space.
CVT_UPPER 0x0020 Convert lowercase to uppercase.
CVT_X_TRAILSP 0x0080 Discard trailing spaces and tabs.
CVT_QUOTES 0x0100 Do not alter characters inside quotes.
cvtss will fail if one or more of the following is true:
[EINVAL] t or s is the NULL pointer.
[EINVAL] n is less than 1.
[ENOMEM] Not enough memory is available for
allocation by the calling process.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise,
a value of -1 is returned, and errno set to indicate the error.
NOTES
cvtss is adapted directly from the BASIC function CVT$$.
------------------------------------------------------------------------------*/
int cvtss(t, s, m, n)
char *t;
CONST char *s;
int m;
int n;
{
int ns = 0; /* length of s including '\0' */
char *ts = 0; /* temporary source string */
char *tt = 0; /* temporary target string */
int quotes = 0; /* preserve quoted characters flag */
int bit = 0; /* bit number */
char *ps = NULL; /* pointer into ts */
char *pt = NULL; /* pointer into tt */
int flag = 0; /* generic flag */
/* validate arguments */
if ((t == NULL) || (s == NULL) || (n < 1)) {
errno = EINVAL;
return -1;
}
/* find size for ts */
ns = strlen(s) + 1;
/* create temporary strings */
ts = (char *)calloc((size_t)ns, (size_t)1);
if (ts == NULL) {
errno = ENOMEM;
return -1;
}
tt = (char *)calloc((size_t)n, (size_t)1);
if (tt == NULL) {
free(ts);
errno = ENOMEM;
return -1;
}
/* initialize ts with s */
strncpy(ts, s, ns);
ts[ns - 1] = 0;
/* perform conversions */
quotes = m & CVT_QUOTES; /* set preserve quoted chars flag */
for (bit = 0; m != 0; bit++) {
if (!(m & 1)) {
m >>= 1;
continue;
}
m >>= 1;
switch (bit) {
case 0: /* trim the parity bit */
/* not implemented */
break; /* case 0: */
case 1: /* discard all spaces and tabs */
ps = ts;
pt = tt;
while ((*ps != '\0') && (pt < (tt + n - 1))) {
QUOTES;
if (!((*ps == ' ') || (*ps == '\t'))) {
*pt++ = *ps;
}
ps++;
}
*pt = '\0';
break; /* case 1: */
case 2: /* discard all control characters */
ps = ts;
pt = tt;
while ((*ps != '\0') && (pt < (tt + n - 1))) {
QUOTES;
if (!iscntrl(*ps)) {
*pt++ = *ps;
}
ps++;
}
*pt = '\0';
break; /* case 2: */
case 3: /* discard leading spaces and tabs */
for (ps = ts; *ps != '\0'; ps++) {
if ((*ps != ' ') && (*ps != '\t')) {
break;
}
}
strncpy(tt, ps, n);
tt[n - 1] = '\0';
break; /* case 3: */
case 4: /* reduce spaces and tabs to one space */
ps = ts;
pt = tt;
flag = 0;
while ((*ps != '\0') && (pt < (tt + n - 1))) {
QUOTES;
if (flag) {
if ((*ps != ' ') && (*ps != '\t')) {
*pt++ = *ps;
flag = 0;
}
} else {
*pt++ = *ps;
if ((*ps == ' ') || (*ps == '\t')) {
flag = 1;
}
}
ps++;
}
*pt = '\0';
break; /* case 4: */
case 5: /* convert lowercase to uppercase */
ps = ts;
pt = tt;
while ((*ps != '\0') && (pt < (tt + n - 1))) {
QUOTES;
*pt++ = toupper(*ps++);
}
*pt = '\0';
break; /* case 5: */
case 6: /* convert [ to ( and ] to ) */
/* not implemented */
break; /* case 6: */
case 7: /* discard trailing spaces and tabs */
for (ps = ts + strlen(ts) - 1; (ps >= ts); ps--) {
if ((*ps != ' ') && (*ps != '\t')) {
break;
}
}
strncpy(tt, ts, n);
tt[min(n - 1, ps + 1 - ts)] = '\0';
break; /* case 7: */
case 8: /* do not alter characters inside quotes */
continue;
break; /* case 8: */
default:
free(tt);
free(ts);
errno = EINVAL;
return -1;
break;
}
strncpy(ts, tt, ns);
ts[ns - 1] = 0;
}
/* load target string */
strncpy(t, ts, n);
t[n - 1] = '\0';
/* free temporary strings */
free(tt);
free(ts);
errno = 0;
return 0;
}