Mercurial > vim
comparison src/quickfix.c @ 9114:f221aec7fcca v7.4.1841
commit https://github.com/vim/vim/commit/2b2b8ae5ab37b04584633c469265d85825166905
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue May 24 19:59:51 2016 +0200
patch 7.4.1841
Problem: The code to reallocate the buffer used for quickfix is repeated.
Solution: Move the code to a function. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/831)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 24 May 2016 20:00:08 +0200 |
parents | a2441f2ff85d |
children | d415c079f84e |
comparison
equal
deleted
inserted
replaced
9113:3191fccf9b72 | 9114:f221aec7fcca |
---|---|
176 | 176 |
177 /* | 177 /* |
178 * Maximum number of bytes allowed per line while reading a errorfile. | 178 * Maximum number of bytes allowed per line while reading a errorfile. |
179 */ | 179 */ |
180 #define LINE_MAXLEN 4096 | 180 #define LINE_MAXLEN 4096 |
181 | |
182 static char_u * | |
183 qf_grow_linebuf(char_u **growbuf, int *growbufsiz, int newsz, int *allocsz) | |
184 { | |
185 /* | |
186 * If the line exceeds LINE_MAXLEN exclude the last | |
187 * byte since it's not a NL character. | |
188 */ | |
189 *allocsz = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz; | |
190 if (*growbuf == NULL) | |
191 { | |
192 *growbuf = alloc(*allocsz + 1); | |
193 if (*growbuf == NULL) | |
194 return NULL; | |
195 *growbufsiz = *allocsz; | |
196 } | |
197 else if (*allocsz > *growbufsiz) | |
198 { | |
199 *growbuf = vim_realloc(*growbuf, *allocsz + 1); | |
200 if (*growbuf == NULL) | |
201 return NULL; | |
202 *growbufsiz = *allocsz; | |
203 } | |
204 return *growbuf; | |
205 } | |
181 | 206 |
182 /* | 207 /* |
183 * Read the errorfile "efile" into memory, line by line, building the error | 208 * Read the errorfile "efile" into memory, line by line, building the error |
184 * list. | 209 * list. |
185 * Alternative: when "efile" is null read errors from buffer "buf". | 210 * Alternative: when "efile" is null read errors from buffer "buf". |
536 else | 561 else |
537 len = (int)STRLEN(p_str); | 562 len = (int)STRLEN(p_str); |
538 | 563 |
539 if (len > IOSIZE - 2) | 564 if (len > IOSIZE - 2) |
540 { | 565 { |
541 /* | 566 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len, |
542 * If the line exceeds LINE_MAXLEN exclude the last | 567 &linelen); |
543 * byte since it's not a NL character. | 568 if (linebuf == NULL) |
544 */ | 569 goto qf_init_end; |
545 linelen = len > LINE_MAXLEN ? LINE_MAXLEN - 1 : len; | |
546 if (growbuf == NULL) | |
547 { | |
548 growbuf = alloc(linelen + 1); | |
549 growbufsiz = linelen; | |
550 } | |
551 else if (linelen > growbufsiz) | |
552 { | |
553 growbuf = vim_realloc(growbuf, linelen + 1); | |
554 if (growbuf == NULL) | |
555 goto qf_init_end; | |
556 growbufsiz = linelen; | |
557 } | |
558 linebuf = growbuf; | |
559 } | 570 } |
560 else | 571 else |
561 { | 572 { |
562 linebuf = IObuff; | 573 linebuf = IObuff; |
563 linelen = len; | 574 linelen = len; |
582 break; | 593 break; |
583 | 594 |
584 len = (int)STRLEN(p_li->li_tv.vval.v_string); | 595 len = (int)STRLEN(p_li->li_tv.vval.v_string); |
585 if (len > IOSIZE - 2) | 596 if (len > IOSIZE - 2) |
586 { | 597 { |
587 linelen = len; | 598 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len, |
588 if (linelen > LINE_MAXLEN) | 599 &linelen); |
589 linelen = LINE_MAXLEN - 1; | 600 if (linebuf == NULL) |
590 if (growbuf == NULL) | 601 goto qf_init_end; |
591 { | |
592 growbuf = alloc(linelen + 1); | |
593 growbufsiz = linelen; | |
594 } | |
595 else if (linelen > growbufsiz) | |
596 { | |
597 if ((growbuf = vim_realloc(growbuf, | |
598 linelen + 1)) == NULL) | |
599 goto qf_init_end; | |
600 growbufsiz = linelen; | |
601 } | |
602 linebuf = growbuf; | |
603 } | 602 } |
604 else | 603 else |
605 { | 604 { |
606 linebuf = IObuff; | 605 linebuf = IObuff; |
607 linelen = len; | 606 linelen = len; |
619 break; | 618 break; |
620 p_buf = ml_get_buf(buf, buflnum++, FALSE); | 619 p_buf = ml_get_buf(buf, buflnum++, FALSE); |
621 linelen = (int)STRLEN(p_buf); | 620 linelen = (int)STRLEN(p_buf); |
622 if (linelen > IOSIZE - 2) | 621 if (linelen > IOSIZE - 2) |
623 { | 622 { |
624 if (growbuf == NULL) | 623 linebuf = qf_grow_linebuf(&growbuf, &growbufsiz, len, |
625 { | 624 &linelen); |
626 growbuf = alloc(linelen + 1); | 625 if (linebuf == NULL) |
627 growbufsiz = linelen; | 626 goto qf_init_end; |
628 } | |
629 else if (linelen > growbufsiz) | |
630 { | |
631 if (linelen > LINE_MAXLEN) | |
632 linelen = LINE_MAXLEN - 1; | |
633 if ((growbuf = vim_realloc(growbuf, linelen + 1)) == NULL) | |
634 goto qf_init_end; | |
635 growbufsiz = linelen; | |
636 } | |
637 linebuf = growbuf; | |
638 } | 627 } |
639 else | 628 else |
640 linebuf = IObuff; | 629 linebuf = IObuff; |
641 vim_strncpy(linebuf, p_buf, linelen); | 630 vim_strncpy(linebuf, p_buf, linelen); |
642 } | 631 } |