clean up some more array iterations

This commit is contained in:
Wim Taymans 2022-09-30 17:23:05 +02:00
parent 0b98614bea
commit 9b6e504c19
14 changed files with 88 additions and 114 deletions

View file

@ -109,10 +109,9 @@ static const struct channelmix_upmix_info {
static inline uint32_t channelmix_upmix_from_label(const char *label) static inline uint32_t channelmix_upmix_from_label(const char *label)
{ {
uint32_t i; SPA_FOR_EACH_ELEMENT_VAR(channelmix_upmix_info, i) {
for (i = 0; i < SPA_N_ELEMENTS(channelmix_upmix_info); i++) { if (spa_streq(i->label, label))
if (spa_streq(channelmix_upmix_info[i].label, label)) return i->upmix;
return channelmix_upmix_info[i].upmix;
} }
return CHANNELMIX_UPMIX_NONE; return CHANNELMIX_UPMIX_NONE;
} }

View file

@ -88,10 +88,9 @@ static enum spa_bt_feature parse_feature(const char *str)
{ "faststream", SPA_BT_FEATURE_FASTSTREAM }, { "faststream", SPA_BT_FEATURE_FASTSTREAM },
{ "a2dp-duplex", SPA_BT_FEATURE_A2DP_DUPLEX }, { "a2dp-duplex", SPA_BT_FEATURE_A2DP_DUPLEX },
}; };
size_t i; SPA_FOR_EACH_ELEMENT_VAR(feature_keys, f) {
for (i = 0; i < SPA_N_ELEMENTS(feature_keys); ++i) { if (spa_streq(str, f->key))
if (spa_streq(str, feature_keys[i].key)) return f->value;
return feature_keys[i].value;
} }
return 0; return 0;
} }

View file

@ -171,19 +171,18 @@ impl_cpu_get_vm_type(void *object)
/* https://wiki.freebsd.org/bhyve */ /* https://wiki.freebsd.org/bhyve */
{ "BHYVE", SPA_CPU_VM_BHYVE }, { "BHYVE", SPA_CPU_VM_BHYVE },
}; };
uint32_t i, j;
for (i = 0; i < SPA_N_ELEMENTS(dmi_vendors); i++) { SPA_FOR_EACH_ELEMENT_VAR(dmi_vendors, dv) {
char buffer[256], *s; char buffer[256], *s;
if ((s = read_file(dmi_vendors[i], buffer, sizeof(buffer))) == NULL) if ((s = read_file(*dv, buffer, sizeof(buffer))) == NULL)
continue; continue;
for (j = 0; j < SPA_N_ELEMENTS(dmi_vendor_table); j++) { SPA_FOR_EACH_ELEMENT_VAR(dmi_vendor_table, t) {
if (spa_strstartswith(s, dmi_vendor_table[j].vendor)) { if (spa_strstartswith(s, t->vendor)) {
spa_log_debug(impl->log, "Virtualization %s found in DMI (%s)", spa_log_debug(impl->log, "Virtualization %s found in DMI (%s)",
s, dmi_vendors[i]); s, *dv);
impl->vm_type = dmi_vendor_table[j].id; impl->vm_type = t->id;
goto done; goto done;
} }
} }

View file

@ -351,11 +351,9 @@ static const struct format_info format_info[] = {
static const struct format_info *fourcc_to_format_info(uint32_t fourcc) static const struct format_info *fourcc_to_format_info(uint32_t fourcc)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(format_info, i) {
if (i->fourcc == fourcc)
for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) { return i;
if (format_info[i].fourcc == fourcc)
return &format_info[i];
} }
return NULL; return NULL;
} }
@ -363,11 +361,9 @@ static const struct format_info *fourcc_to_format_info(uint32_t fourcc)
#if 0 #if 0
static const struct format_info *video_format_to_format_info(uint32_t format) static const struct format_info *video_format_to_format_info(uint32_t format)
{ {
int i; SPA_FOR_EACH_ELEMENT_VAR(format_info, i) {
if (i->format == format)
for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) { return i;
if (format_info[i].format == format)
return &format_info[i];
} }
return NULL; return NULL;
} }
@ -381,10 +377,11 @@ static const struct format_info *find_format_info_by_media_type(uint32_t type,
size_t i; size_t i;
for (i = startidx; i < SPA_N_ELEMENTS(format_info); i++) { for (i = startidx; i < SPA_N_ELEMENTS(format_info); i++) {
if ((format_info[i].media_type == type) && const struct format_info *fi = &format_info[i];
(format_info[i].media_subtype == subtype) && if ((fi->media_type == type) &&
(format == 0 || format_info[i].format == format)) (fi->media_subtype == subtype) &&
return &format_info[i]; (format == 0 || fi->format == format))
return fi;
} }
return NULL; return NULL;
} }

