common: Add two string utility functions.

This commit is contained in:
Juho Hämäläinen 2022-02-23 10:35:39 +02:00
parent eefdb31f57
commit 40c320bc07
3 changed files with 99 additions and 1 deletions

View file

@ -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)

View file

@ -0,0 +1,32 @@
#ifndef foodroidcommonutilsfoo
#define foodroidcommonutilsfoo
/*
* Copyright (C) 2022 Jolla Ltd.
*
* Contact: Juho Hämäläinen <juho.hamalainen@jolla.com>
*
* 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 <config.h>
#endif
void dm_replace_in_place(char **string, const char *a, const char *b);
bool dm_strcasestr(const char *haystack, const char *needle);
#endif

64
src/common/utils.c Normal file
View file

@ -0,0 +1,64 @@
/*
* Copyright (C) 2022 Jolla Ltd.
*
* Contact: Juho Hämäläinen <juho.hamalainen@jolla.com>
*
* 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 <config.h>
#endif
#include <string.h>
#include <strings.h>
#include <pulsecore/core-util.h>
#include <pulse/xmalloc.h>
#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;
}