diff src/ex_getln.c @ 5873:8e9db1f27a00 v7.4.279

updated for version 7.4.279 Problem: globpath() returns a string, making it difficult to get a list of matches. (Greg Novack) Solution: Add an optional argument like with glob(). (Adnan Zafar)
author Bram Moolenaar <bram@vim.org>
date Wed, 07 May 2014 18:35:30 +0200
parents 31217cc48e7d
children 0fc665889e8f
line wrap: on
line diff
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -5095,9 +5095,9 @@ ExpandRTDir(pat, num_file, file, dirname
     char_u	***file;
     char	*dirnames[];
 {
-    char_u	*matches;
     char_u	*s;
     char_u	*e;
+    char_u	*match;
     garray_T	ga;
     int		i;
     int		pat_len;
@@ -5116,33 +5116,27 @@ ExpandRTDir(pat, num_file, file, dirname
 	    return FAIL;
 	}
 	sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
-	matches = globpath(p_rtp, s, 0);
+	globpath(p_rtp, s, &ga, 0);
 	vim_free(s);
-	if (matches == NULL)
-	    continue;
-
-	for (s = matches; *s != NUL; s = e)
+    }
+
+    for (i = 0; i < ga.ga_len; ++i)
+    {
+	match = ((char_u **)ga.ga_data)[i];
+	s = match;
+	e = s + STRLEN(s);
+	if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
 	{
-	    e = vim_strchr(s, '\n');
-	    if (e == NULL)
-		e = s + STRLEN(s);
-	    if (ga_grow(&ga, 1) == FAIL)
-		break;
-	    if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
-	    {
-		for (s = e - 4; s > matches; mb_ptr_back(matches, s))
-		    if (*s == '\n' || vim_ispathsep(*s))
-			break;
-		++s;
-		((char_u **)ga.ga_data)[ga.ga_len] =
-					    vim_strnsave(s, (int)(e - s - 4));
-		++ga.ga_len;
-	    }
-	    if (*e != NUL)
-		++e;
+	    e -= 4;
+	    for (s = e; s > match; mb_ptr_back(match, s))
+		if (s < match || vim_ispathsep(*s))
+		    break;
+	    ++s;
+	    *e = NUL;
+	    mch_memmove(match, s, e - s + 1);
 	}
-	vim_free(matches);
     }
+
     if (ga.ga_len == 0)
 	return FAIL;
 
@@ -5160,33 +5154,28 @@ ExpandRTDir(pat, num_file, file, dirname
 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
 /*
  * Expand "file" for all comma-separated directories in "path".
- * Returns an allocated string with all matches concatenated, separated by
- * newlines.  Returns NULL for an error or no matches.
+ * Adds the matches to "ga".  Caller must init "ga".
  */
-    char_u *
-globpath(path, file, expand_options)
+    void
+globpath(path, file, ga, expand_options)
     char_u	*path;
     char_u	*file;
+    garray_T	*ga;
     int		expand_options;
 {
     expand_T	xpc;
     char_u	*buf;
-    garray_T	ga;
     int		i;
-    int		len;
     int		num_p;
     char_u	**p;
-    char_u	*cur = NULL;
 
     buf = alloc(MAXPATHL);
     if (buf == NULL)
-	return NULL;
+	return;
 
     ExpandInit(&xpc);
     xpc.xp_context = EXPAND_FILES;
 
-    ga_init2(&ga, 1, 100);
-
     /* Loop over all entries in {path}. */
     while (*path != NUL)
     {
@@ -5207,30 +5196,23 @@ globpath(path, file, expand_options)
 			     WILD_SILENT|expand_options) != FAIL && num_p > 0)
 	    {
 		ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
-		for (len = 0, i = 0; i < num_p; ++i)
-		    len += (int)STRLEN(p[i]) + 1;
-
-		/* Concatenate new results to previous ones. */
-		if (ga_grow(&ga, len) == OK)
+
+		if (ga_grow(ga, num_p) == OK)
 		{
-		    cur = (char_u *)ga.ga_data + ga.ga_len;
 		    for (i = 0; i < num_p; ++i)
 		    {
-			STRCPY(cur, p[i]);
-			cur += STRLEN(p[i]);
-			*cur++ = '\n';
+			((char_u **)ga->ga_data)[ga->ga_len] =
+					    vim_strnsave(p[i], STRLEN(p[i]));
+			++ga->ga_len;
 		    }
-		    ga.ga_len += len;
 		}
+
 		FreeWild(num_p, p);
 	    }
 	}
     }
-    if (cur != NULL)
-	*--cur = 0; /* Replace trailing newline with NUL */
 
     vim_free(buf);
-    return (char_u *)ga.ga_data;
 }
 
 #endif