Hi Bram,

I am attaching a patch to refactor the qf_init_ext() function some more.
The code to extract the next line from a file/buffer/list/string is moved
to separate functions.

- Yegappan

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/src/quickfix.c b/src/quickfix.c
index 92a0204..1bd2c2c 100644
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -496,6 +496,292 @@ parse_efm_end:
     return fmt_first;
 }
 
+enum {
+    QF_FAIL = 0,
+    QF_OK = 1,
+    QF_END_OF_INPUT = 2,
+    QF_NOMEM = 3
+};
+
+/* Get the next string (separated by newline) from p_str. */
+    static int
+qf_get_next_str_line(
+       char_u  **pp_str,
+       char_u  **pp_linebuf,
+       int     *p_linelen,
+       char_u  **pp_growbuf,
+       int     *p_growbufsiz)
+{
+    /* Get the next line from the supplied string */
+    char_u     *p_str = *pp_str;
+    char_u     *p;
+    int                len;
+
+    if (*p_str == NUL) /* Reached the end of the string */
+       return QF_END_OF_INPUT;
+
+    p = vim_strchr(p_str, '\n');
+    if (p != NULL)
+       len = (int)(p - p_str) + 1;
+    else
+       len = (int)STRLEN(p_str);
+
+    if (len > IOSIZE - 2)
+    {
+       *pp_linebuf = qf_grow_linebuf(pp_growbuf, p_growbufsiz, len,
+               p_linelen);
+       if (*pp_linebuf == NULL)
+           return QF_NOMEM;
+    }
+    else
+    {
+       *pp_linebuf = IObuff;
+       *p_linelen = len;
+    }
+    vim_strncpy(*pp_linebuf, p_str, *p_linelen);
+
+    /*
+     * Increment using len in order to discard the rest of the
+     * line if it exceeds LINE_MAXLEN.
+     */
+    p_str += len;
+
+    *pp_str = p_str;
+    return QF_OK;
+}
+
+/* Get the next string from a list */
+    static int
+qf_get_next_list_line(
+       listitem_T      **pp_li,
+       char_u          **pp_linebuf,
+       int             *p_linelen,
+       char_u          **pp_growbuf,
+       int             *p_growbufsiz)
+{
+    listitem_T *p_li = *pp_li;
+    int                len;
+
+    while (p_li != NULL
+           && (p_li->li_tv.v_type != VAR_STRING
+               || p_li->li_tv.vval.v_string == NULL))
+       p_li = p_li->li_next;   /* Skip non-string items */
+
+    if (p_li == NULL)          /* End of the list */
+    {
+       *pp_li = NULL;
+       return QF_END_OF_INPUT;
+    }
+
+    len = (int)STRLEN(p_li->li_tv.vval.v_string);
+    if (len > IOSIZE - 2)
+    {
+       *pp_linebuf = qf_grow_linebuf(pp_growbuf, p_growbufsiz, len,
+               p_linelen);
+       if (*pp_linebuf == NULL)
+           return QF_NOMEM;
+    }
+    else
+    {
+       *pp_linebuf = IObuff;
+       *p_linelen = len;
+    }
+
+    vim_strncpy(*pp_linebuf, p_li->li_tv.vval.v_string, *p_linelen);
+
+    *pp_li = p_li->li_next;    /* next item */
+    return QF_OK;
+}
+
+/* Get the next string from a buffer */
+    static int
+qf_get_next_buf_line(
+       buf_T           *buf,
+       linenr_T        *p_buflnum,
+       int             lnumlast,
+       char_u          **pp_linebuf,
+       int             *p_linelen,
+       char_u          **pp_growbuf,
+       int             *p_growbufsiz)
+{
+    char_u     *p_buf = NULL;
+    int                len;
+
+    /* Get the next line from the supplied buffer */
+    if (*p_buflnum > lnumlast)
+       return QF_END_OF_INPUT;
+
+    p_buf = ml_get_buf(buf, *p_buflnum, FALSE);
+    *p_buflnum += 1;
+
+    len = (int)STRLEN(p_buf);
+    if (len > IOSIZE - 2)
+    {
+       *pp_linebuf = qf_grow_linebuf(pp_growbuf, p_growbufsiz, len,
+               p_linelen);
+       if (*pp_linebuf == NULL)
+           return QF_NOMEM;
+    }
+    else
+    {
+       *pp_linebuf = IObuff;
+       *p_linelen = len;
+    }
+    vim_strncpy(*pp_linebuf, p_buf, *p_linelen);
+
+    return QF_OK;
+}
+
+/* Get the next string from a file */
+    static int
+qf_get_next_file_line(
+       FILE            *fd,
+       char_u          **pp_linebuf,
+       int             *p_linelen,
+       char_u          **pp_growbuf,
+       int             *p_growbufsiz)
+{
+    int            discard;
+    int            growbuflen;
+
+    if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
+       return QF_END_OF_INPUT;
+
+    discard = FALSE;
+    *p_linelen = (int)STRLEN(IObuff);
+    if (*p_linelen == IOSIZE - 1 && !(IObuff[*p_linelen - 1] == '\n'
+#ifdef USE_CRNL
+               || IObuff[*p_linelen - 1] == '\r'
+#endif
+               ))
+    {
+       /*
+        * The current line exceeds IObuff, continue reading using
+        * growbuf until EOL or LINE_MAXLEN bytes is read.
+        */
+       if (*pp_growbuf == NULL)
+       {
+           *p_growbufsiz = 2 * (IOSIZE - 1);
+           *pp_growbuf = alloc(*p_growbufsiz);
+           if (*pp_growbuf == NULL)
+               return QF_NOMEM;
+       }
+
+       /* Copy the read part of the line, excluding null-terminator */
+       memcpy(*pp_growbuf, IObuff, IOSIZE - 1);
+       growbuflen = *p_linelen;
+
+       for (;;)
+       {
+           if (fgets((char *)*pp_growbuf + growbuflen,
+                       *p_growbufsiz - growbuflen, fd) == NULL)
+               break;
+           *p_linelen = (int)STRLEN(*pp_growbuf + growbuflen);
+           growbuflen += *p_linelen;
+           if ((*pp_growbuf)[growbuflen - 1] == '\n'
+#ifdef USE_CRNL
+                   || (*pp_growbuf)[growbuflen - 1] == '\r'
+#endif
+              )
+               break;
+           if (*p_growbufsiz == LINE_MAXLEN)
+           {
+               discard = TRUE;
+               break;
+           }
+
+           *p_growbufsiz = 2 * *p_growbufsiz < LINE_MAXLEN
+               ? 2 * *p_growbufsiz : LINE_MAXLEN;
+           *pp_growbuf = vim_realloc(*pp_growbuf, *p_growbufsiz);
+           if (*pp_growbuf == NULL)
+               return QF_NOMEM;
+       }
+
+       while (discard)
+       {
+           /*
+            * The current line is longer than LINE_MAXLEN, continue
+            * reading but discard everything until EOL or EOF is
+            * reached.
+            */
+           if (fgets((char *)IObuff, IOSIZE, fd) == NULL
+                   || (int)STRLEN(IObuff) < IOSIZE - 1
+                   || IObuff[IOSIZE - 1] == '\n'
+#ifdef USE_CRNL
+                   || IObuff[IOSIZE - 1] == '\r'
+#endif
+              )
+               break;
+       }
+
+       *pp_linebuf = *pp_growbuf;
+       *p_linelen = growbuflen;
+    }
+    else
+       *pp_linebuf = IObuff;
+
+    return QF_OK;
+}
+
+/* Get the next string from a file/buffer/list/string */
+    static int
+qf_get_nextline(
+       FILE            *fd,
+       typval_T        *tv,
+       char_u          **pp_str,
+       listitem_T      **pp_li,
+       buf_T           *buf,
+       linenr_T        *p_buflnum,
+       linenr_T        lnumlast,
+       char_u          **pp_linebuf,
+       int             *p_linelen,
+       char_u          **pp_growbuf,
+       int             *p_growbufsiz
+       )
+{
+    int status = QF_FAIL;
+
+    if (fd == NULL)
+    {
+       if (tv != NULL)
+       {
+           if (tv->v_type == VAR_STRING)
+               /* Get the next line from the supplied string */
+               status = qf_get_next_str_line(pp_str, pp_linebuf, p_linelen,
+                       pp_growbuf, p_growbufsiz);
+           else if (tv->v_type == VAR_LIST)
+               /* Get the next line from the supplied list */
+               status = qf_get_next_list_line(pp_li, pp_linebuf, p_linelen,
+                       pp_growbuf, p_growbufsiz);
+       }
+       else
+           /* Get the next line from the supplied buffer */
+           status = qf_get_next_buf_line(buf, p_buflnum, lnumlast,
+                   pp_linebuf, p_linelen, pp_growbuf, p_growbufsiz);
+    }
+    else
+       /* Get the next line from the supplied file */
+       status = qf_get_next_file_line(fd, pp_linebuf, p_linelen, pp_growbuf,
+               p_growbufsiz);
+
+    if (status != QF_OK)
+       return status;
+
+    /* remove newline/CR from the line */
+    if (*p_linelen > 0 && (*pp_linebuf)[*p_linelen - 1] == '\n')
+       (*pp_linebuf)[*p_linelen - 1] = NUL;
+#ifdef USE_CRNL
+    if (*p_linelen > 0 && (*pp_linebuf)[*p_linelen - 1] == '\r')
+       (*pp_linebuf)[*p_linelen - 1] = NUL;
+#endif
+
+#ifdef FEAT_MBYTE
+    remove_bom(*pp_linebuf);
+#endif
+
+    return QF_OK;
+}
+
 /*
  * Read the errorfile "efile" into memory, line by line, building the error
  * list.
@@ -523,11 +809,9 @@ qf_init_ext(
     int                    errmsglen;
     char_u         *pattern;
     char_u         *growbuf = NULL;
-    int                    growbuflen;
     int                    growbufsiz = 0;
     char_u         *linebuf = NULL;
     int                    linelen = 0;
-    int                    discard;
     int                    col = 0;
     char_u         use_viscol = FALSE;
     int                    type = 0;
@@ -549,8 +833,8 @@ qf_init_ext(
     int                    i;
     int                    idx = 0;
     int                    retval = -1;        /* default: return error flag */
