blob: 0a1ced2199dea22579830552646e941e823cd8eb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
}
|