Category : Files from Magazines
Archive   : CGAZV3N3.ZIP
Filename : TENKEY.C

 
Output of file : TENKEY.C contained in archive : CGAZV3N3.ZIP
/****** read10key **************************************************
* Purpose: accept a numeric field entry from right to left,
* displaying commas and decimal point as data is entered.
* It returns exit key, or zero if field is full
*
* Author: Hannah Lindt
*
* Usage: declare as: int read10key(long *, int);
* invoke as: length=read10key(&value, maxwidth);
* Sample usage given in test driver
*
* Limits: The routine works with the value in pennies (i.e., *100),
* and stores this in a long. The
* largest value it can accommodate is $21,474,836.47.
*
* Compilers: verified for TurboC 2.0, MSC 5.1,
* DeSmet 3.03 (uses pcio.a routines)
* Memory models: all
* Compile time switches:
* DEBUG compiles in a test driver
* __DESMET__ compiles for DeSmet
*
* Source code may be freely used if source is acknowledged.
* Object code may be freely used.
******************************************************************/

# define DEBUG

# ifdef __DESMET__ /* use macros to access DeSmet equivalents */
# define getch ci
# define putch scr_co
int ci();
void scr_co();
# else /* Turbo C and MSC */
# include
# endif

# include

# define SPACE ' ' /* space char */
# define BELL '\7' /* the bell for errors */
# define BS '\b' /* backspace */
# define Delete 83 /* the main byte, aux byte sequence 0,83
means we saw the "del" key */
# define MAXSTR 30 /* maximum size of input string. It must allow
for the max width of the input field,
including commas and decimal point. */

# ifdef __DESMET__
int ltodec();
# else
int ltodec(long, char*, int); /* display converter */
#endif

int read10key(value, width)
long *value; /* result -- also contains initial value */
int width;
{
char str[MAXSTR], /* display work area */
*s; /* work pointer */
int pad, /* number of leading blanks */
len, /* current len of str */
i, /* counter */
negative = 0, /* hold sign of result */
ch, ch_aux; /* character read */

while(1)
{
len = ltodec(*value, str, negative); /* format str */
pad = width - len; /* number of pad spaces */
while(pad-- > 0) /* pad with */
putch(SPACE); /* spaces */
for (s=str; *s; s++) /* write string */
putch(*s);
if(len >= width) /* if field full */
return 0;
ch = getch(); /* get char */
if (ch == 0) /* start of a main byte, aux byte seq */
ch_aux = getch(); /* get aux byte */

if(isdigit(ch)) {
*value = *value * 10 + (ch - '0'); /* accum val */
if (*value < 0) { /* oops - too big! */
*value = 0;
putch(BELL); /* ring the bell */
}
}
else if (ch == '-')
negative = !negative; /* change sign */
else if (ch == BS)
*value /= 10; /* strip off a digit */
else if (ch == 0 && ch_aux == Delete) {
*value = 0; /* clear whole entry */
negative = 0;
}
else {
if (negative)
*value = -*value; /* set sign of return value */
return ch; /* return terminating character */
}

for (i=0; i putch(BS);
}

}

/*
** ltodec -- convert long to comma and period separated decimal string
**
** returns length of string
*/

int ltodec(val, str, negative)
long val; /* value to be converted */
char *str; /* location to store string */
int negative; /* is 1 if value is negative */
{
char digs[MAXSTR+1],/* string representation of val */
*f = digs, /* work */
*t = digs, /* ptrs */
sep = '.'; /* separator */
int dlen = 0, /* length of digs string */
flen, /* length of str string */
sdig; /* separator digit count */

while( val )
{ /* convert val */
*t++ = ( val % 10 ) + '0'; /* to string */
val /= 10; /* in reverse order */
}

for( dlen = t - digs; dlen < 3; dlen++ ) /* if less than 3 digits */
*t++ = '0'; /* pad with zeroes */

flen = dlen + (dlen / 3) + negative;
t = str + flen; /* point to end of string */
*t-- = 0; /* terminate string */
for(sdig = 1; dlen; dlen--, sdig++)
{ /* while there are digits */
if(sdig == 3)
{ /* every 3rd digit */
*t-- = sep; /* put in separator */
sdig = 0; /* reset count */
sep = ','; /* easier than test */
}
*t-- = *f++; /* xfer digit */
}
if (negative) /* check flag */
*t-- = '-';

return flen; /* string width */
}


# ifdef DEBUG

# include

main()
{
long val = 0;

# ifdef __TURBOC__
directvideo = 1;
# endif

fputs("\nenter value ==> ", stdout);
read10key(&val, 15); /* a width of 15 allows for values in the range
-$21,474,836.47 to $21,474,836.47. These are
the max & min values the routine will handle */

printf("\nValue entered was %ld\n", val);

}

# endif


  3 Responses to “Category : Files from Magazines
Archive   : CGAZV3N3.ZIP
Filename : TENKEY.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/