mirror of
https://github.com/strongswan/strongswan.git
synced 2025-10-07 00:01:49 -04:00
string: Move string related utility functions to separate files
This commit is contained in:
parent
03cf888277
commit
bbfe7a80b1
@ -40,7 +40,7 @@ settings/settings_parser.c settings/settings_lexer.c utils/cpu_feature.c \
|
|||||||
utils/utils.c utils/chunk.c utils/debug.c utils/enum.c utils/identification.c \
|
utils/utils.c utils/chunk.c utils/debug.c utils/enum.c utils/identification.c \
|
||||||
utils/lexparser.c utils/optionsfrom.c utils/capabilities.c utils/backtrace.c \
|
utils/lexparser.c utils/optionsfrom.c utils/capabilities.c utils/backtrace.c \
|
||||||
utils/parser_helper.c utils/test.c utils/process.c utils/utils/strerror.c \
|
utils/parser_helper.c utils/test.c utils/process.c utils/utils/strerror.c \
|
||||||
utils/utils/atomics.c
|
utils/utils/atomics.c utils/utils/string.c
|
||||||
|
|
||||||
libstrongswan_la_SOURCES += \
|
libstrongswan_la_SOURCES += \
|
||||||
threading/thread.c \
|
threading/thread.c \
|
||||||
|
@ -38,7 +38,7 @@ settings/settings_parser.y settings/settings_lexer.l utils/cpu_feature.c \
|
|||||||
utils/utils.c utils/chunk.c utils/debug.c utils/enum.c utils/identification.c \
|
utils/utils.c utils/chunk.c utils/debug.c utils/enum.c utils/identification.c \
|
||||||
utils/lexparser.c utils/optionsfrom.c utils/capabilities.c utils/backtrace.c \
|
utils/lexparser.c utils/optionsfrom.c utils/capabilities.c utils/backtrace.c \
|
||||||
utils/parser_helper.c utils/test.c utils/process.c utils/utils/strerror.c \
|
utils/parser_helper.c utils/test.c utils/process.c utils/utils/strerror.c \
|
||||||
utils/utils/atomics.c
|
utils/utils/atomics.c utils/utils/string.c
|
||||||
|
|
||||||
if !USE_WINDOWS
|
if !USE_WINDOWS
|
||||||
libstrongswan_la_SOURCES += \
|
libstrongswan_la_SOURCES += \
|
||||||
@ -108,7 +108,8 @@ utils/cpu_feature.h utils/leak_detective.h utils/printf_hook/printf_hook.h \
|
|||||||
utils/printf_hook/printf_hook_vstr.h utils/printf_hook/printf_hook_builtin.h \
|
utils/printf_hook/printf_hook_vstr.h utils/printf_hook/printf_hook_builtin.h \
|
||||||
utils/parser_helper.h utils/test.h utils/integrity_checker.h utils/process.h \
|
utils/parser_helper.h utils/test.h utils/integrity_checker.h utils/process.h \
|
||||||
utils/utils/strerror.h utils/compat/windows.h utils/compat/apple.h \
|
utils/utils/strerror.h utils/compat/windows.h utils/compat/apple.h \
|
||||||
utils/utils/atomics.h utils/utils/types.h utils/utils/byteorder.h
|
utils/utils/atomics.h utils/utils/types.h utils/utils/byteorder.h \
|
||||||
|
utils/utils/string.h
|
||||||
endif
|
endif
|
||||||
|
|
||||||
library.lo : $(top_builddir)/config.status
|
library.lo : $(top_builddir)/config.status
|
||||||
|
@ -215,80 +215,6 @@ void *utils_memrchr(const void *s, int c, size_t n)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Described in header.
|
|
||||||
*/
|
|
||||||
char* translate(char *str, const char *from, const char *to)
|
|
||||||
{
|
|
||||||
char *pos = str;
|
|
||||||
if (strlen(from) != strlen(to))
|
|
||||||
{
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
while (pos && *pos)
|
|
||||||
{
|
|
||||||
char *match;
|
|
||||||
if ((match = strchr(from, *pos)) != NULL)
|
|
||||||
{
|
|
||||||
*pos = to[match - from];
|
|
||||||
}
|
|
||||||
pos++;
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Described in header.
|
|
||||||
*/
|
|
||||||
char* strreplace(const char *str, const char *search, const char *replace)
|
|
||||||
{
|
|
||||||
size_t len, slen, rlen, count = 0;
|
|
||||||
char *res, *pos, *found, *dst;
|
|
||||||
|
|
||||||
if (!str || !*str || !search || !*search || !replace)
|
|
||||||
{
|
|
||||||
return (char*)str;
|
|
||||||
}
|
|
||||||
slen = strlen(search);
|
|
||||||
rlen = strlen(replace);
|
|
||||||
if (slen != rlen)
|
|
||||||
{
|
|
||||||
for (pos = (char*)str; (pos = strstr(pos, search)); pos += slen)
|
|
||||||
{
|
|
||||||
found = pos;
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
if (!count)
|
|
||||||
{
|
|
||||||
return (char*)str;
|
|
||||||
}
|
|
||||||
len = (found - str) + strlen(found) + count * (rlen - slen);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
len = strlen(str);
|
|
||||||
}
|
|
||||||
found = strstr(str, search);
|
|
||||||
if (!found)
|
|
||||||
{
|
|
||||||
return (char*)str;
|
|
||||||
}
|
|
||||||
dst = res = malloc(len + 1);
|
|
||||||
pos = (char*)str;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
len = found - pos;
|
|
||||||
memcpy(dst, pos, len);
|
|
||||||
dst += len;
|
|
||||||
memcpy(dst, replace, rlen);
|
|
||||||
dst += rlen;
|
|
||||||
pos = found + slen;
|
|
||||||
}
|
|
||||||
while ((found = strstr(pos, search)));
|
|
||||||
strcpy(dst, pos);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,6 +78,7 @@
|
|||||||
#include "enum.h"
|
#include "enum.h"
|
||||||
#include "utils/atomics.h"
|
#include "utils/atomics.h"
|
||||||
#include "utils/byteorder.h"
|
#include "utils/byteorder.h"
|
||||||
|
#include "utils/string.h"
|
||||||
#include "utils/strerror.h"
|
#include "utils/strerror.h"
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
# include "compat/apple.h"
|
# include "compat/apple.h"
|
||||||
@ -102,62 +103,6 @@ void utils_init();
|
|||||||
*/
|
*/
|
||||||
void utils_deinit();
|
void utils_deinit();
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function that compares two strings for equality
|
|
||||||
*/
|
|
||||||
static inline bool streq(const char *x, const char *y)
|
|
||||||
{
|
|
||||||
return strcmp(x, y) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function that compares two strings for equality, length limited
|
|
||||||
*/
|
|
||||||
static inline bool strneq(const char *x, const char *y, size_t len)
|
|
||||||
{
|
|
||||||
return strncmp(x, y, len) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function that checks if a string starts with a given prefix
|
|
||||||
*/
|
|
||||||
static inline bool strpfx(const char *x, const char *prefix)
|
|
||||||
{
|
|
||||||
return strneq(x, prefix, strlen(prefix));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function that compares two strings for equality ignoring case
|
|
||||||
*/
|
|
||||||
static inline bool strcaseeq(const char *x, const char *y)
|
|
||||||
{
|
|
||||||
return strcasecmp(x, y) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function that compares two strings for equality ignoring case, length limited
|
|
||||||
*/
|
|
||||||
static inline bool strncaseeq(const char *x, const char *y, size_t len)
|
|
||||||
{
|
|
||||||
return strncasecmp(x, y, len) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function that checks if a string starts with a given prefix
|
|
||||||
*/
|
|
||||||
static inline bool strcasepfx(const char *x, const char *prefix)
|
|
||||||
{
|
|
||||||
return strncaseeq(x, prefix, strlen(prefix));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* NULL-safe strdup variant
|
|
||||||
*/
|
|
||||||
static inline char *strdupnull(const char *s)
|
|
||||||
{
|
|
||||||
return s ? strdup(s) : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function that compares two binary blobs for equality
|
* Helper function that compares two binary blobs for equality
|
||||||
*/
|
*/
|
||||||
@ -609,28 +554,6 @@ void *utils_memrchr(const void *s, int c, size_t n);
|
|||||||
#define memrchr(s,c,n) utils_memrchr(s,c,n)
|
#define memrchr(s,c,n) utils_memrchr(s,c,n)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
|
||||||
* Translates the characters in the given string, searching for characters
|
|
||||||
* in 'from' and mapping them to characters in 'to'.
|
|
||||||
* The two characters sets 'from' and 'to' must contain the same number of
|
|
||||||
* characters.
|
|
||||||
*/
|
|
||||||
char *translate(char *str, const char *from, const char *to);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Replaces all occurrences of search in the given string with replace.
|
|
||||||
*
|
|
||||||
* Allocates memory only if anything is replaced in the string. The original
|
|
||||||
* string is also returned if any of the arguments are invalid (e.g. if search
|
|
||||||
* is empty or any of them are NULL).
|
|
||||||
*
|
|
||||||
* @param str original string
|
|
||||||
* @param search string to search for and replace
|
|
||||||
* @param replace string to replace found occurrences with
|
|
||||||
* @return allocated string, if anything got replaced, str otherwise
|
|
||||||
*/
|
|
||||||
char *strreplace(const char *str, const char *search, const char *replace);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Portable function to wait for SIGINT/SIGTERM (or equivalent).
|
* Portable function to wait for SIGINT/SIGTERM (or equivalent).
|
||||||
*/
|
*/
|
||||||
|
91
src/libstrongswan/utils/utils/string.c
Normal file
91
src/libstrongswan/utils/utils/string.c
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2008-2014 Tobias Brunner
|
||||||
|
* Copyright (C) 2005-2008 Martin Willi
|
||||||
|
* Hochschule fuer Technik Rapperswil
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation; either version 2 of the License, or (at your
|
||||||
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||||
|
*
|
||||||
|
* This program 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 General Public License
|
||||||
|
* for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <utils/utils.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Described in header.
|
||||||
|
*/
|
||||||
|
char* translate(char *str, const char *from, const char *to)
|
||||||
|
{
|
||||||
|
char *pos = str;
|
||||||
|
if (strlen(from) != strlen(to))
|
||||||
|
{
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
while (pos && *pos)
|
||||||
|
{
|
||||||
|
char *match;
|
||||||
|
if ((match = strchr(from, *pos)) != NULL)
|
||||||
|
{
|
||||||
|
*pos = to[match - from];
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Described in header.
|
||||||
|
*/
|
||||||
|
char* strreplace(const char *str, const char *search, const char *replace)
|
||||||
|
{
|
||||||
|
size_t len, slen, rlen, count = 0;
|
||||||
|
char *res, *pos, *found, *dst;
|
||||||
|
|
||||||
|
if (!str || !*str || !search || !*search || !replace)
|
||||||
|
{
|
||||||
|
return (char*)str;
|
||||||
|
}
|
||||||
|
slen = strlen(search);
|
||||||
|
rlen = strlen(replace);
|
||||||
|
if (slen != rlen)
|
||||||
|
{
|
||||||
|
for (pos = (char*)str; (pos = strstr(pos, search)); pos += slen)
|
||||||
|
{
|
||||||
|
found = pos;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
if (!count)
|
||||||
|
{
|
||||||
|
return (char*)str;
|
||||||
|
}
|
||||||
|
len = (found - str) + strlen(found) + count * (rlen - slen);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = strlen(str);
|
||||||
|
}
|
||||||
|
found = strstr(str, search);
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
return (char*)str;
|
||||||
|
}
|
||||||
|
dst = res = malloc(len + 1);
|
||||||
|
pos = (char*)str;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
len = found - pos;
|
||||||
|
memcpy(dst, pos, len);
|
||||||
|
dst += len;
|
||||||
|
memcpy(dst, replace, rlen);
|
||||||
|
dst += rlen;
|
||||||
|
pos = found + slen;
|
||||||
|
}
|
||||||
|
while ((found = strstr(pos, search)));
|
||||||
|
strcpy(dst, pos);
|
||||||
|
return res;
|
||||||
|
}
|
103
src/libstrongswan/utils/utils/string.h
Normal file
103
src/libstrongswan/utils/utils/string.h
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2008-2014 Tobias Brunner
|
||||||
|
* Copyright (C) 2008 Martin Willi
|
||||||
|
* Hochschule fuer Technik Rapperswil
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation; either version 2 of the License, or (at your
|
||||||
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||||
|
*
|
||||||
|
* This program 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 General Public License
|
||||||
|
* for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup string_i string
|
||||||
|
* @{ @ingroup utils_i
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef STRING_H_
|
||||||
|
#define STRING_H_
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that compares two strings for equality
|
||||||
|
*/
|
||||||
|
static inline bool streq(const char *x, const char *y)
|
||||||
|
{
|
||||||
|
return strcmp(x, y) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that compares two strings for equality, length limited
|
||||||
|
*/
|
||||||
|
static inline bool strneq(const char *x, const char *y, size_t len)
|
||||||
|
{
|
||||||
|
return strncmp(x, y, len) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that checks if a string starts with a given prefix
|
||||||
|
*/
|
||||||
|
static inline bool strpfx(const char *x, const char *prefix)
|
||||||
|
{
|
||||||
|
return strneq(x, prefix, strlen(prefix));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that compares two strings for equality ignoring case
|
||||||
|
*/
|
||||||
|
static inline bool strcaseeq(const char *x, const char *y)
|
||||||
|
{
|
||||||
|
return strcasecmp(x, y) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that compares two strings for equality ignoring case, length limited
|
||||||
|
*/
|
||||||
|
static inline bool strncaseeq(const char *x, const char *y, size_t len)
|
||||||
|
{
|
||||||
|
return strncasecmp(x, y, len) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that checks if a string starts with a given prefix
|
||||||
|
*/
|
||||||
|
static inline bool strcasepfx(const char *x, const char *prefix)
|
||||||
|
{
|
||||||
|
return strncaseeq(x, prefix, strlen(prefix));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NULL-safe strdup variant
|
||||||
|
*/
|
||||||
|
static inline char *strdupnull(const char *s)
|
||||||
|
{
|
||||||
|
return s ? strdup(s) : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translates the characters in the given string, searching for characters
|
||||||
|
* in 'from' and mapping them to characters in 'to'.
|
||||||
|
* The two characters sets 'from' and 'to' must contain the same number of
|
||||||
|
* characters.
|
||||||
|
*/
|
||||||
|
char *translate(char *str, const char *from, const char *to);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces all occurrences of search in the given string with replace.
|
||||||
|
*
|
||||||
|
* Allocates memory only if anything is replaced in the string. The original
|
||||||
|
* string is also returned if any of the arguments are invalid (e.g. if search
|
||||||
|
* is empty or any of them are NULL).
|
||||||
|
*
|
||||||
|
* @param str original string
|
||||||
|
* @param search string to search for and replace
|
||||||
|
* @param replace string to replace found occurrences with
|
||||||
|
* @return allocated string, if anything got replaced, str otherwise
|
||||||
|
*/
|
||||||
|
char *strreplace(const char *str, const char *search, const char *replace);
|
||||||
|
|
||||||
|
#endif /** STRING_H_ @} */
|
Loading…
x
Reference in New Issue
Block a user