qse/ase/awk/str.c

168 lines
3.4 KiB
C
Raw Normal View History

2006-08-31 16:00:20 +00:00
/*
2007-02-03 10:47:41 +00:00
* $Id: str.c,v 1.16 2007-02-03 10:47:41 bacon Exp $
*
* {License}
2006-08-31 16:00:20 +00:00
*/
2006-10-24 04:10:12 +00:00
#include <ase/awk/awk_i.h>
2006-08-31 16:00:20 +00:00
2006-10-24 04:10:12 +00:00
ase_awk_str_t* ase_awk_str_open (
ase_awk_str_t* str, ase_size_t capa, ase_awk_t* awk)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
if (str == ASE_NULL)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
str = (ase_awk_str_t*)
2006-11-29 02:54:17 +00:00
ASE_AWK_MALLOC (awk, ASE_SIZEOF(ase_awk_str_t));
2006-10-24 04:10:12 +00:00
if (str == ASE_NULL) return ASE_NULL;
str->__dynamic = ase_true;
2006-08-31 16:00:20 +00:00
}
2006-10-24 04:10:12 +00:00
else str->__dynamic = ase_false;
2006-08-31 16:00:20 +00:00
str->awk = awk;
2006-10-24 04:10:12 +00:00
str->buf = (ase_char_t*) ASE_AWK_MALLOC (
2006-11-29 02:54:17 +00:00
awk, ASE_SIZEOF(ase_char_t) * (capa + 1));
2006-10-24 04:10:12 +00:00
if (str->buf == ASE_NULL)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
if (str->__dynamic) ASE_AWK_FREE (awk, str);
return ASE_NULL;
2006-08-31 16:00:20 +00:00
}
str->size = 0;
str->capa = capa;
2006-10-24 04:10:12 +00:00
str->buf[0] = ASE_T('\0');
2006-08-31 16:00:20 +00:00
return str;
}
2006-10-24 04:10:12 +00:00
void ase_awk_str_close (ase_awk_str_t* str)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (str->awk, str->buf);
if (str->__dynamic) ASE_AWK_FREE (str->awk, str);
2006-08-31 16:00:20 +00:00
}
2006-10-24 04:10:12 +00:00
void ase_awk_str_forfeit (ase_awk_str_t* str)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
if (str->__dynamic) ASE_AWK_FREE (str->awk, str);
2006-08-31 16:00:20 +00:00
}
2006-10-24 04:10:12 +00:00
void ase_awk_str_swap (ase_awk_str_t* str, ase_awk_str_t* str1)
2006-09-10 15:50:34 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_str_t tmp;
2006-09-10 15:50:34 +00:00
tmp.buf = str->buf;
tmp.size = str->size;
tmp.capa = str->capa;
tmp.awk = str->awk;
str->buf = str1->buf;
str->size = str1->size;
str->capa = str1->capa;
str->awk = str1->awk;
str1->buf = tmp.buf;
str1->size = tmp.size;
str1->capa = tmp.capa;
str1->awk = tmp.awk;
}
2006-10-24 04:10:12 +00:00
ase_size_t ase_awk_str_cpy (ase_awk_str_t* str, const ase_char_t* s)
2006-08-31 16:00:20 +00:00
{
/* TODO: improve it */
2006-10-24 04:10:12 +00:00
return ase_awk_str_ncpy (str, s, ase_awk_strlen(s));
2006-08-31 16:00:20 +00:00
}
2006-10-24 04:10:12 +00:00
ase_size_t ase_awk_str_ncpy (
ase_awk_str_t* str, const ase_char_t* s, ase_size_t len)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* buf;
2006-08-31 16:00:20 +00:00
if (len > str->capa)
{
2006-10-24 04:10:12 +00:00
buf = (ase_char_t*) ASE_AWK_MALLOC (
2006-11-29 02:54:17 +00:00
str->awk, ASE_SIZEOF(ase_char_t) * (len + 1));
2006-10-24 04:10:12 +00:00
if (buf == ASE_NULL) return (ase_size_t)-1;
2006-08-31 16:00:20 +00:00
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (str->awk, str->buf);
2006-08-31 16:00:20 +00:00
str->capa = len;
str->buf = buf;
}
2006-10-24 04:10:12 +00:00
str->size = ase_awk_strncpy (str->buf, s, len);
str->buf[str->size] = ASE_T('\0');
2006-08-31 16:00:20 +00:00
return str->size;
}
2006-10-24 04:10:12 +00:00
ase_size_t ase_awk_str_cat (ase_awk_str_t* str, const ase_char_t* s)
2006-08-31 16:00:20 +00:00
{
/* TODO: improve it */
2006-10-24 04:10:12 +00:00
return ase_awk_str_ncat (str, s, ase_awk_strlen(s));
2006-08-31 16:00:20 +00:00
}
2006-10-24 04:10:12 +00:00
ase_size_t ase_awk_str_ncat (
ase_awk_str_t* str, const ase_char_t* s, ase_size_t len)
2006-08-31 16:00:20 +00:00
{
if (len > str->capa - str->size)
{
2006-10-24 04:10:12 +00:00
ase_char_t* tmp;
ase_size_t capa;
2006-08-31 16:00:20 +00:00
capa = str->size + len;
/* double the capa if necessary for concatenation */
if (capa < str->capa * 2) capa = str->capa * 2;
2007-02-01 08:38:24 +00:00
if (str->awk->prmfns.realloc != ASE_NULL)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
tmp = (ase_char_t*) ASE_AWK_REALLOC (
2006-08-31 16:00:20 +00:00
str->awk, str->buf,
2006-11-29 02:54:17 +00:00
ASE_SIZEOF(ase_char_t) * (capa + 1));
2006-10-24 04:10:12 +00:00
if (tmp == ASE_NULL) return (ase_size_t)-1;
2006-08-31 16:00:20 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
tmp = (ase_char_t*) ASE_AWK_MALLOC (
2006-11-29 02:54:17 +00:00
str->awk, ASE_SIZEOF(ase_char_t) * (capa + 1));
2006-10-24 04:10:12 +00:00
if (tmp == ASE_NULL) return (ase_size_t)-1;
if (str->buf != ASE_NULL)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_MEMCPY (str->awk, tmp, str->buf,
2006-11-29 02:54:17 +00:00
ASE_SIZEOF(ase_char_t) * (str->capa + 1));
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (str->awk, str->buf);
2006-08-31 16:00:20 +00:00
}
}
str->capa = capa;
str->buf = tmp;
}
2006-10-24 04:10:12 +00:00
str->size += ase_awk_strncpy (&str->buf[str->size], s, len);
str->buf[str->size] = ASE_T('\0');
2006-08-31 16:00:20 +00:00
return str->size;
}
2006-10-24 04:10:12 +00:00
ase_size_t ase_awk_str_ccat (ase_awk_str_t* str, ase_char_t c)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
return ase_awk_str_ncat (str, &c, 1);
2006-08-31 16:00:20 +00:00
}
2006-10-24 04:10:12 +00:00
ase_size_t ase_awk_str_nccat (ase_awk_str_t* str, ase_char_t c, ase_size_t len)
2006-08-31 16:00:20 +00:00
{
while (len > 0)
{
2006-10-24 04:10:12 +00:00
if (ase_awk_str_ncat (str, &c, 1) == (ase_size_t)-1)
2006-08-31 16:00:20 +00:00
{
2006-10-24 04:10:12 +00:00
return (ase_size_t)-1;
2006-08-31 16:00:20 +00:00
}
len--;
}
return str->size;
}
2006-10-24 04:10:12 +00:00
void ase_awk_str_clear (ase_awk_str_t* str)
2006-08-31 16:00:20 +00:00
{
str->size = 0;
2006-10-24 04:10:12 +00:00
str->buf[0] = ASE_T('\0');
2006-08-31 16:00:20 +00:00
}