whelk/string.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Aug 25 20:41:08 2011 +0100 (2011-08-25)
changeset 16 579831f324c7
permissions -rw-r--r--
Fix checks & usage for softpub.h and mscat.h
ali@3
     1
#include <stdlib.h>
ali@3
     2
#include "_whelk.h"
ali@3
     3
ali@3
     4
char *whelk_string_prealloc(struct whelk_string *string,size_t len)
ali@3
     5
{
ali@3
     6
    char *new;
ali@3
     7
    if (string->alloc<string->len+len)
ali@3
     8
    {
ali@3
     9
	new=realloc(string->buffer,string->len+len);
ali@3
    10
	if (!new)
ali@3
    11
	    return NULL;
ali@3
    12
	string->buffer=new;
ali@3
    13
	string->alloc=string->len+len;
ali@3
    14
    }
ali@3
    15
    return string->buffer+string->len;
ali@3
    16
}
ali@3
    17
ali@3
    18
void whelk_string_seek(struct whelk_string *string,ssize_t offset)
ali@3
    19
{
ali@3
    20
    if (offset>=0 || string->len>=(size_t)-offset)
ali@3
    21
	string->len+=offset;
ali@3
    22
    else
ali@3
    23
	string->len=0;
ali@3
    24
}
ali@3
    25
ali@3
    26
char *whelk_string_finalize(struct whelk_string *string)
ali@3
    27
{
ali@3
    28
    whelk_string_prealloc(string,1);
ali@3
    29
    string->buffer[string->len]='\0';
ali@3
    30
    return string->buffer;
ali@3
    31
}
ali@3
    32
ali@3
    33
void whelk_string_free(struct whelk_string *string)
ali@3
    34
{
ali@3
    35
    free(string->buffer);
ali@3
    36
    memset(string,0,sizeof(*string));
ali@3
    37
}