📄 dprt100.c
字号:
/************************************************************************
** MODULE INFORMATION*
**********************
** FILE NAME: dprt100.c
** SYSTEM NAME:
** ORIGINAL AUTHOR(S): Paul Lemmers
** VERSION NUMBER:
** CREATION DATE: 1990/8/14
**
** DESCRIPTION:
** This file contains the format function for all
** printf variants.
**
*************************************************************************
** CHANGES INFORMATION **
*************************
** REVISION: $Revision: 1.2 $
** WORKFILE: $Workfile: dprt100.c $
** LOGINFO: $Log: D:/CPROG/MYDEV/DEVLIB/VCS/DPRT100.C_V $
**
** Rev 1.2 17 Dec 1990 14:34:32 PAUL
** os2def.h changes
**
** Rev 1.1 09 Dec 1990 19:07:04 PAUL
** Adaptions for MSC 6.00
**
** Rev 1.0 14 Aug 1990 14:26:34 PAUL
** Initial revision.
*************************************************************************/
#if ! defined(PRD)
static char _pvcs_hdr[] =
"$Header: D:/CPROG/MYDEV/DEVLIB/VCS/DPRT100.C_V 1.2 17 Dec 1990 14:34:32 PAUL $";
#endif
/* my mixed model variant for varargs.h */
#define va_arg(ap,t) ((t far *)(ap += sizeof(t)))[-1]
#include <os2def.h>
#include "devlib.h"
/**************************************************************
** NAME: format
** SYNOPSIS: unsigned format(dest, fmt, args)
** char far *dest;
** const char far *fmt;
** char far *args;
** DESCRIPTION:
** This is the main function for formatting of strings.
** It uses a pointer to the arguments that have to
** be formatted in the fmt string. NOT an argument list!
** Supports:
** NO: '-', '+', ' ', and '#'
** NO: width.precision
** MODEL: only F for far (N is default!!!!)
** PREFIX: only l for long
** TYPE: only c, d, o, s, u and x
** Yes, yes it supports a lot more, as long as it is not
** a flag and not a float, but it has not been tested!
** SO, it does not support it....
** BUGS:
** precision for string is incorrect (no trunc!!)
** RETURNS: As sprintf()
**************************************************************/
unsigned format(char far *dest, const char far *fmt, char far *args)
{
char c;
char fillc; /* filling character */
unsigned len = 0; /* total length of returned string */
union {
char ch;
short int i;
void near *n;
long l;
void far *p;
} value; /* union for value in args */
int width;
int prec;
int rj; /* right justify flag */
int slen; /* length of data in s */
char s[40]; /* local storage to format numbers etc. */
while ( (c = *fmt++) != 0 )
{
switch ( c )
{
case '%':
/* Yes this is a format */
prec = 1;
fillc = ' ';
rj = 1;
if ( (c=*fmt) == '-' )
{
rj = 0; /* left justify */
c = *fmt++;
}
if ( c == '0' )
fillc = '0';
for ( width=0 ; ((c=*fmt++) >= '0') && c<='9' ; )
width = 10*width + c - '0';
/* width 0..0xFFFFh */
if ( c == '.' )
{
for ( prec=0 ; ((c=*fmt++) >= '0') && c<='9' ; )
prec = 10*prec + c - '0';
if ( prec <= 0 )
prec = 1;
/* prec 1..0x7FFFh */
}
/* now get arg into value */
switch ( c )
{
case 'l':
case 'F':
value.l = va_arg(args, long);
c = *fmt++;
break;
case 's':
/* cast near pointer to far pointer */
value.p = (char far *)
va_arg(args, char *);
break;
case 'u':
case 'x':
value.l = va_arg(args, unsigned int);
break;
default:
value.l = va_arg(args, int);
break;
}
/* format the argument */
switch ( c )
{
case 'c':
s[0] = value.ch;
s[1] = '\0';
fillc = ' ';
prec = 1; /* ignore any precision */
break;
case 'd':
dev_ltoa(value.l, s, 10);
break;
case 'o':
dev_ultoa(value.l, s, 8);
break;
case 's':
fillc = ' ';
break;
case 'u':
dev_ultoa(value.l, s, 10);
break;
case 'x':
case 'X':
dev_ultoa(value.l, s, 16);
break;
default:
s[0] = c;
s[1] = '\0';
prec = width = 1;
break;
}
if ( c != 's')
value.p = s;
/*******************************************/
/* now we have: */
/* - c == type */
/* - value.p holds char ptr to concatenate */
/* - width and precision are accurate */
/*******************************************/
slen = dev_strlen(value.p);
if ( slen > prec )
prec = slen;
if ( rj && (width>prec) )
{
/* left padding */
while ( width>prec )
{
len++; width--;
*dest++ = fillc;
}
}
if ( prec > slen )
{
/* zero padding for precision */
len += prec-slen;
dev_memset(dest, '0', prec-slen);
dest += prec-slen;
}
/* now real copy */
dev_memcpy(dest, value.p, slen);
dest += slen; len += slen;
if ( !rj )
{
/* right padding */
while ( width>prec )
{
len++; width--;
*dest++ = ' ';
}
}
break;
default:
/* normal character */
*dest++ = c;
len++;
break;
}
}
*dest = '\0';
return(len);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -