diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-03-14 14:07:05 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-03-14 14:07:05 +0400 |
commit | 0159647ada64591e4edbcb1284cf499fc2ae9c09 (patch) | |
tree | 4261f47e922ac29fa4e0123a31d0d515f53b4b9b | |
parent | ccd05c33ede3ead7b249afbb0e93586f083b5921 (diff) |
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.
-rw-r--r-- | source/blender/blenlib/BLI_string.h | 80 | ||||
-rw-r--r-- | source/blender/blenlib/intern/string.c | 106 | ||||
-rw-r--r-- | source/blender/blenlib/intern/winstuff.c | 2 | ||||
-rw-r--r-- | source/blender/editors/include/ED_armature.h | 2 |
4 files changed, 105 insertions, 85 deletions
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index 603831246ac..3bc9d733254 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -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 @@ __attribute__((nonnull)) #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 @@ __attribute__((nonnull)) #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 @@ __attribute__((nonnull)) #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 @@ __attribute__((nonnull)) #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 @@ __attribute__((nonnull)) #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 @@ void BLI_timestr(double _time, char *str) #ifdef __GNUC__ __attribute__((nonnull)) #endif -; /* time var is global */ +; void BLI_ascii_strtolower(char *str, const size_t len) #ifdef __GNUC__ diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index c03daa97e3c..815295b8eac 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -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 @@ char *BLI_strdupn(const char *str, const size_t len) 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 @@ char *BLI_strdupcat(const char *__restrict str1, const char *__restrict str2) 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 @@ char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t 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 @@ size_t BLI_vsnprintf(char *__restrict buffer, size_t count, const char *__restri 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 @@ size_t BLI_snprintf(char *__restrict buffer, size_t count, const char *__restric 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 @@ escape_finish: 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 @@ char *BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict 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 @@ char *BLI_replacestr(char *__restrict str, const char *__restrict oldText, const } } +/** + * 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; diff --git a/source/blender/blenlib/intern/winstuff.c b/source/blender/blenlib/intern/winstuff.c index 767c616e3f2..d11bee255c2 100644 --- a/source/blender/blenlib/intern/winstuff.c +++ b/source/blender/blenlib/intern/winstuff.c @@ -140,7 +140,7 @@ void RegisterBlendExtension(void) 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"); lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE *)buffer, strlen(buffer) + 1); RegCloseKey(hkey); } diff --git a/source/blender/editors/include/ED_armature.h b/source/blender/editors/include/ED_armature.h index 98f1a93f72b..310b60f13e8 100644 --- a/source/blender/editors/include/ED_armature.h +++ b/source/blender/editors/include/ED_armature.h @@ -61,7 +61,7 @@ typedef struct EditBone { * normal bones when leaving editmode. */ void *temp; /* Used to store temporary data */ - char name[64]; /* MAX_NAME */ + char name[64]; /* MAXBONENAME */ float roll; /* Roll along axis. We'll ultimately use the axis/angle method * for determining the transformation matrix of the bone. The axis * is tail-head while roll provides the angle. Refer to Graphics |