[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [55275] trunk/blender/source/blender: add BLI_strncpy_rlen(), a clone of BLI_strncpy() that returns the number of bytes copied.

Campbell Barton ideasman42 at gmail.com
Thu Mar 14 11:07:05 CET 2013


Revision: 55275
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=55275
Author:   campbellbarton
Date:     2013-03-14 10:07:05 +0000 (Thu, 14 Mar 2013)
Log Message:
-----------
add BLI_strncpy_rlen(), a clone of BLI_strncpy() that returns the number of bytes copied.

also move doxy comments for BLI_string.h into the C file.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_string.h
    trunk/blender/source/blender/blenlib/intern/string.c
    trunk/blender/source/blender/blenlib/intern/winstuff.c
    trunk/blender/source/blender/editors/include/ED_armature.h

Modified: trunk/blender/source/blender/blenlib/BLI_string.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_string.h	2013-03-14 09:55:05 UTC (rev 55274)
+++ trunk/blender/source/blender/blenlib/BLI_string.h	2013-03-14 10:07:05 UTC (rev 55275)
@@ -38,42 +38,20 @@
 extern "C" {
 #endif
 
-/**
- * Duplicates the cstring \a str into a newly mallocN'd
- * string and returns it.
- *
- * \param str The string to be duplicated
- * \retval Returns the duplicated string
- */
-char *BLI_strdup(const char *str)
+char *BLI_strdupn(const char *str, const size_t len)
 #ifdef __GNUC__
 __attribute__((warn_unused_result))
 __attribute__((nonnull))
 #endif
 ;
 
-/**
- * Duplicates the first \a len bytes of cstring \a str
- * into a newly mallocN'd string and returns it. \a str
- * is assumed to be at least len bytes long.
- *
- * \param str The string to be duplicated
- * \param len The number of bytes to duplicate
- * \retval Returns the duplicated string
- */
-char *BLI_strdupn(const char *str, const size_t len)
+char *BLI_strdup(const char *str)
 #ifdef __GNUC__
 __attribute__((warn_unused_result))
 __attribute__((nonnull))
 #endif
 ;
 
-/**
- * Appends the two strings, and returns new mallocN'ed string
- * \param str1 first string for copy
- * \param str2 second string for append
- * \retval Returns dst
- */
 char *BLI_strdupcat(const char *__restrict str1, const char *__restrict str2)
 #ifdef __GNUC__
 __attribute__((warn_unused_result))
@@ -81,32 +59,19 @@
 #endif
 ;
 
-/**
- * Like strncpy but ensures dst is always
- * '\0' terminated.
- *
- * \param dst Destination for copy
- * \param src Source string to copy
- * \param maxncpy Maximum number of characters to copy (generally
- *   the size of dst)
- * \retval Returns dst
- */
 char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
 #ifdef __GNUC__
 __attribute__((nonnull))
 #endif
 ;
 
-/**
- *Makes a copy of the text within the "" that appear after some text 'blahblah'
- * i.e. for string 'pose["apples"]' with prefix 'pose[', it should grab "apples"
- *
- *  - str: is the entire string to chop
- *	- prefix: is the part of the string to leave out
- *
- * Assume that the strings returned must be freed afterwards, and that the inputs will contain
- * data we want...
- */
+size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
+#ifdef __GNUC__
+__attribute__((warn_unused_result))
+__attribute__((nonnull))
+#endif
+;
+
 char *BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict prefix)
 #ifdef __GNUC__
 __attribute__((warn_unused_result))
@@ -114,16 +79,6 @@
 #endif
 ;
 
-/**
- * Returns a copy of the cstring \a str into a newly mallocN'd
- * string with all instances of oldText replaced with newText,
- * and returns it.
- *
- * \param str The string to replace occurrences of oldText in
- * \param oldText The text in the string to find and replace
- * \param newText The text in the string to find and replace
- * \retval Returns the duplicated string
- */
 char *BLI_replacestr(char *__restrict str, const char *__restrict oldText, const char *__restrict newText)
 #ifdef __GNUC__
 __attribute__((warn_unused_result))
@@ -131,9 +86,6 @@
 #endif
 ;
 
-/*
- * Replacement for snprintf
- */
 size_t BLI_snprintf(char *__restrict buffer, size_t len, const char *__restrict format, ...)
 #ifdef __GNUC__
 __attribute__ ((format(printf, 3, 4)))
@@ -141,19 +93,12 @@
 #endif
 ;
 
-/*
- * Replacement for vsnprintf
- */
 size_t BLI_vsnprintf(char *__restrict buffer, size_t count, const char *__restrict format, va_list arg)
 #ifdef __GNUC__
 __attribute__ ((format(printf, 3, 0)))
 #endif
 ;
 
-/*
- * Print formatted string into a newly mallocN'd string
- * and return it.
- */
 char *BLI_sprintfN(const char *__restrict format, ...)
 #ifdef __GNUC__
 __attribute__ ((format(printf, 1, 2)))
@@ -168,11 +113,6 @@
 #endif
 ;
 
-/**
- * Compare two strings without regard to case.
- *
- * \retval True if the strings are equal, false otherwise.
- */
 int BLI_strcaseeq(const char *a, const char *b)
 #ifdef __GNUC__
 __attribute__((warn_unused_result))
@@ -214,7 +154,7 @@
 #ifdef __GNUC__
 __attribute__((nonnull))
 #endif
-; /* time var is global */
+;
 
 void BLI_ascii_strtolower(char *str, const size_t len)
 #ifdef __GNUC__

Modified: trunk/blender/source/blender/blenlib/intern/string.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/string.c	2013-03-14 09:55:05 UTC (rev 55274)
+++ trunk/blender/source/blender/blenlib/intern/string.c	2013-03-14 10:07:05 UTC (rev 55275)
@@ -43,6 +43,15 @@
 
 #include "BLI_utildefines.h"
 
+/**
+ * Duplicates the first \a len bytes of cstring \a str
+ * into a newly mallocN'd string and returns it. \a str
+ * is assumed to be at least len bytes long.
+ *
+ * \param str The string to be duplicated
+ * \param len The number of bytes to duplicate
+ * \retval Returns the duplicated string
+ */
 char *BLI_strdupn(const char *str, const size_t len)
 {
 	char *n = MEM_mallocN(len + 1, "strdup");
@@ -51,11 +60,25 @@
 	
 	return n;
 }
+
+/**
+ * Duplicates the cstring \a str into a newly mallocN'd
+ * string and returns it.
+ *
+ * \param str The string to be duplicated
+ * \retval Returns the duplicated string
+ */
 char *BLI_strdup(const char *str)
 {
 	return BLI_strdupn(str, strlen(str));
 }
 
+/**
+ * Appends the two strings, and returns new mallocN'ed string
+ * \param str1 first string for copy
+ * \param str2 second string for append
+ * \retval Returns dst
+ */
 char *BLI_strdupcat(const char *__restrict str1, const char *__restrict str2)
 {
 	size_t len;
@@ -69,6 +92,16 @@
 	return n;
 }
 
+/**
+ * Like strncpy but ensures dst is always
+ * '\0' terminated.
+ *
+ * \param dst Destination for copy
+ * \param src Source string to copy
+ * \param maxncpy Maximum number of characters to copy (generally
+ * the size of dst)
+ * \retval Returns dst
+ */
 char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
 {
 	size_t srclen = BLI_strnlen(src, maxncpy - 1);
@@ -79,6 +112,32 @@
 	return dst;
 }
 
+/**
+ * Like strncpy but ensures dst is always
+ * '\0' terminated.
+ *
+ * \note This is a duplicate of #BLI_strncpy that returns bytes copied.
+ * And is a drop in replacement for 'snprintf(str, sizeof(str), "%s", arg);'
+ *
+ * \param dst Destination for copy
+ * \param src Source string to copy
+ * \param maxncpy Maximum number of characters to copy (generally
+ * the size of dst)
+ * \retval The number of bytes copied (The only difference from BLI_strncpy).
+ */
+size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
+{
+	size_t srclen = BLI_strnlen(src, maxncpy - 1);
+	BLI_assert(maxncpy != 0);
+
+	memcpy(dst, src, srclen);
+	dst[srclen] = '\0';
+	return srclen;
+}
+
+/**
+ * Portable replacement for #vsnprintf
+ */
 size_t BLI_vsnprintf(char *__restrict buffer, size_t count, const char *__restrict format, va_list arg)
 {
 	size_t n;
@@ -99,6 +158,9 @@
 	return n;
 }
 
+/**
+ * Portable replacement for #snprintf
+ */
 size_t BLI_snprintf(char *__restrict buffer, size_t count, const char *__restrict format, ...)
 {
 	size_t n;
@@ -111,6 +173,10 @@
 	return n;
 }
 
+/**
+ * Print formatted string into a newly #MEM_mallocN'd string
+ * and return it.
+ */
 char *BLI_sprintfN(const char *__restrict format, ...)
 {
 	DynStr *ds;
@@ -178,17 +244,17 @@
 	return len;
 }
 
-
-/* Makes a copy of the text within the "" that appear after some text 'blahblah'
+/**
+ * Makes a copy of the text within the "" that appear after some text 'blahblah'
  * i.e. for string 'pose["apples"]' with prefix 'pose[', it should grab "apples"
- * 
- *  - str: is the entire string to chop
- *	- prefix: is the part of the string to leave out 
  *
- * Assume that the strings returned must be freed afterwards, and that the inputs will contain 
+ * - str: is the entire string to chop
+ * - prefix: is the part of the string to leave out
+ *
+ * Assume that the strings returned must be freed afterwards, and that the inputs will contain
  * data we want...
  *
- * TODO, return the offset and a length so as to avoid doing an allocation.
+ * \return the offset and a length so as to avoid doing an allocation.
  */
 char *BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict prefix)
 {
@@ -208,12 +274,19 @@
 	return BLI_strdupn("", 0);
 }
 
