core string buffer implementation.
This commit is contained in:
parent
f35b780e53
commit
92477a7261
4 changed files with 207 additions and 0 deletions
112
src/common/strbuf.c
Normal file
112
src/common/strbuf.c
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/* common/strbuf.c
|
||||
*
|
||||
* Copyright (C) 2010 Henrik Hautakoski <henrik@fiktivkod.org>
|
||||
*
|
||||
* 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <malloc.h>
|
||||
#include <assert.h>
|
||||
#include "strbuf.h"
|
||||
|
||||
#define CHNK_SIZE 128
|
||||
|
||||
static void buf_expand(strbuf_t *s, size_t l) {
|
||||
|
||||
if (l <= s->alloc_size)
|
||||
return;
|
||||
|
||||
do
|
||||
s->alloc_size += CHNK_SIZE;
|
||||
while(l > s->alloc_size);
|
||||
|
||||
s->buf = realloc(s->buf, s->alloc_size);
|
||||
assert(s->buf != NULL);
|
||||
}
|
||||
|
||||
static inline void swap(char *a, char *b) {
|
||||
|
||||
char tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
void strbuf_init(strbuf_t *s) {
|
||||
|
||||
s->buf = NULL;
|
||||
s->len = 0;
|
||||
s->alloc_size = 0;
|
||||
}
|
||||
|
||||
void strbuf_append(strbuf_t *s, char *str, size_t len) {
|
||||
|
||||
if (str == NULL)
|
||||
return;
|
||||
|
||||
buf_expand(s, s->len + len + 1);
|
||||
|
||||
memcpy(s->buf + s->len, str, len);
|
||||
s->len += len;
|
||||
s->buf[s->len] = '\0';
|
||||
}
|
||||
|
||||
void strbuf_trim(strbuf_t *s) {
|
||||
|
||||
strbuf_rtrim(s);
|
||||
strbuf_ltrim(s);
|
||||
}
|
||||
|
||||
void strbuf_rtrim(strbuf_t *s) {
|
||||
|
||||
for(; s->len > 0 && isspace(s->buf[s->len-1]); s->len--);
|
||||
s->buf[s->len] = '\0';
|
||||
}
|
||||
|
||||
void strbuf_ltrim(strbuf_t *s) {
|
||||
|
||||
size_t i, of = 0;
|
||||
|
||||
for(; of < s->len && isspace(s->buf[of]); of++);
|
||||
|
||||
if (of < 1)
|
||||
return;
|
||||
|
||||
s->len -= of;
|
||||
for(i=0; i <= s->len; i++)
|
||||
s->buf[i] = s->buf[i + of];
|
||||
}
|
||||
|
||||
void strbuf_rev(strbuf_t *s) {
|
||||
|
||||
int i;
|
||||
|
||||
for(i=0; i < s->len / 2; i++)
|
||||
swap(s->buf + i, s->buf + (s->len-1)-i);
|
||||
}
|
||||
|
||||
char* strbuf_release(strbuf_t *s) {
|
||||
|
||||
char *ret;
|
||||
|
||||
if (s->len + 1 != s->alloc_size) {
|
||||
ret = realloc(s->buf, s->len + 1);
|
||||
assert(ret != NULL);
|
||||
} else {
|
||||
ret = s->buf;
|
||||
}
|
||||
|
||||
strbuf_init(s);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void strbuf_free(strbuf_t *s) {
|
||||
|
||||
if (s->alloc_size > 0)
|
||||
free(s->buf);
|
||||
s->buf = NULL;
|
||||
}
|
||||
40
src/common/strbuf.h
Normal file
40
src/common/strbuf.h
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* common/strbuf.h
|
||||
*
|
||||
* Copyright (C) 2010 Henrik Hautakoski <henrik@fiktivkod.org>
|
||||
*
|
||||
* 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
#ifndef __COMMON_STRBUF_H
|
||||
#define __COMMON_STRBUF_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define STRBUF_INIT { 0, 0, NULL }
|
||||
|
||||
typedef struct {
|
||||
size_t alloc_size;
|
||||
size_t len;
|
||||
char *buf;
|
||||
} strbuf_t;
|
||||
|
||||
void strbuf_init(strbuf_t *s);
|
||||
|
||||
void strbuf_append(strbuf_t *s, char *str, size_t len);
|
||||
|
||||
void strbuf_trim(strbuf_t *s);
|
||||
|
||||
void strbuf_rtrim(strbuf_t *s);
|
||||
|
||||
void strbuf_ltrim(strbuf_t *s);
|
||||
|
||||
void strbuf_rev(strbuf_t *s);
|
||||
|
||||
char* strbuf_release(strbuf_t *s);
|
||||
|
||||
void strbuf_free(strbuf_t *s);
|
||||
|
||||
#endif /* __COMMON_STRBUF_H */
|
||||
|
|
@ -10,6 +10,12 @@ all :
|
|||
|
||||
raw_inotify :
|
||||
$(CC) -linotifytools raw_inotify.c -o raw_inotify
|
||||
|
||||
strbuf :
|
||||
$(CC) $(CFLAGS) ../src/common/strbuf.c t_strbuf.c -o test_strbuf
|
||||
|
||||
config :
|
||||
$(CC) ../src/common/config.c t_config.c -o test_config
|
||||
|
||||
path :
|
||||
$(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/path.c t_path.c -o test_path
|
||||
|
|
|
|||
49
test/t_strbuf.c
Normal file
49
test/t_strbuf.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include "../src/common/strbuf.h"
|
||||
|
||||
void print_strbuf(strbuf_t *s) {
|
||||
|
||||
printf("block: %i, len: %i |%s|\n", s->alloc_size, s->len, s->buf);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
strbuf_t b = STRBUF_INIT;
|
||||
char *str;
|
||||
|
||||
strbuf_append(&b, " ", 4);
|
||||
strbuf_append(&b, "abcdef", 6);
|
||||
|
||||
print_strbuf(&b);
|
||||
|
||||
strbuf_append(&b, "012345678901234567890123456789", 30);
|
||||
strbuf_append(&b, " ", 6);
|
||||
|
||||
print_strbuf(&b);
|
||||
|
||||
strbuf_rtrim(&b);
|
||||
|
||||
print_strbuf(&b);
|
||||
|
||||
strbuf_ltrim(&b);
|
||||
|
||||
print_strbuf(&b);
|
||||
|
||||
strbuf_trim(&b);
|
||||
|
||||
print_strbuf(&b);
|
||||
|
||||
strbuf_rev(&b);
|
||||
|
||||
print_strbuf(&b);
|
||||
|
||||
str = strbuf_release(&b);
|
||||
|
||||
printf("released |%s|\n", str);
|
||||
|
||||
free(str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in a new issue