Commit: e660793896a7b91243306935b30c563270bbb6a5
Author: Bastien Montagne
Date:   Thu Jan 1 15:23:16 2015 +0100
Branches: asset-experiments
https://developer.blender.org/rBe660793896a7b91243306935b30c563270bbb6a5

Add BLI helpers for fnmatch head/tail wildcards handling.

`BLI_fnmatch_strncpy_add_endswildcards()` will copy and add '*' wildcards on 
both ends of given string, if needed.

`BLI_fnmatch_strcmp_ignore_endswildcards()` is same as strcmp, but ignores any 
'*' wildcards on both ends of given strings.

===================================================================

M       source/blender/blenlib/BLI_fnmatch.h
M       source/blender/blenlib/intern/fnmatch.c

===================================================================

diff --git a/source/blender/blenlib/BLI_fnmatch.h 
b/source/blender/blenlib/BLI_fnmatch.h
index f69f5b3..d7d463e 100644
--- a/source/blender/blenlib/BLI_fnmatch.h
+++ b/source/blender/blenlib/BLI_fnmatch.h
@@ -78,4 +78,10 @@ extern int fnmatch __P((const char *__pattern, const char 
*__string,
 }
 #endif
 
+/* Helpers. */
+#include "BLI_sys_types.h"
+
+void BLI_fnmatch_strncpy_add_endswildcards(char *dst, const char *src, size_t 
len);
+int BLI_fnmatch_strcmp_ignore_endswildcards(const char *str1, const char 
*str2);
+
 #endif /* __BLI_FNMATCH_H__ */
diff --git a/source/blender/blenlib/intern/fnmatch.c 
b/source/blender/blenlib/intern/fnmatch.c
index 60e898a..90cbfe0 100644
--- a/source/blender/blenlib/intern/fnmatch.c
+++ b/source/blender/blenlib/intern/fnmatch.c
@@ -17,6 +17,8 @@
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
+#include "BLI_fnmatch.h"
+
 #ifdef WIN32
 
 /* Enable GNU extensions in fnmatch.h.  */
@@ -26,7 +28,6 @@
 
 #include <errno.h>
 #include <ctype.h>
-#include "BLI_fnmatch.h"
 
 
 /* Comment out all this code if we are using the GNU C Library, and are not
@@ -243,4 +244,88 @@ fnmatch (const char *pattern, const char *string, int 
flags)
 
 #endif /* WIN32 */
 
+/* Helpers. */
+
+#include <string.h>
+
+#include "BLI_utildefines.h"
+#include "BLI_string.h"
+
+/**
+ * Copy src into dst, adding heading/trailing fnmatch wildcards ('*') if 
needed.
+ *
+ * \param len the size of dst.
+ */
+void BLI_fnmatch_strncpy_add_endswildcards(char *dst, const char *src, size_t 
len)
+{
+       if (src[0] == '\0') {
+               dst[0] = '\0';
+       }
+       else {
+               /* Add heading/trailing wildcards if needed. */
+               int idx = 0;
+               size_t src_len;
+
+               if (src[idx] != '*') {
+                       dst[idx++] = '*';
+                       len--;
+               }
+               len--;  /* trailing '\0' */
+
+               src_len = BLI_strnlen(src, len);
+               if ((src[src_len - 1] != '*') && (src_len == len)) {
+                       src_len--;
+               }
+
+               memcpy(&dst[idx], src, src_len);
+               idx += src_len;
+
+               if (dst[idx - 1] != '*') {
+                       dst[idx++] = '*';
+               }
+               dst[idx] = '\0';
+       }
+}
 
+/**
+ * Like strcmp, but ignore heading/trailing fnmatch wildcards ('*').
+ */
+int BLI_fnmatch_strcmp_ignore_endswildcards(const char *str1, const char *str2)
+{
+       size_t str1_len, str2_len;
+
+       if (*str1 == '*') {
+               str1++;
+       }
+       if (*str2 == '*') {
+               str2++;
+       }
+
+       str1_len = strlen(str1);
+       str2_len = strlen(str2);
+
+       if (str1_len && (str1[str1_len - 1] == '*')) {
+               str1_len--;
+       }
+       if (str2_len && (str2[str2_len - 1] == '*')) {
+               str2_len--;
+       }
+
+       if (str1_len == str2_len) {
+               return strncmp(str1, str2, str2_len);
+       }
+       else if (str1_len > str2_len) {
+               int ret = strncmp(str1, str2, str2_len);
+               if (ret == 0) {
+                       ret = 1;
+               }
+               return ret;
+       }
+       else {
+               int ret = strncmp(str1, str2, str1_len);
+               if (ret == 0) {
+                       ret = -1;
+               }
+               return ret;
+       }
+}

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to