Skip to content

Commit ce14086

Browse files
committed
aplay/wasapi: move parsing to sep fn
1 parent a2c4afc commit ce14086

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/audio/playback/wasapi.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,28 @@ static void audio_play_wasapi_help() {
210210
printf("\nDevice " TBOLD("name") " can be a substring (selects first matching device).\n");
211211
}
212212

213+
static void
214+
parse_fmt(const char *cfg, int *req_index, char *req_dev_name,
215+
size_t req_dev_name_sz, wchar_t *req_deviceID, size_t req_deviceID_sz)
216+
{
217+
if (strlen(cfg) == 0) {
218+
return;
219+
}
220+
221+
if (isdigit(cfg[0])) {
222+
*req_index = atoi(cfg);
223+
} else if (cfg[0] == '{') { // ID
224+
const char *uuid = cfg;
225+
mbstate_t state{};
226+
mbsrtowcs(req_deviceID, &uuid,
227+
req_deviceID_sz - 1,
228+
&state);
229+
assert(uuid == NULL);
230+
} else { // name
231+
snprintf(req_dev_name, req_dev_name_sz, "%s", cfg);
232+
}
233+
}
234+
213235
static void *
214236
audio_play_wasapi_init(const struct audio_playback_opts *opts)
215237
{
@@ -222,20 +244,9 @@ audio_play_wasapi_init(const struct audio_playback_opts *opts)
222244
wchar_t deviceID[1024] = L""; // or:
223245
char req_dev_name[1024] = "";
224246

225-
if (strlen(opts->cfg) > 0) {
226-
if (isdigit(opts->cfg[0])) {
227-
index = atoi(opts->cfg);
228-
} else if (opts->cfg[0] == '{') { // ID
229-
const char *uuid = opts->cfg;
230-
mbstate_t state{};
231-
mbsrtowcs(deviceID, &uuid,
232-
(sizeof deviceID / sizeof deviceID[0]) - 1,
233-
&state);
234-
assert(uuid == NULL);
235-
} else { // name
236-
snprintf_ch(req_dev_name, "%s", opts->cfg);
237-
}
238-
}
247+
parse_fmt(opts->cfg, &index, req_dev_name, sizeof req_dev_name,
248+
deviceID, sizeof deviceID);
249+
239250
auto s = new state_aplay_wasapi();
240251
if (!com_initialize(&s->com_initialized, MOD_NAME)) {
241252
delete s;

0 commit comments

Comments
 (0)