File: artstruct.c

package info (click to toggle)
xrn 9.02-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,784 kB
  • ctags: 3,047
  • sloc: ansic: 24,689; makefile: 2,240; yacc: 888; sh: 274; lex: 92; perl: 35; awk: 31; csh: 13
file content (573 lines) | stat: -rw-r--r-- 13,338 bytes parent folder | download | duplicates (4)
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include <X11/Intrinsic.h>
#include <assert.h>

#define ARTSTRUCT_C
#include "config.h"
#include "news.h"
#include "artstruct.h"
#include "file_cache.h"

static Boolean artsame _ARGUMENTS((struct article *, struct article *));
static void freeartstruct _ARGUMENTS((struct article *));


/*
  Initialize a newsgroup structure from the point of view of the
  routines in this file.
  */
void artListInit(newsgroup)
    struct newsgroup *newsgroup;
{
    newsgroup->articles = newsgroup->ref_art = 0;
}


/*
  Make sure that the article list for a newsgroup exists.  Create it
  if it doesn't.
  */
void artListSet(newsgroup)
    struct newsgroup *newsgroup;
{
    struct article *old;

    if (newsgroup->articles)
	return;

    newsgroup->articles = (struct article *)
	XtCalloc(1, sizeof(struct article));
    newsgroup->articles->status = ART_CLEAR;
    newsgroup->articles->first = newsgroup->first;

    if (newsgroup->first > 1) {
	old = (struct article *)
	    XtCalloc(1, sizeof(struct article));
	old->first = 1;
	old->status = ART_CLEAR_READ;
	SET_UNAVAIL(old);
	old->next = newsgroup->articles;
	old->next->previous = old;
	newsgroup->articles = old;
    }
    newsgroup->ref_art = newsgroup->articles;
}


int artStructNumChildren(art)
     struct article *art;
{
  int i;
  art_num *ptr;

  if (! art->children)
    return 0;

  for (ptr = art->children, i = 0; *ptr; ptr++)
    i++;

  return i;
}

void artStructAddChild(art, child)
     struct article *art;
     art_num child;
{
  int i = artStructNumChildren(art);

  art->children = (art_num *) XtRealloc((char *) art->children,
					(i + 2) * sizeof(*art->children));

  art->children[i++] = child;
  art->children[i] = 0;
}

void artStructRemoveChild(art, child)
     struct article *art;
     art_num child;
{
  art_num *ptr, *ptr2;

  if (! art->children)
    return;

  for (ptr = art->children; *ptr; ptr++) {
    if (*ptr == child) {
      for (ptr2 = ptr + 1; *ptr2; ptr++, ptr2++)
	*ptr = *ptr2;
      *ptr = 0;
    }
  }

  return;
}
      
  
static art_num *copy_art_list _ARGUMENTS((struct article *));

static art_num *copy_art_list(art)
     struct article *art;
{
  int i;
  art_num *new;

  if (! art->children)
    return 0;

  i = artStructNumChildren(art);

  new = (art_num *) XtCalloc(i + 1, sizeof(*new));

  (void) memcpy((char *) new, (char *) art->children, (i + 1) * sizeof(*new));

  return new;
}

/*
  Free an article structure.
  */
static void freeartstruct(art)
    struct article *art;
{
  CLEAR_ALL(art);
  XtFree((char *) art);
}


/*
  Free an article list for a newsgroup.
  */
void artListFree(newsgroup)
    struct newsgroup *newsgroup;
{
    struct article *next, *list = newsgroup->articles;

    while (list) {
	next = list->next;
	freeartstruct(list);
	list = next;
    }
    newsgroup->articles = newsgroup->ref_art = 0;
}

    
/*
  Get an article structure for a particular article in a newsgroup.

  The newsgroup article list must have been initialized previously
  (with artListSet(), followed by calls to populate it).

  The specified article must be between 1 and newsgroup->last.

  If "writeable" is true, than the returned structure can be modified
  by the caller.  Otherwise, it shouldn't be.
  */
