(yet Another test AND a PD getopt) AND nestable strtok
Robert Osborne
robert at isgtec.UUCP
Fri May 24 08:08:41 AEST 1991
In article <3454 at travis.csd.harris.com>, brad at SSD.CSD.HARRIS.COM (Brad Appleton) writes:
|> In article <1074 at isgtec.UUCP> robert at isgtec.UUCP writes:
|> >
|> >Here is a getopt I wrote (now PD) because Turbo C doesn't have one!
|> >(Maybe they'll put this one into their libraries now :-)
|> >
|> First of all - AT&T already put getopt into the Public domain.
First of all, I didn't have it when I spent 20 minutes knocking this off.
|> Second,
|> getopt is a piece of crap. DOWN WITH GETOPT! There is much better stuff
|> available.
You seem very, uumh, emotional about this.
|>Take Parseargs for example (which I released).
Ahhh, I see why.
For simple, ten line test programs, getopt is a quick way to
modify the way the program runs. Besides it pretty standard,
everybody knows how it works.
|>For any
|> interested parties, the following is a brief description of Parseargs.
|> You can get the latest version from me, or you can look in your local
|> comp.sources.misc archives for the sources and latest patches (patch05).
I don't think my getopt will need patches (certainly not 5). When I'm
writing simple test programs I don't want to spend my time debugging
somebody's parseargs package. Now if I was writing a product maybe
I'd take a look at it.
Rob.
Obj Source:
Here's a nestable strtok (strtoknst) it needs a third argument
(ptr to char *) to accomplish this. I wrote this after getting
burned for about the tenth time by nested strtok's.
#!/bin/sh
# This is a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 05/23/1991 21:58 UTC by robert at isgtec.UUCP
# Source directory /tmp_mnt/home/joker1/robert
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 1717 -rw-rw-r-- mystrtok.c
#
# ============= mystrtok.c ==============
if test -f 'mystrtok.c' -a X"$1" != X"-c"; then
echo 'x - skipping mystrtok.c (File already exists)'
else
echo 'x - extracting mystrtok.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'mystrtok.c' &&
X#include <string.h>
X/*
X** $Id: strtoknst.c,v 1.1 1991/03/13 22:11:06 robert Exp $
X**
X** This is a clone of strtok that takes another argument
X** a char pointer (assumed unchanged between calls!)
X** that allows nested use)
X**
X** I hereby release this function into the public domain.
X** Robert Osborne, 1990.
X*/
Xchar *
Xstrtoknst(s1, s2, ptr)
X char *s1;
X char *s2;
X char **ptr;
X{
X register char *first, *second, *third;
X if( s1 ) {
X *ptr = s1;
X }
X if( !*ptr ) return (char *) 0;
X first = *ptr + strspn(*ptr, s2);
X if( *first == '\0' ) return (char *)0;
X second = first + strcspn(first, s2);
X if( *second == '\0' ) {
X *ptr = (char *) 0;
X }
X else {
X *ptr = second + strspn(second, s2);
X if( **ptr == '\0' ) {
X *ptr = (char *) 0;
X }
X *second = '\0';
X }
X return first;
X}
X#ifdef TESTING
X
Xmain()
X{
X char *tmp, *tmp2;
X char *str, *str2;
X
X printf("test 1\n");
X for(str = strtoknst(" aaaa\t bbbb cccc\tdddd ", "\t ", &tmp);
X str;
X str = strtoknst((char *) 0, "\t ", &tmp) )
X {
X printf("\"%s\"\n", str);
X }
X printf("test 2\n");
X for(str = strtoknst("aaaa\t bbbb cccc\tdddd ", "\t ", &tmp);
X str;
X str = strtoknst((char *) 0, "\t ", &tmp) )
X {
X printf("\"%s\"\n", str);
X }
X printf("test 3\n");
X for(str = strtoknst(" aaaa\t bbbb cccc\tdddd", "\t ", &tmp);
X str;
X str = strtoknst((char *) 0, "\t ", &tmp) )
X {
X printf("\"%s\"\n", str);
X }
X printf("test 4\n");
X for(str = strtoknst(" aaaa\t bbbb \t\t \tcccc\tdddd", "\t ", &tmp);
X str;
X str = strtoknst((char *) 0, "\t ", &tmp) )
X {
X printf("\"%s\"\n", str);
X for(str2 = strtoknst(" AAAA\t BBBB \t\t \tCCCC\tDDDD", "\t ", &tmp2);
X str2;
X str2 = strtoknst((char *) 0, "\t ", &tmp2) )
X {
X printf("\"%s\"\n", str2);
X }
X }
X}
X#endif
SHAR_EOF
chmod 0664 mystrtok.c ||
echo 'restore of mystrtok.c failed'
Wc_c="`wc -c < 'mystrtok.c'`"
test 1717 -eq "$Wc_c" ||
echo 'mystrtok.c: original size 1717, current size' "$Wc_c"
fi
exit 0
More information about the Alt.sources
mailing list