-/* Replaces all occurrences of oldText with newText in str, returning a new string that doesn't 
- * contain the 'replaced' occurrences.
+/**
+ * Returns a copy of the cstring \a str into a newly mallocN'd
+ * string with all instances of oldText replaced with newText,
+ * and returns it.
+ *
+ * \note A rather wasteful string-replacement utility, though this shall do for now...
+ * Feel free to replace this with an even safe + nicer alternative
+ *
+ * \param str The string to replace occurrences of oldText in
+ * \param oldText The text in the string to find and replace
+ * \param newText The text in the string to find and replace
+ * \retval Returns the duplicated string
  */
-
-/* A rather wasteful string-replacement utility, though this shall do for now...
- * Feel free to replace this with an even safe + nicer alternative */
 char *BLI_replacestr(char *__restrict str, const char *__restrict oldText, const char *__restrict newText)
 {
 	DynStr *ds = NULL;
@@ -280,12 +353,19 @@
 	}
 } 
 
+/**
+ * Compare two strings without regard to case.
+ *
+ * \retval True if the strings are equal, false otherwise.
+ */
 int BLI_strcaseeq(const char *a, const char *b) 
 {
 	return (BLI_strcasecmp(a, b) == 0);
 }
 
-/* strcasestr not available in MSVC */
+/**
+ * Portable replacement for #strcasestr (not available in MSVC)
+ */
 char *BLI_strcasestr(const char *s, const char *find)
 {
 	register char c, sc;

Modified: trunk/blender/source/blender/blenlib/intern/winstuff.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/winstuff.c	2013-03-14 09:55:05 UTC (rev 55274)
+++ trunk/blender/source/blender/blenlib/intern/winstuff.c	2013-03-14 10:07:05 UTC (rev 55275)
@@ -140,7 +140,7 @@
 	lresult = RegCreateKeyEx(root, ".blend", 0,
 	                         NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
 	if (lresult == ERROR_SUCCESS) {
-		sprintf(buffer, "%s", "blendfile");
+		strcpy(buffer, "blendfile");

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list