comparison src/fetchlist.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 fetchlist.c
3
4 $Id: fetchlist.c 49 2000-05-05 21:45:56Z uh1763 $
5 */
6
7 #if HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <stdio.h>
12 #include "fetchlist.h"
13 #include "configfile.h"
14 #include "log.h"
15 #include "util.h"
16
17 struct Elem
18 {
19 Str name;
20 FetchMode mode;
21 };
22
23 static struct Fetchlist
24 {
25 struct Elem *elem;
26 int size;
27 int max;
28 } fetchlist = { NULL, 0, 0 };
29
30 static const char *
31 getFile( void )
32 {
33 static Str file;
34 snprintf( file, MAXCHAR, "%s/fetchlist", Cfg_spoolDir() );
35 return file;
36 }
37
38 static void
39 clearList( void )
40 {
41 fetchlist.size = 0;
42 }
43
44 static int
45 compareElem( const void *elem1, const void *elem2 )
46 {
47 struct Elem* e1 = (struct Elem*)elem1;
48 struct Elem* e2 = (struct Elem*)elem2;
49 return strcmp( e1->name, e2->name );
50 }
51
52 static struct Elem *
53 searchElem( const char *name )
54 {
55 int i;
56
57 for ( i = 0; i < fetchlist.size; ++i )
58 if ( strcmp( name, fetchlist.elem[ i ].name ) == 0 )
59 return &fetchlist.elem[ i ];
60 return NULL;
61 }
62
63 static void
64 appGrp( const char *name, FetchMode mode )
65 {
66 struct Elem elem;
67
68 if ( fetchlist.max < fetchlist.size + 1 )
69 {
70 if ( ! ( fetchlist.elem
71 = realloc( fetchlist.elem,
72 ( fetchlist.max + 50 )
73 * sizeof( fetchlist.elem[ 0 ] ) ) ) )
74 {
75 Log_err( "Could not realloc fetchlist" );
76 exit( EXIT_FAILURE );
77 }
78 fetchlist.max += 50;
79 }
80 strcpy( elem.name, name );
81 elem.mode = mode;
82 fetchlist.elem[ fetchlist.size++ ] = elem;
83 }
84
85 void
86 Fetchlist_read( void )
87 {
88 FILE *f;
89 const char *file = getFile();
90 char *p;
91 FetchMode mode = OVER;
92 Bool valid;
93 int ret;
94 Str line, grp, modeStr;
95
96 Log_dbg( "Reading %s", file );
97 clearList();
98 if ( ! ( f = fopen( file, "r" ) ) )
99 {
100 Log_inf( "No file %s", file );
101 return;
102 }
103 while ( fgets( line, MAXCHAR, f ) )
104 {
105 p = Utl_stripWhiteSpace( line );
106 if ( *p == '#' || *p == '\0' )
107 continue;
108 ret = sscanf( p, "%s %s", grp, modeStr );
109 valid = TRUE;
110 if ( ret < 1 || ret > 2 )
111 valid = FALSE;
112 else if ( ret >= 2 )
113 {
114 if ( strcmp( modeStr, "full" ) == 0 )
115 mode = FULL;
116 else if ( strcmp( modeStr, "thread" ) == 0 )
117 mode = THREAD;
118 else if ( strcmp( modeStr, "over" ) == 0 )
119 mode = OVER;
120 else
121 valid = FALSE;
122 }
123 if ( ! valid )
124 {
125 Log_err( "Invalid entry in %s: %s", file, line );
126 continue;
127 }
128 appGrp( grp, mode );
129 }
130 fclose( f );
131 }
132
133 Bool
134 Fetchlist_write( void )
135 {
136 int i;
137 FILE *f;
138 const char *file = getFile();
139 const char *modeStr = "";
140
141 qsort( fetchlist.elem, fetchlist.size, sizeof( fetchlist.elem[ 0 ] ),
142 compareElem );
143 if ( ! ( f = fopen( file, "w" ) ) )
144 {
145 Log_err( "Could not open %s for writing", file );
146 return FALSE;
147 }
148 for ( i = 0; i < fetchlist.size; ++i )
149 {
150 switch ( fetchlist.elem[ i ].mode )
151 {
152 case FULL:
153 modeStr = "full"; break;
154 case THREAD:
155 modeStr = "thread"; break;
156 case OVER:
157 modeStr = "over"; break;
158 }
159 fprintf( f, "%s %s\n", fetchlist.elem[ i ].name, modeStr );
160 }
161 fclose( f );
162 return TRUE;
163 }
164
165 int
166 Fetchlist_size( void )
167 {
168 return fetchlist.size;
169 }
170
171 Bool
172 Fetchlist_contains( const char *name )
173 {
174 return ( searchElem( name ) != NULL );
175 }
176
177 Bool
178 Fetchlist_element( const char **name, FetchMode *mode, int index )
179 {
180 if ( index < 0 || index >= fetchlist.size )
181 return FALSE;
182 *name = fetchlist.elem[ index ].name;
183 *mode = fetchlist.elem[ index ].mode;
184 return TRUE;
185 }
186
187 Bool
188 Fetchlist_add( const char *name, FetchMode mode )
189 {
190 struct Elem *elem = searchElem( name );
191 if ( elem == NULL )
192 {
193 appGrp( name, mode );
194 return TRUE;
195 }
196 strcpy( elem->name, name );
197 elem->mode = mode;
198 return FALSE;
199 }
200
201 Bool
202 Fetchlist_remove( const char *name )
203 {
204 struct Elem *elem = searchElem( name );
205 if ( elem == NULL )
206 return FALSE;
207 *elem = fetchlist.elem[ fetchlist.size - 1 ];
208 --fetchlist.size;
209 return TRUE;
210 }