append.c revision 1.1.1.1 1 1.1 bjh21 /*-
2 1.1 bjh21 * Copyright (c) 1993
3 1.1 bjh21 * The Regents of the University of California. All rights reserved.
4 1.1 bjh21 *
5 1.1 bjh21 * This code is derived from software contributed to Berkeley by
6 1.1 bjh21 * Peter McIlroy.
7 1.1 bjh21 *
8 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
9 1.1 bjh21 * modification, are permitted provided that the following conditions
10 1.1 bjh21 * are met:
11 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
12 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
13 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
15 1.1 bjh21 * documentation and/or other materials provided with the distribution.
16 1.1 bjh21 * 3. All advertising materials mentioning features or use of this software
17 1.1 bjh21 * must display the following acknowledgement:
18 1.1 bjh21 * This product includes software developed by the University of
19 1.1 bjh21 * California, Berkeley and its contributors.
20 1.1 bjh21 * 4. Neither the name of the University nor the names of its contributors
21 1.1 bjh21 * may be used to endorse or promote products derived from this software
22 1.1 bjh21 * without specific prior written permission.
23 1.1 bjh21 *
24 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 bjh21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 bjh21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 bjh21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 bjh21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 bjh21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 bjh21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 bjh21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 bjh21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 bjh21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 bjh21 * SUCH DAMAGE.
35 1.1 bjh21 */
36 1.1 bjh21
37 1.1 bjh21 #ifndef lint
38 1.1 bjh21 static char sccsid[] = "@(#)append.c 8.1 (Berkeley) 6/6/93";
39 1.1 bjh21 #endif /* not lint */
40 1.1 bjh21
41 1.1 bjh21 #include "sort.h"
42 1.1 bjh21
43 1.1 bjh21 #include <stdlib.h>
44 1.1 bjh21 #include <string.h>
45 1.1 bjh21
46 1.1 bjh21 #define OUTPUT { \
47 1.1 bjh21 if ((n = cpos - ppos) > 1) { \
48 1.1 bjh21 for (; ppos < cpos; ++ppos) \
49 1.1 bjh21 *ppos -= odepth; \
50 1.1 bjh21 ppos -= n; \
51 1.1 bjh21 radixsort(ppos, n, wts1, REC_D); \
52 1.1 bjh21 for (; ppos < cpos; ppos++) { \
53 1.1 bjh21 prec = (RECHEADER *) (*ppos - sizeof(TRECHEADER));\
54 1.1 bjh21 put(prec, fd); \
55 1.1 bjh21 } \
56 1.1 bjh21 } else put(prec, fd); \
57 1.1 bjh21 }
58 1.1 bjh21
59 1.1 bjh21 /*
60 1.1 bjh21 * copy sorted lines to output; check for uniqueness
61 1.1 bjh21 */
62 1.1 bjh21 void
63 1.1 bjh21 append(keylist, nelem, depth, fd, put, ftbl)
64 1.1 bjh21 u_char **keylist;
65 1.1 bjh21 int nelem;
66 1.1 bjh21 register int depth;
67 1.1 bjh21 FILE *fd;
68 1.1 bjh21 void (*put)(RECHEADER *, FILE *);
69 1.1 bjh21 struct field *ftbl;
70 1.1 bjh21 {
71 1.1 bjh21 register u_char *wts, *wts1;
72 1.1 bjh21 register n, odepth;
73 1.1 bjh21 register u_char **cpos, **ppos, **lastkey;
74 1.1 bjh21 register u_char *cend, *pend, *start;
75 1.1 bjh21 register struct recheader *crec, *prec;
76 1.1 bjh21
77 1.1 bjh21 if (*keylist == '\0' && UNIQUE)
78 1.1 bjh21 return;
79 1.1 bjh21 wts1 = wts = ftbl[0].weights;
80 1.1 bjh21 if ((!UNIQUE) && SINGL_FLD) {
81 1.1 bjh21 if (ftbl[0].flags & F && ftbl[0].flags & R)
82 1.1 bjh21 wts1 = Rascii;
83 1.1 bjh21 else if (ftbl[0].flags & F)
84 1.1 bjh21 wts1 = ascii;
85 1.1 bjh21 odepth = depth;
86 1.1 bjh21 }
87 1.1 bjh21 lastkey = keylist + nelem;
88 1.1 bjh21 depth += sizeof(TRECHEADER);
89 1.1 bjh21 if (SINGL_FLD && (UNIQUE || wts1 != wts)) {
90 1.1 bjh21 ppos = keylist;
91 1.1 bjh21 prec = (RECHEADER *) (*ppos - depth);
92 1.1 bjh21 if (UNIQUE)
93 1.1 bjh21 put(prec, fd);
94 1.1 bjh21 for (cpos = keylist+1; cpos < lastkey; cpos++) {
95 1.1 bjh21 crec = (RECHEADER *) (*cpos - depth);
96 1.1 bjh21 if (crec->length == prec->length) {
97 1.1 bjh21 pend = (u_char *) &prec->offset + prec->length;
98 1.1 bjh21 cend = (u_char *) &crec->offset + crec->length;
99 1.1 bjh21 for (start = *cpos; cend >= start; cend--) {
100 1.1 bjh21 if (wts[*cend] != wts[*pend])
101 1.1 bjh21 break;
102 1.1 bjh21 pend--;
103 1.1 bjh21 }
104 1.1 bjh21 if (pend + 1 != *ppos) {
105 1.1 bjh21 if (!UNIQUE) {
106 1.1 bjh21 OUTPUT;
107 1.1 bjh21 } else
108 1.1 bjh21 put(crec, fd);
109 1.1 bjh21 ppos = cpos;
110 1.1 bjh21 prec = crec;
111 1.1 bjh21 }
112 1.1 bjh21 } else {
113 1.1 bjh21 if (!UNIQUE) {
114 1.1 bjh21 OUTPUT;
115 1.1 bjh21 } else
116 1.1 bjh21 put(crec, fd);
117 1.1 bjh21 ppos = cpos;
118 1.1 bjh21 prec = crec;
119 1.1 bjh21 }
120 1.1 bjh21 }
121 1.1 bjh21 if (!UNIQUE) { OUTPUT; }
122 1.1 bjh21 } else if (UNIQUE) {
123 1.1 bjh21 ppos = keylist;
124 1.1 bjh21 prec = (RECHEADER *) (*ppos - depth);
125 1.1 bjh21 put(prec, fd);
126 1.1 bjh21 for (cpos = keylist+1; cpos < lastkey; cpos++) {
127 1.1 bjh21 crec = (RECHEADER *) (*cpos - depth);
128 1.1 bjh21 if (crec->offset == prec->offset) {
129 1.1 bjh21 pend = (u_char *) &prec->offset + prec->offset;
130 1.1 bjh21 cend = (u_char *) &crec->offset + crec->offset;
131 1.1 bjh21 for (start = *cpos; cend >= start; cend--) {
132 1.1 bjh21 if (wts[*cend] != wts[*pend])
133 1.1 bjh21 break;
134 1.1 bjh21 pend--;
135 1.1 bjh21 }
136 1.1 bjh21 if (pend + 1 != *ppos) {
137 1.1 bjh21 ppos = cpos;
138 1.1 bjh21 prec = crec;
139 1.1 bjh21 put(prec, fd);
140 1.1 bjh21 }
141 1.1 bjh21 } else {
142 1.1 bjh21 ppos = cpos;
143 1.1 bjh21 prec = crec;
144 1.1 bjh21 put(prec, fd);
145 1.1 bjh21 }
146 1.1 bjh21 }
147 1.1 bjh21 } else for (cpos = keylist; cpos < lastkey; cpos++) {
148 1.1 bjh21 crec = (RECHEADER *) (*cpos - depth);
149 1.1 bjh21 put(crec, fd);
150 1.1 bjh21 }
151 1.1 bjh21 }
152 1.1 bjh21
153 1.1 bjh21 /*
154 1.1 bjh21 * output the already sorted eol bin.
155 1.1 bjh21 */
156 1.1 bjh21 void
157 1.1 bjh21 rd_append(binno, infl0, nfiles, outfd, buffer, bufend)
158 1.1 bjh21 u_char *buffer, *bufend;
159 1.1 bjh21 int binno, nfiles;
160 1.1 bjh21 union f_handle infl0;
161 1.1 bjh21 FILE *outfd;
162 1.1 bjh21 {
163 1.1 bjh21 struct recheader *rec;
164 1.1 bjh21 rec = (RECHEADER *) buffer;
165 1.1 bjh21 if (!getnext(binno, infl0, nfiles, (RECHEADER *) buffer, bufend, 0)) {
166 1.1 bjh21 putline(rec, outfd);
167 1.1 bjh21 while (getnext(binno, infl0, nfiles, (RECHEADER *) buffer,
168 1.1 bjh21 bufend, 0) == 0) {
169 1.1 bjh21 if (!UNIQUE)
170 1.1 bjh21 putline(rec, outfd);
171 1.1 bjh21 }
172 1.1 bjh21 }
173 1.1 bjh21 }
174 1.1 bjh21
175 1.1 bjh21 /*
176 1.1 bjh21 * append plain text--used after sorting the biggest bin.
177 1.1 bjh21 */
178 1.1 bjh21 void
179 1.1 bjh21 concat(a, b)
180 1.1 bjh21 FILE *a, *b;
181 1.1 bjh21 {
182 1.1 bjh21 int nread;
183 1.1 bjh21 char buffer[4096];
184 1.1 bjh21
185 1.1 bjh21 rewind(b);
186 1.1 bjh21 while ((nread = fread(buffer, 1, 4096, b)) > 0)
187 1.1 bjh21 EWRITE(buffer, 1, nread, a);
188 1.1 bjh21 }
189