changeset 72:78e2ae741240 noffle

[svn] Fix stupid bug in Itl_next
author bears
date Sat, 13 May 2000 16:34:15 +0100
parents 6aa3a8eff5a9
children c874bd3c4bb8
files src/itemlist.c
diffstat 1 files changed, 63 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/itemlist.c	Sat May 13 11:53:48 2000 +0100
+++ b/src/itemlist.c	Sat May 13 16:34:15 2000 +0100
@@ -1,7 +1,7 @@
 /*
   itemlist.c
 
-  $Id: itemlist.c 60 2000-05-09 22:28:38Z uh1763 $
+  $Id: itemlist.c 78 2000-05-13 15:34:15Z bears $
 */
 
 #if HAVE_CONFIG_H
@@ -16,10 +16,15 @@
 #include "log.h"
 #include "portable.h"
 
+#if defined(ITEMLIST_TEST)
+#define	Log_err	printf
+#endif
+
+#define	SEP_CHAR	'\1'	/* Replace all separators with this */
+
 struct ItemList
 {
     char *list;
-    char *separators;
     char *next;
     size_t count;
 };
@@ -47,12 +52,6 @@
     }
     strcpy( res->list, list );
 
-    if (  ( res->separators = strdup( separators ) ) == NULL )
-    {
-	Log_err( "Malloc of ItemList.separators failed." );
-	exit( EXIT_FAILURE );
-    }
-
     res->count = 0;
     res->next = res->list;
 
@@ -72,7 +71,9 @@
 	}
 	else
 	{
-	    if ( ! isSep )
+	    if ( isSep )
+		p[ 0 ] = SEP_CHAR;
+	    else
 		inItem = TRUE;
 	}
     }
@@ -89,7 +90,6 @@
     if ( self == NULL )
 	return;
     free( self->list );
-    free( self->separators );
     free( self );
 }
 
@@ -105,18 +105,18 @@
 const char *
 Itl_next( ItemList *self )
 {
-    const char *res = self->next;
+    char *res = self->next;
 
     if ( res[ 0 ] == '\0' )
 	return NULL;
 
-    while ( strchr( self->separators, res[ 0 ] ) != NULL )
+    while ( res[ 0 ] == SEP_CHAR )
 	res++;
 
     if ( res[ 0 ] == '\0' && res[ 1 ] == '\0' )
 	return NULL;
 
-    self->next += strlen( res ) + 1;
+    self->next = res + strlen( res ) + 1;
     return res;
 }
 
@@ -126,3 +126,53 @@
 {
     return self->count;
 }
+
+#if defined(ITEMLIST_TEST)
+
+/* Test code borrowed from wildmat.c. Yep, still uses gets(). */
+extern char	*gets();
+
+int
+main()
+{
+    Str line;
+    Str seps;
+    ItemList * itl;
+    int count;
+    const char *item;
+
+    printf( "Itemlist tester.  Enter seperators, then strings to test.\n" );
+    printf( "A blank line gets prompts for new seperators; blank separators\n" );
+    printf( "exits the program.\n" );
+
+    for ( ; ; )
+    {
+	printf( "\nEnter seperators:  " );
+	(void) fflush( stdout );
+	if ( gets( seps ) == NULL || seps[0] == '\0' )
+	    break;
+	for ( ; ; )
+	{
+	    printf( "Enter line:  " );
+	    (void) fflush( stdout );
+	    if ( gets( line ) == NULL )
+		exit( 0 );
+	    if ( line[0] == '\0' )
+		break;
+	    itl = new_Itl( line, seps );
+	    printf( "%d items on list\n", Itl_count( itl ) );
+	    count = 0;
+	    for ( item = Itl_first( itl );
+		  item != NULL;
+		  item = Itl_next( itl ) )
+		printf( "  Item %d is '%s'\n", ++count, item );
+	    if ( count != Itl_count( itl ) )
+		printf( "*** Warning - counts don't match ***\n" );
+	    del_Itl( itl );
+	}
+    }
+
+    exit(0);
+    /* NOTREACHED */
+}
+#endif	/* defined(TEST) */