View file

@ -130,25 +130,22 @@ static struct {
static inline uint32_t sdl_format_to_id(Uint32 format) static inline uint32_t sdl_format_to_id(Uint32 format)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(sdl_video_formats, f) {
for (i = 0; i < SPA_N_ELEMENTS(sdl_video_formats); i++) { if (f->format == format)
if (sdl_video_formats[i].format == format) return f->id;
return sdl_video_formats[i].id;
} }
return SPA_VIDEO_FORMAT_UNKNOWN; return SPA_VIDEO_FORMAT_UNKNOWN;
} }
static inline Uint32 id_to_sdl_format(uint32_t id) static inline Uint32 id_to_sdl_format(uint32_t id)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(sdl_video_formats, f) {
for (i = 0; i < SPA_N_ELEMENTS(sdl_video_formats); i++) { if (f->id == id)
if (sdl_video_formats[i].id == id) return f->format;
return sdl_video_formats[i].format;
} }
return SDL_PIXELFORMAT_UNKNOWN; return SDL_PIXELFORMAT_UNKNOWN;
} }
static inline struct spa_pod *sdl_build_formats(SDL_RendererInfo *info, struct spa_pod_builder *b) static inline struct spa_pod *sdl_build_formats(SDL_RendererInfo *info, struct spa_pod_builder *b)
{ {
uint32_t i, c; uint32_t i, c;
@ -178,8 +175,8 @@ static inline struct spa_pod *sdl_build_formats(SDL_RendererInfo *info, struct s
spa_pod_builder_id(b, id); spa_pod_builder_id(b, id);
} }
/* then all the other ones SDL can convert from/to */ /* then all the other ones SDL can convert from/to */
for (i = 0; i < SPA_N_ELEMENTS(sdl_video_formats); i++) { SPA_FOR_EACH_ELEMENT_VAR(sdl_video_formats, f) {
uint32_t id = sdl_video_formats[i].id; uint32_t id = f->id;
if (id != SPA_VIDEO_FORMAT_UNKNOWN) if (id != SPA_VIDEO_FORMAT_UNKNOWN)
spa_pod_builder_id(b, id); spa_pod_builder_id(b, id);
} }

View file

@ -345,11 +345,10 @@ static const struct msg_info msg_info[] = {
static inline const struct msg_info *find_msg_info(uint16_t type, const char *name) static inline const struct msg_info *find_msg_info(uint16_t type, const char *name)
{ {
uint32_t i; SPA_FOR_EACH_ELEMENT_VAR(msg_info, i) {
for (i = 0; i < SPA_N_ELEMENTS(msg_info); i++) { if ((name == NULL && type == i->type) ||
if ((name == NULL && type == msg_info[i].type) || (name != NULL && spa_streq(name, i->name)))
(name != NULL && spa_streq(name, msg_info[i].name))) return i;
return &msg_info[i];
} }
return NULL; return NULL;
} }

View file

@ -250,11 +250,10 @@ static const struct cmd_info cmd_info[] = {
static inline const struct cmd_info *find_cmd_info(uint16_t type, const char *name) static inline const struct cmd_info *find_cmd_info(uint16_t type, const char *name)
{ {
uint32_t i; SPA_FOR_EACH_ELEMENT_VAR(cmd_info, i) {
for (i = 0; i < SPA_N_ELEMENTS(cmd_info); i++) { if ((name == NULL && type == i->type) ||
if ((name == NULL && type == cmd_info[i].type) || (name != NULL && spa_streq(name, i->name)))
(name != NULL && spa_streq(name, cmd_info[i].name))) return i;
return &cmd_info[i];
} }
return NULL; return NULL;
} }

View file

@ -67,11 +67,10 @@ static const struct msg_info msg_info[] = {
static inline const struct msg_info *find_msg_info(uint16_t type, const char *name) static inline const struct msg_info *find_msg_info(uint16_t type, const char *name)
{ {
uint32_t i; SPA_FOR_EACH_ELEMENT_VAR(msg_info, i) {
for (i = 0; i < SPA_N_ELEMENTS(msg_info); i++) { if ((name == NULL && type == i->type) ||
if ((name == NULL && type == msg_info[i].type) || (name != NULL && spa_streq(name, i->name)))
(name != NULL && spa_streq(name, msg_info[i].name))) return i;
return &msg_info[i];
} }
return NULL; return NULL;
} }

View file

@ -1333,13 +1333,12 @@ static void convert_properties(struct pw_properties *properties)
{ "pipewire.target.node", PW_KEY_NODE_TARGET, } { "pipewire.target.node", PW_KEY_NODE_TARGET, }
}; };
uint32_t i;
const char *str; const char *str;
for(i = 0; i < SPA_N_ELEMENTS(props); i++) { SPA_FOR_EACH_ELEMENT_VAR(props, p) {
if ((str = pw_properties_get(properties, props[i].from)) != NULL) { if ((str = pw_properties_get(properties, p->from)) != NULL) {
pw_properties_set(properties, props[i].to, str); pw_properties_set(properties, p->to, str);
pw_properties_set(properties, props[i].from, NULL); pw_properties_set(properties, p->from, NULL);
} }
} }
} }

