Created a bigger buffer for write/read.
[m6w6/libmemcached] / lib / memcached_string.c
1 #include "common.h"
2
3 memcached_return memcached_string_check(memcached_string_st *string, size_t need)
4 {
5 if (need > (size_t)(string->current_size - (size_t)(string->end - string->string)))
6 {
7 size_t current_offset= string->end - string->string;
8 char *new_value;
9
10 new_value= (char *)realloc(string->string, sizeof(char) * string->block_size);
11
12 if (!new_value)
13 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
14
15 string->string= new_value;
16 string->end= string->string + current_offset;
17
18 string->current_size+= string->block_size;
19
20 /* We zero the block structure we just realloced */
21 memset((string->string + string->current_size) - string->block_size , 0,
22 sizeof(char) * string->block_size);
23 }
24
25 return MEMCACHED_SUCCESS;
26 }
27
28 memcached_string_st *memcached_string_create(memcached_st *ptr, size_t initial_size)
29 {
30 memcached_string_st *string;
31
32 /* Saving malloc calls :) */
33 string= (memcached_string_st *)malloc(sizeof(memcached_string_st) + (sizeof(char) * initial_size));
34 if (!string)
35 return NULL;
36 memset(string, 0, sizeof(memcached_string_st));
37 string->block_size= initial_size;
38 memcached_string_check(string, initial_size);
39
40 return string;
41 }
42
43 memcached_return memcached_string_append_character(memcached_st *ptr,
44 memcached_string_st *string,
45 char character)
46 {
47 memcached_string_check(string, 1);
48 *string->end= ' ';
49 string->end++;
50
51 return MEMCACHED_SUCCESS;
52 }
53
54 memcached_return memcached_string_append(memcached_st *ptr, memcached_string_st *string,
55 char *value, size_t length)
56 {
57 memcached_string_check(string, length);
58 memcpy(string->end, value, length);
59 string->end+= length;
60
61 return MEMCACHED_SUCCESS;
62 }
63
64 #ifdef CRAP
65 size_t memcached_string_length(memcached_st *ptr, memcached_string_st *string)
66 {
67 return (size_t)(string->end - string->string);
68 }
69
70 size_t memcached_string_size(memcached_st *ptr, memcached_string_st *string)
71 {
72 return string->current_size;
73 }
74 #endif
75
76 size_t memcached_string_backspace(memcached_st *ptr, memcached_string_st *string, size_t remove)
77 {
78 if (string->end - string->string > remove)
79 {
80 size_t difference;
81
82 difference= string->end - string->string;
83 string->end= string->string;
84
85 return difference;
86 }
87 string->end-= remove;
88
89 return remove;
90 }
91
92 memcached_return memcached_string_reset(memcached_st *ptr, memcached_string_st *string)
93 {
94 string->end= string->string;
95
96 return MEMCACHED_SUCCESS;
97 }
98
99 void memcached_string_free(memcached_st *ptr, memcached_string_st *string)
100 {
101 free(string->string);
102 free(string);
103 }