SDDSlib
Loading...
Searching...
No Matches
replace_string.c
Go to the documentation of this file.
1/**
2 * @file replace_string.c
3 * @brief Provides functions to replace occurrences of substrings within strings.
4 *
5 * @copyright
6 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
7 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
8 *
9 * @license
10 * This file is distributed under the terms of the Software License Agreement
11 * found in the file LICENSE included with this distribution.
12 *
13 * @author M. Borland, C. Saunders, R. Soliday
14 */
15#include "mdb.h"
16
17/**
18 * @brief Replace all occurrences of one string with another string.
19 *
20 * This function replaces all instances of the substring `orig` in the source string `s` with the substring `repl`,
21 * and stores the result in the destination string `t`.
22 *
23 * @param t Destination string where the result is stored.
24 * @param s Source string in which replacements are to be made.
25 * @param orig The substring to be replaced.
26 * @param repl The substring to replace with.
27 * @return The number of replacements made.
28 */
29int replace_string(char *t, char *s, char *orig, char *repl) {
30 return replaceString(t, s, orig, repl, -1, 0);
31}
32
33/**
34 * @brief Replace a limited number of occurrences of one string with another string.
35 *
36 * This function replaces up to `count_limit` instances of the substring `orig` in the source string `s` with the substring `repl`,
37 * and stores the result in the destination string `t`.
38 *
39 * @param t Destination string where the result is stored.
40 * @param s Source string in which replacements are to be made.
41 * @param orig The substring to be replaced.
42 * @param repl The substring to replace with.
43 * @param count_limit The maximum number of replacements to perform.
44 * @return The number of replacements made.
45 */
46int replace_stringn(char *t, char *s, char *orig, char *repl, long count_limit) {
47 return replaceString(t, s, orig, repl, count_limit, 0);
48}
49
50/**
51 * @brief Replace occurrences of one string with another string with additional options.
52 *
53 * This function replaces instances of the substring `orig` in the source string `s` with the substring `repl`,
54 * up to a maximum of `count_limit` replacements. The `here` parameter controls additional replacement behavior.
55 * The result is stored in the destination string `t`.
56 *
57 * @param t Destination string where the result is stored.
58 * @param s Source string in which replacements are to be made.
59 * @param orig The substring to be replaced.
60 * @param repl The substring to replace with.
61 * @param count_limit The maximum number of replacements to perform. If negative, no limit is applied.
62 * @param here Additional parameter to control replacement behavior.
63 * @return The number of replacements made.
64 */
65int replaceString(char *t, char *s, char *orig, char *repl, long count_limit, long here) {
66 char *ptr0, *ptr1;
67 int count;
68 char temp;
69
70 ptr0 = s;
71 t[0] = 0;
72 count = 0;
73 while ((count_limit < 0 || count < count_limit) && (ptr1 = str_in(ptr0, orig))) {
74 if (here && ptr1 != ptr0)
75 break;
76 count++;
77 temp = *ptr1;
78 *ptr1 = 0;
79 strcat(t, ptr0);
80 strcat(t, repl);
81 ptr0 = ptr1 + strlen(orig);
82 *ptr1 = temp;
83 }
84 if (strlen(ptr0))
85 strcat(t, ptr0);
86 return (count);
87}
int replaceString(char *t, char *s, char *orig, char *repl, long count_limit, long here)
Replace occurrences of one string with another string with additional options.
int replace_stringn(char *t, char *s, char *orig, char *repl, long count_limit)
Replace a limited number of occurrences of one string with another string.
int replace_string(char *t, char *s, char *orig, char *repl)
Replace all occurrences of one string with another string.
char * str_in(char *s, char *t)
Finds the first occurrence of the substring t in the string s.
Definition str_in.c:29