/************************************
* 版权所有(C) 2003-2050, Y&D.
* 文 件 名 : util.c
* 作 者 : 唐 威
* 完成日期 : 2004年 04月 19日
* 描 述 : 通用工具函数模块
* 修改记录 : 作者 时间 版本 描述
* 数据结构 :
* 备 注 :
************************************/
#include "en_util.h"
#include "en_derror.h"
#include "en_xmalloc.h"
#define NOTRACE
#include "en_debug.h"
#define IN_UTIL_C
#include "en_util_charset.c"
char *str_lcase (char *s)
{
register char *p;
TRACE0;
ASSERT (s != NULL);
p = s;
while (*p)
{
*p = to_lower (*p);
p++;
}
return s;
}
char *str_ucase (char *s)
{
register char *p;
TRACE0;
ASSERT (s != NULL);
p = s;
while (*p)
{
*p = to_upper (*p);
p++;
}
return s;
}
char *str_rtrim (char *s)
{
register char *p;
//TRACE0;
ASSERT (s != NULL);
p = s + strlen (s);
if (p > s)
{
do { --p; } while (is_space (*p));
p[1] = '\0';
}
return s;
}
char *str_ltrim (char *s)
{
register char *p, *q;;
//TRACE0;
ASSERT (s != NULL);
p = q = s;
while (is_space (*q))
++q;
while (*q)
*p++ = *q++;
*p = '\0';
return s;
}
char *str_strip(char* s, char c)
{
register char *p, *q, *k;
if (c == '\0') return s;
p = q = s;
do{
while((*p) == c) ++p; //www@11www11www - w
if (*p == '\0') { *q='\0'; break; }
k = q;
while((*p) && (*p) != c && p != q) { *q++=*p++; }
if (q == k) { ++q; } else { *q = '\0'; }
}while((*p++) !='\0') ;
if (s == q) *s='\0';
return s;
}
/*
char *str_strip(char* s, char c)
{
register char *p, *q, *k;
if (c == '\0') return s;
p = q = s;
do{
while ((*p)!='\0' && (*p) != c) ++p;
if ((*p) == c) {
q = k = p; ++p;
while((*p) == c) ++p;
while((*q++=*p++)!='\0');
p = k;
}
}while((*p++) !='\0');
return s;
}
*/
char *str_cut (char *s, size_t len)
{
register char *p;
register int n;
TRACE0;
ASSERT (s != NULL);
p = s + len;
if (p < (s + strlen (s)))
{
*p = '\0';
n = 3;
/* while (n-- && (--p > s))
*p = '.'; */
}
return s;
}
/* 去掉指定串末尾的'\n'字符(一般用于fgets) */
char *str_chomp (char *s)
{
size_t L;
//TRACE0;
ASSERT (s != NULL);
L = strlen (s);
if ((L > 0) && (s [L - 1] == '\n'))
s [L - 1] = '\0';
return s;
}
char *str_toline (char *s)
{
register char *i;
TRACE0;
ASSERT (s != NULL);
i = s;
while (*i)
{
if ((*i < 32) || (*i > 126))
*i = '.';
i++;
}
return s;
}
int str_cmpsep (const char *a, const char *b, char sep)
{
TRACE0;
ASSERT (a != NULL);
ASSERT (b != NULL);
while (*a && (*a != sep))
{
if ((*a > *b) || (*b == sep) || (*b == '\0'))
return 1;
else if (*a < *b)
return -1;
a++;
b++;
}
return ((*b == sep) || (*b == '\0')) ? 0 : 1;
}
int str_casecmpsep (const char *a, const char *b, char sep)
{
register char ca, cb;
TRACE0;
ASSERT (a != NULL);
ASSERT (b != NULL);
sep = to_lower (sep);
while ((ca = to_lower (*a)) && (ca != sep))
{
cb = to_lower (*b);
if ((ca > cb) || (cb == sep) || (cb == '\0'))
return 1;
else if (ca < cb)
return -1;
a++;
b++;
}
cb = to_lower (*b);
return ((cb == sep) || (cb == '\0')) ? 0 : 1;
}
char *str_ncpy (char *dest, const char *src, size_t size)
{
register char *d;
TRACE0;
ASSERT (dest != NULL);
ASSERT (src != NULL);
d = dest;
while (*src)
{
if (size < 2)
{
*d = '\0';
return NULL;
}
size--;
*d++ = *src++;
}
*d = '\0';
return dest;
}
char *str_lncpy (char *dest, const char *src, size_t size, size_t len)
{
register char *d;
TRACE0;
ASSERT (dest != NULL);
ASSERT (src != NULL);
d = dest;
while (*src && len)
{
len--;
if (size < 2)
{
*d = '\0';
return NULL;
}
*d++ = *src++;
size--;
}
*d = '\0';
return dest;
}
size_t str_lencat (const char *s, ...)
{
register char *p;
register size_t L;
va_list args;
TRACE0;
ASSERT (s != NULL);
p = (char *) s;
L = 0;
va_start (args, s);
while (s != NULL)
{
while (*p++)
L++;
p = va_arg (args, char *);
}
va_end (args);
return L;
}
char *str_ncat (char *dest, size_t size, ...)
{
register char *p, *q;
va_list args;
TRACE0;
ASSERT (dest != NULL);
q = dest;
va_start (args, size);
while ((p = va_arg (args, char *)) != NULL)
{
while (*p)
{
if (size < 2)
{
*q = '\0';
va_end (args);
return NULL;
}
*q++ = *p++;
size--;
}
}
va_end (args);
*q = '\0';
return dest;
}
char *str_cat (char *dest, ...)
{
register char *p, *q;
va_list args;
TRACE0;
ASSERT (dest != NULL);
q = dest;
va_start (args, dest);
while ((p = va_arg (args, char *)) != NULL)
while (*p != '\0')
*q++ = *p++;
va_end (args);
*q = '\0';
return dest;
}
size_t str_lendiv (const char *s, size_t interval)
{
register size_t l;
TRACE0;
ASSERT (s != NULL);
ASSERT (interval > 0);
l = strlen (s);
return l + ((l - 1) / interval);
}
char *str_div (char *dest, const char *src, char delim, size_t interval)
{
register char *p, *q;
register size_t n;
TRACE0;
ASSERT (dest != NULL);
ASSERT (src != NULL);
ASSERT (interval > 0);
STR_LENDIV (n, src, interval);
p = dest + n;
*p-- = '\0';
if (n)
{
q = (char *) src + strlen (src);
n = interval;
while (--q >= src)
{
if (n == 0)
{
*p-- = delim;
n = interval;
}
n--;
*p-- = *q;
}
}
return dest;
}
char *str_casechr (const char *s, char c)
{
TRACE0;
ASSERT (s != NULL);
c = to_lower (c);
while (*s)
{
if (c == to_lower (*s))
return (char *) s;
++s;
}
return NULL;
}
char *str_casestr (const char *s, const char *t)
{
register char *p, *q, c;
TRACE0;
ASSERT (s != NULL);
ASSERT (t != NULL);
c = to_lower (*t);
while (*s)
{
if (c == to_lower (*s))
{
p = (char *) s;
q = (char *) t;
for (;;)
{
++p;
++q;
if (*q == '\0')
return (char *) s;
if (to_lower (*p) != to_lower (*q))
break;
}
}
++s;
}
return NULL;
}
char *str_in (const char *s, ...)
{
register char *p;
va_list args;
TRACE0;
ASSERT (s != NULL);
va_start (args, s);
while ((p = va_arg (args, char *)) != NULL)
if (strcmp (s, p) == 0)
break;
va_end (args);
return p;
}
char *str_casein (const char *s, ...)
{
register char *p;
va_list args;
TRACE0;
ASSERT (s != NULL);
va_start (args, s);
while ((p = va_arg (args, char *)) != NULL)
if (strcasecmp (s, p) == 0)
break;
va_end (args);
return p;
}
size_t str_lenescape (const char *s)
{
register char c;
size_t L;
TRACE0;
ASSERT (s != NULL);
for (L = 0; (c = *s); s++)
{
if (ASCII_HEXESCAPE [(int)((uchar) c)])
L +=