I have a C
application (not using C99 features) that does some heavy string processing. Since the string lengths are not known, statically allocated buffers is not an option for me. I have created a simple string
implementation which will abstract the null termination and dynamic expansion of buffers.
Here is how it looks like,
struct strbuf {
char *buffer; /* null terminated buffer */
size_t length; /* length of the string excluding null terminator */
size_t allocated; /* total memory allocated */
};
add
function which adds the supplied string into the buffer.
int strbuf_add(struct strbuf *string, const char *c)
{
if(string == NULL) return 0;
while(*c != '\0') {
if(!add_char(string, *c++))
return 0;
}
return 1;
}
static int add_char(struct strbuf *string, char c)
{
size_t space_available;
assert(string != NULL);
space_available = string->allocated - string->length;
if(space_available <= 1) {
if(!grow_buffer(string)) {
return 0;
}
}
string->buffer[string->length++] = c;
string->buffer[string->length] = '\0';
return 1;
}
Now, I need to add a new method something like, addformatted
which will take a format like sprintf
. I am wondering what would be the best way to do this? Following are my thoughts.
- Use
vsnprintf
. But I am not sure that this is portable and has got the same behaviour on all platforms. - Write a format parser myself. But this seems to be more work.
Any help in implementing this would be great. I am only interested in portable solutions.
- Dev Env : Linux with GCC
- Expected to compile on MSVC