struct article *artStructGet(
			     _ANSIDECL(struct newsgroup *,	newsgroup),
			     _ANSIDECL(art_num,			artnum),
			     _ANSIDECL(Boolean,			writeable)
			     )
     _KNRDECL(struct newsgroup *,	newsgroup)
     _KNRDECL(art_num,			artnum)
     _KNRDECL(Boolean,			writeable)
{
    struct article *reference = newsgroup->ref_art;

    if (! reference)
	reference = newsgroup->articles;

    /*
      Find the structure containing the article we want.
      */

    while ((artnum < reference->first) ||
	   (reference->next && (reference->next->first <= artnum))) {
	if (artnum < reference->first)
	    reference = reference->previous;
	else
	    reference = reference->next;
    }

    if (! writeable) {
	newsgroup->ref_art = reference;
	return reference;
    }

#define COPY_FIELD(field) \
    if (reference->field) \
	new->field = XtNewString(reference->field);

    /*
      If there are articles in the structure after the one we want,
      create a new structure to hold them.
      */
    if ((reference->next && (reference->next->first > (artnum + 1))) ||
	(!reference->next && (artnum < newsgroup->last))) {
	struct article *new = (struct article *)
	    XtMalloc(sizeof(struct article));
	*new = *reference;
	COPY_FIELD(subject);
	COPY_FIELD(from);
	COPY_FIELD(author);
	COPY_FIELD(lines);
	COPY_FIELD(newsgroups);
	COPY_FIELD(date);
	COPY_FIELD(id);
	COPY_FIELD(xref);
	COPY_FIELD(approved);
	COPY_FIELD(references);
	new->file = reference->file;
	new->base_file = reference->base_file;
	new->parent = reference->parent;
	new->children = copy_art_list(reference);
	new->first = artnum + 1;
	new->previous = reference;
	if (reference->next)
	    reference->next->previous = new;
	reference->next = new;
    }

    /*
      If there are articles in the structure before the one we want,
      create a new structure for just the one we want.
      */
    if (reference->first < artnum) {
	struct article *new = (struct article *)
	    XtMalloc(sizeof(struct article));
	*new = *reference;
	COPY_FIELD(subject);
	COPY_FIELD(from);
	COPY_FIELD(author);
	COPY_FIELD(lines);
	COPY_FIELD(newsgroups);
	COPY_FIELD(date);
	COPY_FIELD(id);
	COPY_FIELD(xref);
	COPY_FIELD(approved);
	COPY_FIELD(references);
	new->file = reference->file;
	new->base_file = reference->base_file;
	new->parent = reference->parent;
	new->children = copy_art_list(reference);
	new->first = artnum;
	new->previous = reference;
	if (reference->next)
	    reference->next->previous = new;
	reference->next = new;
	reference = new;
    }

#undef COPY_FIELD

    newsgroup->ref_art = reference;
    return reference;
}


#define CHECK(field, staying, going) \
	if ((staying)->field != (going)->field) { \
	    XtFree((char *)(going)->field); \
	    (going)->field = (staying)->field; \
	}

/*
  Given an article structure which was previously returned by
  artStructGet with "writeable" True and which may have been modified
  in the meantime, pack the article list containing the article
  structure as necessary.

  The "art" parameter is an input/output parameter; when the function
  returns, it will point at the current article structure for the
  article, but that structure should then be treated as read-only by
  the caller.
  */
void artStructSet(newsgroup, art)
    struct newsgroup *newsgroup;
    struct article **art;
{
    if (artsame(*art, (*art)->previous)) {
      struct article *tmp = *art;

      CHECK(subject, (*art)->previous, *art);
      CHECK(from, (*art)->previous, *art);
      CHECK(author, (*art)->previous, *art);
      CHECK(lines, (*art)->previous, *art);
      CHECK(newsgroups, (*art)->previous, *art);
      CHECK(date, (*art)->previous, *art);
      CHECK(children, (*art)->previous, *art);
      /* "file" doesn't need checking because artsame() says it's the
	 same in both structures. */
      CHECK(id, (*art)->previous, *art);
      CHECK(xref, (*art)->previous, *art);
      CHECK(approved, (*art)->previous, *art);
      CHECK(references, (*art)->previous, *art);

      (*art)->previous->next = (*art)->next;
      if ((*art)->next)
	(*art)->next->previous = (*art)->previous;
      *art = (*art)->previous;
      XtFree((char *) tmp);
    }

    if (artsame(*art, (*art)->next)) {
      struct article *tmp = (*art)->next;

      CHECK(subject, (*art)->next, *art);
      CHECK(from, (*art)->next, *art);
      CHECK(author, (*art)->next, *art);
      CHECK(lines, (*art)->next, *art);
      CHECK(newsgroups, (*art)->next, *art);
      CHECK(date, (*art)->next, *art);
      CHECK(children, (*art)->next, *art);
      /* "file" doesn't need checking because artsame() says it's the
	 same in both structures. */
      CHECK(id, (*art)->next, *art);
      CHECK(xref, (*art)->next, *art);
      CHECK(approved, (*art)->next, *art);
      CHECK(references, (*art)->next, *art);

      (*art)->next->first = (*art)->first;
      if ((*art)->previous)
	(*art)->previous->next = tmp;
      else
	newsgroup->articles = tmp;
      (*art)->next->previous = (*art)->previous;
      XtFree((char *) *art);
      *art = tmp;
    }

    newsgroup->ref_art = *art;
}


/*
  Like artStructSet, but an original article structure, which may have
  been retrieved with "writeable" False, and a copy are specified.  
  Only actually does a replacement if something was changed.

  The "original" parameter is input/output and will contain a
  read-only pointer to the structure for the article when the function
  returns.
  */
