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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 | /*
* Copyright © 2018 Christian Persch
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <glib.h>
#include <assert.h>
#include <cstdint>
#include <cstring>
/*
* bte_seq_string_t:
*
* A type to hold the argument string of a DSC or OSC sequence.
*/
typedef struct bte_seq_string_t {
uint32_t capacity;
uint32_t len;
uint32_t* buf;
} bte_seq_string_t;
#define BTE_SEQ_STRING_DEFAULT_CAPACITY (1 << 7) /* must be power of two */
#define BTE_SEQ_STRING_MAX_CAPACITY (1 << 12)
/*
* bte_seq_string_init:
*
* Returns: a new #bte_seq_string_t
*/
static inline void bte_seq_string_init(bte_seq_string_t* str) noexcept
{
str->capacity = BTE_SEQ_STRING_DEFAULT_CAPACITY;
str->len = 0;
str->buf = (uint32_t*)g_malloc0_n(str->capacity, sizeof(uint32_t));<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
}
/*
* bte_seq_string_free:
* @string:
*
* Frees @string's storage and itself.
*/
static inline void bte_seq_string_free(bte_seq_string_t* str) noexcept
{
g_free(str->buf);
}
/*
* bte_seq_string_ensure_capacity:
* @string:
*
* If @string's length is at capacity, and capacity is not maximal,
* expands the string's capacity.
*
* Returns: %true if the string has capacity for at least one more character
*/
static inline bool bte_seq_string_ensure_capacity(bte_seq_string_t* str) noexcept
{
if (str->len < str->capacity)
return true;
if (str->capacity >= BTE_SEQ_STRING_MAX_CAPACITY)
return false;
str->capacity *= 2;
str->buf = (uint32_t*)g_realloc_n(str->buf, str->capacity, sizeof(uint32_t));<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return true;
}
/*
* bte_seq_string_push:
* @string:
* @c: a character
*
* Appends @c to @str, or iff @str already has maximum length, does nothing.
*
* Returns: %true if the character was appended
*/
static inline bool bte_seq_string_push(bte_seq_string_t* str,
uint32_t c) noexcept
{
if (!bte_seq_string_ensure_capacity(str))
return false;
str->buf[str->len++] = c;
return true;
}
/*
* bte_seq_string_finish:
* @string:
*
* Finishes @string; after this no more bte_seq_string_push() calls
* are allowed until the string is reset with bte_seq_string_reset().
*/
static inline void bte_seq_string_finish(bte_seq_string_t* str)
{
}
/*
* bte_seq_string_reset:
* @string:
*
* Resets @string.
*/
static inline void bte_seq_string_reset(bte_seq_string_t* str) noexcept
{
/* Zero length. However, don't clear the buffer, nor shrink the capacity. */
str->len = 0;
}
/*
* bte_seq_string_get:
* @string:
* @len: location to store the buffer length in code units
*
* Returns: the string's buffer as an array of uint32_t code units
*/
static constexpr inline uint32_t* bte_seq_string_get(bte_seq_string_t const* str,
size_t* len) noexcept
{
assert(len != nullptr);
*len = str->len;
return str->buf;
}
|