comparison src/itemlist.c @ 43:2842f50feb55 noffle

[svn] * client.c, client.h, common.h, config.c, config.h, content.c, content.h, control.c, control.h, database.c, database.h, dynamicstring.c, dynamicstring.h, fetch.c, fetch.h, fetchlist.c, fetchlist.h, group.c, group.h, itemlist.c, itemlist.h, lock.c, lock.h, log.c, log.h, noffle.c, online.c, online.h, outgoing.c, outgoing.h, over.c, over.h, post.c, post.h, protocol.c, protocol.h, pseudo.c, pseudo.h, request.c, request.h, server.c, server.h, util.c, util.h, wildmat.c, wildmat.h: Moved files to the subdirectory src/ * Makefile.am, acconfig.h, configure.in, docs/Makefile.am, src/Makefile.am, Makefile.in, aclocal.m4, config.h.in, configure, install-sh, missing, mkinstalldirs, stamp-h.in, docs/Makefile.in, src/Makefile.in: Added files. They are used by aclocal, autoheader, autoconf and automake. * src/config.c, src/config.h: Renamed to configfile.c and configfile.h, because configure will generate a config.h file itself. * src/client.c, src/content.c, src/database.c, src/fetch.c, src/fetchlist.c, src/group.c, src/lock.c, src/noffle.c, src/online.c, src/outgoing.c, src/over.c, src/pseudo.c, src/request.c, src/server.c, src/util.c: Changed '#include "config.h"' to '#include "configfile.h"'. * src/client.c, src/content.c, src/database.c, src/fetch.c, src/fetchlist.c, src/group.c, src/lock.c, src/online.c, src/outgoing.c, src/post.c, src/protocol.c, src/request.c, src/server.c: Files now #include <config.h>. Added missing <stdio.h>. This removes the warnings about snprintf() not being declared. * Makefile: Removed. This is now generated by configure.
author uh1763
date Fri, 05 May 2000 22:45:56 +0100
parents
children 32ba1198c6fa
comparison
equal deleted inserted replaced
42:2467ff423c15 43:2842f50feb55
1 /*
2 itemlist.c
3
4 $Id: itemlist.c 49 2000-05-05 21:45:56Z uh1763 $
5 */
6
7 #include "itemlist.h"
8 #include <ctype.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include "common.h"
12 #include "log.h"
13
14 struct ItemList
15 {
16 char *list;
17 char *separators;
18 char *next;
19 size_t count;
20 };
21
22 /* Make a new item list. */
23 ItemList *
24 new_Itl( const char *list, const char *separators )
25 {
26 ItemList * res;
27 char *p;
28 Bool inItem;
29
30 res = (ItemList *) malloc( sizeof( ItemList ) );
31 if ( res == NULL )
32 {
33 Log_err( "Malloc of ItemList failed." );
34 exit( EXIT_FAILURE );
35 }
36
37 res->list = (char *) malloc ( strlen(list) + 2 );
38 if ( res->list == NULL )
39 {
40 Log_err( "Malloc of ItemList.list failed." );
41 exit( EXIT_FAILURE );
42 }
43 strcpy( res->list, list );
44
45 if ( ( res->separators = strdup( separators ) ) == NULL )
46 {
47 Log_err( "Malloc of ItemList.separators failed." );
48 exit( EXIT_FAILURE );
49 }
50
51 res->count = 0;
52 res->next = res->list;
53
54 /* Separate items into strings and have final zero-length string. */
55 for( p = res->list, inItem = FALSE; *p != '\0'; p++ )
56 {
57 Bool isSep = ( strchr( separators, p[ 0 ] ) != NULL );
58
59 if ( inItem )
60 {
61 if ( isSep )
62 {
63 p[ 0 ] = '\0';
64 inItem = FALSE;
65 res->count++;
66 }
67 }
68 else
69 {
70 if ( ! isSep )
71 inItem = TRUE;
72 }
73 }
74 if ( inItem )
75 res->count++;
76 p[ 1 ] = '\0';
77 return res;
78 }
79
80 /* Delete an item list. */
81 void
82 del_Itl( ItemList *self )
83 {
84 if ( self == NULL )
85 return;
86 free( self->list );
87 free( self->separators );
88 free( self );
89 }
90
91 /* Get first item. */
92 const char *
93 Itl_first( ItemList *self)
94 {
95 self->next = self->list;
96 return Itl_next( self );
97 }
98
99 /* Get next item or NULL. */
100 const char *
101 Itl_next( ItemList *self )
102 {
103 const char *res = self->next;
104
105 if ( res[ 0 ] == '\0' )
106 return NULL;
107
108 while ( strchr( self->separators, res[ 0 ] ) != NULL )
109 res++;
110
111 if ( res[ 0 ] == '\0' && res[ 1 ] == '\0' )
112 return NULL;
113
114 self->next += strlen( res ) + 1;
115 return res;
116 }
117
118 /* Get count of items in list. */
119 size_t
120 Itl_count( const ItemList *self )
121 {
122 return self->count;
123 }