2006-08-31 16:00:20 +00:00
|
|
|
/*
|
2006-10-22 11:34:53 +00:00
|
|
|
* $Id: str.c,v 1.9 2006-10-22 11:34:53 bacon Exp $
|
2006-08-31 16:00:20 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
#include <sse/awk/awk_i.h>
|
2006-08-31 16:00:20 +00:00
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_awk_str_t* sse_awk_str_open (
|
|
|
|
sse_awk_str_t* str, sse_size_t capa, sse_awk_t* awk)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (str == SSE_NULL)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
str = (sse_awk_str_t*) SSE_AWK_MALLOC (awk, sizeof(sse_awk_str_t));
|
|
|
|
if (str == SSE_NULL) return SSE_NULL;
|
|
|
|
str->__dynamic = sse_true;
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
2006-10-22 11:34:53 +00:00
|
|
|
else str->__dynamic = sse_false;
|
2006-08-31 16:00:20 +00:00
|
|
|
|
|
|
|
str->awk = awk;
|
2006-10-22 11:34:53 +00:00
|
|
|
str->buf = (sse_char_t*) SSE_AWK_MALLOC (
|
|
|
|
awk, sse_sizeof(sse_char_t) * (capa + 1));
|
|
|
|
if (str->buf == SSE_NULL)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (str->__dynamic) SSE_AWK_FREE (awk, str);
|
|
|
|
return SSE_NULL;
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
str->size = 0;
|
|
|
|
str->capa = capa;
|
2006-10-22 11:34:53 +00:00
|
|
|
str->buf[0] = SSE_T('\0');
|
2006-08-31 16:00:20 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
void sse_awk_str_close (sse_awk_str_t* str)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
SSE_AWK_FREE (str->awk, str->buf);
|
|
|
|
if (str->__dynamic) SSE_AWK_FREE (str->awk, str);
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
void sse_awk_str_forfeit (sse_awk_str_t* str)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (str->__dynamic) SSE_AWK_FREE (str->awk, str);
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
void sse_awk_str_swap (sse_awk_str_t* str, sse_awk_str_t* str1)
|
2006-09-10 15:50:34 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_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-22 11:34:53 +00:00
|
|
|
sse_size_t sse_awk_str_cpy (sse_awk_str_t* str, const sse_char_t* s)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
|
|
|
/* TODO: improve it */
|
2006-10-22 11:34:53 +00:00
|
|
|
return sse_awk_str_ncpy (str, s, sse_awk_strlen(s));
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_size_t sse_awk_str_ncpy (sse_awk_str_t* str, const sse_char_t* s, sse_size_t len)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_char_t* buf;
|
2006-08-31 16:00:20 +00:00
|
|
|
|
|
|
|
if (len > str->capa)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
buf = (sse_char_t*) SSE_AWK_MALLOC (
|
|
|
|
str->awk, sse_sizeof(sse_char_t) * (len + 1));
|
|
|
|
if (buf == SSE_NULL) return (sse_size_t)-1;
|
2006-08-31 16:00:20 +00:00
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
SSE_AWK_FREE (str->awk, str->buf);
|
2006-08-31 16:00:20 +00:00
|
|
|
str->capa = len;
|
|
|
|
str->buf = buf;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
str->size = sse_awk_strncpy (str->buf, s, len);
|
|
|
|
str->buf[str->size] = SSE_T('\0');
|
2006-08-31 16:00:20 +00:00
|
|
|
return str->size;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_size_t sse_awk_str_cat (sse_awk_str_t* str, const sse_char_t* s)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
|
|
|
/* TODO: improve it */
|
2006-10-22 11:34:53 +00:00
|
|
|
return sse_awk_str_ncat (str, s, sse_awk_strlen(s));
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_size_t sse_awk_str_ncat (sse_awk_str_t* str, const sse_char_t* s, sse_size_t len)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
|
|
|
if (len > str->capa - str->size)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_char_t* tmp;
|
|
|
|
sse_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;
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
if (str->awk->syscas.realloc != SSE_NULL)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
tmp = (sse_char_t*) SSE_AWK_REALLOC (
|
2006-08-31 16:00:20 +00:00
|
|
|
str->awk, str->buf,
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_sizeof(sse_char_t) * (capa + 1));
|
|
|
|
if (tmp == SSE_NULL) return (sse_size_t)-1;
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
tmp = (sse_char_t*) SSE_AWK_MALLOC (
|
|
|
|
str->awk, sse_sizeof(sse_char_t) * (capa + 1));
|
|
|
|
if (tmp == SSE_NULL) return (sse_size_t)-1;
|
|
|
|
if (str->buf != SSE_NULL)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
SSE_AWK_MEMCPY (str->awk, tmp, str->buf,
|
|
|
|
sse_sizeof(sse_char_t) * (str->capa + 1));
|
|
|
|
SSE_AWK_FREE (str->awk, str->buf);
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str->capa = capa;
|
|
|
|
str->buf = tmp;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
str->size += sse_awk_strncpy (&str->buf[str->size], s, len);
|
|
|
|
str->buf[str->size] = SSE_T('\0');
|
2006-08-31 16:00:20 +00:00
|
|
|
return str->size;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_size_t sse_awk_str_ccat (sse_awk_str_t* str, sse_char_t c)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
return sse_awk_str_ncat (str, &c, 1);
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
sse_size_t sse_awk_str_nccat (sse_awk_str_t* str, sse_char_t c, sse_size_t len)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
|
|
|
while (len > 0)
|
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
if (sse_awk_str_ncat (str, &c, 1) == (sse_size_t)-1)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
2006-10-22 11:34:53 +00:00
|
|
|
return (sse_size_t)-1;
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
return str->size;
|
|
|
|
}
|
|
|
|
|
2006-10-22 11:34:53 +00:00
|
|
|
void sse_awk_str_clear (sse_awk_str_t* str)
|
2006-08-31 16:00:20 +00:00
|
|
|
{
|
|
|
|
str->size = 0;
|
2006-10-22 11:34:53 +00:00
|
|
|
str->buf[0] = SSE_T('\0');
|
2006-08-31 16:00:20 +00:00
|
|
|
}
|
|
|
|
|