#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "lp.h"
#define N_COMPRESSED 9999
#define DFLT_PREFIX 0
#define DFLT_SUFFIX 0
#define DFLT_NEWLINE "\n"
static char *print_prefix = DFLT_PREFIX,
*print_suffix = DFLT_SUFFIX,
*print_newline = DFLT_NEWLINE;
void
printsdn_setup(char *prefix, char *suffix, char *newline)
{
if (prefix)
print_prefix = prefix;
if (suffix)
print_suffix = suffix;
if (newline)
print_newline = newline;
return;
}
void
printsdn_unsetup ()
{
print_prefix = DFLT_PREFIX;
print_suffix = DFLT_SUFFIX;
print_newline = DFLT_NEWLINE;
return;
}
void
printsdn(FILE *fp, SCALED sdn)
{
fdprintsdn(fileno(fp), sdn);
}
void
fdprintsdn(int fd, SCALED sdn)
{
register char *dec = "9999.999",
*z;
if (sdn.val <= 0)
return;
(void)fdprintf (fd, "%s", NB(print_prefix));
if (-1000. < sdn.val && sdn.val < 10000.) {
sprintf (dec, "%.3f", sdn.val);
z = dec + strlen(dec) - 1;
while (*z == '0' && *z != '.')
z--;
if (*z == '.')
*z = '\0';
else
*++z = '\0';
(void)fdprintf(fd, "%s", dec);
} else
(void)fdprintf(fd, "%.3f", sdn.val);
if (sdn.sc == 'i' || sdn.sc == 'c')
fdputc(sdn.sc, fd);
(void)fdprintf(fd, "%s%s", NB(print_suffix), NB(print_newline));
return;
}
SCALED
_getsdn(char *str, char **p_after, int is_cpi)
{
static SCALED sdn = { 0.0 , 0 };
char * rest;
errno = 0;
if (is_cpi && STREQU(str, NAME_PICA)) {
sdn.val = 10;
sdn.sc = 0;
if (p_after)
*p_after = str + strlen(NAME_PICA);
} else if (is_cpi && STREQU(str, NAME_ELITE)) {
sdn.val = 12;
sdn.sc = 0;
if (p_after)
*p_after = str + strlen(NAME_ELITE);
} else if (is_cpi && STREQU(str, NAME_COMPRESSED)) {
sdn.val = N_COMPRESSED;
sdn.sc = 0;
if (p_after)
*p_after = str + strlen(NAME_COMPRESSED);
} else {
sdn.val = strtod(str, &rest);
if (sdn.val <= 0) {
lp_errno = LP_EBADSDN;
errno = EINVAL;
return (sdn);
}
while (*rest && *rest == ' ')
rest++;
switch (*rest) {
case 0:
sdn.sc = 0;
if (p_after)
*p_after = rest;
break;
case 'i':
case 'c':
sdn.sc = *rest++;
if (p_after)
*p_after = rest;
break;
default:
lp_errno = LP_EBADSDN;
errno = EINVAL;
sdn.sc = 0;
break;
}
}
return (sdn);
}