diff --git a/docs/strbuf.txt b/docs/strbuf.txt new file mode 100644 index 0000000..ae63406 --- /dev/null +++ b/docs/strbuf.txt @@ -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. + diff --git a/src/arch.c b/src/arch.c index f05a7d5..b7f8045 100644 --- a/src/arch.c +++ b/src/arch.c @@ -102,8 +102,8 @@ int main(int argc, char **argv) { "Root Directory - Path to indexroot. All subdirectories will be indexed.\n" "Db host - Database host\n" "Db user - Database user\n" - "Db pass - Database password\n" - "Db name - Database to use for indexing\n" + "Db pass - Database password\n" + "Db name - Database to use for indexing\n" "Db tbl - Database tablename", argv[0]); return EXIT_FAILURE; diff --git a/src/arch/mysql.c b/src/arch/mysql.c index 3c882bd..a3640aa 100644 --- a/src/arch/mysql.c +++ b/src/arch/mysql.c @@ -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); #endif - /* setup database */ - db_setup(); + /* setup database */ + db_setup(); return 1; } @@ -83,7 +83,7 @@ void arch_db_close() { } /* - * Truncate database table + * Database setup */ int db_setup() { diff --git a/src/common/strbuf.c b/src/common/strbuf.c index 8687f64..beeee10 100644 --- a/src/common/strbuf.c +++ b/src/common/strbuf.c @@ -47,6 +47,15 @@ void strbuf_append(strbuf_t *s, char *str, size_t len) { 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) { strbuf_rtrim(s); diff --git a/src/common/strbuf.h b/src/common/strbuf.h index 8e06484..6c74741 100644 --- a/src/common/strbuf.h +++ b/src/common/strbuf.h @@ -25,6 +25,8 @@ void strbuf_init(strbuf_t *s); 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_rtrim(strbuf_t *s); diff --git a/test/t_strbuf.c b/test/t_strbuf.c index a942acf..6ad14b6 100644 --- a/test/t_strbuf.c +++ b/test/t_strbuf.c @@ -6,7 +6,7 @@ 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); } @@ -41,6 +41,14 @@ int main() { strbuf_rev(&b); print_strbuf(&b); + + strbuf_reduce(&b, 6); + + print_strbuf(&b); + + strbuf_reduce(&b, 95); + + print_strbuf(&b); str = strbuf_release(&b);