View file

@ -159,32 +159,28 @@ uint32_t format_name2id(const char *name)
uint32_t format_paname2id(const char *name, size_t size) uint32_t format_paname2id(const char *name, size_t size)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(audio_formats, f) {
for (i = 0; i < SPA_N_ELEMENTS(audio_formats); i++) { if (f->name != NULL &&
if (audio_formats[i].name != NULL && strncmp(name, f->name, size) == 0)
strncmp(name, audio_formats[i].name, size) == 0) return f->id;
return audio_formats[i].id;
} }
return SPA_AUDIO_FORMAT_UNKNOWN; return SPA_AUDIO_FORMAT_UNKNOWN;
} }
enum sample_format format_id2pa(uint32_t id) enum sample_format format_id2pa(uint32_t id)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(audio_formats, f) {
for (i = 0; i < SPA_N_ELEMENTS(audio_formats); i++) { if (id == f->id)
if (id == audio_formats[i].id) return f->pa;
return audio_formats[i].pa;
} }
return SAMPLE_INVALID; return SAMPLE_INVALID;
} }
const char *format_id2paname(uint32_t id) const char *format_id2paname(uint32_t id)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(audio_formats, f) {
for (i = 0; i < SPA_N_ELEMENTS(audio_formats); i++) { if (id == f->id && f->name != NULL)
if (id == audio_formats[i].id && return f->name;
audio_formats[i].name != NULL)
return audio_formats[i].name;
} }
return "invalid"; return "invalid";
} }
@ -266,21 +262,18 @@ enum channel_position channel_id2pa(uint32_t id, uint32_t *aux)
const char *channel_id2paname(uint32_t id, uint32_t *aux) const char *channel_id2paname(uint32_t id, uint32_t *aux)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(audio_channels, i) {
for (i = 0; i < SPA_N_ELEMENTS(audio_channels); i++) { if (id == i->channel && i->name != NULL)
if (id == audio_channels[i].channel && return i->name;
audio_channels[i].name != NULL)
return audio_channels[i].name;
} }
return audio_channels[CHANNEL_POSITION_AUX0 + ((*aux)++ & 31)].name; return audio_channels[CHANNEL_POSITION_AUX0 + ((*aux)++ & 31)].name;
} }
uint32_t channel_paname2id(const char *name, size_t size) uint32_t channel_paname2id(const char *name, size_t size)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(audio_channels, i) {
for (i = 0; i < SPA_N_ELEMENTS(audio_channels); i++) { if (strncmp(name, i->name, size) == 0)
if (strncmp(name, audio_channels[i].name, size) == 0) return i->channel;
return audio_channels[i].channel;
} }
return SPA_AUDIO_CHANNEL_UNKNOWN; return SPA_AUDIO_CHANNEL_UNKNOWN;
} }

View file

@ -553,11 +553,10 @@ static const struct object_info *objects[] =
static const struct object_info *find_info(const char *type, uint32_t version) static const struct object_info *find_info(const char *type, uint32_t version)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(objects, i) {
for (i = 0; i < SPA_N_ELEMENTS(objects); i++) { if (spa_streq((*i)->type, type) &&
if (spa_streq(objects[i]->type, type) && (*i)->version <= version)
objects[i]->version <= version) return *i;
return objects[i];
} }
return NULL; return NULL;
} }

