const char jb_socket_set_rcs[] = "$Id: jb_socket_set.c,v 1.1 2003/05/23 17:02:20 lionel Exp $";
/*********************************************************************
 *
 * File        :  $Source: /ds6/cvs/privoxy/src/jb_socket_set.c,v $
 *
 * Purpose     :  Declares functions to handle sets of sockets
 *
 * Copyright   :  Written by and Copyright (C) 2002 Lionel Elie Mamane
 *                <lionel@mamane.lu>
 *
 *                This program is free software; you can redistribute it
 *                and/or modify it under the terms of the GNU General
 *                Public License as published by the Free Software
 *                Foundation; either version 2 of the License, or (at
 *                your option) any later version.
 *
 *                This program is distributed in the hope that it will
 *                be useful, but WITHOUT ANY WARRANTY; without even the
 *                implied warranty of MERCHANTABILITY or FITNESS FOR A
 *                PARTICULAR PURPOSE.  See the GNU General Public
 *                License for more details.
 *
 *                The GNU General Public License should be included with
 *                this file.  If not, you can view it at
 *                http://www.gnu.org/copyleft/gpl.html
 *                or write to the Free Software Foundation, Inc., 59
 *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Revisions   :
 *    $Log: jb_socket_set.c,v $
 *    Revision 1.1  2003/05/23 17:02:20  lionel
 *    Socket set implementation
 *
 *
 *********************************************************************/

#include "jb_socket_set.h"
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "project.h"

/*********************************************************************
 *
 * Function    :  jb_socket_set_add
 *
 * Description :  Add a socket to the set
 *
 * Parameters  :
 *          0  :  l = the set to add to
 *          1  :  the elemen to add to the set
 *
 * Returns     :  0 on success
 *                non-0 on failure
 *
 *********************************************************************/

int jb_socket_set_add(jb_socket_set *l, jb_socket e)
{
   if (l->data==NULL)
   {
      l->size=2;
      l->data=malloc(l->size * sizeof(jb_socket));
      if (l->data==NULL)
      {
         l->size = 0;
         return -1;
      }
      l->occupied=0;
   }
   l->data[(l->occupied)++] = e;
   if (l->occupied == l->size)
   {
      jb_socket *new_data;
      l->size *= 2;
      new_data = realloc(l->data,l->size * sizeof(jb_socket));
      if (new_data == NULL)
      {
         /* Not enough memory to continue. Cancel changes. */
         l->data[--(l->occupied)] = JB_INVALID_SOCKET;
         l->size /= 2;
         return -1;
      }
      l->data = new_data;
   }
   l->data[l->occupied] = JB_INVALID_SOCKET;
   return 0;
}

/*********************************************************************
 *
 * Function    :  destroy_jb_socket_set
 *
 * Description :  Unallocate memory allocated to a socket set
 *
 * Parameters  :
 *          0  :  l = the set to unallocate
 *
 * Returns     :  nothing
 *
 *********************************************************************/
void destroy_jb_socket_set(jb_socket_set *l)
{
   free(l->data);
   init_jb_socket_set(l);
}
/*********************************************************************
 *
 * Function    :  is_empty_jb_socket_set
 *
 * Description :  Test wheter a set is empty
 *
 * Parameters  :
 *          0  :  l = the set to test
 *
 * Returns     :  0             = false if set has a head,
 *                anything else = true if set is nil
 *
 *********************************************************************/
int is_nil_jb_socket_set(jb_socket_set *l)
{
   return (l->occupied == 0);
}

/*********************************************************************
 *
 * Function    :  init_jb_socket_set
 *
 * Description :  Init a set to empty
 *
 * Parameters  :
 *          0  :  l = the set to init
 *
 *********************************************************************/
void init_jb_socket_set(jb_socket_set *l)
{
   l->data=NULL;
   l->size=0;
   l->occupied=0;
}

/*********************************************************************
 *
 * Function    :  jb_socket_set_iteration_begin
 *
 * Description :  Return an iterator on the set
 *
 * Parameters  :
 *          0  :  l = the set
 *
 *********************************************************************/
jb_socket_set_iterate_state jb_socket_set_iteration_begin(jb_socket_set *l)
{
   return l->data;
}

/*********************************************************************
 *
 * Function    :  jb_socket_set_iteration_next
 *
 * Description :  Return value pointed to by iterator and step
 *                iterator to next position
 *
 * Parameters  :
 *          0  :  s = the iterator
 *
 *********************************************************************/
jb_socket jb_socket_set_iteration_next(jb_socket_set_iterate_state*s)
{
   return(*((*s)++));
}

/*
  Local Variables:
  tab-width: 3
  c-file-style: "stroustrup"
  c-basic-offset: 3
  indent-tabs-mode: nil
  end:
*/
