/src/yaml_write_handler.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef YAML_WRITE_HANDLER_H_ |
16 | | #define YAML_WRITE_HANDLER_H_ |
17 | | |
18 | | typedef struct yaml_output_buffer { |
19 | | unsigned char *buf; |
20 | | size_t size; |
21 | | size_t capacity; |
22 | | } yaml_output_buffer_t; |
23 | | |
24 | 0 | static int yaml_write_handler(void *data, unsigned char *buffer, size_t size) { |
25 | 0 | size_t newsize; |
26 | 0 | yaml_output_buffer_t *out = (yaml_output_buffer_t *)data; |
27 | | |
28 | | /* Double buffer size whenever necessary */ |
29 | 0 | if (out->size + size >= out->capacity) { |
30 | 0 | newsize = out->capacity << 1; |
31 | 0 | if (newsize < out->size + size) { |
32 | 0 | newsize = out->size + size; |
33 | 0 | } |
34 | 0 | out->buf = (unsigned char *)realloc(out->buf, newsize); |
35 | 0 | out->capacity = newsize; |
36 | 0 | } |
37 | 0 | if (!out->buf) { |
38 | 0 | out->size = 0; |
39 | 0 | return 0; |
40 | 0 | } |
41 | | |
42 | 0 | memcpy(out->buf + out->size, buffer, size); |
43 | 0 | out->size += size; |
44 | 0 | return 1; |
45 | 0 | } |
46 | | |
47 | | #endif // YAML_WRITE_HANDLER_H_ |