Blame common/set.c

Packit 6bd9ab
/*
Packit 6bd9ab
   set.c - set functions
Packit 6bd9ab
   This file is part of the nss-pam-ldapd library.
Packit 6bd9ab
Packit 6bd9ab
   Copyright (C) 2008, 2009, 2010, 2012 Arthur de Jong
Packit 6bd9ab
Packit 6bd9ab
   This library is free software; you can redistribute it and/or
Packit 6bd9ab
   modify it under the terms of the GNU Lesser General Public
Packit 6bd9ab
   License as published by the Free Software Foundation; either
Packit 6bd9ab
   version 2.1 of the License, or (at your option) any later version.
Packit 6bd9ab
Packit 6bd9ab
   This library is distributed in the hope that it will be useful,
Packit 6bd9ab
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6bd9ab
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6bd9ab
   Lesser General Public License for more details.
Packit 6bd9ab
Packit 6bd9ab
   You should have received a copy of the GNU Lesser General Public
Packit 6bd9ab
   License along with this library; if not, write to the Free Software
Packit 6bd9ab
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6bd9ab
   02110-1301 USA
Packit 6bd9ab
*/
Packit 6bd9ab
Packit 6bd9ab
#include "config.h"
Packit 6bd9ab
Packit 6bd9ab
#include <stdio.h>
Packit 6bd9ab
#include <stdlib.h>
Packit 6bd9ab
#include <string.h>
Packit 6bd9ab
#include <strings.h>
Packit 6bd9ab
Packit 6bd9ab
#include "set.h"
Packit 6bd9ab
#include "dict.h"
Packit 6bd9ab
Packit 6bd9ab
/*
Packit 6bd9ab
   The SET object is just a DICT which is passed around. The value
Packit 6bd9ab
   for each entry in the dict is just the pointer to the dict.
Packit 6bd9ab
   Another API is provided to give it a more set-like interface.
Packit 6bd9ab
*/
Packit 6bd9ab
Packit 6bd9ab
SET *set_new(void)
Packit 6bd9ab
{
Packit 6bd9ab
  return (SET *)dict_new();
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
int set_add(SET *set, const char *value)
Packit 6bd9ab
{
Packit 6bd9ab
  return dict_put((DICT *)set, value, set);
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
char *set_pop(SET *set)
Packit 6bd9ab
{
Packit 6bd9ab
  const char *key;
Packit 6bd9ab
  char *value;
Packit 6bd9ab
  key = dict_getany((DICT *)set);
Packit 6bd9ab
  if (key == NULL)
Packit 6bd9ab
    return NULL; /* no more entries in set */
Packit 6bd9ab
  /* remove the entry from the dict and return a copy */
Packit 6bd9ab
  value = strdup(key);
Packit 6bd9ab
  dict_put((DICT *)set, key, NULL);
Packit 6bd9ab
  return value;
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
int set_contains(SET *set, const char *value)
Packit 6bd9ab
{
Packit 6bd9ab
  return dict_get((DICT *)set, value) != NULL;
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
void set_free(SET *set)
Packit 6bd9ab
{
Packit 6bd9ab
  dict_free((DICT *)set);
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
const char **set_tolist(SET *set)
Packit 6bd9ab
{
Packit 6bd9ab
  return dict_keys((DICT *)set);
Packit 6bd9ab
}