2014-10-11 23:03:21 +07:00
|
|
|
/*
|
|
|
|
* libkmod - interface to kernel module operations
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011-2013 ProFUSION embedded systems
|
|
|
|
* Copyright (C) 2014 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
|
|
|
|
#define BUF_STEP (2048)
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
static bool buf_grow(struct strbuf *buf, size_t newsize)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
void *tmp;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
if (newsize % BUF_STEP == 0)
|
|
|
|
sz = newsize;
|
|
|
|
else
|
|
|
|
sz = ((newsize / BUF_STEP) + 1) * BUF_STEP;
|
|
|
|
|
|
|
|
if (buf->size == sz)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
tmp = realloc(buf->bytes, sz);
|
|
|
|
if (sz > 0 && tmp == NULL)
|
|
|
|
return false;
|
|
|
|
buf->bytes = tmp;
|
|
|
|
buf->size = sz;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
void strbuf_init(struct strbuf *buf)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
buf->bytes = NULL;
|
|
|
|
buf->size = 0;
|
|
|
|
buf->used = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
void strbuf_release(struct strbuf *buf)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
free(buf->bytes);
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
char *strbuf_steal(struct strbuf *buf)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
char *bytes;
|
|
|
|
|
|
|
|
bytes = realloc(buf->bytes, buf->used + 1);
|
|
|
|
if (!bytes) {
|
|
|
|
free(buf->bytes);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bytes[buf->used] = '\0';
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
const char *strbuf_str(struct strbuf *buf)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
if (!buf_grow(buf, buf->used + 1))
|
|
|
|
return NULL;
|
|
|
|
buf->bytes[buf->used] = '\0';
|
|
|
|
return buf->bytes;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
bool strbuf_pushchar(struct strbuf *buf, char ch)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
if (!buf_grow(buf, buf->used + 1))
|
|
|
|
return false;
|
|
|
|
buf->bytes[buf->used] = ch;
|
|
|
|
buf->used++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
unsigned strbuf_pushchars(struct strbuf *buf, const char *str)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
unsigned i = 0;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = str[i])) {
|
2014-10-11 23:25:51 +07:00
|
|
|
strbuf_pushchar(buf, ch);
|
2014-10-11 23:03:21 +07:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
void strbuf_popchar(struct strbuf *buf)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
assert(buf->used > 0);
|
|
|
|
buf->used--;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
void strbuf_popchars(struct strbuf *buf, unsigned n)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
assert(buf->used >= n);
|
|
|
|
buf->used -= n;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:25:51 +07:00
|
|
|
void strbuf_clear(struct strbuf *buf)
|
2014-10-11 23:03:21 +07:00
|
|
|
{
|
|
|
|
buf->used = 0;
|
|
|
|
}
|
|
|
|
|