Directory: | src/ |
---|---|
File: | src/buf.c |
Date: | 2024-04-25 03:45:42 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 16 | 18 | 88.9% |
Branches: | 1 | 4 | 25.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2023 Egor Tensin <egor@tensin.name> | ||
3 | * This file is part of the "cimple" project. | ||
4 | * For details, see https://github.com/egor-tensin/cimple. | ||
5 | * Distributed under the MIT License. | ||
6 | */ | ||
7 | |||
8 | #include "buf.h" | ||
9 | #include "log.h" | ||
10 | |||
11 | #include <stdint.h> | ||
12 | #include <stdlib.h> | ||
13 | #include <string.h> | ||
14 | |||
15 | struct buf { | ||
16 | uint32_t size; | ||
17 | const void *data; | ||
18 | }; | ||
19 | |||
20 | 92012 | int buf_create(struct buf **_buf, const void *data, uint32_t size) | |
21 | { | ||
22 | 92012 | struct buf *buf = malloc(sizeof(struct buf)); | |
23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 92012 times.
|
92012 | if (!buf) { |
24 | ✗ | log_errno("malloc"); | |
25 | ✗ | return -1; | |
26 | } | ||
27 | |||
28 | 92012 | buf->data = data; | |
29 | 92012 | buf->size = size; | |
30 | |||
31 | 92012 | *_buf = buf; | |
32 | 92012 | return 0; | |
33 | } | ||
34 | |||
35 | 46006 | int buf_create_from_string(struct buf **buf, const char *str) | |
36 | { | ||
37 | 46006 | return buf_create(buf, str, strlen(str) + 1); | |
38 | } | ||
39 | |||
40 | 92012 | void buf_destroy(struct buf *buf) | |
41 | { | ||
42 | 92012 | free(buf); | |
43 | 92012 | } | |
44 | |||
45 | 92012 | uint32_t buf_get_size(const struct buf *buf) | |
46 | { | ||
47 | 92012 | return buf->size; | |
48 | } | ||
49 | |||
50 | 138018 | const void *buf_get_data(const struct buf *buf) | |
51 | { | ||
52 | 138018 | return buf->data; | |
53 | } | ||
54 |