/*
|
/*
|
* misc.c -- Miscellaneous routines.
|
* misc.c -- Miscellaneous routines.
|
*
|
*
|
* Copyright (c) GoAhead Software Inc., 1995-1999. All Rights Reserved.
|
* Copyright (c) GoAhead Software Inc., 1995-1999. All Rights Reserved.
|
*
|
*
|
* See the file "license.txt" for usage and redistribution license requirements
|
* See the file "license.txt" for usage and redistribution license requirements
|
*/
|
*/
|
|
|
/********************************* Includes ***********************************/
|
/********************************* Includes ***********************************/
|
|
|
#if UEMF
|
#if UEMF
|
#include "uemf.h"
|
#include "uemf.h"
|
#else
|
#else
|
#include "basic/basicInternal.h"
|
#include "basic/basicInternal.h"
|
#endif
|
#endif
|
|
|
/********************************* Defines ************************************/
|
/********************************* Defines ************************************/
|
/*
|
/*
|
* Sprintf buffer structure. Make the increment 8 less than 64 so that
|
* Sprintf buffer structure. Make the increment 8 less than 64 so that
|
* a balloc can use a 64 byte block.
|
* a balloc can use a 64 byte block.
|
*/
|
*/
|
|
|
#define STR_REALLOC 0x1 /* Reallocate the buffer as required */
|
#define STR_REALLOC 0x1 /* Reallocate the buffer as required */
|
#define STR_INC 58 /* Growth increment */
|
#define STR_INC 58 /* Growth increment */
|
|
|
typedef struct {
|
typedef struct {
|
char_t *s; /* Pointer to buffer */
|
char_t *s; /* Pointer to buffer */
|
int size; /* Current buffer size */
|
int size; /* Current buffer size */
|
int max; /* Maximum buffer size */
|
int max; /* Maximum buffer size */
|
int count; /* Buffer count */
|
int count; /* Buffer count */
|
int flags; /* Allocation flags */
|
int flags; /* Allocation flags */
|
} strbuf_t;
|
} strbuf_t;
|
|
|
/*
|
/*
|
* Sprintf formatting flags
|
* Sprintf formatting flags
|
*/
|
*/
|
enum flag {
|
enum flag {
|
flag_none = 0,
|
flag_none = 0,
|
flag_minus = 1,
|
flag_minus = 1,
|
flag_plus = 2,
|
flag_plus = 2,
|
flag_space = 4,
|
flag_space = 4,
|
flag_hash = 8,
|
flag_hash = 8,
|
flag_zero = 16,
|
flag_zero = 16,
|
flag_short = 32,
|
flag_short = 32,
|
flag_long = 64
|
flag_long = 64
|
};
|
};
|
|
|
/************************** Forward Declarations ******************************/
|
/************************** Forward Declarations ******************************/
|
|
|
static int dsnprintf(char_t **s, int size, char_t *fmt, va_list arg,
|
static int dsnprintf(char_t **s, int size, char_t *fmt, va_list arg,
|
int msize);
|
int msize);
|
static int strnlen(char_t *s, unsigned int n);
|
static int strnlen(char_t *s, unsigned int n);
|
static void put_char(strbuf_t *buf, char_t c);
|
static void put_char(strbuf_t *buf, char_t c);
|
static void put_string(strbuf_t *buf, char_t *s, int len,
|
static void put_string(strbuf_t *buf, char_t *s, int len,
|
int width, int prec, enum flag f);
|
int width, int prec, enum flag f);
|
static void put_ulong(strbuf_t *buf, unsigned long int value, int base,
|
static void put_ulong(strbuf_t *buf, unsigned long int value, int base,
|
int upper, char_t *prefix, int width, int prec, enum flag f);
|
int upper, char_t *prefix, int width, int prec, enum flag f);
|
|
|
/************************************ Code ************************************/
|
/************************************ Code ************************************/
|
/*
|
/*
|
* "basename" returns a pointer to the last component of a pathname
|
* "basename" returns a pointer to the last component of a pathname
|
* LINUX, RTEMS, and LynxOS have their own basename function
|
* LINUX, RTEMS, and LynxOS have their own basename function
|
*/
|
*/
|
|
|
#if ! LINUX & ! __rtems__ & ! LYNX
|
#if ! LINUX & ! __rtems__ & ! LYNX
|
char_t *basename(char_t* name)
|
char_t *basename(char_t* name)
|
{
|
{
|
char_t *cp;
|
char_t *cp;
|
|
|
#if NW || WIN
|
#if NW || WIN
|
if (((cp = gstrrchr(name, '\\')) == NULL) &&
|
if (((cp = gstrrchr(name, '\\')) == NULL) &&
|
((cp = gstrrchr(name, '/')) == NULL)) {
|
((cp = gstrrchr(name, '/')) == NULL)) {
|
return name;
|
return name;
|
#else
|
#else
|
if ((cp = gstrrchr(name, '/')) == NULL) {
|
if ((cp = gstrrchr(name, '/')) == NULL) {
|
return name;
|
return name;
|
#endif
|
#endif
|
} else if (*(cp + 1) == '\0' && cp == name) {
|
} else if (*(cp + 1) == '\0' && cp == name) {
|
return name;
|
return name;
|
} else if (*(cp + 1) == '\0' && cp != name) {
|
} else if (*(cp + 1) == '\0' && cp != name) {
|
return T("");
|
return T("");
|
} else {
|
} else {
|
return ++cp;
|
return ++cp;
|
}
|
}
|
}
|
}
|
#endif /* ! LINUX && ! __rtems__ && ! LYNX */
|
#endif /* ! LINUX && ! __rtems__ && ! LYNX */
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Returns a pointer to the directory component of a pathname. bufsize is
|
* Returns a pointer to the directory component of a pathname. bufsize is
|
* the size of the buffer in BYTES!
|
* the size of the buffer in BYTES!
|
*/
|
*/
|
|
|
char_t *dirname(char_t* buf, char_t* name, int bufsize)
|
char_t *dirname(char_t* buf, char_t* name, int bufsize)
|
{
|
{
|
char_t* cp;
|
char_t* cp;
|
int len;
|
int len;
|
|
|
a_assert(name);
|
a_assert(name);
|
a_assert(buf);
|
a_assert(buf);
|
a_assert(bufsize > 0);
|
a_assert(bufsize > 0);
|
|
|
#if WIN || NW
|
#if WIN || NW
|
if ((cp = gstrrchr(name, '/')) == NULL &&
|
if ((cp = gstrrchr(name, '/')) == NULL &&
|
(cp = gstrrchr(name, '\\')) == NULL)
|
(cp = gstrrchr(name, '\\')) == NULL)
|
#else
|
#else
|
if ((cp = gstrrchr(name, '/')) == NULL)
|
if ((cp = gstrrchr(name, '/')) == NULL)
|
#endif
|
#endif
|
{
|
{
|
gstrcpy(buf, T("."));
|
gstrcpy(buf, T("."));
|
return buf;
|
return buf;
|
}
|
}
|
|
|
if ((*(cp + 1) == '\0' && cp == name)) {
|
if ((*(cp + 1) == '\0' && cp == name)) {
|
gstrncpy(buf, T("."), TSZ(bufsize));
|
gstrncpy(buf, T("."), TSZ(bufsize));
|
gstrcpy(buf, T("."));
|
gstrcpy(buf, T("."));
|
return buf;
|
return buf;
|
}
|
}
|
|
|
len = cp - name;
|
len = cp - name;
|
|
|
if (len < bufsize) {
|
if (len < bufsize) {
|
gstrncpy(buf, name, len);
|
gstrncpy(buf, name, len);
|
buf[len] = '\0';
|
buf[len] = '\0';
|
} else {
|
} else {
|
gstrncpy(buf, name, TSZ(bufsize));
|
gstrncpy(buf, name, TSZ(bufsize));
|
buf[bufsize - 1] = '\0';
|
buf[bufsize - 1] = '\0';
|
}
|
}
|
|
|
return buf;
|
return buf;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* sprintf and vsprintf are bad, ok. You can easily clobber memory. Use
|
* sprintf and vsprintf are bad, ok. You can easily clobber memory. Use
|
* gsnprintf and gvsnprintf instead! These functions do _not_ support floating
|
* gsnprintf and gvsnprintf instead! These functions do _not_ support floating
|
* point, like %e, %f, %g...
|
* point, like %e, %f, %g...
|
*/
|
*/
|
|
|
int gsnprintf(char_t **s, int n, char_t *fmt, ...)
|
int gsnprintf(char_t **s, int n, char_t *fmt, ...)
|
{
|
{
|
va_list ap;
|
va_list ap;
|
int result;
|
int result;
|
|
|
a_assert(s);
|
a_assert(s);
|
a_assert(fmt);
|
a_assert(fmt);
|
|
|
*s = NULL;
|
*s = NULL;
|
va_start(ap, fmt);
|
va_start(ap, fmt);
|
result = gvsnprintf(s, n, fmt, ap);
|
result = gvsnprintf(s, n, fmt, ap);
|
va_end(ap);
|
va_end(ap);
|
return result;
|
return result;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* This function appends the formatted string to the supplied string,
|
* This function appends the formatted string to the supplied string,
|
* reallocing if required.
|
* reallocing if required.
|
*/
|
*/
|
|
|
int gsprintfRealloc(char_t **s, int n, int msize, char_t *fmt, ...)
|
int gsprintfRealloc(char_t **s, int n, int msize, char_t *fmt, ...)
|
{
|
{
|
va_list ap;
|
va_list ap;
|
int result;
|
int result;
|
|
|
a_assert(s);
|
a_assert(s);
|
a_assert(fmt);
|
a_assert(fmt);
|
|
|
if (msize == -1) {
|
if (msize == -1) {
|
*s = NULL;
|
*s = NULL;
|
}
|
}
|
va_start(ap, fmt);
|
va_start(ap, fmt);
|
result = dsnprintf(s, n, fmt, ap, msize);
|
result = dsnprintf(s, n, fmt, ap, msize);
|
va_end(ap);
|
va_end(ap);
|
return result;
|
return result;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* A vsprintf replacement.
|
* A vsprintf replacement.
|
*/
|
*/
|
|
|
int gvsnprintf(char_t **s, int n, char_t *fmt, va_list arg)
|
int gvsnprintf(char_t **s, int n, char_t *fmt, va_list arg)
|
{
|
{
|
a_assert(s);
|
a_assert(s);
|
a_assert(fmt);
|
a_assert(fmt);
|
|
|
return dsnprintf(s, n, fmt, arg, 0);
|
return dsnprintf(s, n, fmt, arg, 0);
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Dynamic sprintf implementation. Supports dynamic buffer allocation also.
|
* Dynamic sprintf implementation. Supports dynamic buffer allocation also.
|
* This function can be called multiple times to grow an existing allocated
|
* This function can be called multiple times to grow an existing allocated
|
* buffer. In this case, msize is set to the size of the previously allocated
|
* buffer. In this case, msize is set to the size of the previously allocated
|
* buffer. The buffer will be realloced, as required. If msize is set, we
|
* buffer. The buffer will be realloced, as required. If msize is set, we
|
* return the size of the allocated buffer for use with the next call. For
|
* return the size of the allocated buffer for use with the next call. For
|
* the first call, msize can be set to -1.
|
* the first call, msize can be set to -1.
|
*/
|
*/
|
|
|
static int dsnprintf(char_t **s, int size, char_t *fmt, va_list arg, int msize)
|
static int dsnprintf(char_t **s, int size, char_t *fmt, va_list arg, int msize)
|
{
|
{
|
strbuf_t buf;
|
strbuf_t buf;
|
char_t c;
|
char_t c;
|
|
|
a_assert(s);
|
a_assert(s);
|
a_assert(fmt);
|
a_assert(fmt);
|
|
|
memset(&buf, 0, sizeof(buf));
|
memset(&buf, 0, sizeof(buf));
|
buf.s = *s;
|
buf.s = *s;
|
|
|
if (*s == NULL || msize != 0) {
|
if (*s == NULL || msize != 0) {
|
buf.max = size;
|
buf.max = size;
|
buf.flags |= STR_REALLOC;
|
buf.flags |= STR_REALLOC;
|
if (msize != 0) {
|
if (msize != 0) {
|
buf.size = max(msize, 0);
|
buf.size = max(msize, 0);
|
}
|
}
|
if (*s != NULL && msize != 0) {
|
if (*s != NULL && msize != 0) {
|
buf.count = gstrlen(*s);
|
buf.count = gstrlen(*s);
|
}
|
}
|
} else {
|
} else {
|
buf.size = size;
|
buf.size = size;
|
}
|
}
|
|
|
while ((c = *fmt++) != '\0') {
|
while ((c = *fmt++) != '\0') {
|
if (c != '%' || (c = *fmt++) == '%') {
|
if (c != '%' || (c = *fmt++) == '%') {
|
put_char(&buf, c);
|
put_char(&buf, c);
|
} else {
|
} else {
|
enum flag f = flag_none;
|
enum flag f = flag_none;
|
int width = 0;
|
int width = 0;
|
int prec = -1;
|
int prec = -1;
|
for ( ; c != '\0'; c = *fmt++) {
|
for ( ; c != '\0'; c = *fmt++) {
|
if (c == '-') {
|
if (c == '-') {
|
f |= flag_minus;
|
f |= flag_minus;
|
} else if (c == '+') {
|
} else if (c == '+') {
|
f |= flag_plus;
|
f |= flag_plus;
|
} else if (c == ' ') {
|
} else if (c == ' ') {
|
f |= flag_space;
|
f |= flag_space;
|
} else if (c == '#') {
|
} else if (c == '#') {
|
f |= flag_hash;
|
f |= flag_hash;
|
} else if (c == '0') {
|
} else if (c == '0') {
|
f |= flag_zero;
|
f |= flag_zero;
|
} else {
|
} else {
|
break;
|
break;
|
}
|
}
|
}
|
}
|
if (c == '*') {
|
if (c == '*') {
|
width = va_arg(arg, int);
|
width = va_arg(arg, int);
|
if (width < 0) {
|
if (width < 0) {
|
f |= flag_minus;
|
f |= flag_minus;
|
width = -width;
|
width = -width;
|
}
|
}
|
c = *fmt++;
|
c = *fmt++;
|
} else {
|
} else {
|
for ( ; gisdigit(c); c = *fmt++) {
|
for ( ; gisdigit(c); c = *fmt++) {
|
width = width * 10 + (c - '0');
|
width = width * 10 + (c - '0');
|
}
|
}
|
}
|
}
|
if (c == '.') {
|
if (c == '.') {
|
f &= ~flag_zero;
|
f &= ~flag_zero;
|
c = *fmt++;
|
c = *fmt++;
|
if (c == '*') {
|
if (c == '*') {
|
prec = va_arg(arg, int);
|
prec = va_arg(arg, int);
|
c = *fmt++;
|
c = *fmt++;
|
} else {
|
} else {
|
for (prec = 0; gisdigit(c); c = *fmt++) {
|
for (prec = 0; gisdigit(c); c = *fmt++) {
|
prec = prec * 10 + (c - '0');
|
prec = prec * 10 + (c - '0');
|
}
|
}
|
}
|
}
|
}
|
}
|
if (c == 'h' || c == 'l') {
|
if (c == 'h' || c == 'l') {
|
f |= (c == 'h' ? flag_short : flag_long);
|
f |= (c == 'h' ? flag_short : flag_long);
|
c = *fmt++;
|
c = *fmt++;
|
}
|
}
|
if (c == 'd' || c == 'i') {
|
if (c == 'd' || c == 'i') {
|
long int value;
|
long int value;
|
if (f & flag_short) {
|
if (f & flag_short) {
|
value = (short int) va_arg(arg, int);
|
value = (short int) va_arg(arg, int);
|
} else if (f & flag_long) {
|
} else if (f & flag_long) {
|
value = va_arg(arg, long int);
|
value = va_arg(arg, long int);
|
} else {
|
} else {
|
value = va_arg(arg, int);
|
value = va_arg(arg, int);
|
}
|
}
|
if (value >= 0) {
|
if (value >= 0) {
|
if (f & flag_plus) {
|
if (f & flag_plus) {
|
put_ulong(&buf, value, 10, 0, T("+"), width, prec, f);
|
put_ulong(&buf, value, 10, 0, T("+"), width, prec, f);
|
} else if (f & flag_space) {
|
} else if (f & flag_space) {
|
put_ulong(&buf, value, 10, 0, T(" "), width, prec, f);
|
put_ulong(&buf, value, 10, 0, T(" "), width, prec, f);
|
} else {
|
} else {
|
put_ulong(&buf, value, 10, 0, NULL, width, prec, f);
|
put_ulong(&buf, value, 10, 0, NULL, width, prec, f);
|
}
|
}
|
} else {
|
} else {
|
put_ulong(&buf, -value, 10, 0, T("-"), width, prec, f);
|
put_ulong(&buf, -value, 10, 0, T("-"), width, prec, f);
|
}
|
}
|
} else if (c == 'o' || c == 'u' || c == 'x' || c == 'X') {
|
} else if (c == 'o' || c == 'u' || c == 'x' || c == 'X') {
|
unsigned long int value;
|
unsigned long int value;
|
if (f & flag_short) {
|
if (f & flag_short) {
|
value = (unsigned short int) va_arg(arg, unsigned int);
|
value = (unsigned short int) va_arg(arg, unsigned int);
|
} else if (f & flag_long) {
|
} else if (f & flag_long) {
|
value = va_arg(arg, unsigned long int);
|
value = va_arg(arg, unsigned long int);
|
} else {
|
} else {
|
value = va_arg(arg, unsigned int);
|
value = va_arg(arg, unsigned int);
|
}
|
}
|
if (c == 'o') {
|
if (c == 'o') {
|
if (f & flag_hash && value != 0) {
|
if (f & flag_hash && value != 0) {
|
put_ulong(&buf, value, 8, 0, T("0"), width, prec, f);
|
put_ulong(&buf, value, 8, 0, T("0"), width, prec, f);
|
} else {
|
} else {
|
put_ulong(&buf, value, 8, 0, NULL, width, prec, f);
|
put_ulong(&buf, value, 8, 0, NULL, width, prec, f);
|
}
|
}
|
} else if (c == 'u') {
|
} else if (c == 'u') {
|
put_ulong(&buf, value, 10, 0, NULL, width, prec, f);
|
put_ulong(&buf, value, 10, 0, NULL, width, prec, f);
|
} else {
|
} else {
|
if (f & flag_hash && value != 0) {
|
if (f & flag_hash && value != 0) {
|
if (c == 'x') {
|
if (c == 'x') {
|
put_ulong(&buf, value, 16, 0, T("0x"), width,
|
put_ulong(&buf, value, 16, 0, T("0x"), width,
|
prec, f);
|
prec, f);
|
} else {
|
} else {
|
put_ulong(&buf, value, 16, 1, T("0X"), width,
|
put_ulong(&buf, value, 16, 1, T("0X"), width,
|
prec, f);
|
prec, f);
|
}
|
}
|
} else {
|
} else {
|
put_ulong(&buf, value, 16, 0, NULL, width, prec, f);
|
put_ulong(&buf, value, 16, 0, NULL, width, prec, f);
|
}
|
}
|
}
|
}
|
|
|
} else if (c == 'c') {
|
} else if (c == 'c') {
|
char_t value = va_arg(arg, int);
|
char_t value = va_arg(arg, int);
|
put_char(&buf, value);
|
put_char(&buf, value);
|
|
|
} else if (c == 's' || c == 'S') {
|
} else if (c == 's' || c == 'S') {
|
char_t *value = va_arg(arg, char_t *);
|
char_t *value = va_arg(arg, char_t *);
|
if (value == NULL) {
|
if (value == NULL) {
|
put_string(&buf, T("(null)"), -1, width, prec, f);
|
put_string(&buf, T("(null)"), -1, width, prec, f);
|
} else if (f & flag_hash) {
|
} else if (f & flag_hash) {
|
put_string(&buf,
|
put_string(&buf,
|
value + 1, (char_t) *value, width, prec, f);
|
value + 1, (char_t) *value, width, prec, f);
|
} else {
|
} else {
|
put_string(&buf, value, -1, width, prec, f);
|
put_string(&buf, value, -1, width, prec, f);
|
}
|
}
|
} else if (c == 'p') {
|
} else if (c == 'p') {
|
void *value = va_arg(arg, void *);
|
void *value = va_arg(arg, void *);
|
put_ulong(&buf,
|
put_ulong(&buf,
|
(unsigned long int) value, 16, 0, T("0x"), width, prec, f);
|
(unsigned long int) value, 16, 0, T("0x"), width, prec, f);
|
} else if (c == 'n') {
|
} else if (c == 'n') {
|
if (f & flag_short) {
|
if (f & flag_short) {
|
short int *value = va_arg(arg, short int *);
|
short int *value = va_arg(arg, short int *);
|
*value = buf.count;
|
*value = buf.count;
|
} else if (f & flag_long) {
|
} else if (f & flag_long) {
|
long int *value = va_arg(arg, long int *);
|
long int *value = va_arg(arg, long int *);
|
*value = buf.count;
|
*value = buf.count;
|
} else {
|
} else {
|
int *value = va_arg(arg, int *);
|
int *value = va_arg(arg, int *);
|
*value = buf.count;
|
*value = buf.count;
|
}
|
}
|
} else {
|
} else {
|
put_char(&buf, c);
|
put_char(&buf, c);
|
}
|
}
|
}
|
}
|
}
|
}
|
if (buf.s == NULL) {
|
if (buf.s == NULL) {
|
put_char(&buf, '\0');
|
put_char(&buf, '\0');
|
}
|
}
|
|
|
/*
|
/*
|
* If the user requested a dynamic buffer (*s == NULL), ensure it is returned.
|
* If the user requested a dynamic buffer (*s == NULL), ensure it is returned.
|
*/
|
*/
|
if (*s == NULL || msize != 0) {
|
if (*s == NULL || msize != 0) {
|
*s = buf.s;
|
*s = buf.s;
|
}
|
}
|
|
|
if (*s != NULL && size > 0) {
|
if (*s != NULL && size > 0) {
|
if (buf.count < size) {
|
if (buf.count < size) {
|
(*s)[buf.count] = '\0';
|
(*s)[buf.count] = '\0';
|
} else {
|
} else {
|
(*s)[buf.size - 1] = '\0';
|
(*s)[buf.size - 1] = '\0';
|
}
|
}
|
}
|
}
|
|
|
if (msize != 0) {
|
if (msize != 0) {
|
return buf.size;
|
return buf.size;
|
}
|
}
|
return buf.count;
|
return buf.count;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Return the length of a string limited by a given length
|
* Return the length of a string limited by a given length
|
*/
|
*/
|
|
|
static int strnlen(char_t *s, unsigned int n)
|
static int strnlen(char_t *s, unsigned int n)
|
{
|
{
|
unsigned int len;
|
unsigned int len;
|
|
|
len = gstrlen(s);
|
len = gstrlen(s);
|
return min(len, n);
|
return min(len, n);
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Add a character to a string buffer
|
* Add a character to a string buffer
|
*/
|
*/
|
|
|
static void put_char(strbuf_t *buf, char_t c)
|
static void put_char(strbuf_t *buf, char_t c)
|
{
|
{
|
if (buf->count >= buf->size) {
|
if (buf->count >= buf->size) {
|
if (! (buf->flags & STR_REALLOC)) {
|
if (! (buf->flags & STR_REALLOC)) {
|
return;
|
return;
|
}
|
}
|
buf->size += STR_INC;
|
buf->size += STR_INC;
|
if (buf->size > buf->max && buf->size > STR_INC) {
|
if (buf->size > buf->max && buf->size > STR_INC) {
|
a_assert(buf->size <= buf->max);
|
a_assert(buf->size <= buf->max);
|
buf->size -= STR_INC;
|
buf->size -= STR_INC;
|
return;
|
return;
|
}
|
}
|
if (buf->s == NULL) {
|
if (buf->s == NULL) {
|
buf->s = balloc(B_L, buf->size * sizeof(char_t*));
|
buf->s = balloc(B_L, buf->size * sizeof(char_t*));
|
} else {
|
} else {
|
buf->s = brealloc(B_L, buf->s, buf->size * sizeof(char_t*));
|
buf->s = brealloc(B_L, buf->s, buf->size * sizeof(char_t*));
|
}
|
}
|
}
|
}
|
buf->s[buf->count] = c;
|
buf->s[buf->count] = c;
|
++buf->count;
|
++buf->count;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Add a string to a string buffer
|
* Add a string to a string buffer
|
*/
|
*/
|
|
|
static void put_string(strbuf_t *buf, char_t *s, int len, int width,
|
static void put_string(strbuf_t *buf, char_t *s, int len, int width,
|
int prec, enum flag f)
|
int prec, enum flag f)
|
{
|
{
|
int i;
|
int i;
|
|
|
if (len < 0) {
|
if (len < 0) {
|
len = strnlen(s, prec >= 0 ? prec : ULONG_MAX);
|
len = strnlen(s, prec >= 0 ? prec : ULONG_MAX);
|
} else if (prec >= 0 && prec < len) {
|
} else if (prec >= 0 && prec < len) {
|
len = prec;
|
len = prec;
|
}
|
}
|
if (width > len && !(f & flag_minus)) {
|
if (width > len && !(f & flag_minus)) {
|
for (i = len; i < width; ++i) {
|
for (i = len; i < width; ++i) {
|
put_char(buf, ' ');
|
put_char(buf, ' ');
|
}
|
}
|
}
|
}
|
for (i = 0; i < len; ++i) {
|
for (i = 0; i < len; ++i) {
|
put_char(buf, s[i]);
|
put_char(buf, s[i]);
|
}
|
}
|
if (width > len && f & flag_minus) {
|
if (width > len && f & flag_minus) {
|
for (i = len; i < width; ++i) {
|
for (i = len; i < width; ++i) {
|
put_char(buf, ' ');
|
put_char(buf, ' ');
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Add a long to a string buffer
|
* Add a long to a string buffer
|
*/
|
*/
|
|
|
static void put_ulong(strbuf_t *buf, unsigned long int value, int base,
|
static void put_ulong(strbuf_t *buf, unsigned long int value, int base,
|
int upper, char_t *prefix, int width, int prec, enum flag f)
|
int upper, char_t *prefix, int width, int prec, enum flag f)
|
{
|
{
|
unsigned long x, x2;
|
unsigned long x, x2;
|
int len, zeros, i;
|
int len, zeros, i;
|
|
|
for (len = 1, x = 1; x < ULONG_MAX / base; ++len, x = x2) {
|
for (len = 1, x = 1; x < ULONG_MAX / base; ++len, x = x2) {
|
x2 = x * base;
|
x2 = x * base;
|
if (x2 > value) {
|
if (x2 > value) {
|
break;
|
break;
|
}
|
}
|
}
|
}
|
zeros = (prec > len) ? prec - len : 0;
|
zeros = (prec > len) ? prec - len : 0;
|
width -= zeros + len;
|
width -= zeros + len;
|
if (prefix != NULL) {
|
if (prefix != NULL) {
|
width -= strnlen(prefix, ULONG_MAX);
|
width -= strnlen(prefix, ULONG_MAX);
|
}
|
}
|
if (!(f & flag_minus)) {
|
if (!(f & flag_minus)) {
|
for (i = 0; i < width; ++i) {
|
for (i = 0; i < width; ++i) {
|
put_char(buf, ' ');
|
put_char(buf, ' ');
|
}
|
}
|
}
|
}
|
if (prefix != NULL) {
|
if (prefix != NULL) {
|
put_string(buf, prefix, -1, 0, -1, flag_none);
|
put_string(buf, prefix, -1, 0, -1, flag_none);
|
}
|
}
|
for (i = 0; i < zeros; ++i) {
|
for (i = 0; i < zeros; ++i) {
|
put_char(buf, '0');
|
put_char(buf, '0');
|
}
|
}
|
for ( ; x > 0; x /= base) {
|
for ( ; x > 0; x /= base) {
|
int digit = (value / x) % base;
|
int digit = (value / x) % base;
|
put_char(buf, (char) ((digit < 10 ? '0' : (upper ? 'A' : 'a') - 10) +
|
put_char(buf, (char) ((digit < 10 ? '0' : (upper ? 'A' : 'a') - 10) +
|
digit));
|
digit));
|
}
|
}
|
if (f & flag_minus) {
|
if (f & flag_minus) {
|
for (i = 0; i < width; ++i) {
|
for (i = 0; i < width; ++i) {
|
put_char(buf, ' ');
|
put_char(buf, ' ');
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Convert an ansi string to a unicode string. On an error, we return the
|
* Convert an ansi string to a unicode string. On an error, we return the
|
* original ansi string which is better than returning NULL. nBytes is the
|
* original ansi string which is better than returning NULL. nBytes is the
|
* size of the destination buffer (ubuf) in _bytes_.
|
* size of the destination buffer (ubuf) in _bytes_.
|
*/
|
*/
|
|
|
char_t *ascToUni(char_t *ubuf, char *str, int nBytes)
|
char_t *ascToUni(char_t *ubuf, char *str, int nBytes)
|
{
|
{
|
#if UNICODE
|
#if UNICODE
|
if (MultiByteToWideChar(CP_ACP, 0, str, nBytes / sizeof(char_t), ubuf,
|
if (MultiByteToWideChar(CP_ACP, 0, str, nBytes / sizeof(char_t), ubuf,
|
nBytes / sizeof(char_t)) < 0) {
|
nBytes / sizeof(char_t)) < 0) {
|
return (char_t*) str;
|
return (char_t*) str;
|
}
|
}
|
#else
|
#else
|
memcpy(ubuf, str, nBytes);
|
memcpy(ubuf, str, nBytes);
|
#endif
|
#endif
|
return ubuf;
|
return ubuf;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* Convert a unicode string to an ansi string. On an error, return the
|
* Convert a unicode string to an ansi string. On an error, return the
|
* original unicode string which is better than returning NULL.
|
* original unicode string which is better than returning NULL.
|
* N.B. nBytes is the number of _bytes_ in the destination buffer, buf.
|
* N.B. nBytes is the number of _bytes_ in the destination buffer, buf.
|
*/
|
*/
|
|
|
char *uniToAsc(char *buf, char_t* ustr, int nBytes)
|
char *uniToAsc(char *buf, char_t* ustr, int nBytes)
|
{
|
{
|
#if UNICODE
|
#if UNICODE
|
if (WideCharToMultiByte(CP_ACP, 0, ustr, nBytes, buf, nBytes, NULL,
|
if (WideCharToMultiByte(CP_ACP, 0, ustr, nBytes, buf, nBytes, NULL,
|
NULL) < 0) {
|
NULL) < 0) {
|
return (char*) ustr;
|
return (char*) ustr;
|
}
|
}
|
#else
|
#else
|
memcpy(buf, ustr, nBytes);
|
memcpy(buf, ustr, nBytes);
|
#endif
|
#endif
|
return (char*) buf;
|
return (char*) buf;
|
}
|
}
|
|
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* allocate (balloc) a buffer and do ascii to unicode conversion into it.
|
* allocate (balloc) a buffer and do ascii to unicode conversion into it.
|
* cp points to the ascii string which must be NULL terminated.
|
* cp points to the ascii string which must be NULL terminated.
|
* Return a pointer to the unicode buffer which must be bfree'd later.
|
* Return a pointer to the unicode buffer which must be bfree'd later.
|
* Return NULL on failure to get buffer.
|
* Return NULL on failure to get buffer.
|
*/
|
*/
|
char_t *ballocAscToUni(char * cp)
|
char_t *ballocAscToUni(char * cp)
|
{
|
{
|
char_t * unip;
|
char_t * unip;
|
int ulen;
|
int ulen;
|
|
|
ulen = (strlen(cp) + 1) * sizeof(char_t);
|
ulen = (strlen(cp) + 1) * sizeof(char_t);
|
if ((unip = balloc(B_L, ulen)) == NULL) {
|
if ((unip = balloc(B_L, ulen)) == NULL) {
|
return NULL;
|
return NULL;
|
}
|
}
|
ascToUni(unip, cp, ulen);
|
ascToUni(unip, cp, ulen);
|
return unip;
|
return unip;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
/*
|
/*
|
* allocate (balloc) a buffer and do unicode to ascii conversion into it.
|
* allocate (balloc) a buffer and do unicode to ascii conversion into it.
|
* unip points to the unicoded string. ulen is the number of characters
|
* unip points to the unicoded string. ulen is the number of characters
|
* in the unicode string including teminating null, if there is one.
|
* in the unicode string including teminating null, if there is one.
|
* Return a pointer to the ascii buffer which must be bfree'd later.
|
* Return a pointer to the ascii buffer which must be bfree'd later.
|
* Return NULL on failure to get buffer.
|
* Return NULL on failure to get buffer.
|
*/
|
*/
|
char *ballocUniToAsc(char_t * unip, int ulen)
|
char *ballocUniToAsc(char_t * unip, int ulen)
|
{
|
{
|
char * cp;
|
char * cp;
|
|
|
if ((cp = balloc(B_L, ulen)) == NULL) {
|
if ((cp = balloc(B_L, ulen)) == NULL) {
|
return NULL;
|
return NULL;
|
}
|
}
|
uniToAsc(cp, unip, ulen);
|
uniToAsc(cp, unip, ulen);
|
return cp;
|
return cp;
|
}
|
}
|
|
|
/******************************************************************************/
|
/******************************************************************************/
|
|
|
|
|