string: use spa_strbuf instead of snprintf magic

This commit is contained in:
Wim Taymans 2025-10-24 17:00:11 +02:00
parent f7c3d37969
commit c4244a6cf3
5 changed files with 41 additions and 48 deletions

View file

@ -245,7 +245,7 @@ static void init_device(pa_card *impl, pa_alsa_device *dev, pa_alsa_direction_t
pa_proplist_update(dev->proplist, PA_UPDATE_REPLACE, m->input_proplist);
}
if (m->split) {
char pos[2048];
char pos[PA_CHANNELS_MAX*8];
struct spa_strbuf b;
int i;
@ -1142,8 +1142,9 @@ static int hdmi_eld_changed(snd_mixer_elem_t *melem, unsigned int mask)
pa_proplist_unset(p->proplist, ACP_KEY_AUDIO_POSITION_DETECTED);
} else {
uint32_t positions[eld.lpcm_channels];
char position[64];
int i = 0, pos = 0;
char position[eld.lpcm_channels * 8];
struct spa_strbuf b;
int i = 0;
if (eld.speakers & 0x01) {
positions[i++] = ACP_CHANNEL_FL;
@ -1172,16 +1173,14 @@ static int hdmi_eld_changed(snd_mixer_elem_t *melem, unsigned int mask)
if (eld.speakers & 0x10) {
positions[i++] = ACP_CHANNEL_RC;
}
while (i < eld.lpcm_channels)
positions[i++] = ACP_CHANNEL_UNKNOWN;
for (i = 0, pos = 0; i < eld.lpcm_channels; i++) {
pos += snprintf(&position[pos], sizeof(position) - pos, "%s,", channel_names[positions[i]]);
}
/* Overwrite trailing , */
position[pos - 1] = 0;
spa_strbuf_init(&b, position, sizeof(position));
spa_strbuf_append(&b, "[");
for (i = 0; i < eld.lpcm_channels; i++)
spa_strbuf_append(&b, "%s%s", i ? "," : "", channel_names[positions[i]]);
spa_strbuf_append(&b, "]");
changed |= (old_position == NULL) || (!spa_streq(old_position, position));
pa_proplist_sets(p->proplist, ACP_KEY_AUDIO_POSITION_DETECTED, position);

View file

@ -451,7 +451,6 @@ static inline int pa_channel_map_equal(const pa_channel_map *a, const pa_channel
static inline char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_map *map) {
unsigned channel;
bool first = true;
char *e;
if (!pa_channel_map_valid(map)) {
pa_snprintf(s, l, "%s", _("(invalid)"));
@ -460,12 +459,10 @@ static inline char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_m
*(e = s) = 0;
for (channel = 0; channel < map->channels && l > 1; channel++) {
l -= pa_snprintf(e, l, "%s%s",
first ? "" : ",",
channel == 0 ? "" : ",",
pa_channel_position_to_string(map->map[channel]));
e = strchr(e, 0);
first = false;
}
return s;
}

View file

@ -156,12 +156,13 @@ static int emit_node(struct impl *this, struct acp_device *dev)
const struct acp_dict_item *it;
uint32_t n_items, i;
char device_name[128], path[210], channels[16], ch[12], routes[16];
char card_index[16], card_name[64], *p;
char card_index[16], card_name[64];
char positions[MAX_CHANNELS * 12];
char codecs[512];
struct spa_device_object_info info;
struct acp_card *card = this->card;
const char *stream, *card_id, *bus;
struct spa_strbuf b;
info = SPA_DEVICE_OBJECT_INFO_INIT();
info.type = SPA_TYPE_INTERFACE_Node;
@ -200,11 +201,13 @@ static int emit_node(struct impl *this, struct acp_device *dev)
snprintf(channels, sizeof(channels), "%d", dev->format.channels);
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_AUDIO_CHANNELS, channels);
p = positions;
spa_strbuf_init(&b, positions, sizeof(positions));
spa_strbuf_append(&b, "[");
for (i = 0; i < dev->format.channels; i++) {
p += snprintf(p, 12, "%s%s", i == 0 ? "" : ",",
spa_strbuf_append(&b, "%s%s", i == 0 ? " " : ", ",
acp_channel_str(ch, sizeof(ch), dev->format.map[i]));
}
spa_strbuf_append(&b, " ]");
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_AUDIO_POSITION, positions);
if (dev->n_codecs > 0) {

View file

@ -240,36 +240,33 @@ static int alsa_set_param(struct state *state, const char *k, const char *s)
static int position_to_string(struct channel_map *map, char *val, size_t len)
{
uint32_t i, o = 0;
uint32_t i;
char pos[8];
int r;
o += snprintf(val, len, "[ ");
struct spa_strbuf b;
spa_strbuf_init(&b, val, len);
spa_strbuf_append(&b, "[");
for (i = 0; i < map->n_pos; i++) {
r = snprintf(val+o, len-o, "%s%s", i == 0 ? "" : ", ",
spa_strbuf_append(&b, "%s%s", i == 0 ? " " : ", ",
spa_type_audio_channel_make_short_name(map->pos[i],
pos, sizeof(pos), "UNK"));
if (r < 0 || o + r >= len)
return -ENOSPC;
o += r;
}
if (len > o)
o += snprintf(val+o, len-o, " ]");
if (spa_strbuf_append(&b, " ]") < 2)
return -ENOSPC;
return 0;
}
static int uint32_array_to_string(uint32_t *vals, uint32_t n_vals, char *val, size_t len)
{
uint32_t i, o = 0;
int r;
o += snprintf(val, len, "[ ");
for (i = 0; i < n_vals; i++) {
r = snprintf(val+o, len-o, "%s%d", i == 0 ? "" : ", ", vals[i]);
if (r < 0 || o + r >= len)
uint32_t i;
struct spa_strbuf b;
spa_strbuf_init(&b, val, len);
spa_strbuf_append(&b, "[");
for (i = 0; i < n_vals; i++)
spa_strbuf_append(&b, "%s%d", i == 0 ? " " : ", ", vals[i]);
if (spa_strbuf_append(&b, " ]") < 2)
return -ENOSPC;
o += r;
}
if (len > o)
o += snprintf(val+o, len-o, " ]");
return 0;
}

View file

@ -245,24 +245,21 @@ void pw_settings_init(struct pw_context *this)
static void expose_settings(struct pw_context *context, struct pw_impl_metadata *metadata)
{
struct settings *s = &context->settings;
uint32_t i, o;
char rates[MAX_RATES*16] = "";
uint32_t i;
char rates[MAX_RATES*16];
struct spa_strbuf b;
pw_impl_metadata_set_propertyf(metadata,
PW_ID_CORE, "log.level", "", "%d", s->log_level);
pw_impl_metadata_set_propertyf(metadata,
PW_ID_CORE, "clock.rate", "", "%d", s->clock_rate);
for (i = 0, o = 0; i < s->n_clock_rates; i++) {
int r = snprintf(rates+o, sizeof(rates)-o, "%s%d", i == 0 ? "" : ", ",
spa_strbuf_init(&b, rates, sizeof(rates));
for (i = 0; i < s->n_clock_rates; i++)
spa_strbuf_append(&b, "%s%d", i == 0 ? "" : ", ",
s->clock_rates[i]);
if (r < 0 || o + r >= (int)sizeof(rates)) {
snprintf(rates, sizeof(rates), "%d", s->clock_rate);
break;
}
o += r;
}
if (s->n_clock_rates == 0)
snprintf(rates, sizeof(rates), "%d", s->clock_rate);
spa_strbuf_append(&b, "%d", s->clock_rate);
pw_impl_metadata_set_propertyf(metadata,
PW_ID_CORE, "clock.allowed-rates", "", "[ %s ]", rates);