Archived
1
0
Fork 0

docs for strbuf and some small changes.

This commit is contained in:
H Hautakoski 2010-08-06 11:31:04 +02:00 committed by Henrik Hautakoski
parent d6537b235c
commit f1dbd880a7
6 changed files with 79 additions and 6 deletions

54
docs/strbuf.txt Normal file
View file

@ -0,0 +1,54 @@
---------------------
String buffer API
---------------------
First off, the major design choices. the most important thing to keep in mind
when using the API is that it is designed for low-level usage, The basic error checking
is removed (like checking if the input strbuf pointer is null). This is not done to
gain speed, but because this is a buffer API, you should in almost every case allocate
the structure on the stack or have it passed from a function that has it allocated on the stack.
Other types of skipped error checking is for example if the ->len member is in range of
the allocated block. (obviously this is checked in functions that may need to expand the memory)
-- Data structures
strbuf_t:
The ->buf member is yours to mess with if you want, but you should never go beyond ->len.
A NULL terminating character is located at ->len+1 at all times so it is safe to use the ->buf member
on any function that relies on the input being a valid C string. The API will never rely on this
and it's possible to have embedded null's because of that.
->alloc_len and ->len should not be messed with, only strbuf_* functions will know how to handle those properly.
-- Functions
strbuf_append():
Like strncat() this function will append the buffer with the contents of 'str'
and will always copy exactly 'len' bytes.
strbuf_reduce():
Will reduce ->buf by 'len' bytes from the end, Note that this don't shrink the memory block
just changes the size and properly NULL terminates the now reduced space.
strbuf_trim():
strbuf_rtrim():
strbuf_ltrim():
Removes space characters from the beginning (ltrim) of the ->buf string, the end (rtrim) or both (trim).
strbuf_rev():
Reverses the ->buf string.
strbuf_release():
This function should be used to detach the ->buf member from the strbuf_t structure.
A malloc():ed C string of size strlen()+1 is returned that you are now responsible for.
strbuf_free():
Free's all the memory associted with the strbuf_t structure.

View file

@ -102,8 +102,8 @@ int main(int argc, char **argv) {
"Root Directory - Path to indexroot. All subdirectories will be indexed.\n" "Root Directory - Path to indexroot. All subdirectories will be indexed.\n"
"Db host - Database host\n" "Db host - Database host\n"
"Db user - Database user\n" "Db user - Database user\n"
"Db pass - Database password\n" "Db pass - Database password\n"
"Db name - Database to use for indexing\n" "Db name - Database to use for indexing\n"
"Db tbl - Database tablename", argv[0]); "Db tbl - Database tablename", argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;

View file

@ -59,8 +59,8 @@ int arch_db_init(char *host, char *username, char *password, char *database, cha
fprintf(stderr, "Mysql init: %li\n", dbthread_id); fprintf(stderr, "Mysql init: %li\n", dbthread_id);
#endif #endif
/* setup database */ /* setup database */
db_setup(); db_setup();
return 1; return 1;
} }
@ -83,7 +83,7 @@ void arch_db_close() {
} }
/* /*
* Truncate database table * Database setup
*/ */
int db_setup() { int db_setup() {

View file

@ -47,6 +47,15 @@ void strbuf_append(strbuf_t *s, char *str, size_t len) {
s->buf[s->len] = '\0'; s->buf[s->len] = '\0';
} }
void strbuf_reduce(strbuf_t *s, size_t len) {
if (len > s->len)
len = s->len;
s->len -= len;
s->buf[s->len] = '\0';
}
void strbuf_trim(strbuf_t *s) { void strbuf_trim(strbuf_t *s) {
strbuf_rtrim(s); strbuf_rtrim(s);

View file

@ -25,6 +25,8 @@ void strbuf_init(strbuf_t *s);
void strbuf_append(strbuf_t *s, char *str, size_t len); void strbuf_append(strbuf_t *s, char *str, size_t len);
void strbuf_reduce(strbuf_t *s, size_t len);
void strbuf_trim(strbuf_t *s); void strbuf_trim(strbuf_t *s);
void strbuf_rtrim(strbuf_t *s); void strbuf_rtrim(strbuf_t *s);

View file

@ -6,7 +6,7 @@
void print_strbuf(strbuf_t *s) { void print_strbuf(strbuf_t *s) {
assert(s->len = strlen(s->buf)); assert(s->len == strlen(s->buf));
printf("block: %i, len: %i |%s|\n", s->alloc_size, s->len, s->buf); printf("block: %i, len: %i |%s|\n", s->alloc_size, s->len, s->buf);
} }
@ -41,6 +41,14 @@ int main() {
strbuf_rev(&b); strbuf_rev(&b);
print_strbuf(&b); print_strbuf(&b);
strbuf_reduce(&b, 6);
print_strbuf(&b);
strbuf_reduce(&b, 95);
print_strbuf(&b);
str = strbuf_release(&b); str = strbuf_release(&b);