Wiggle 0.6 - first release
[wiggle/upstream.git] / split.c
blobb5987bd27df1b816a58e087ceb6622346434ab76
1 /*
2 * wiggle - apply rejected patches
4 * Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
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.
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.
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
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
31 * split a stream into words or line
32 * When splitting into words we can either be approximate or precise.
33 * Precise mode includes every char in a word.
34 * Approximate mode excluses white-space words and might unite some special chars
36 * In general, a word is one of:
37 * string of [A-Za-z0-9_]
38 * or string of [ \t]
39 * or single char.
41 * A line is any string that ends with \n
43 * As a special case to allow proper aligning of multiple chunks
44 * in a patch, a word starting \0 will include 5 chars and a newline
47 * We make two passes through the stream.
48 * Firstly we count the number of item so an array can be allocated,
49 * then we store start and length of each item in the array
53 #include "wiggle.h"
54 #include <stdlib.h>
55 #include <ctype.h>
56 #include <malloc.h>
57 #define BITS_PER_LONG 32
59 #include "hash.h"
61 static int split_internal(char *start, char *end, int type, struct elmnt *list, int reverse)
63 int cnt = 0;
65 while (start < end) {
66 char *cp = start;
68 if (*cp == '\0' && cp+16 < end && cp[18] == '\n') {
69 /* special word */
70 cp += 19;
71 } else switch(type) {
72 case ByLine:
73 while (cp < end && *cp != '\n')
74 cp++;
75 if (cp<end) cp++;
76 break;
77 case ByWord:
78 case ApproxWord:
79 if (isalnum(*cp) || *cp == '_') {
80 do cp++;
81 while (cp<end && (isalnum(*cp) || *cp == '_'));
82 } else if (*cp == ' ' || *cp == '\t') {
83 do cp++;
84 while (cp<end && (*cp == ' ' || *cp == '\t'));
85 } else
86 cp++;
87 break;
89 if (type != ApproxWord || *start =='\0' ||
90 (isalnum(*start) || *start == '_')) {
91 if (list) {
92 if (reverse) list--;
93 list->start = start;
94 list->len = cp-start;
95 if (*start)
96 list->hash = hash_mem(start, list->len, BITS_PER_LONG);
97 else
98 list->hash = atoi(start+1);
99 if (!reverse) list++;
101 cnt++;
103 start = cp;
105 return cnt;
108 struct file split_stream(struct stream s, int type, int reverse)
110 int cnt;
111 struct file f;
113 char *c, *end;
115 end = s.body+s.len;
116 c = s.body;
118 cnt = split_internal(c, end, type, NULL, reverse);
119 /* fprintf(stderr, "cnt %d\n", cnt);*/
120 f.list = malloc(cnt*sizeof(struct elmnt));
122 f.elcnt = split_internal(c, end, type, f.list + reverse*cnt, reverse);
123 return f;