Skip to content

Commit d21c44c

Browse files
committed
portaudio: allow also case insensitive substr
1 parent 3080fe3 commit d21c44c

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/audio/portaudio_common.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <stdlib.h> // for calloc
4343
#include <string.h> // for strncpy, strstr
4444

45+
#include "compat/strings.h" // for ug_strcasecmp
4546
#include "debug.h"
4647
#include "host.h" // for uv_argv
4748
#include "portaudio_common.h"
@@ -259,6 +260,7 @@ portaudio_select_device_by_name(const char *name,
259260
{
260261
int prefix_idx = -1; // dev idx starting with name
261262
int substr_idx = -1; // dev idx with name as substr
263+
int substr_icase_idx = -1; // dev idx with name as substr (ignore case)
262264
for (int i = 0; i < Pa_GetDeviceCount(); i++) {
263265
const PaDeviceInfo *device_info = Pa_GetDeviceInfo(i);
264266
if (!has_channels(device_info, dir)) {
@@ -274,13 +276,20 @@ portaudio_select_device_by_name(const char *name,
274276
prefix_idx = i;
275277
}
276278
}
279+
if (ug_strcasestr(device_info->name, name) ==
280+
device_info->name) {
281+
substr_icase_idx = i;
282+
}
277283
}
278284
if (prefix_idx != -1) {
279285
return prefix_idx;
280286
}
281287
if (substr_idx != -1) {
282288
return substr_idx;
283289
}
290+
if (substr_icase_idx != -1) {
291+
return substr_icase_idx;
292+
}
284293
log_msg(LOG_LEVEL_ERROR, MOD_NAME "No device named \"%s\" was found!\n", name);
285294
return -2;
286295
}

src/compat/strings.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* compatibility header for strcasecmp. strdup, strerror_s
66
*/
77
/*
8-
* Copyright (c) 2024 CESNET
8+
* Copyright (c) 2024-2025 CESNET
99
* All rights reserved.
1010
*
1111
* Redistribution and use in source and binary forms, with or without
@@ -77,6 +77,9 @@ COMPAT_MISC_EXT_C int __xpg_strerror_r(int errcode, char *buffer, size_t length)
7777
#define strdupa(s) (char *) memcpy(alloca(strlen(s) + 1), s, strlen(s) + 1)
7878
#endif // defined strdupa
7979

80+
COMPAT_MISC_EXT_C const char *ug_strcasestr(const char *haystack,
81+
const char *needle);
82+
8083
#undef COMPAT_MISC_EXT_C
8184

8285
#endif // ! defined COMPAT_STRINGS_H_D54CAFC8_A1A0_4FF5_80A0_91F34FB11E12

src/utils/string.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author Martin Pulec <[email protected]>
44
*/
55
/*
6-
* Copyright (c) 2014-2024 CESNET, z. s. p. o.
6+
* Copyright (c) 2014-2025 CESNET
77
* All rights reserved.
88
*
99
* Redistribution and use in source and binary forms, with or without
@@ -47,6 +47,7 @@
4747
#include <unistd.h>
4848
#endif
4949

50+
#include "compat/strings.h"
5051
#include "debug.h"
5152
#include "utils/string.h"
5253

@@ -223,3 +224,25 @@ pretty_print_fourcc(const void *fcc)
223224
*out_ptr = '\0';
224225
return out;
225226
}
227+
228+
const char *
229+
ug_strcasestr(const char *haystick, const char *needle)
230+
{
231+
while (strlen(haystick) >= strlen(needle)) {
232+
const char *cur_haystick = haystick;
233+
const char *cur_needle = needle;
234+
while (*cur_needle != '\0') {
235+
if (tolower(*cur_haystick) != tolower(*cur_needle)) {
236+
break;
237+
}
238+
cur_haystick += 1;
239+
cur_needle += 1;
240+
}
241+
if (*cur_needle == '\0') {
242+
return haystick;
243+
}
244+
245+
haystick += 1;
246+
}
247+
return NULL;
248+
}

0 commit comments

Comments
 (0)