qse/ase/awk/sa.c

571 lines
10 KiB
C
Raw Normal View History

2006-01-20 07:33:46 +00:00
/*
2006-07-17 04:17:40 +00:00
* $Id: sa.c,v 1.28 2006-07-17 04:17:40 bacon Exp $
2006-01-20 07:33:46 +00:00
*/
2006-03-31 16:35:37 +00:00
#include <xp/awk/awk_i.h>
2006-01-20 07:33:46 +00:00
2006-04-16 04:31:38 +00:00
#ifdef XP_AWK_STAND_ALONE
2006-01-20 07:33:46 +00:00
2006-01-20 16:28:57 +00:00
static xp_char_t* __adjust_format (const xp_char_t* format);
2006-03-29 16:39:04 +00:00
xp_size_t xp_strlen (const xp_char_t* str)
{
const xp_char_t* p = str;
2006-05-06 12:52:36 +00:00
while (*p != XP_T('\0')) p++;
2006-03-29 16:39:04 +00:00
return p - str;
}
2006-01-20 16:02:03 +00:00
xp_char_t* xp_strdup (const xp_char_t* str)
{
xp_char_t* tmp;
2006-03-27 14:40:58 +00:00
tmp = (xp_char_t*) xp_malloc (
(xp_strlen(str) + 1) * xp_sizeof(xp_char_t));
2006-01-20 16:02:03 +00:00
if (tmp == XP_NULL) return XP_NULL;
xp_strcpy (tmp, str);
return tmp;
}
2006-03-05 17:07:33 +00:00
xp_char_t* xp_strxdup (const xp_char_t* str, xp_size_t len)
{
xp_char_t* tmp;
2006-03-27 14:40:58 +00:00
tmp = (xp_char_t*) xp_malloc ((len + 1) * xp_sizeof(xp_char_t));
2006-03-05 17:07:33 +00:00
if (tmp == XP_NULL) return XP_NULL;
2006-04-06 16:25:37 +00:00
xp_strncpy (tmp, str, len);
return tmp;
}
xp_char_t* xp_strxdup2 (
const xp_char_t* str1, xp_size_t len1,
const xp_char_t* str2, xp_size_t len2)
{
xp_char_t* tmp;
tmp = (xp_char_t*) xp_malloc (
(len1 + len2 + 1) * xp_sizeof(xp_char_t));
if (tmp == XP_NULL) return XP_NULL;
xp_strncpy (tmp, str1, len1);
xp_strncpy (tmp + len1, str2, len2);
2006-03-05 17:07:33 +00:00
return tmp;
}
2006-03-29 16:39:04 +00:00
xp_size_t xp_strcpy (xp_char_t* buf, const xp_char_t* str)
{
xp_char_t* org = buf;
2006-05-06 12:52:36 +00:00
while ((*buf++ = *str++) != XP_T('\0'));
2006-03-29 16:39:04 +00:00
return buf - org - 1;
}
2006-04-06 16:25:37 +00:00
xp_size_t xp_strncpy (xp_char_t* buf, const xp_char_t* str, xp_size_t len)
2006-03-05 17:07:33 +00:00
{
2006-04-06 16:25:37 +00:00
const xp_char_t* end = str + len;
while (str < end) *buf++ = *str++;
2006-05-06 12:52:36 +00:00
*buf = XP_T('\0');
2006-04-06 16:25:37 +00:00
return len;
2006-03-05 17:07:33 +00:00
}
2006-03-29 16:39:04 +00:00
int xp_strcmp (const xp_char_t* s1, const xp_char_t* s2)
{
2006-06-23 11:50:34 +00:00
while (*s1 == *s2)
{
2006-06-28 08:56:59 +00:00
if (*s1 == XP_C('\0')) return 0;
2006-06-23 11:50:34 +00:00
s1++, s2++;
}
return (*s1 > *s2)? 1: -1;
2006-03-29 16:39:04 +00:00
}
2006-04-06 16:25:37 +00:00
int xp_strxncmp (
const xp_char_t* s1, xp_size_t len1,
const xp_char_t* s2, xp_size_t len2)
{
const xp_char_t* end1 = s1 + len1;
const xp_char_t* end2 = s2 + len2;
2006-04-21 16:21:27 +00:00
while (s1 < end1 && s2 < end2 && *s1 == *s2) s1++, s2++;
2006-04-06 16:25:37 +00:00
if (s1 == end1 && s2 == end2) return 0;
if (*s1 == *s2) return (s1 < end1)? 1: -1;
return (*s1 > *s2)? 1: -1;
}
2006-07-05 16:20:23 +00:00
xp_char_t* xp_strtok (const xp_char_t* s,
const xp_char_t* delim, xp_char_t** tok, xp_size_t* tok_len)
{
const xp_char_t* p = s, *d;
const xp_char_t* sp = XP_NULL, * ep = XP_NULL;
xp_char_t c;
int delim_mode;
/* skip preceding space xp_char_tacters */
while (/* *p != XP_T('\0') && */ xp_isspace(*p)) p++;
if (delim == XP_NULL) delim_mode = 0;
else {
delim_mode = 1;
for (d = delim; *d != XP_T('\0'); d++)
if (!xp_isspace(*d)) delim_mode = 2;
}
if (delim_mode == 0) {
/* when XP_NULL is given as "delim", it has an effect of cutting
preceding and trailing space characters off "s". */
2006-07-07 09:48:23 +00:00
while ((c = *p) != XP_T('\0'))
{
if (!xp_isspace(c))
{
2006-07-05 16:20:23 +00:00
if (sp == XP_NULL) sp = p;
ep = p;
}
p++;
}
}
2006-07-07 09:48:23 +00:00
else if (delim_mode == 1)
{
while ((c = *p) != XP_T('\0'))
{
2006-07-05 16:20:23 +00:00
if (xp_isspace(c)) break;
if (sp == XP_NULL) sp = p;
ep = p++;
}
}
2006-07-07 09:48:23 +00:00
else /* if (delim_mode == 2) */
{
2006-07-05 16:20:23 +00:00
while ((c = *p) != XP_T('\0')) {
if (xp_isspace(c)) {
p++;
continue;
}
for (d = delim; *d; d++) {
if (c == *d) {
goto exit_loop;
}
}
if (sp == XP_NULL) sp = p;
ep = p++;
}
}
exit_loop:
if (sp == XP_NULL) {
*tok = XP_NULL;
*tok_len = (xp_size_t)0;
}
else {
*tok = (xp_char_t*)sp;
*tok_len = ep - sp + 1;
}
return (c == XP_T('\0'))? XP_NULL: ((xp_char_t*)++p);
}
xp_char_t* xp_strxtok (const xp_char_t* s, xp_size_t len,
const xp_char_t* delim, xp_char_t** tok, xp_size_t* tok_len)
{
const xp_char_t* p = s, *d;
const xp_char_t* end = s + len;
const xp_char_t* sp = XP_NULL, * ep = XP_NULL;
xp_char_t c;
int delim_mode;
/* skip preceding space xp_char_tacters */
while (p < end && xp_isspace(*p)) p++;
if (delim == XP_NULL) delim_mode = 0;
else
{
delim_mode = 1;
for (d = delim; *d != XP_T('\0'); d++)
if (!xp_isspace(*d)) delim_mode = 2;
}
if (delim_mode == 0)
{
/* when XP_NULL is given as "delim", it has an effect of cutting
preceding and trailing space xp_char_tacters off "s". */
2006-07-07 09:48:23 +00:00
while (p < end)
{
2006-07-05 16:20:23 +00:00
c = *p;
2006-07-07 09:48:23 +00:00
if (!xp_isspace(c))
{
2006-07-05 16:20:23 +00:00
if (sp == XP_NULL) sp = p;
ep = p;
}
p++;
}
}
else if (delim_mode == 1)
{
while (p < end)
{
c = *p;
if (xp_isspace(c)) break;
if (sp == XP_NULL) sp = p;
ep = p++;
}
}
2006-07-07 09:48:23 +00:00
else /* if (delim_mode == 2) */
2006-07-05 16:20:23 +00:00
{
while (p < end)
{
c = *p;
if (xp_isspace(c))
{
p++;
continue;
}
for (d = delim; *d != XP_T('\0'); d++)
{
if (c == *d) goto exit_loop;
}
if (sp == XP_NULL) sp = p;
ep = p++;
}
}
exit_loop:
if (sp == XP_NULL)
{
*tok = XP_NULL;
*tok_len = (xp_size_t)0;
}
else
{
*tok = (xp_char_t*)sp;
*tok_len = ep - sp + 1;
}
return (p >= end)? XP_NULL: ((xp_char_t*)++p);
}
2006-01-20 16:28:57 +00:00
int xp_printf (const xp_char_t* fmt, ...)
{
int n;
xp_va_list ap;
xp_va_start (ap, fmt);
n = xp_vprintf (fmt, ap);
xp_va_end (ap);
return n;
}
int xp_vprintf (const xp_char_t* fmt, xp_va_list ap)
{
2006-01-20 16:37:04 +00:00
int n;
xp_char_t* nf = __adjust_format (fmt);
if (nf == XP_NULL) return -1;
#ifdef XP_CHAR_IS_MCHAR
2006-01-20 17:00:36 +00:00
n = vprintf (nf, ap);
2006-01-20 16:37:04 +00:00
#else
2006-01-20 17:00:36 +00:00
n = vwprintf (nf, ap);
2006-01-20 16:37:04 +00:00
#endif
xp_free (nf);
return n;
2006-01-20 16:28:57 +00:00
}
int xp_sprintf (xp_char_t* buf, xp_size_t size, const xp_char_t* fmt, ...)
{
int n;
xp_va_list ap;
xp_va_start (ap, fmt);
n = xp_vsprintf (buf, size, fmt, ap);
xp_va_end (ap);
return n;
}
int xp_vsprintf (xp_char_t* buf, xp_size_t size, const xp_char_t* fmt, xp_va_list ap)
{
int n;
xp_char_t* nf = __adjust_format (fmt);
if (nf == XP_NULL) return -1;
2006-04-30 18:05:07 +00:00
#if defined(dos) || defined(__dos)
n = vsprintf (buf, nf, ap); /* TODO: write your own vsnprintf */
#elif defined(XP_CHAR_IS_MCHAR)
2006-01-20 16:28:57 +00:00
n = vsnprintf (buf, size, nf, ap);
#elif defined(_WIN32)
n = _vsnwprintf (buf, size, nf, ap);
#else
n = vswprintf (buf, size, nf, ap);
#endif
xp_free (nf);
return n;
}
2006-01-20 07:33:46 +00:00
xp_str_t* xp_str_open (xp_str_t* str, xp_size_t capa)
{
2006-04-21 16:21:27 +00:00
if (str == XP_NULL)
{
2006-07-06 13:57:32 +00:00
str = (xp_str_t*) xp_malloc (sizeof(xp_str_t));
2006-01-20 07:33:46 +00:00
if (str == XP_NULL) return XP_NULL;
str->__dynamic = xp_true;
}
else str->__dynamic = xp_false;
2006-04-21 16:21:27 +00:00
str->buf = (xp_char_t*) xp_malloc (xp_sizeof(xp_char_t) * (capa + 1));
if (str->buf == XP_NULL)
{
2006-01-20 07:33:46 +00:00
if (str->__dynamic) xp_free (str);
return XP_NULL;
}
2006-04-19 04:18:43 +00:00
str->size = 0;
2006-01-20 07:33:46 +00:00
str->capa = capa;
2006-05-06 12:52:36 +00:00
str->buf[0] = XP_T('\0');
2006-01-20 07:33:46 +00:00
return str;
}
void xp_str_close (xp_str_t* str)
{
xp_free (str->buf);
if (str->__dynamic) xp_free (str);
}
2006-01-20 16:52:25 +00:00
void xp_str_forfeit (xp_str_t* str)
{
if (str->__dynamic) xp_free (str);
}
2006-07-06 15:54:41 +00:00
xp_size_t xp_str_cpy (xp_str_t* str, const xp_char_t* s)
2006-01-20 07:33:46 +00:00
{
/* TODO: improve it */
2006-07-06 15:54:41 +00:00
return xp_str_ncpy (str, s, xp_strlen(s));
2006-01-20 07:33:46 +00:00
}
2006-07-06 15:54:41 +00:00
xp_size_t xp_str_ncpy (xp_str_t* str, const xp_char_t* s, xp_size_t len)
2006-01-20 07:33:46 +00:00
{
2006-07-06 15:54:41 +00:00
xp_char_t* buf;
if (len > str->capa)
{
buf = (xp_char_t*) xp_malloc (xp_sizeof(xp_char_t) * (len + 1));
if (buf == XP_NULL) return (xp_size_t)-1;
xp_free (str->buf);
str->capa = len;
str->buf = buf;
}
str->size = xp_strncpy (str->buf, s, len);
2006-07-17 04:17:40 +00:00
str->buf[str->size] = XP_T('\0');
2006-07-06 15:54:41 +00:00
return str->size;
}
xp_size_t xp_str_cat (xp_str_t* str, const xp_char_t* s)
{
/* TODO: improve it */
return xp_str_ncat (str, s, xp_strlen(s));
2006-01-20 07:33:46 +00:00
}
xp_size_t xp_str_ncat (xp_str_t* str, const xp_char_t* s, xp_size_t len)
{
xp_char_t* buf;
xp_size_t capa;
2006-04-21 16:21:27 +00:00
if (len > str->capa - str->size)
{
2006-01-20 07:33:46 +00:00
capa = str->size + len;
/* double the capa if necessary for concatenation */
if (capa < str->capa * 2) capa = str->capa * 2;
2006-04-21 16:21:27 +00:00
buf = (xp_char_t*) xp_realloc (
2006-01-20 07:33:46 +00:00
str->buf, xp_sizeof(xp_char_t) * (capa + 1));
if (buf == XP_NULL) return (xp_size_t)-1;
str->capa = capa;
str->buf = buf;
}
2006-07-06 15:54:41 +00:00
str->size += xp_strncpy (&str->buf[str->size], s, len);
2006-05-06 12:52:36 +00:00
str->buf[str->size] = XP_T('\0');
2006-01-20 07:33:46 +00:00
return str->size;
}
xp_size_t xp_str_ccat (xp_str_t* str, xp_char_t c)
{
return xp_str_ncat (str, &c, 1);
}
2006-05-04 15:59:43 +00:00
xp_size_t xp_str_nccat (xp_str_t* str, xp_char_t c, xp_size_t len)
{
while (len > 0)
{
if (xp_str_ncat (str, &c, 1) == (xp_size_t)-1)
{
return (xp_size_t)-1;
}
len--;
}
return str->size;
}
2006-01-20 07:33:46 +00:00
void xp_str_clear (xp_str_t* str)
{
str->size = 0;
2006-05-06 12:52:36 +00:00
str->buf[0] = XP_T('\0');
2006-01-20 07:33:46 +00:00
}
2006-01-20 16:28:57 +00:00
#define MOD_SHORT 1
#define MOD_LONG 2
#define MOD_LONGLONG 3
#define ADDC(str,c) \
do { \
if (xp_str_ccat(&str, c) == (xp_size_t)-1) { \
xp_str_close (&str); \
return XP_NULL; \
} \
} while (0)
static xp_char_t* __adjust_format (const xp_char_t* format)
{
const xp_char_t* fp = format;
2006-01-20 16:52:25 +00:00
xp_char_t* tmp;
2006-01-20 16:28:57 +00:00
xp_str_t str;
xp_char_t ch;
2006-01-20 16:52:25 +00:00
int modifier;
2006-01-20 16:28:57 +00:00
if (xp_str_open (&str, 256) == XP_NULL) return XP_NULL;
2006-05-06 12:52:36 +00:00
while (*fp != XP_T('\0'))
2006-04-19 04:18:43 +00:00
{
2006-05-06 12:52:36 +00:00
while (*fp != XP_T('\0') && *fp != XP_T('%'))
2006-04-19 04:18:43 +00:00
{
2006-01-20 16:28:57 +00:00
ADDC (str, *fp++);
}
2006-05-06 12:52:36 +00:00
if (*fp == XP_T('\0')) break;
xp_assert (*fp == XP_T('%'));
2006-01-20 16:28:57 +00:00
ch = *fp++;
ADDC (str, ch); /* add % */
ch = *fp++;
/* flags */
2006-04-19 04:18:43 +00:00
for (;;)
{
2006-05-06 12:52:36 +00:00
if (ch == XP_T(' ') || ch == XP_T('+') ||
ch == XP_T('-') || ch == XP_T('#'))
2006-04-19 04:18:43 +00:00
{
2006-01-20 16:28:57 +00:00
ADDC (str, ch);
}
2006-05-06 12:52:36 +00:00
else if (ch == XP_T('0'))
2006-04-19 04:18:43 +00:00
{
2006-01-20 16:28:57 +00:00
ADDC (str, ch);
ch = *fp++;
break;
}
else break;
ch = *fp++;
}
/* check the width */
2006-05-06 12:52:36 +00:00
if (ch == XP_T('*')) ADDC (str, ch);
2006-04-19 04:18:43 +00:00
else
{
while (xp_isdigit(ch))
{
2006-01-20 16:28:57 +00:00
ADDC (str, ch);
ch = *fp++;
}
}
/* precision */
2006-05-06 12:52:36 +00:00
if (ch == XP_T('.'))
2006-04-19 04:18:43 +00:00
{
2006-01-20 16:28:57 +00:00
ADDC (str, ch);
ch = *fp++;
2006-05-06 12:52:36 +00:00
if (ch == XP_T('*')) ADDC (str, ch);
2006-04-19 04:18:43 +00:00
else
{
while (xp_isdigit(ch))
{
2006-01-20 16:28:57 +00:00
ADDC (str, ch);
ch = *fp++;
}
}
}
/* modifier */
2006-04-19 04:18:43 +00:00
for (modifier = 0;;)
{
2006-05-06 12:52:36 +00:00
if (ch == XP_T('h')) modifier = MOD_SHORT;
else if (ch == XP_T('l'))
2006-04-19 04:18:43 +00:00
{
2006-01-20 16:28:57 +00:00
modifier = (modifier == MOD_LONG)? MOD_LONGLONG: MOD_LONG;
}
else break;
ch = *fp++;
}
/* type */
2006-05-06 12:52:36 +00:00
if (ch == XP_T('%')) ADDC (str, ch);
else if (ch == XP_T('c') || ch == XP_T('s'))
2006-04-19 04:18:43 +00:00
{
2006-01-20 16:28:57 +00:00
#if !defined(XP_CHAR_IS_MCHAR) && !defined(_WIN32)
ADDC (str, 'l');
#endif
ADDC (str, ch);
}
2006-05-06 12:52:36 +00:00
else if (ch == XP_T('C') || ch == XP_T('S'))
2006-04-19 04:18:43 +00:00
{
2006-01-20 16:28:57 +00:00
#ifdef _WIN32
ADDC (str, ch);
#else
#ifdef XP_CHAR_IS_MCHAR
ADDC (str, 'l');
#endif
ADDC (str, xp_tolower(ch));
#endif
}
2006-05-06 12:52:36 +00:00
else if (ch == XP_T('d') || ch == XP_T('i') ||
ch == XP_T('o') || ch == XP_T('u') ||
ch == XP_T('x') || ch == XP_T('X'))
2006-04-19 04:18:43 +00:00
{
if (modifier == MOD_SHORT)
{
2006-01-20 16:28:57 +00:00
ADDC (str, 'h');
}
2006-04-19 04:18:43 +00:00
else if (modifier == MOD_LONG)
{
2006-01-20 16:28:57 +00:00
ADDC (str, 'l');
}
2006-04-19 04:18:43 +00:00
else if (modifier == MOD_LONGLONG)
{
2006-01-20 16:28:57 +00:00
#if defined(_WIN32) && !defined(__LCC__)
ADDC (str, 'I');
ADDC (str, '6');
ADDC (str, '4');
#else
ADDC (str, 'l');
ADDC (str, 'l');
#endif
}
ADDC (str, ch);
}
2006-05-06 12:52:36 +00:00
else if (ch == XP_T('\0')) break;
2006-01-20 16:28:57 +00:00
else ADDC (str, ch);
}
2006-01-20 16:52:25 +00:00
tmp = XP_STR_BUF(&str);
xp_str_forfeit (&str);
return tmp;
2006-01-20 16:28:57 +00:00
}
2006-01-20 07:33:46 +00:00
#endif