92 return -1; |
92 return -1; |
93 } |
93 } |
94 } |
94 } |
95 |
95 |
96 int flib_vector_append(flib_vector *vec, const void *data, size_t len) { |
96 int flib_vector_append(flib_vector *vec, const void *data, size_t len) { |
97 if(!vec) { |
97 if(!log_badparams_if(!vec || (!data && len>0)) |
98 flib_log_e("null parameter in flib_vector_append"); |
98 && !log_oom_if(len > SIZE_MAX-vec->size)) { |
99 return 0; |
99 size_t oldSize = vec->size; |
|
100 if(!log_oom_if(flib_vector_resize(vec, vec->size+len))) { |
|
101 memmove(((uint8_t*)vec->data) + oldSize, data, len); |
|
102 return 0; |
|
103 } |
100 } |
104 } |
101 |
105 return -1; |
102 if(len > SIZE_MAX-vec->size) { |
|
103 return 0; |
|
104 } |
|
105 |
|
106 size_t oldSize = vec->size; |
|
107 if(flib_vector_resize(vec, vec->size+len)) { |
|
108 return 0; |
|
109 } |
|
110 |
|
111 memmove(((uint8_t*)vec->data) + oldSize, data, len); |
|
112 return len; |
|
113 } |
106 } |
114 |
107 |
115 int flib_vector_appendf(flib_vector *vec, const char *fmt, ...) { |
108 int flib_vector_appendf(flib_vector *vec, const char *fmt, ...) { |
116 int result = -1; |
109 int result = -1; |
117 if(!vec || !fmt) { |
110 if(!log_badparams_if(!vec || !fmt)) { |
118 flib_log_e("null parameter in flib_vector_appendf"); |
|
119 } else { |
|
120 va_list argp; |
111 va_list argp; |
121 va_start(argp, fmt); |
112 va_start(argp, fmt); |
122 char *formatted = flib_vasprintf(fmt, argp); |
113 char *formatted = flib_vasprintf(fmt, argp); |
123 va_end(argp); |
114 va_end(argp); |
124 |
115 |
125 |
116 |
126 if(formatted) { |
117 if(formatted) { |
127 size_t len = strlen(formatted); |
118 size_t len = strlen(formatted); |
128 if(flib_vector_append(vec, formatted, len) == len) { |
119 result = flib_vector_append(vec, formatted, len); |
129 result = 0; |
|
130 } |
|
131 } |
120 } |
132 } |
121 } |
133 return result; |
122 return result; |
134 } |
123 } |
135 |
124 |