2012-05-19 04:32:03 +00:00
|
|
|
/*
|
|
|
|
Serval string buffer primitives
|
|
|
|
Copyright (C) 2012 The Serval Project
|
2012-05-20 03:32:41 +00:00
|
|
|
|
2012-05-19 04:32:03 +00:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
2012-05-20 03:32:41 +00:00
|
|
|
|
2012-05-19 04:32:03 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2012-05-20 03:32:41 +00:00
|
|
|
|
2012-05-19 04:32:03 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
#define __STRBUF_INLINE
|
2012-05-19 04:32:03 +00:00
|
|
|
#include "strbuf.h"
|
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
static inline size_t min(size_t a, size_t b) {
|
|
|
|
return a < b ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf strbuf_init(strbuf sb, char *buffer, size_t size)
|
2012-05-19 04:32:03 +00:00
|
|
|
{
|
|
|
|
sb->start = sb->current = buffer;
|
|
|
|
sb->end = sb->start + size - 1;
|
|
|
|
if (sb->start && sb->end >= sb->start) {
|
|
|
|
*sb->start = '\0';
|
2012-05-20 03:32:41 +00:00
|
|
|
*sb->end = '\0'; // should never get overwritten
|
|
|
|
}
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf strbuf_ncat(strbuf sb, const char *text, size_t len)
|
|
|
|
{
|
|
|
|
if (sb->start && sb->current < sb->end) {
|
|
|
|
register size_t n = min(sb->end - sb->current, len);
|
|
|
|
while (n-- && (*sb->current = *text++))
|
|
|
|
++sb->current;
|
2012-05-19 04:32:03 +00:00
|
|
|
}
|
2012-05-20 03:32:41 +00:00
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf strbuf_puts(strbuf sb, const char *text)
|
|
|
|
{
|
|
|
|
if (sb->start && sb->current < sb->end) {
|
|
|
|
register size_t n = sb->end - sb->current;
|
|
|
|
while (n-- && (*sb->current = *text++))
|
|
|
|
++sb->current;
|
|
|
|
}
|
|
|
|
return sb;
|
2012-05-19 04:32:03 +00:00
|
|
|
}
|
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
strbuf strbuf_putc(strbuf sb, char ch)
|
2012-05-19 04:32:03 +00:00
|
|
|
{
|
|
|
|
if (sb->start && sb->current < sb->end) {
|
2012-05-20 03:32:41 +00:00
|
|
|
*sb->current++ = ch;
|
|
|
|
*sb->current = '\0';
|
2012-05-19 04:32:03 +00:00
|
|
|
}
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
int strbuf_sprintf(strbuf sb, const char *fmt, ...)
|
2012-05-19 04:32:03 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int n = strbuf_vsprintf(sb, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
int strbuf_vsprintf(strbuf sb, const char *fmt, va_list ap)
|
2012-05-19 04:32:03 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
if (sb->start && sb->current < sb->end) {
|
2012-05-23 06:37:52 +00:00
|
|
|
n = vsnprintf(sb->current, sb->end - sb->current + 1, fmt, ap);
|
2012-05-22 00:53:56 +00:00
|
|
|
*sb->end = '\0';
|
2012-05-19 04:32:03 +00:00
|
|
|
} else {
|
|
|
|
char tmp[1];
|
2012-05-23 06:37:52 +00:00
|
|
|
n = vsnprintf(tmp, sizeof tmp, fmt, ap);
|
2012-05-19 04:32:03 +00:00
|
|
|
}
|
|
|
|
if (n != -1)
|
|
|
|
sb->current += n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
char *strbuf_substr(const_strbuf sb, int offset)
|
2012-05-19 04:32:03 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
if (offset < 0) {
|
|
|
|
s = sb->end + offset;
|
|
|
|
if (s < sb->start)
|
|
|
|
s = sb->start;
|
|
|
|
} else {
|
|
|
|
s = sb->start + offset;
|
|
|
|
if (s > sb->end)
|
|
|
|
s = sb->end;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|