utstring: dynamic string macros for C ===================================== Troy D. Hanson v1.9.5, November 2011 include::sflogo.txt[] include::topnav_utstring.txt[] Introduction ------------ include::toc.txt[] A set of very basic dynamic string macros for C programs are included with uthash in `utstring.h`. To use these macros in your own C program, just copy `utstring.h` into your source directory and use it in your programs. #include "utstring.h" The dynamic string supports basic operations such as inserting data (including binary data-- despite its name, utstring is not limited to string content), concatenation, getting the length and content, and clearing it. The string <> are listed below. Download ~~~~~~~~ To download the `utstring.h` header file, follow the link on the http://uthash.sourceforge.net[uthash home page]. BSD licensed ~~~~~~~~~~~~ This software is made available under the link:license.html[revised BSD license]. It is free and open source. Platforms ~~~~~~~~~ The 'utstring' macros have been tested on: * Linux, * Windows, using Visual Studio 2008 and Visual Studio 2010 Usage ----- Declaration ~~~~~~~~~~~ The dynamic string itself has the data type `UT_string`. It is declared like, UT_string *str; New and free ~~~~~~~~~~~~ The next step is to create the string using `utstring_new`. Later when you're done with it, `utstring_free` will free it and all its content. Manipulation ~~~~~~~~~~~~ The `utstring_printf` or `utstring_bincpy` operations insert (copy) data into the string. To concatenate one utstring to another, use `utstring_concat`. To clear the content of the string, use `utstring_clear`. The length of the string is available from `utstring_len`, and its content from `utstring_body`. This evaluates to a `char*`. The buffer it points to is always null-terminated. So, it can be used directly with external functions that expect a string. This automatic null terminator is not counted in the length of the string. Samples ~~~~~~~ These examples show how to use utstring. .Sample 1 ------------------------------------------------------------------------------- #include #include "utstring.h" int main() { UT_string *s; utstring_new(s); utstring_printf(s, "hello world!" ); printf("%s\n", utstring_body(s)); utstring_free(s); return 0; } ------------------------------------------------------------------------------- The next example is meant to demonstrate that printf 'appends' to the string. It also shows concatenation. .Sample 2 ------------------------------------------------------------------------------- #include #include "utstring.h" int main() { UT_string *s, *t; utstring_new(s); utstring_new(t); utstring_printf(s, "hello " ); utstring_printf(s, "world " ); utstring_printf(t, "hi " ); utstring_printf(t, "there " ); utstring_concat(s, t); printf("length: %u\n", utstring_len(s)); printf("%s\n", utstring_body(s)); utstring_free(s); utstring_free(t); return 0; } ------------------------------------------------------------------------------- The last example shows how binary data can be inserted into the string. It also clears the string and prints new data into it. .Sample 3 ------------------------------------------------------------------------------- #include #include "utstring.h" int main() { UT_string *s; char binary[] = "\xff\xff"; utstring_new(s); utstring_bincpy(s, binary, sizeof(binary)); printf("length is %u\n", utstring_len(s)); utstring_clear(s); utstring_printf(s,"number %d", 10); printf("%s\n", utstring_body(s)); utstring_free(s); return 0; } ------------------------------------------------------------------------------- [[operations]] Reference --------- These are the utstring operations. Operations ~~~~~~~~~~ [width="100%",cols="50