summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2014-04-24 18:21:32 +0000
committerrubidium <rubidium@openttd.org>2014-04-24 18:21:32 +0000
commit382ca0941fe53db8d01e145cff12bed91f51aaea (patch)
tree6ca4f0856b2bd4d7847acf3ee09c7fb16fce5264
parent389d61f2faa0f90ec86b63ac01a7d9f4c58254af (diff)
downloadopenttd-382ca0941fe53db8d01e145cff12bed91f51aaea.tar.xz
(svn r26498) -Add: method for duplicating strings with similar behaviours as strecpy and strecat; give end pointer instead of a size
-rw-r--r--src/string.cpp15
-rw-r--r--src/string_func.h1
2 files changed, 16 insertions, 0 deletions
diff --git a/src/string.cpp b/src/string.cpp
index 6f9e4e2f0..90005071d 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -114,6 +114,21 @@ char *strecpy(char *dst, const char *src, const char *last)
}
/**
+ * Create a duplicate of the given string.
+ * @param s The string to duplicate.
+ * @param last The last character that is safe to duplicate. If NULL, the whole string is duplicated.
+ * @note The maximum length of the resulting string might therefore be last - s + 1.
+ * @return The duplicate of the string.
+ */
+char *stredup(const char *s, const char *last)
+{
+ size_t len = last == NULL ? strlen(s) : ttd_strnlen(s, last - s + 1);
+ char *tmp = CallocT<char>(len + 1);
+ memcpy(tmp, s, len);
+ return tmp;
+}
+
+/**
* Format, "printf", into a newly allocated string.
* @param str The formatting string.
* @return The formatted string. You must free this!
diff --git a/src/string_func.h b/src/string_func.h
index df9062cef..8354d67cd 100644
--- a/src/string_func.h
+++ b/src/string_func.h
@@ -31,6 +31,7 @@
char *strecat(char *dst, const char *src, const char *last);
char *strecpy(char *dst, const char *src, const char *last);
+char *stredup(const char *src, const char *last = NULL);
int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);