diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | c90c389a8a8d9d8661e9772ec4144c5cf2039f23 (patch) | |
tree | 6d8391395bce9eaea4ad78958617edb20c6a7573 /kpat/freecell-solver/app_str.c | |
download | tdegames-c90c389a8a8d9d8661e9772ec4144c5cf2039f23.tar.gz tdegames-c90c389a8a8d9d8661e9772ec4144c5cf2039f23.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdegames@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kpat/freecell-solver/app_str.c')
-rw-r--r-- | kpat/freecell-solver/app_str.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/kpat/freecell-solver/app_str.c b/kpat/freecell-solver/app_str.c new file mode 100644 index 00000000..0a1ced21 --- /dev/null +++ b/kpat/freecell-solver/app_str.c @@ -0,0 +1,74 @@ +#include <string.h> +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> + +#define GROW_BY 4000 + +struct freecell_solver_append_string_struct +{ + char * buffer; + char * end_of_buffer; + size_t max_size; + size_t size_of_margin; +}; + +typedef struct freecell_solver_append_string_struct freecell_solver_append_string_t; + +freecell_solver_append_string_t * freecell_solver_append_string_alloc(int size_margin) +{ + freecell_solver_append_string_t * app_str; + + if (size_margin > GROW_BY) + { + return NULL; + } + + app_str = malloc(sizeof(freecell_solver_append_string_t)); + app_str->max_size = GROW_BY; + app_str->end_of_buffer = app_str->buffer = malloc(app_str->max_size); + app_str->size_of_margin = size_margin; + + return app_str; +} + +int freecell_solver_append_string_sprintf( + freecell_solver_append_string_t * app_str, + char * format, + ... + ) +{ + int num_chars_written; + va_list my_va_list; + + va_start(my_va_list, format); + num_chars_written = vsprintf(app_str->end_of_buffer, format, my_va_list); + app_str->end_of_buffer += num_chars_written; + /* + * Check to see if we don't have enough space in which case we should + * resize + * */ + if (app_str->buffer + app_str->max_size - app_str->end_of_buffer < (int)app_str->size_of_margin ) + { + char * old_buffer = app_str->buffer; + app_str->max_size += GROW_BY; + app_str->buffer = realloc(app_str->buffer, app_str->max_size); + /* + * Adjust end_of_buffer to the new buffer start + * */ + app_str->end_of_buffer = app_str->buffer + (app_str->end_of_buffer - old_buffer); + } + + return num_chars_written; +} + +char * freecell_solver_append_string_finalize( + freecell_solver_append_string_t * app_str + ) +{ + char * ret; + ret = strdup(app_str->buffer); + free(app_str->buffer); + free(app_str); + return ret; +} |