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

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

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

  while (l--)
    {
26 27
      if (!sp)
	*buf++ = ' ';
28 29 30 31 32
      if (buf > end)
	{
	  strcpy(buf, "...");
	  return;
	}
33 34 35 36

      if (way)
	buf += bsprintf(buf, "(%d,%d)", *z >> 16, *z & 0xffff);
      else
37
	buf += bsprintf(buf, "%R", *z);
38

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

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

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

struct adata *
int_set_del(struct linpool *pool, struct adata *list, u32 val)
{
  struct adata *res;
  u32 *l, *k;
73
  unsigned int i;
74 75 76 77 78 79 80

  if (!int_set_contains(list, val))
    return list;

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

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

  return res;
}