]> git.neil.brown.name Git - wiggle.git/blob - split.c
man page: update instructions for git usage.
[wiggle.git] / split.c
1 /*
2  * wiggle - apply rejected patches
3  *
4  * Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5  *
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2 of the License, or
10  *    (at your option) any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program; if not, write to the Free Software Foundation, Inc.,
19  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  *    Author: Neil Brown
22  *    Email: <neilb@suse.de>
23  */
24
25 /*
26  * Split a stream into words or lines
27  *
28  * A word is one of:
29  *    string of [A-Za-z0-9_]
30  *    or string of [ \t]
31  *    or single char (i.e. punctuation and newlines).
32  *
33  * A line is any string that ends with \n
34  *
35  * As a special case to allow proper aligning of multiple chunks
36  * in a patch, a word starting \0 will include 20+ chars with a newline
37  * second from the end.
38  *
39  * We make two passes through the stream.
40  * Firstly we count the number of item so an array can be allocated,
41  * then we store start and length of each item in the array
42  *
43  */
44
45 #include        "wiggle.h"
46 #include        <stdlib.h>
47 #include        <ctype.h>
48 #include        <stdlib.h>
49
50 #include "ccan/hash/hash.h"
51
52 static int split_internal(char *start, char *end, int type,
53                           struct elmnt *list)
54 {
55         int cnt = 0;
56
57         while (start < end) {
58                 char *cp = start;
59                 char *cp2;
60                 int prefix = 0;
61
62                 if (type == (ByWord | IgnoreBlanks))
63                         while (cp < end &&
64                                (*cp == ' ' || *cp == '\t')) {
65                                 prefix++;
66                                 cp++;
67                         }
68                 start = cp;
69
70                 if (*cp == '\0' && cp+19 < end) {
71                         /* special word */
72                         cp += 19;
73                         cp += strlen(cp) + 1;
74                 } else
75                         switch (type & ByMask) {
76                         case ByLine:
77                                 while (cp < end && *cp != '\n')
78                                         cp++;
79                                 if (cp < end)
80                                         cp++;
81                                 break;
82                         case ByWord:
83                                 if (isalnum(*cp) || *cp == '_') {
84                                         do
85                                                 cp++;
86                                         while (cp < end
87                                                && (isalnum(*cp)
88                                                    || *cp == '_'));
89                                 } else if (*cp == ' ' || *cp == '\t') {
90                                         do
91                                                 cp++;
92                                         while (cp < end
93                                                && (*cp == ' '
94                                                    || *cp == '\t'));
95                                 } else
96                                         cp++;
97                                 break;
98                         }
99                 cp2 = cp;
100                 if (type == (ByWord | IgnoreBlanks) &&
101                     *start && *start != '\n')
102                         while (cp2 < end &&
103                                (*cp2 == ' ' || *cp2 == '\t' || *cp2 == '\n')) {
104                                 cp2++;
105                                 if (cp2[-1] == '\n')
106                                         break;
107                         }
108                 if (list) {
109                         list->start = start;
110                         list->len = cp-start;
111                         list->plen = cp2-start;
112                         list->prefix = prefix;
113                         if (*start)
114                                 list->hash = hash(start, list->len, 0);
115                         else
116                                 list->hash = atoi(start+1);
117                         list++;
118                 }
119                 cnt++;
120                 start = cp2;
121         }
122         return cnt;
123 }
124
125 struct file split_stream(struct stream s, int type)
126 {
127         int cnt;
128         struct file f;
129
130         char *c, *end;
131
132         end = s.body+s.len;
133         c = s.body;
134
135         cnt = split_internal(c, end, type, NULL);
136         f.list = xmalloc(cnt*sizeof(struct elmnt));
137
138         f.elcnt = split_internal(c, end, type, f.list);
139         return f;
140 }