View file

@ -38,10 +38,9 @@ static uint64_t parse_quirks(const char *str)
{ "force-s16-info", QUIRK_FORCE_S16_FORMAT }, { "force-s16-info", QUIRK_FORCE_S16_FORMAT },
{ "remove-capture-dont-move", QUIRK_REMOVE_CAPTURE_DONT_MOVE }, { "remove-capture-dont-move", QUIRK_REMOVE_CAPTURE_DONT_MOVE },
}; };
size_t i; SPA_FOR_EACH_ELEMENT_VAR(quirk_keys, i) {
for (i = 0; i < SPA_N_ELEMENTS(quirk_keys); ++i) { if (spa_streq(str, i->key))
if (spa_streq(str, quirk_keys[i].key)) return i->value;
return quirk_keys[i].value;
} }
return 0; return 0;
} }

View file

@ -174,20 +174,18 @@ static const struct format_info {
static const struct format_info *format_info_by_name(const char *str) static const struct format_info *format_info_by_name(const char *str)
{ {
uint32_t i; SPA_FOR_EACH_ELEMENT_VAR(format_info, i)
for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) if (spa_streq(str, i->name))
if (spa_streq(str, format_info[i].name)) return i;
return &format_info[i];
return NULL; return NULL;
} }
static const struct format_info *format_info_by_sf_format(int format) static const struct format_info *format_info_by_sf_format(int format)
{ {
uint32_t i;
int sub_type = (format & SF_FORMAT_SUBMASK); int sub_type = (format & SF_FORMAT_SUBMASK);
for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) SPA_FOR_EACH_ELEMENT_VAR(format_info, i)
if (format_info[i].sf_format == sub_type) if (i->sf_format == sub_type)
return &format_info[i]; return i;
return NULL; return NULL;
} }
@ -431,10 +429,10 @@ static int parse_channelmap(const char *channel_map, struct channelmap *map)
int i, nch; int i, nch;
char **ch; char **ch;
for (i = 0; i < (int) SPA_N_ELEMENTS(maps); i++) { SPA_FOR_EACH_ELEMENT_VAR(maps, m) {
if (spa_streq(maps[i].name, channel_map)) { if (spa_streq(m->name, channel_map)) {
map->n_channels = maps[i].channels; map->n_channels = m->channels;
spa_memcpy(map->channels, &maps[i].values, spa_memcpy(map->channels, &m->values,
map->n_channels * sizeof(unsigned int)); map->n_channels * sizeof(unsigned int));
return 0; return 0;
} }
@ -1589,17 +1587,16 @@ int main(int argc, char *argv[])
case TYPE_DSD: case TYPE_DSD:
{ {
struct spa_audio_info_dsd info; struct spa_audio_info_dsd info;
size_t i;
spa_zero(info); spa_zero(info);
info.channels = data.dsf.info.channels; info.channels = data.dsf.info.channels;
info.rate = data.dsf.info.rate / 8; info.rate = data.dsf.info.rate / 8;
for (i = 0; i < SPA_N_ELEMENTS(dsd_layouts); i++) { SPA_FOR_EACH_ELEMENT_VAR(dsd_layouts, i) {
if (dsd_layouts[i].type != data.dsf.info.channel_type) if (i->type != data.dsf.info.channel_type)
continue; continue;
info.channels = dsd_layouts[i].info.n_channels; info.channels = i->info.n_channels;
memcpy(info.position, dsd_layouts[i].info.position, memcpy(info.position, i->info.position,
info.channels * sizeof(uint32_t)); info.channels * sizeof(uint32_t));
} }
params[0] = spa_format_audio_dsd_build(&b, SPA_PARAM_EnumFormat, &info); params[0] = spa_format_audio_dsd_build(&b, SPA_PARAM_EnumFormat, &info);

View file

@ -1241,11 +1241,10 @@ static const struct class *classes[] =
static const struct class *find_class(const char *type, uint32_t version) static const struct class *find_class(const char *type, uint32_t version)
{ {
size_t i; SPA_FOR_EACH_ELEMENT_VAR(classes, c) {
for (i = 0; i < SPA_N_ELEMENTS(classes); i++) { if (spa_streq((*c)->type, type) &&
if (spa_streq(classes[i]->type, type) && (*c)->version <= version)
classes[i]->version <= version) return *c;
return classes[i];
} }
return NULL; return NULL;
} }