void artStructReplace(newsgroup, original, copy, artnum)
    struct newsgroup *newsgroup;
    struct article **original;
    struct article *copy;
    art_num artnum;
{
    if (! artsame(*original, copy)) {
	*original = artStructGet(newsgroup, artnum, True);
	(*original)->status = copy->status;
	CHECK(subject, copy, *original);
	CHECK(from, copy, *original);
	CHECK(author, copy, *original);
	CHECK(lines, copy, *original);
	CHECK(newsgroups, copy, *original);
	CHECK(date, copy, *original);
	(*original)->parent = copy->parent;
	CHECK(children, copy, *original);
	if ((*original)->file != copy->file) {
	    CLEAR_FILE(*original);
	    CHECK(file, copy, *original);
	}
	if ((*original)->base_file != copy->base_file) {
	    CLEAR_BASE_FILE(*original);
	    CHECK(base_file, copy, *original);
	}
	CHECK(id, copy, *original);
	CHECK(xref, copy, *original);
	CHECK(approved, copy, *original);
	CHECK(references, copy, *original);
	artStructSet(newsgroup, original);
    }
}

#undef CHECK

/*
  Return the next article structure in an article list, as well as the
  first and last article numbers it represents.  Returns null when
  there are no more.

  The structure that's returned is read-only.
  */
struct article *artStructNext(newsgroup, art, first, last)
    struct newsgroup *newsgroup;
    struct article *art;
    art_num *first, *last;
{
    art = art->next;
    if (! art) {
	newsgroup->ref_art = 0;
	return 0;
    }

    if (first)
	if (art->first)
	    *first = art->first;
	else
	    *first = newsgroup->first;

    if (last)
	if (art->next)
	    *last = art->next->first - 1;
	else
	    *last = newsgroup->last;

    newsgroup->ref_art = art;
    return art;
}


/*
  Like artStructNext(), but returns the previous article structure
  instead of the next one.
  */
struct article *artStructPrevious(newsgroup, art, first, last)
    struct newsgroup *newsgroup;
    struct article *art;
    art_num *first, *last;
{
    art = art->previous;
    if (! art) {
	newsgroup->ref_art = 0;
	return 0;
    }

    if (first)
	if (art->first)
	    *first = art->first;
	else
	    *first = newsgroup->first;

    if (last)
	if (art->next)
	    *last = art->next->first - 1;
	else
	    *last = newsgroup->last;

    newsgroup->ref_art = art;
    return art;
}


/*
  Returns the first article structure in the article list for a
  newsgroup, as well as the first and last article numbers it
  represents.
  */
struct article *artListFirst(newsgroup, first, last)
    struct newsgroup *newsgroup;
    art_num *first, *last;
{
    struct article *art = newsgroup->articles;

    if (! art) {
	newsgroup->ref_art = 0;
	return 0;
    }

    if (first)
	*first = art->first;

    if (last)
	if (art->next)
	    *last = art->next->first - 1;
	else
	    *last = newsgroup->last;

    newsgroup->ref_art = art;
    return art;
}


/*
  Returns the last article structure in the article list for a
  newsgroup.
  */
struct article *artListLast(newsgroup, first, last)
    struct newsgroup *newsgroup;
    art_num *first, *last;
{
    struct article *art = newsgroup->articles;

    if (! art) {
	newsgroup->ref_art = 0;
	return 0;
    }

    while (art->next)
	art = art->next;

    if (first)
	if (art->first)
	    *first = art->first;
	else
	    *first = newsgroup->first;

    if (last)
	*last = newsgroup->last;

    newsgroup->ref_art = 0;
    return art;
}


/*
  Compare two article structures, returning True if they are equal in
  everything but pointers and ranges and False otherwise.  Returns
  False if either structure pointer is null.
  */
static Boolean artsame(art1, art2)
    struct article *art1, *art2;
{
    int num_children;

    if (! (art1 && art2))
	return False;

    if (art1->status != art2->status)
	return False;

#define ARTSTRCMP(field) \
    if (art1->field) \
	if (art2->field) { \
	    if (strcmp(art1->field, art2->field)) \
		return False; \
	} \
	else \
	    return False; \
    else if (art2->field) \
	return False;

    ARTSTRCMP(subject);
    ARTSTRCMP(from);
    ARTSTRCMP(author);
    ARTSTRCMP(lines);
    ARTSTRCMP(newsgroups);
    ARTSTRCMP(date);
    ARTSTRCMP(id);
    ARTSTRCMP(xref);
    ARTSTRCMP(approved);
    ARTSTRCMP(references);

    if (art1->file != art2->file)
      return False;

    if (art1->base_file != art2->base_file)
      return False;

    if (art1->parent != art2->parent)
      return False;

    if ((art1->children != art2->children) ||
	((num_children = artStructNumChildren(art1)) !=
	 artStructNumChildren(art2)) ||
	(art1->children &&
	 memcmp((void *)art1->children, (void *)art2->children,
		(num_children + 1) * sizeof(*art1->children))))
      return False;

#undef ARTSTRCMP

    return True;
}