Logo Search packages:      
Sourcecode: dcgui version File versions  Download package

str_array.c

/* dc_gui2 - a GTK+2 GUI for DCTC
 * Copyright (C) 2002 Eric Prevoteau
 *
 * str_array.c: Copyright (C) Eric Prevoteau <www@a2pb.gotdns.org>
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
/*
$Id: str_array.c,v 1.3 2003/12/26 14:35:04 uid68112 Exp $
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

/***********************************************************/
/* get the position of the given string in the given array */
/***********************************************************/
/* output: -1 if not found, else index */
/***************************************/
static int str_array_idx(const GPtrArray *array, const char *str)
{
      int i;

      if((array==NULL)||(str==NULL))
            return -1;

      for(i=0;i<array->len;i++)
      {
            char *t;

            t=g_ptr_array_index(array,i);
            if(t&&(!strcmp(t,str)))
            {
                  return i;
            }
      }
      return -1;
}

/***********************************************/
/* add the given string inside the given array */
/***********************************************/
GPtrArray *str_array_add(GPtrArray *array, char *str)
{
      if(array==NULL)
      {
            array=g_ptr_array_new();
            g_ptr_array_add(array,strdup(str));
      }
      else
      {
            /* check if str is not still in the array */
            if(str_array_idx(array,str)==-1)
            {
                  g_ptr_array_add(array,strdup(str));
            }
      }
      return array;
}

/**************************************************/
/* remove the given string inside the given array */
/**************************************************/
GPtrArray *str_array_del(GPtrArray *array, char *str)
{
      if(array!=NULL)
      {
            int i;

            i=str_array_idx(array,str);

            if(i!=-1)
            {
                  char *t;

                  t=g_ptr_array_remove_index_fast(array,i);
                  if(t!=NULL)
                        free(t);
            }
      }
      return array;
}

/*************************************************/
/* check if the given string is inside the array */
/*************************************************/
/* output: FALSE=no, TRUE=yes */
/******************************/
int str_array_is_inside(GPtrArray *array, char *str)
{
      return (str_array_idx(array,str)!=-1);
}

/********************************************************/
/* delete all entries of the array and the array itself */
/********************************************************/
void str_array_destroy(GPtrArray *array)
{
      int i;

      if(array==NULL)
            return;

      for(i=0;i<array->len;i++)
      {
            char *t;

            t=g_ptr_array_remove_index_fast(array,i);
            if(t!=NULL)
                  free(t);
      }
      g_ptr_array_free(array,TRUE);
}


Generated by  Doxygen 1.6.0   Back to index