diff --git a/src/common/Makefile.am b/src/common/Makefile.am index e5efb97..07e139e 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -19,7 +19,8 @@ include_HEADERS = include/droid/version.h \ include/droid/conversion.h \ include/droid/droid-config.h \ include/droid/droid-util.h \ - include/droid/sllist.h + include/droid/sllist.h \ + include/droid/utils.h pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libdroid-util.pc @@ -29,6 +30,7 @@ libdroid_util_la_SOURCES = droid-util.c \ conversion.c \ config-parser-xml.c \ sllist.c \ + utils.c \ droid-util-audio.h libdroid_util_la_LDFLAGS = -avoid-version -Wl,-z,noexecstack -lhybris-common $(SBJ_DEVICE_LDFLAGS) libdroid_util_la_LIBADD = $(AM_LIBADD) diff --git a/src/common/include/droid/utils.h b/src/common/include/droid/utils.h new file mode 100644 index 0000000..884d33b --- /dev/null +++ b/src/common/include/droid/utils.h @@ -0,0 +1,32 @@ +#ifndef foodroidcommonutilsfoo +#define foodroidcommonutilsfoo + +/* + * Copyright (C) 2022 Jolla Ltd. + * + * Contact: Juho Hämäläinen + * + * These PulseAudio Modules are 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 + * version 2.1 of the License. + * + * 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 Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +void dm_replace_in_place(char **string, const char *a, const char *b); +bool dm_strcasestr(const char *haystack, const char *needle); + +#endif diff --git a/src/common/utils.c b/src/common/utils.c new file mode 100644 index 0000000..9d697d6 --- /dev/null +++ b/src/common/utils.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2022 Jolla Ltd. + * + * Contact: Juho Hämäläinen + * + * These PulseAudio Modules are 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 + * version 2.1 of the License. + * + * 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 Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include +#include +#include "droid/utils.h" + +void dm_replace_in_place(char **string, const char *a, const char *b) { + char *tmp; + + pa_assert(*string); + pa_assert(a); + pa_assert(b); + + tmp = pa_replace(*string, a, b); + pa_xfree(*string); + *string = tmp; +} + +/* Simple strcasestr replacement. */ +bool dm_strcasestr(const char *haystack, const char *needle) { + size_t len_haystack, len_needle; + + len_haystack = strlen(haystack); + len_needle = strlen(needle); + + if (len_needle > len_haystack) + return false; + + for (size_t i = 0; i < len_haystack; i++) { + if (len_needle > len_haystack - i) + return false; + + if (strncasecmp(haystack + i, needle, len_needle) == 0) + return true; + } + + return false; +}