2013-01-10 23:44:14 +04:00
|
|
|
/*
|
|
|
|
Functions for replacing substrings in strings.
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 2013-2024
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2013-01-10 23:44:14 +04:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2013;
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander 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 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander 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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2013-04-09 13:56:43 +04:00
|
|
|
#include "lib/global.h"
|
|
|
|
#include "lib/strutil.h"
|
2013-01-10 23:44:14 +04:00
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2013-01-10 23:44:14 +04:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Replace all substrings 'needle' in string 'haystack' by 'replacement'.
|
2023-07-23 13:18:32 +03:00
|
|
|
* If the 'needle' in the 'haystack' is escaped by backslash,
|
2013-06-13 03:29:07 +04:00
|
|
|
* then this occurrence isn't be replaced.
|
2013-01-10 23:44:14 +04:00
|
|
|
*
|
2023-07-23 13:18:32 +03:00
|
|
|
* @param haystack string contains substrings for replacement. Cannot be NULL.
|
|
|
|
* @param needle string for search. Cannot be NULL.
|
|
|
|
* @param replacement string for replace. Cannot be NULL.
|
|
|
|
* @return newly allocated string with replaced substrings or NULL if @haystack is empty.
|
2013-01-10 23:44:14 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
str_replace_all (const char *haystack, const char *needle, const char *replacement)
|
|
|
|
{
|
2023-07-23 13:18:32 +03:00
|
|
|
size_t needle_len, replacement_len;
|
|
|
|
GString *return_str = NULL;
|
|
|
|
char *needle_in_str;
|
2013-01-10 23:44:14 +04:00
|
|
|
|
|
|
|
needle_len = strlen (needle);
|
2023-07-23 13:18:32 +03:00
|
|
|
replacement_len = strlen (replacement);
|
2013-01-10 23:44:14 +04:00
|
|
|
|
2023-07-23 13:18:32 +03:00
|
|
|
while ((needle_in_str = strstr (haystack, needle)) != NULL)
|
2013-01-10 23:44:14 +04:00
|
|
|
{
|
2023-07-23 13:18:32 +03:00
|
|
|
if (return_str == NULL)
|
|
|
|
return_str = g_string_sized_new (32);
|
2013-01-10 23:44:14 +04:00
|
|
|
|
2024-03-09 14:41:08 +03:00
|
|
|
if (str_is_char_escaped (haystack, needle_in_str))
|
2013-01-10 23:44:14 +04:00
|
|
|
{
|
|
|
|
char *backslash = needle_in_str - 1;
|
|
|
|
|
|
|
|
if (haystack != backslash)
|
2023-07-23 13:18:32 +03:00
|
|
|
g_string_append_len (return_str, haystack, backslash - haystack);
|
|
|
|
g_string_append_len (return_str, needle_in_str, needle_in_str - backslash);
|
2013-01-10 23:44:14 +04:00
|
|
|
haystack = needle_in_str + 1;
|
|
|
|
}
|
2023-07-23 13:18:32 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (needle_in_str != haystack)
|
|
|
|
g_string_append_len (return_str, haystack, needle_in_str - haystack);
|
|
|
|
g_string_append_len (return_str, replacement, replacement_len);
|
|
|
|
haystack = needle_in_str + needle_len;
|
|
|
|
}
|
2013-01-10 23:44:14 +04:00
|
|
|
}
|
|
|
|
|
2023-07-23 13:18:32 +03:00
|
|
|
if (*haystack != '\0')
|
|
|
|
{
|
|
|
|
if (return_str == NULL)
|
|
|
|
return strdup (haystack);
|
|
|
|
|
|
|
|
g_string_append (return_str, haystack);
|
|
|
|
}
|
2013-01-10 23:44:14 +04:00
|
|
|
|
2023-07-23 13:18:32 +03:00
|
|
|
return (return_str != NULL ? g_string_free (return_str, FALSE) : NULL);
|
2013-01-10 23:44:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|