qse/ase/utl/printf.c

321 lines
5.5 KiB
C
Raw Normal View History

2006-12-24 16:07:13 +00:00
/*
2007-02-19 06:13:03 +00:00
* $Id: printf.c,v 1.11 2007-02-19 06:13:03 bacon Exp $
2007-02-03 10:52:36 +00:00
*
* {License}
2006-12-24 16:07:13 +00:00
*/
#include <stdarg.h>
#include <stdio.h>
#include <wchar.h>
2007-01-19 03:23:47 +00:00
#include <stdlib.h>
2006-12-24 16:07:13 +00:00
2007-01-21 13:21:14 +00:00
#include <ctype.h>
#include <wctype.h>
2007-01-26 16:10:32 +00:00
#if defined(_WIN32)
2007-01-27 02:55:55 +00:00
2007-01-28 11:28:27 +00:00
#include <tchar.h>
#define ase_printf _tprintf
#define ase_vprintf _vtprintf
#define ase_fprintf _ftprintf
#define ase_vfprintf _vftprintf
2007-01-27 02:55:55 +00:00
int ase_vsprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, va_list ap)
{
int n;
n = _vsntprintf (buf, size, fmt, ap);
if (n < 0 || (size_t)n >= size)
{
if (size > 0) buf[size-1] = ASE_T('\0');
n = -1;
}
return n;
}
int ase_sprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
n = ase_vsprintf (buf, size, fmt, ap);
va_end (ap);
return n;
}
2007-01-21 13:21:14 +00:00
#else
2007-01-26 16:10:32 +00:00
2007-01-28 11:28:27 +00:00
#if defined(ASE_CHAR_IS_MCHAR)
#define ase_tolower(x) tolower(x)
#define ase_isdigit(x) isdigit(x)
#elif defined(ASE_CHAR_IS_WCHAR)
#define ase_tolower(x) towlower(x)
#define ase_isdigit(x) iswdigit(x)
#else
#error define ASE_CHAR_IS_MCHAR or ASE_CHAR_IS_WCHAR
#endif
2007-01-21 13:21:14 +00:00
2006-12-24 16:07:13 +00:00
static ase_char_t* __adjust_format (const ase_char_t* format);
2007-01-28 11:28:27 +00:00
int ase_vfprintf (FILE *stream, const ase_char_t* fmt, va_list ap)
2006-12-24 16:07:13 +00:00
{
int n;
2007-01-28 11:28:27 +00:00
ase_char_t* nf = __adjust_format (fmt);
if (nf == NULL) return -1;
2006-12-24 16:07:13 +00:00
2007-01-28 11:28:27 +00:00
#ifdef ASE_CHAR_IS_MCHAR
n = vfprintf (stream, nf, ap);
#else
n = vfwprintf (stream, nf, ap);
#endif
free (nf);
2006-12-24 16:07:13 +00:00
return n;
}
2007-01-19 03:23:47 +00:00
int ase_vprintf (const ase_char_t* fmt, va_list ap)
2006-12-24 16:07:13 +00:00
{
2007-01-19 03:23:47 +00:00
return ase_vfprintf (stdout, fmt, ap);
2006-12-24 16:07:13 +00:00
}
2007-01-28 11:28:27 +00:00
int ase_fprintf (FILE* file, const ase_char_t* fmt, ...)
2006-12-24 16:07:13 +00:00
{
int n;
2007-01-28 11:28:27 +00:00
va_list ap;
2006-12-24 16:07:13 +00:00
2007-01-28 11:28:27 +00:00
va_start (ap, fmt);
n = ase_vfprintf (file, fmt, ap);
va_end (ap);
2006-12-24 16:07:13 +00:00
return n;
}
2007-01-28 11:28:27 +00:00
int ase_printf (const ase_char_t* fmt, ...)
2006-12-24 16:07:13 +00:00
{
int n;
va_list ap;
va_start (ap, fmt);
2007-01-28 11:28:27 +00:00
n = ase_vprintf (fmt, ap);
2006-12-24 16:07:13 +00:00
va_end (ap);
return n;
}
2007-01-19 03:23:47 +00:00
int ase_vsprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, va_list ap)
2006-12-24 16:07:13 +00:00
{
int n;
ase_char_t* nf = __adjust_format (fmt);
if (nf == NULL) return -1;
#if defined(ASE_CHAR_IS_MCHAR)
n = vsnprintf (buf, size, nf, ap);
#elif defined(_WIN32)
n = _vsnwprintf (buf, size, nf, ap);
#else
n = vswprintf (buf, size, nf, ap);
#endif
if (n < 0 || (size_t)n >= size)
{
if (size > 0) buf[size-1] = ASE_T('\0');
n = -1;
}
free (nf);
return n;
}
2007-01-28 11:28:27 +00:00
int ase_sprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
n = ase_vsprintf (buf, size, fmt, ap);
va_end (ap);
return n;
}
2006-12-24 16:07:13 +00:00
#define MOD_SHORT 1
#define MOD_LONG 2
#define MOD_LONGLONG 3
2007-01-19 03:23:47 +00:00
#define ADDC(buf,c) \
2006-12-24 16:07:13 +00:00
do { \
2007-01-19 03:23:47 +00:00
if (buf.len >= buf.cap) \
{ \
ase_char_t* tmp; \
tmp = (ase_char_t*)realloc ( \
buf.ptr, sizeof(ase_char_t)*(buf.cap+256+1)); \
if (tmp == NULL) \
{ \
free (buf.ptr); \
return NULL; \
} \
buf.ptr = tmp; \
buf.cap = buf.cap + 256; \
2006-12-24 16:07:13 +00:00
} \
2007-01-19 03:23:47 +00:00
buf.ptr[buf.len++] = c; \
2006-12-24 16:07:13 +00:00
} while (0)
static ase_char_t* __adjust_format (const ase_char_t* format)
{
const ase_char_t* fp = format;
int modifier;
ase_char_t ch;
2007-01-19 03:23:47 +00:00
struct
{
ase_char_t* ptr;
ase_size_t len;
ase_size_t cap;
} buf;
buf.len = 0;
buf.cap = 256;
2007-02-19 06:13:03 +00:00
#if (defined(vms) || defined(__vms)) && (ASE_SIZEOF_VOID_P >= 8)
buf.ptr = (ase_char_t*) _malloc32 (sizeof(ase_char_t)*(buf.cap+1));
#else
2007-01-19 03:23:47 +00:00
buf.ptr = (ase_char_t*) malloc (sizeof(ase_char_t)*(buf.cap+1));
2007-02-19 06:13:03 +00:00
#endif
2007-01-19 03:23:47 +00:00
if (buf.ptr == NULL) return NULL;
2006-12-24 16:07:13 +00:00
while (*fp != ASE_T('\0'))
{
while (*fp != ASE_T('\0') && *fp != ASE_T('%'))
{
2007-01-19 03:23:47 +00:00
ADDC (buf, *fp++);
2006-12-24 16:07:13 +00:00
}
if (*fp == ASE_T('\0')) break;
ch = *fp++;
2007-01-19 03:23:47 +00:00
ADDC (buf, ch); /* add % */
2006-12-24 16:07:13 +00:00
ch = *fp++;
/* flags */
while (1)
{
if (ch == ASE_T(' ') || ch == ASE_T('+') ||
ch == ASE_T('-') || ch == ASE_T('#'))
{
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
ch = *fp++;
}
else
{
if (ch == ASE_T('0'))
{
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
ch = *fp++;
}
break;
}
}
/* check the width */
if (ch == ASE_T('*'))
{
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
ch = *fp++;
}
else
{
2007-01-19 03:23:47 +00:00
while (ase_isdigit(ch))
2006-12-24 16:07:13 +00:00
{
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
ch = *fp++;
}
}
/* precision */
if (ch == ASE_T('.'))
{
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
ch = *fp++;
if (ch == ASE_T('*'))
{
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
ch = *fp++;
}
else
{
2007-01-19 03:23:47 +00:00
while (ase_isdigit(ch))
2006-12-24 16:07:13 +00:00
{
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
ch = *fp++;
}
}
}
/* modifier */
for (modifier = 0;;)
{
if (ch == ASE_T('h')) modifier = MOD_SHORT;
else if (ch == ASE_T('l'))
{
modifier = (modifier == MOD_LONG)? MOD_LONGLONG: MOD_LONG;
}
else break;
ch = *fp++;
}
/* type */
2007-01-19 03:23:47 +00:00
if (ch == ASE_T('%')) ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
else if (ch == ASE_T('c') || ch == ASE_T('s'))
{
#if !defined(ASE_CHAR_IS_MCHAR) && !defined(_WIN32)
2007-01-19 03:23:47 +00:00
ADDC (buf, 'l');
2006-12-24 16:07:13 +00:00
#endif
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
}
else if (ch == ASE_T('C') || ch == ASE_T('S'))
{
#ifdef _WIN32
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
#else
#ifdef ASE_CHAR_IS_MCHAR
2007-01-19 03:23:47 +00:00
ADDC (buf, 'l');
2006-12-24 16:07:13 +00:00
#endif
2007-01-19 03:23:47 +00:00
ADDC (buf, ase_tolower(ch));
2006-12-24 16:07:13 +00:00
#endif
}
else if (ch == ASE_T('d') || ch == ASE_T('i') ||
ch == ASE_T('o') || ch == ASE_T('u') ||
ch == ASE_T('x') || ch == ASE_T('X'))
{
if (modifier == MOD_SHORT)
{
2007-01-19 03:23:47 +00:00
ADDC (buf, 'h');
2006-12-24 16:07:13 +00:00
}
else if (modifier == MOD_LONG)
{
2007-01-19 03:23:47 +00:00
ADDC (buf, 'l');
2006-12-24 16:07:13 +00:00
}
else if (modifier == MOD_LONGLONG)
{
#if defined(_WIN32) && !defined(__LCC__)
2007-01-19 03:23:47 +00:00
ADDC (buf, 'I');
ADDC (buf, '6');
ADDC (buf, '4');
2006-12-24 16:07:13 +00:00
#else
2007-01-19 03:23:47 +00:00
ADDC (buf, 'l');
ADDC (buf, 'l');
2006-12-24 16:07:13 +00:00
#endif
}
2007-01-19 03:23:47 +00:00
ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
}
else if (ch == ASE_T('\0')) break;
2007-01-19 03:23:47 +00:00
else ADDC (buf, ch);
2006-12-24 16:07:13 +00:00
}
2007-01-19 03:23:47 +00:00
buf.ptr[buf.len] = ASE_T('\0');
2007-02-07 05:43:37 +00:00
2007-01-19 03:23:47 +00:00
return buf.ptr;
2006-12-24 16:07:13 +00:00
}
2007-01-26 16:15:54 +00:00
#endif