pollymc/libraries/hoedown/src/buffer.c

309 lines
6.1 KiB
C
Raw Normal View History

#include "hoedown/buffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
void *
hoedown_malloc(size_t size)
{
2018-07-15 18:21:05 +05:30
void *ret = malloc(size);
2018-07-15 18:21:05 +05:30
if (!ret) {
fprintf(stderr, "Allocation failed.\n");
abort();
}
2018-07-15 18:21:05 +05:30
return ret;
}
void *
hoedown_calloc(size_t nmemb, size_t size)
{
2018-07-15 18:21:05 +05:30
void *ret = calloc(nmemb, size);
2018-07-15 18:21:05 +05:30
if (!ret) {
fprintf(stderr, "Allocation failed.\n");
abort();
}
2018-07-15 18:21:05 +05:30
return ret;
}
void *
hoedown_realloc(void *ptr, size_t size)
{
2018-07-15 18:21:05 +05:30
void *ret = realloc(ptr, size);
2018-07-15 18:21:05 +05:30
if (!ret) {
fprintf(stderr, "Allocation failed.\n");
abort();
}
2018-07-15 18:21:05 +05:30
return ret;
}
void
hoedown_buffer_init(
2018-07-15 18:21:05 +05:30
hoedown_buffer *buf,
size_t unit,
hoedown_realloc_callback data_realloc,
hoedown_free_callback data_free,
hoedown_free_callback buffer_free)
{
2018-07-15 18:21:05 +05:30
assert(buf);
buf->data = NULL;
buf->size = buf->asize = 0;
buf->unit = unit;
buf->data_realloc = data_realloc;
buf->data_free = data_free;
buf->buffer_free = buffer_free;
}
void
hoedown_buffer_uninit(hoedown_buffer *buf)
{
2018-07-15 18:21:05 +05:30
assert(buf && buf->unit);
buf->data_free(buf->data);
}
hoedown_buffer *
hoedown_buffer_new(size_t unit)
{
2018-07-15 18:21:05 +05:30
hoedown_buffer *ret = hoedown_malloc(sizeof (hoedown_buffer));
hoedown_buffer_init(ret, unit, hoedown_realloc, free, free);
return ret;
}
void
hoedown_buffer_free(hoedown_buffer *buf)
{
2018-07-15 18:21:05 +05:30
if (!buf) return;
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
buf->data_free(buf->data);
2018-07-15 18:21:05 +05:30
if (buf->buffer_free)
buf->buffer_free(buf);
}
void
hoedown_buffer_reset(hoedown_buffer *buf)
{
2018-07-15 18:21:05 +05:30
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
buf->data_free(buf->data);
buf->data = NULL;
buf->size = buf->asize = 0;
}
void
hoedown_buffer_grow(hoedown_buffer *buf, size_t neosz)
{
2018-07-15 18:21:05 +05:30
size_t neoasz;
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
if (buf->asize >= neosz)
return;
2018-07-15 18:21:05 +05:30
neoasz = buf->asize + buf->unit;
while (neoasz < neosz)
neoasz += buf->unit;
2018-07-15 18:21:05 +05:30
buf->data = (uint8_t *) buf->data_realloc(buf->data, neoasz);
buf->asize = neoasz;
}
void
hoedown_buffer_put(hoedown_buffer *buf, const uint8_t *data, size_t size)
{
2018-07-15 18:21:05 +05:30
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
if (buf->size + size > buf->asize)
hoedown_buffer_grow(buf, buf->size + size);
2018-07-15 18:21:05 +05:30
memcpy(buf->data + buf->size, data, size);
buf->size += size;
}
void
hoedown_buffer_puts(hoedown_buffer *buf, const char *str)
{
2018-07-15 18:21:05 +05:30
hoedown_buffer_put(buf, (const uint8_t *)str, strlen(str));
}
void
hoedown_buffer_putc(hoedown_buffer *buf, uint8_t c)
{
2018-07-15 18:21:05 +05:30
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
if (buf->size >= buf->asize)
hoedown_buffer_grow(buf, buf->size + 1);
2018-07-15 18:21:05 +05:30
buf->data[buf->size] = c;
buf->size += 1;
}
int
hoedown_buffer_putf(hoedown_buffer *buf, FILE *file)
{
2018-07-15 18:21:05 +05:30
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
while (!(feof(file) || ferror(file))) {
hoedown_buffer_grow(buf, buf->size + buf->unit);
buf->size += fread(buf->data + buf->size, 1, buf->unit, file);
}
2018-07-15 18:21:05 +05:30
return ferror(file);
}
void
hoedown_buffer_set(hoedown_buffer *buf, const uint8_t *data, size_t size)
{
2018-07-15 18:21:05 +05:30
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
if (size > buf->asize)
hoedown_buffer_grow(buf, size);
2018-07-15 18:21:05 +05:30
memcpy(buf->data, data, size);
buf->size = size;
}
void
hoedown_buffer_sets(hoedown_buffer *buf, const char *str)
{
2018-07-15 18:21:05 +05:30
hoedown_buffer_set(buf, (const uint8_t *)str, strlen(str));
}
int
hoedown_buffer_eq(const hoedown_buffer *buf, const uint8_t *data, size_t size)
{
2018-07-15 18:21:05 +05:30
if (buf->size != size) return 0;
return memcmp(buf->data, data, size) == 0;
}
int
hoedown_buffer_eqs(const hoedown_buffer *buf, const char *str)
{
2018-07-15 18:21:05 +05:30
return hoedown_buffer_eq(buf, (const uint8_t *)str, strlen(str));
}
int
hoedown_buffer_prefix(const hoedown_buffer *buf, const char *prefix)
{
2018-07-15 18:21:05 +05:30
size_t i;
2018-07-15 18:21:05 +05:30
for (i = 0; i < buf->size; ++i) {
if (prefix[i] == 0)
return 0;
2018-07-15 18:21:05 +05:30
if (buf->data[i] != prefix[i])
return buf->data[i] - prefix[i];
}
2018-07-15 18:21:05 +05:30
return 0;
}
void
hoedown_buffer_slurp(hoedown_buffer *buf, size_t size)
{
2018-07-15 18:21:05 +05:30
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
if (size >= buf->size) {
buf->size = 0;
return;
}
2018-07-15 18:21:05 +05:30
buf->size -= size;
memmove(buf->data, buf->data + size, buf->size);
}
const char *
hoedown_buffer_cstr(hoedown_buffer *buf)
{
2018-07-15 18:21:05 +05:30
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
if (buf->size < buf->asize && buf->data[buf->size] == 0)
return (char *)buf->data;
2018-07-15 18:21:05 +05:30
hoedown_buffer_grow(buf, buf->size + 1);
buf->data[buf->size] = 0;
2018-07-15 18:21:05 +05:30
return (char *)buf->data;
}
void
hoedown_buffer_printf(hoedown_buffer *buf, const char *fmt, ...)
{
2018-07-15 18:21:05 +05:30
va_list ap;
int n;
2018-07-15 18:21:05 +05:30
assert(buf && buf->unit);
2018-07-15 18:21:05 +05:30
if (buf->size >= buf->asize)
hoedown_buffer_grow(buf, buf->size + 1);
2018-07-15 18:21:05 +05:30
va_start(ap, fmt);
n = vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
va_end(ap);
2018-07-15 18:21:05 +05:30
if (n < 0) {
#ifndef _MSC_VER
2018-07-15 18:21:05 +05:30
return;
#else
2018-07-15 18:21:05 +05:30
va_start(ap, fmt);
n = _vscprintf(fmt, ap);
va_end(ap);
#endif
2018-07-15 18:21:05 +05:30
}
2018-07-15 18:21:05 +05:30
if ((size_t)n >= buf->asize - buf->size) {
hoedown_buffer_grow(buf, buf->size + n + 1);
2018-07-15 18:21:05 +05:30
va_start(ap, fmt);
n = vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
va_end(ap);
}
2018-07-15 18:21:05 +05:30
if (n < 0)
return;
2018-07-15 18:21:05 +05:30
buf->size += n;
}
void hoedown_buffer_put_utf8(hoedown_buffer *buf, unsigned int c) {
2018-07-15 18:21:05 +05:30
unsigned char unichar[4];
assert(buf && buf->unit);
if (c < 0x80) {
hoedown_buffer_putc(buf, c);
}
else if (c < 0x800) {
unichar[0] = 192 + (c / 64);
unichar[1] = 128 + (c % 64);
hoedown_buffer_put(buf, unichar, 2);
}
else if (c - 0xd800u < 0x800) {
HOEDOWN_BUFPUTSL(buf, "\xef\xbf\xbd");
}
else if (c < 0x10000) {
unichar[0] = 224 + (c / 4096);
unichar[1] = 128 + (c / 64) % 64;
unichar[2] = 128 + (c % 64);
hoedown_buffer_put(buf, unichar, 3);
}
else if (c < 0x110000) {
unichar[0] = 240 + (c / 262144);
unichar[1] = 128 + (c / 4096) % 64;
unichar[2] = 128 + (c / 64) % 64;
unichar[3] = 128 + (c % 64);
hoedown_buffer_put(buf, unichar, 4);
}
else {
HOEDOWN_BUFPUTSL(buf, "\xef\xbf\xbd");
}
}