1995-12-14 00:01:36 +08:00
|
|
|
#include <stdlib.h>
|
1996-07-03 03:16:06 +08:00
|
|
|
#include <ctype.h>
|
1997-02-14 11:32:26 +08:00
|
|
|
#include <string.h>
|
1995-12-14 00:01:36 +08:00
|
|
|
#include "stringbuf.h"
|
|
|
|
|
|
|
|
#define BUF_CHUNK 1024
|
|
|
|
|
|
|
|
struct StringBufRec {
|
|
|
|
char *buf;
|
|
|
|
char *tail; /* Points to first "free" char (usually '\0') */
|
|
|
|
int allocated;
|
|
|
|
int free;
|
|
|
|
};
|
|
|
|
|
|
|
|
StringBuf newStringBuf(void)
|
|
|
|
{
|
|
|
|
StringBuf sb = malloc(sizeof(struct StringBufRec));
|
|
|
|
|
|
|
|
sb->buf = malloc(BUF_CHUNK * sizeof(char));
|
|
|
|
sb->buf[0] = '\0';
|
|
|
|
sb->tail = sb->buf;
|
|
|
|
sb->allocated = BUF_CHUNK;
|
|
|
|
sb->free = BUF_CHUNK;
|
|
|
|
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeStringBuf(StringBuf sb)
|
|
|
|
{
|
|
|
|
free(sb->buf);
|
|
|
|
free(sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void truncStringBuf(StringBuf sb)
|
|
|
|
{
|
|
|
|
sb->buf[0] = '\0';
|
|
|
|
sb->tail = sb->buf;
|
|
|
|
sb->free = sb->allocated;
|
|
|
|
}
|
|
|
|
|
1996-07-03 03:16:06 +08:00
|
|
|
void stripTrailingBlanksStringBuf(StringBuf sb)
|
|
|
|
{
|
|
|
|
while (sb->free != sb->allocated) {
|
|
|
|
if (! isspace(*(sb->tail - 1))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sb->free++;
|
|
|
|
sb->tail--;
|
|
|
|
}
|
|
|
|
sb->tail[0] = '\0';
|
|
|
|
}
|
|
|
|
|
1995-12-14 00:01:36 +08:00
|
|
|
char *getStringBuf(StringBuf sb)
|
|
|
|
{
|
|
|
|
return sb->buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void appendStringBufAux(StringBuf sb, char *s, int nl)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
|
|
|
|
l = strlen(s);
|
|
|
|
/* If free == l there is no room for NULL terminator! */
|
1996-02-21 06:25:21 +08:00
|
|
|
while ((l + nl + 1) > sb->free) {
|
1995-12-14 00:01:36 +08:00
|
|
|
sb->allocated += BUF_CHUNK;
|
|
|
|
sb->free += BUF_CHUNK;
|
|
|
|
sb->buf = realloc(sb->buf, sb->allocated);
|
|
|
|
sb->tail = sb->buf + (sb->allocated - sb->free);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(sb->tail, s);
|
|
|
|
sb->tail += l;
|
|
|
|
sb->free -= l;
|
|
|
|
if (nl) {
|
|
|
|
sb->tail[0] = '\n';
|
|
|
|
sb->tail[1] = '\0';
|
|
|
|
sb->tail++;
|
|
|
|
sb->free--;
|
|
|
|
}
|
|
|
|
}
|