+    int                    status;
     char_u         *tail = NULL;
-    char_u         *p_buf = NULL;
     char_u         *p_str = NULL;
     listitem_T     *p_li = NULL;
     regmatch_T     regmatch;
@@ -647,186 +931,13 @@ qf_init_ext(
      */
     while (!got_int)
     {
-       /* Get the next line. */
-       if (fd == NULL)
-       {
-           if (tv != NULL)
-           {
-               if (tv->v_type == VAR_STRING)
-               {
-                   /* Get the next line from the supplied string */
-                   char_u *p;
-
-                   if (*p_str == NUL) /* Reached the end of the string */
-                       break;
-
-                   p = vim_strchr(p_str, '\n');
-                   if (p != NULL)
-                       len = (int)(p - p_str) + 1;
-                   else
-                       len = (int)STRLEN(p_str);
-
-                   if (len > IOSIZE - 2)
-                   {
-                       linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
-                                                                   &linelen);
-                       if (linebuf == NULL)
-                           goto qf_init_end;
-                   }
-                   else
-                   {
-                       linebuf = IObuff;
-                       linelen = len;
-                   }
-                   vim_strncpy(linebuf, p_str, linelen);
-
-                   /*
-                    * Increment using len in order to discard the rest of the
-                    * line if it exceeds LINE_MAXLEN.
-                    */
-                   p_str += len;
-               }
-               else if (tv->v_type == VAR_LIST)
-               {
-                   /* Get the next line from the supplied list */
-                   while (p_li != NULL
-                           && (p_li->li_tv.v_type != VAR_STRING
-                                       || p_li->li_tv.vval.v_string == NULL))
-                       p_li = p_li->li_next;   /* Skip non-string items */
-
-                   if (p_li == NULL)           /* End of the list */
-                       break;
-
-                   len = (int)STRLEN(p_li->li_tv.vval.v_string);
-                   if (len > IOSIZE - 2)
-                   {
-                       linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
-                                                                   &linelen);
-                       if (linebuf == NULL)
-                           goto qf_init_end;
-                   }
-                   else
-                   {
-                       linebuf = IObuff;
-                       linelen = len;
-                   }
-
-                   vim_strncpy(linebuf, p_li->li_tv.vval.v_string, linelen);
-
-                   p_li = p_li->li_next;       /* next item */
-               }
-           }
-           else
-           {
-               /* Get the next line from the supplied buffer */
-               if (buflnum > lnumlast)
-                   break;
-               p_buf = ml_get_buf(buf, buflnum++, FALSE);
-               len = (int)STRLEN(p_buf);
-               if (len > IOSIZE - 2)
-               {
-                   linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len,
-                                                                   &linelen);
-                   if (linebuf == NULL)
-                       goto qf_init_end;
-               }
-               else
-               {
-                   linebuf = IObuff;
-                   linelen = len;
-               }
-               vim_strncpy(linebuf, p_buf, linelen);
-           }
-       }
-       else
-       {
-           if (fgets((char *)IObuff, IOSIZE, fd) == NULL)
-               break;
-
-           discard = FALSE;
-           linelen = (int)STRLEN(IObuff);
-           if (linelen == IOSIZE - 1 && !(IObuff[linelen - 1] == '\n'
-#ifdef USE_CRNL
-                       || IObuff[linelen - 1] == '\r'
-#endif
-                       ))
-           {
-               /*
-                * The current line exceeds IObuff, continue reading using
-                * growbuf until EOL or LINE_MAXLEN bytes is read.
-                */
-               if (growbuf == NULL)
-               {
-                   growbufsiz = 2 * (IOSIZE - 1);
-                   growbuf = alloc(growbufsiz);
-                   if (growbuf == NULL)
-                       goto qf_init_end;
-               }
-
-               /* Copy the read part of the line, excluding null-terminator */
-               memcpy(growbuf, IObuff, IOSIZE - 1);
-               growbuflen = linelen;
-
-               for (;;)
-               {
-                   if (fgets((char *)growbuf + growbuflen,
-                                        growbufsiz - growbuflen, fd) == NULL)
-                       break;
-                   linelen = (int)STRLEN(growbuf + growbuflen);
-                   growbuflen += linelen;
-                   if (growbuf[growbuflen - 1] == '\n'
-#ifdef USE_CRNL
-                           || growbuf[growbuflen - 1] == '\r'
-#endif
-                               )
-                       break;
-                   if (growbufsiz == LINE_MAXLEN)
-                   {
-                       discard = TRUE;
-                       break;
-                   }
-
-                   growbufsiz = 2 * growbufsiz < LINE_MAXLEN
-                                              ? 2 * growbufsiz : LINE_MAXLEN;
-                   growbuf = vim_realloc(growbuf, 2 * growbufsiz);
-                   if (growbuf == NULL)
-                       goto qf_init_end;
-               }
-
-               while (discard)
-               {
-                   /*
-                    * The current line is longer than LINE_MAXLEN, continue
-                    * reading but discard everything until EOL or EOF is
-                    * reached.
-                    */
-                   if (fgets((char *)IObuff, IOSIZE, fd) == NULL
-                           || (int)STRLEN(IObuff) < IOSIZE - 1
-                           || IObuff[IOSIZE - 1] == '\n'
-#ifdef USE_CRNL
-                           || IObuff[IOSIZE - 1] == '\r'
-#endif
-                      )
-                       break;
-               }
-
-               linebuf = growbuf;
-               linelen = growbuflen;
-           }
-           else
-               linebuf = IObuff;
-       }
-
-       if (linelen > 0 && linebuf[linelen - 1] == '\n')
-           linebuf[linelen - 1] = NUL;
-#ifdef USE_CRNL
-       if (linelen > 0 && linebuf[linelen - 1] == '\r')
-           linebuf[linelen - 1] = NUL;
-#endif
-
-#ifdef FEAT_MBYTE
-       remove_bom(linebuf);
-#endif
+       /* Get the next line from a file/buffer/list/string */
+       status = qf_get_nextline(fd, tv, &p_str, &p_li, buf, &buflnum,
+               lnumlast, &linebuf, &linelen, &growbuf, &growbufsiz);
+       if (status == QF_NOMEM)         /* memory alloc failure */
+           goto qf_init_end;
+       if (status == QF_END_OF_INPUT)  /* end of input */
+           break;
 
        /* If there was no %> item start at the first pattern */
        if (fmt_start == NULL)

Raspunde prin e-mail lui