Statistics
| Branch: | Revision:

iof-bird-daemon / nest / a-set.c @ fbde6c39

History | View | Annotate | Download (1.77 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, int way, 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

    
34
      if (way)
35
        buf += bsprintf(buf, "(%d,%d)", *z >> 16, *z & 0xffff);
36
      else
37
        buf += bsprintf(buf, "%d.%d.%d.%d",
38
                        (*z >> 24) & 0xff, (*z >> 16) & 0xff,
39
                        (*z >> 8) & 0xff, *z & 0xff);
40

    
41
      z++;
42
      sp = 0;
43
    }
44
  *buf = 0;
45
}
46

    
47
struct adata *
48
int_set_add(struct linpool *pool, struct adata *list, u32 val)
49
{
50
  int len = list ? list->length : 0;
51
  struct adata *res = lp_alloc(pool, len + sizeof(struct adata) + 4);
52
  res->length = len + 4;
53
  * (u32 *) res->data = val;
54
  if (list)
55
    memcpy((char *) res->data + 4, list->data, list->length);
56
  return res;
57
}
58

    
59
int
60
int_set_contains(struct adata *list, u32 val)
61
{
62
  u32 *l = (u32 *) list->data;
63
  unsigned int i;
64
  for (i=0; i<list->length/4; i++)
65
    if (*l++ == val)
66
      return 1;
67
  return 0;
68
}
69

    
70
struct adata *
71
int_set_del(struct linpool *pool, struct adata *list, u32 val)
72
{
73
  struct adata *res;
74
  u32 *l, *k;
75
  unsigned int i;
76

    
77
  if (!int_set_contains(list, val))
78
    return list;
79

    
80
  res = lp_alloc(pool, list->length + sizeof(struct adata) - 4);
81
  res->length = list->length-4;
82

    
83
  l = (u32 *) list->data;
84
  k = (u32 *) res->data;
85
  for (i=0; i<list->length/4; i++)
86
    if (l[i] != val)
87
      *k++ = l[i];
88

    
89
  return res;
90
}