📄 xntoy.cpp
字号:
#include "stdafx.h"
#include <string.h>
#include <math.h>
#include "xntoy.h"
//*************************************************************
// 安全版本的xntoy系列函数
// 修改:袁斌
// oldworm@21cn.com
// 时间:2000年4月11日
// bug report: 内部->yb@compagis.com
// 外部->oldworm@21cn.com
//
// 特点:保证格式化以后的内容不超过size指定的缓冲区。
//*************************************************************
/*
* cvt.c - IEEE floating point formatting routines for FreeBSD
* from GNU libc-4.6.27. Modified to be thread safe.
*/
/*
* ap_ecvt converts to decimal
* the number of digits is specified by ndigit
* decpt is set to the position of the decimal point
* sign is set to 0 for positive, 1 for negative
*/
typedef enum
{
NO = 0, YES = 1
} boolean_e;
#define NDIG 80
/* buf must have at least NDIG bytes */
static char *ap_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag, char *buf)
{
register int r2;
double fi, fj;
register char *p, *p1;
if (ndigits >= NDIG - 1)
ndigits = NDIG - 2;
r2 = 0;
*sign = 0;
p = &buf[0];
if (arg < 0) {
*sign = 1;
arg = -arg;
}
arg = modf(arg, &fi);
p1 = &buf[NDIG];
/*
* Do integer part
*/
if (fi != 0) {
p1 = &buf[NDIG];
while (fi != 0) {
fj = modf(fi / 10, &fi);
*--p1 = (int) ((fj + .03) * 10) + '0';
r2++;
}
while (p1 < &buf[NDIG])
*p++ = *p1++;
}
else if (arg > 0) {
while ((fj = arg * 10) < 1) {
arg = fj;
r2--;
}
}
p1 = &buf[ndigits];
if (eflag == 0)
p1 += r2;
*decpt = r2;
if (p1 < &buf[0]) {
buf[0] = '\0';
return (buf);
}
while (p <= p1 && p < &buf[NDIG]) {
arg *= 10;
arg = modf(arg, &fj);
*p++ = (int) fj + '0';
}
if (p1 >= &buf[NDIG]) {
buf[NDIG - 1] = '\0';
return (buf);
}
p = p1;
*p1 += 5;
while (*p1 > '9') {
*p1 = '0';
if (p1 > buf)
++ * --p1;
else {
*p1 = '1';
(*decpt)++;
if (eflag == 0) {
if (p > buf)
*p = '0';
p++;
}
}
}
*p = '\0';
return (buf);
}
static char *ap_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
{
return (ap_cvt(arg, ndigits, decpt, sign, 1, buf));
}
static char *ap_fcvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
{
return (ap_cvt(arg, ndigits, decpt, sign, 0, buf));
}
/*
* ap_gcvt - Floating output conversion to
* minimal length string
*/
static char *ap_gcvt(double number, int ndigit, char *buf, boolean_e altform)
{
int sign, decpt;
register char *p1, *p2;
register int i;
char buf1[NDIG];
p1 = ap_ecvt(number, ndigit, &decpt, &sign, buf1);
p2 = buf;
if (sign)
*p2++ = '-';
for (i = ndigit - 1; i > 0 && p1[i] == '0'; i--)
ndigit--;
if ((decpt >= 0 && decpt - ndigit > 4)
|| (decpt < 0 && decpt < -3)) { /* use E-style */
decpt--;
*p2++ = *p1++;
*p2++ = '.';
for (i = 1; i < ndigit; i++)
*p2++ = *p1++;
*p2++ = 'e';
if (decpt < 0) {
decpt = -decpt;
*p2++ = '-';
}
else
*p2++ = '+';
if (decpt / 100 > 0)
*p2++ = decpt / 100 + '0';
if (decpt / 10 > 0)
*p2++ = (decpt % 100) / 10 + '0';
*p2++ = decpt % 10 + '0';
}
else {
if (decpt <= 0) {
if (*p1 != '0')
*p2++ = '.';
while (decpt < 0) {
decpt++;
*p2++ = '0';
}
}
for (i = 1; i <= ndigit; i++) {
*p2++ = *p1++;
if (i == decpt)
*p2++ = '.';
}
if (ndigit < decpt) {
while (ndigit++ < decpt)
*p2++ = '0';
*p2++ = '.';
}
}
if (p2[-1] == '.' && !altform)
p2--;
*p2 = '\0';
return (buf);
}
static inline void xntoa(unsigned long val, char *buf,
int size, unsigned radix, int is_neg )
{
char ibuf[32], *p, *pdet ;
unsigned digval;
ibuf[sizeof(ibuf)-1] = '\0';
p = (char *)&ibuf[sizeof(ibuf)-2];
pdet = (char *)buf;
if (is_neg)
{
*pdet++ = '-';
size --;
val = (unsigned long)(-(long)val);
}
do {
digval = (unsigned) (val % radix);
val /= radix; /* get next digit */
if (digval > 9)
*p-- = (char) (digval - 10 + 'a'); /* a letter */
else
*p-- = (char) (digval + '0'); /* a digit */
} while ((val > 0) && (p>=ibuf));
strncpy(pdet, ++p, size);
}
/* Actual functions just call conversion helper with neg flag set correctly,
and return pointer to buffer. */
char * intoa(int val, char *buf, int size, int radix )
{
if (radix == 10 && val < 0)
xntoa((unsigned long)val, buf, size, radix, 1);
else
xntoa((unsigned long)(unsigned int)val, buf, size, radix, 0);
return buf;
}
char * lntoa(long val, char *buf, int size,int radix)
{
xntoa((unsigned long)val, buf, size, radix, (radix == 10 && val < 0));
return buf;
}
char * ulntoa(unsigned long val,char *buf,int size,int radix)
{
xntoa(val, buf, size, radix, 0);
return buf;
}
char * strupr (char * string)
{
char * cp;
for (cp=string; *cp; ++cp)
{
if ('a' <= *cp && *cp <= 'z')
*cp += 'A' - 'a';
}
return(string);
}
char * strlwr(char * string)
{
char * cp;
for (cp=string; *cp; ++cp)
{
if ('A' <= *cp && *cp <= 'Z')
*cp += 'a' - 'A';
}
return(string);
}
int strnicmp(const char * first, const char * last, size_t count)
{
int f,l;
if(count)
{
do {
if ( ((f = (unsigned char)(*(first++))) >= 'A') &&
(f <= 'Z') )
f -= 'A' - 'a';
if ( ((l = (unsigned char)(*(last++))) >= 'A') &&
(l <= 'Z') )
l -= 'A' - 'a';
} while ( --count && f && (f == l) );
return( f - l );
}
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -