1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
/*******************WARNING*********************
This is a *MODIFIED* version of Geoff Coller's proof-of-concept NOV
implementation.
It has been modified to support threading directly from a file handle
to a NNTP server without a temporary file.
This is not a complete distribution. We have only distributed enough
to support NN's needs.
The original version came from world.std.com:/src/news/nov.dist.tar.Z
and was dated 11 Aug 1993.
In any case, bugs you find here are probably my fault, as I've trimmed
a fair bit of unused code.
-Peter Wemm <peter@DIALix.oz.au>
*/
/*
* Copyright (c) Geoffrey Collyer 1992, 1993.
* All rights reserved.
* Written by Geoffrey Collyer.
* Thanks to UUNET Communications Services Inc for financial support.
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company, the Regents of the University of California, or
* the Free Software Foundation.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. The authors are not responsible for the consequences of use of this
* software, no matter how awful, even if they arise from flaws in it.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Since few users ever read sources,
* credits must appear in the documentation.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software. Since few users
* ever read sources, credits must appear in the documentation.
*
* 4. This notice may not be removed or altered.
*/
#include "config.h"
/*
* split - divide a string into fields, like awk split()
*/
int /* number of fields, including overflow */
split(char *string, char *fields[], int nfields, char *sep)
/* fields list is not NULL-terminated */
/* nfields number of entries available in fields[] */
/* sep "" white, "c" single char, "ab" [ab]+ */
{
register char *p = string;
register char c; /* latest character */
register char sepc = sep[0];
register char sepc2;
register int fn;
register char **fp = fields;
register char *sepp;
register int trimtrail;
/* white space */
if (sepc == '\0') {
while ((c = *p++) == ' ' || c == '\t')
continue;
p--;
trimtrail = 1;
sep = " \t"; /* note, code below knows this is 2 long */
sepc = ' ';
} else
trimtrail = 0;
sepc2 = sep[1]; /* now we can safely pick this up */
/* catch empties */
if (*p == '\0')
return (0);
/* single separator */
if (sepc2 == '\0') {
fn = nfields;
for (;;) {
*fp++ = p;
fn--;
if (fn == 0)
break;
while ((c = *p++) != sepc)
if (c == '\0')
return (nfields - fn);
*(p - 1) = '\0';
}
/* we have overflowed the fields vector -- just count them */
fn = nfields;
for (;;) {
while ((c = *p++) != sepc)
if (c == '\0')
return (fn);
fn++;
}
/* not reached */
}
/* two separators */
if (sep[2] == '\0') {
fn = nfields;
for (;;) {
*fp++ = p;
fn--;
while ((c = *p++) != sepc && c != sepc2)
if (c == '\0') {
if (trimtrail && **(fp - 1) == '\0')
fn++;
return (nfields - fn);
}
if (fn == 0)
break;
*(p - 1) = '\0';
while ((c = *p++) == sepc || c == sepc2)
continue;
p--;
}
/* we have overflowed the fields vector -- just count them */
fn = nfields;
while (c != '\0') {
while ((c = *p++) == sepc || c == sepc2)
continue;
p--;
fn++;
while ((c = *p++) != '\0' && c != sepc && c != sepc2)
continue;
}
/* might have to trim trailing white space */
if (trimtrail) {
p--;
while ((c = *--p) == sepc || c == sepc2)
continue;
p++;
if (*p != '\0') {
if (fn == nfields + 1)
*p = '\0';
fn--;
}
}
return (fn);
}
/* n separators */
fn = 0;
for (;;) {
if (fn < nfields)
*fp++ = p;
fn++;
for (;;) {
c = *p++;
if (c == '\0')
return (fn);
sepp = sep;
while ((sepc = *sepp++) != '\0' && sepc != c)
continue;
if (sepc != '\0') /* it was a separator */
break;
}
if (fn < nfields)
*(p - 1) = '\0';
for (;;) {
c = *p++;
sepp = sep;
while ((sepc = *sepp++) != '\0' && sepc != c)
continue;
if (sepc == '\0') /* it wasn't a separator */
break;
}
p--;
}
/* not reached */
}
#ifdef TEST_SPLIT
/*
* test program
* pgm runs regression
* pgm sep splits stdin lines by sep
* pgm str sep splits str by sep
* pgm str sep n splits str by sep n times
*/
int
main(int argc, char *argv[])
{
char buf[512];
register int n;
#define MNF 10
char *fields[MNF];
if (argc > 4)
for (n = atoi(argv[3]); n > 0; n--) {
(void) strcpy(buf, argv[1]);
}
else if (argc > 3)
for (n = atoi(argv[3]); n > 0; n--) {
(void) strcpy(buf, argv[1]);
(void) split(buf, fields, MNF, argv[2]);
}
else if (argc > 2)
dosplit(argv[1], argv[2]);
else if (argc > 1)
while (fgets(buf, sizeof(buf), stdin) != NULL) {
buf[strlen(buf) - 1] = '\0'; /* stomp newline */
dosplit(buf, argv[1]);
}
else
regress();
exit(0);
}
dosplit(char *string, char *seps)
{
#define NF 5
char *fields[NF];
register int nf;
nf = split(string, fields, NF, seps);
print(nf, NF, fields);
}
print(int nf, int nfp, char *fields[])
{
register int fn;
register int bound;
bound = (nf > nfp) ? nfp : nf;
printf("%d:\t", nf);
for (fn = 0; fn < bound; fn++)
printf("\"%s\"%s", fields[fn], (fn + 1 < nf) ? ", " : "\n");
}
#define RNF 5 /* some table entries know this */
struct {
char *str;
char *seps;
int nf;
char *fi[RNF];
} tests[] = {
"", " ", 0, {
""
},
" ", " ", 2, {
"", ""
},
"x", " ", 1, {
"x"
},
"xy", " ", 1, {
"xy"
},
"x y", " ", 2, {
"x", "y"
},
"abc def g ", " ", 5, {
"abc", "def", "", "g", ""
},
" a bcd", " ", 4, {
"", "", "a", "bcd"
},
"a b c d e f", " ", 6, {
"a", "b", "c", "d", "e f"
},
" a b c d ", " ", 6, {
"", "a", "b", "c", "d "
},
"", " _", 0, {
""
},
" ", " _", 2, {
"", ""
},
"x", " _", 1, {
"x"
},
"x y", " _", 2, {
"x", "y"
},
"ab _ cd", " _", 2, {
"ab", "cd"
},
" a_b c ", " _", 5, {
"", "a", "b", "c", ""
},
"a b c_d e f", " _", 6, {
"a", "b", "c", "d", "e f"
},
" a b c d ", " _", 6, {
"", "a", "b", "c", "d "
},
"", " _~", 0, {
""
},
" ", " _~", 2, {
"", ""
},
"x", " _~", 1, {
"x"
},
"x y", " _~", 2, {
"x", "y"
},
"ab _~ cd", " _~", 2, {
"ab", "cd"
},
" a_b c~", " _~", 5, {
"", "a", "b", "c", ""
},
"a b_c d~e f", " _~", 6, {
"a", "b", "c", "d", "e f"
},
"~a b c d ", " _~", 6, {
"", "a", "b", "c", "d "
},
"", " _~-", 0, {
""
},
" ", " _~-", 2, {
"", ""
},
"x", " _~-", 1, {
"x"
},
"x y", " _~-", 2, {
"x", "y"
},
"ab _~- cd", " _~-", 2, {
"ab", "cd"
},
" a_b c~", " _~-", 5, {
"", "a", "b", "c", ""
},
"a b_c-d~e f", " _~-", 6, {
"a", "b", "c", "d", "e f"
},
"~a-b c d ", " _~-", 6, {
"", "a", "b", "c", "d "
},
"", " ", 0, {
""
},
" ", " ", 2, {
"", ""
},
"x", " ", 1, {
"x"
},
"xy", " ", 1, {
"xy"
},
"x y", " ", 2, {
"x", "y"
},
"abc def g ", " ", 4, {
"abc", "def", "g", ""
},
" a bcd", " ", 3, {
"", "a", "bcd"
},
"a b c d e f", " ", 6, {
"a", "b", "c", "d", "e f"
},
" a b c d ", " ", 6, {
"", "a", "b", "c", "d "
},
"", "", 0, {
""
},
" ", "", 0, {
""
},
"x", "", 1, {
"x"
},
"xy", "", 1, {
"xy"
},
"x y", "", 2, {
"x", "y"
},
"abc def g ", "", 3, {
"abc", "def", "g"
},
"\t a bcd", "", 2, {
"a", "bcd"
},
" a \tb\t c ", "", 3, {
"a", "b", "c"
},
"a b c d e ", "", 5, {
"a", "b", "c", "d", "e"
},
"a b\tc d e f", "", 6, {
"a", "b", "c", "d", "e f"
},
" a b c d e f ", "", 6, {
"a", "b", "c", "d", "e f "
},
NULL, NULL, 0, {
NULL
},
};
regress(void)
{
char buf[512];
register int n;
char *fields[RNF + 1];
register int nf;
register int i;
register int printit;
register char *f;
for (n = 0; tests[n].str != NULL; n++) {
(void) strcpy(buf, tests[n].str);
fields[RNF] = NULL;
nf = split(buf, fields, RNF, tests[n].seps);
printit = 0;
if (nf != tests[n].nf) {
printf("split `%s' by `%s' gave %d fields, not %d\n",
tests[n].str, tests[n].seps, nf, tests[n].nf);
printit = 1;
} else if (fields[RNF] != NULL) {
printf("split() went beyond array end\n");
printit = 1;
} else {
for (i = 0; i < nf && i < RNF; i++) {
f = fields[i];
if (f == NULL)
f = "(NULL)";
if (strcmp(f, tests[n].fi[i]) != 0) {
printf("split `%s' by `%s', field %d is `%s', not `%s'\n",
tests[n].str, tests[n].seps,
i, fields[i], tests[n].fi[i]);
printit = 1;
}
}
}
if (printit)
print(nf, RNF, fields);
}
}
#endif
|