summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1aaedbc)
raw | patch | inline | side by side (parent: 1aaedbc)
author | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Sat, 20 Dec 2008 15:54:32 +0000 (16:54 +0100) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Sat, 20 Dec 2008 15:54:32 +0000 (16:54 +0100) |
Works like `subst', but instead of specifying start and end offsets you
specify `needle', the string that is to be replaced. If `needle' is found
in `string' (using strstr(3)), the offset is calculated and `subst' is
called with the determined parameters.
specify `needle', the string that is to be replaced. If `needle' is found
in `string' (using strstr(3)), the offset is calculated and `subst' is
called with the determined parameters.
src/utils_subst.c | patch | blob | history | |
src/utils_subst.h | patch | blob | history |
diff --git a/src/utils_subst.c b/src/utils_subst.c
index 05dc8441c8f965a088134132411c27bac4c180c5..3c9fe5a27d4a34436aff902d847550113c5af64f 100644 (file)
--- a/src/utils_subst.c
+++ b/src/utils_subst.c
return ret;
} /* asubst */
+char *subst_string (char *buf, size_t buflen, const char *string,
+ const char *needle, const char *replacement)
+{
+ char *temp;
+ size_t needle_len;
+ size_t i;
+
+ if ((buf == NULL) || (string == NULL)
+ || (needle == NULL) || (replacement == NULL))
+ return (NULL);
+
+ temp = (char *) malloc (buflen);
+ if (temp == NULL)
+ {
+ ERROR ("subst_string: malloc failed.");
+ return (NULL);
+ }
+
+ needle_len = strlen (needle);
+ strncpy (buf, string, buflen);
+
+ /* Limit the loop to prevent endless loops. */
+ for (i = 0; i < buflen; i++)
+ {
+ char *begin_ptr;
+ size_t begin;
+
+ /* Find `needle' in `buf'. */
+ begin_ptr = strstr (buf, needle);
+ if (begin_ptr == NULL)
+ break;
+
+ /* Calculate the start offset. */
+ begin = begin_ptr - buf;
+
+ /* Substitute the region using `subst'. The result is stored in
+ * `temp'. */
+ begin_ptr = subst (temp, buflen, buf,
+ begin, begin + needle_len,
+ replacement);
+ if (begin_ptr == NULL)
+ {
+ WARNING ("subst_string: subst failed.");
+ break;
+ }
+
+ /* Copy the new string in `temp' to `buf' for the next round. */
+ strncpy (buf, temp, buflen);
+ }
+
+ if (i >= 100)
+ {
+ WARNING ("subst_string: Loop exited after %zu iterations: "
+ "string = %s; needle = %s; replacement = %s;",
+ i, string, needle, replacement);
+ }
+
+ sfree (temp);
+ return (buf);
+} /* char *subst_string */
+
/* vim: set sw=4 ts=4 tw=78 noexpandtab : */
diff --git a/src/utils_subst.h b/src/utils_subst.h
index bba33af4ea3d6d572949c20f8b875f997536dfe5..4387b85d2d8bc801a3940b601ceb7dba4eaed267 100644 (file)
--- a/src/utils_subst.h
+++ b/src/utils_subst.h
*/
char *asubst (const char *string, int off1, int off2, const char *replacement);
+/*
+ * subst_string:
+ *
+ * Works like `subst', but instead of specifying start and end offsets you
+ * specify `needle', the string that is to be replaced. If `needle' is found
+ * in `string' (using strstr(3)), the offset is calculated and `subst' is
+ * called with the determined parameters.
+ *
+ * If the substring is not found, no error will be indicated and
+ * `subst_string' works mostly like `strncpy'.
+ *
+ * If the substring appears multiple times, all appearances will be replaced.
+ * If the substring has been found `buflen' times, an endless loop is assumed
+ * and the loop is broken. A warning is printed and the function returns
+ * success.
+ */
+char *subst_string (char *buf, size_t buflen, const char *string,
+ const char *needle, const char *replacement);
+
#endif /* UTILS_SUBST_H */
/* vim: set sw=4 ts=4 tw=78 noexpandtab : */