Statistics
| Branch: | Revision:

iof-bird-daemon / nest / a-set.c @ 4847a894

History | View | Annotate | Download (1.63 KB)

1
/*
2
 *        BIRD -- Set/Community-list Operations
3
 *
4
 *        (c) 2000 Martin Mares <mj@ucw.cz>
5
 *        (c) 2000 Pavel Machek <pavel@ucw.cz>
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 */
9

    
10
#include "nest/bird.h"
11
#include "nest/route.h"
12
#include "nest/attrs.h"
13
#include "lib/resource.h"
14
#include "lib/string.h"
15

    
16
void
17
int_set_format(struct adata *set, byte *buf, unsigned int size)
18
{
19
  u32 *z = (u32 *) set->data;
20
  int l = set->length / 4;
21
  int sp = 1;
22
  byte *end = buf + size - 16;
23

    
24
  while (l--)
25
    {
26
      if (!sp)
27
        *buf++ = ' ';
28
      if (buf > end)
29
        {
30
          strcpy(buf, "...");
31
          return;
32
        }
33
      buf += bsprintf(buf, "(%d,%d)", *z >> 16, *z & 0xffff);
34
      z++;
35
      sp = 0;
36
    }
37
  *buf = 0;
38
}
39

    
40
struct adata *
41
int_set_add(struct linpool *pool, struct adata *list, u32 val)
42
{
43
  int len = list ? list->length : 0;
44
  struct adata *res = lp_alloc(pool, len + sizeof(struct adata) + 4);
45
  res->length = len + 4;
46
  * (u32 *) res->data = val;
47
  if (list)
48
    memcpy((char *) res->data + 4, list->data, list->length);
49
  return res;
50
}
51

    
52
int
53
int_set_contains(struct adata *list, u32 val)
54
{
55
  u32 *l = (u32 *) list->data;
56
  unsigned int i;
57
  for (i=0; i<list->length/4; i++)
58
    if (*l++ == val)
59
      return 1;
60
  return 0;
61
}
62

    
63
struct adata *
64
int_set_del(struct linpool *pool, struct adata *list, u32 val)
65
{
66
  struct adata *res;
67
  u32 *l, *k;
68
  unsigned int i;
69

    
70
  if (!int_set_contains(list, val))
71
    return list;
72

    
73
  res = lp_alloc(pool, list->length + sizeof(struct adata) - 4);
74
  res->length = list->length-4;
75

    
76
  l = (u32 *) list->data;
77
  k = (u32 *) res->data;
78
  for (i=0; i<list->length/4; i++)
79
    if (l[i] != val)
80
      *k++ = l[i];
81

    
82
  return res;
83
}