Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

test_array.c

This program shows how to use the array API


#include <stdio.h>
#include <string.h>
#include "array.h"


#define ARRAY_SIZE   5
#define BUFFER_SIZE  32


/* -------------------------------------------------------------- */
/*                                                                */
/*                       Custom definitions                       */
/*                     ----------------------                     */
/*                                                                */
/* FIRST: Define tha user data ans all functions used to manipu-  */
/*        -te these type of data.                                 */
/*                                                                */
/* -------------------------------------------------------------- */





/* -------------------------------------------------------------- */
/* User defined data (that will appear in the array)              */
/* -------------------------------------------------------------- */

struct s_data {
                 char *s;
                 int  size;
              };

/* -------------------------------------------------------------- */
/* User defined function used to copy data from/to the array      */
/*                                                                */
/*        WARNING: The **FIRST** element is the SOURCE.           */
/*                 The **SECOND** element is the DESTINATION.     */
/* -------------------------------------------------------------- */

int copy_data(void *src, void *dest)
{
  struct s_data *from, *to;

  from = (struct s_data*)src;
  to   = (struct s_data*)dest;

  if (to->s != NULL) { free(to->s); }

  to->s = (char*)malloc((strlen(from->s)+1)*sizeof(char));
  if (to->s == NULL) { return 1; }

  strcpy (to->s, from->s);
  to->size = strlen(to->s);
  
  return 0;
}

/* -------------------------------------------------------------- */
/* User defined function used to remove an element from the array */
/* -------------------------------------------------------------- */

int remove_data(void *target)
{
  struct s_data *location;

  location = (struct s_data*)target;
  if (location->s != NULL) { free (location->s); location->s = NULL; }

  location->size = 0;

  return 0;
}

/* -------------------------------------------------------------- */
/* User defined function used to compare two elements of the      */
/* array                                                          */
/* -------------------------------------------------------------- */

int compare(void *d1, void *d2)
{
  struct s_data *data1, *data2;

  data1 = (struct s_data*)d1;
  data2 = (struct s_data*)d2;

  if (strcmp (data1->s, data2->s) <  0) { return -1; }
  if (strcmp (data1->s, data2->s) == 0) { return 0; }
  if (strcmp (data1->s, data2->s) >  0) { return 1; }

  return -2;
}

/* -------------------------------------------------------------- */
/* User defined function that returns a pointer to the element    */
/* which index is 'idx'.                                          */
/* -------------------------------------------------------------- */

void* get_index(void *start, int idx)
{
  struct s_data *s;

  s = (struct s_data*)start;

  return (void*)(s+idx);
}

/* -------------------------------------------------------------- */
/* User defined function used to initialize a data                */
/* -------------------------------------------------------------- */

int init_data (void *data)
{
  struct s_data *d;

  d = (struct s_data*)data;
  d->s    = NULL;
  d->size = 0;

  return 0;
}



/* -------------------------------------------------------------- */
/*                                                                */
/*                   End of custom definitions                    */
/*                                                                */
/* -------------------------------------------------------------- */











/* -------------------------------------------------------------- */
/* Main entry point for the test program                          */
/* -------------------------------------------------------------- */

int main (int argc, char *argv[])
{
  struct s_data    datas[ARRAY_SIZE], data;
  Array            my_array;
  char             buffer[BUFFER_SIZE];
  int              i;


  /* ------------------------------------------------------------ */
  /* Initialize the array                                         */
  /* ------------------------------------------------------------ */

  if (array_init (
                   &my_array,
                   (void*)datas,
                   ARRAY_SIZE,
                   copy_data,
                   remove_data,
                   compare,
                   get_index,
                   init_data
                ) == -1)
                {
                  fprintf (stderr, "\nERROR: Can not allocate memory!\n");
                  return 1;
                }

  /* ------------------------------------------------------------ */
  /* Initialize my array (this may be done by the user)           */
  /* This is not required by the array librairy, it depends on    */
  /* what the user wants to do.                                   */
  /* ------------------------------------------------------------ */

  array_init_element (&my_array);

  /* ------------------------------------------------------------ */
  /* Put data into the array                                      */
  /* ------------------------------------------------------------ */

  fprintf (stdout, "\nPutting data into the array");
  fprintf (stdout, "\n---------------------------");
  fprintf (stdout, "\n\n");
  fflush  (stdout);


  i = 1;
  sprintf (buffer, "%d", i);
  data.s    = buffer;
  data.size = strlen(data.s);


  while (array_add(&my_array, (void*)&data) != -1)
  {
    fprintf (stdout, "[%d:%s] ", array_number_of_elements(my_array), data.s); fflush (stdout);
    i *= 10;
    sprintf (buffer, "%d", i);
    data.s    = buffer;
    data.size = strlen(data.s);
  }

  fprintf (stdout, "\n"); fflush (stdout);

  /* ------------------------------------------------------------ */
  /* Print array's content to check that everything is OK         */
  /* ------------------------------------------------------------ */

  fprintf (stdout, "\nPrinting the entire array");
  fprintf (stdout, "\n-------------------------");
  fprintf (stdout, "\n\n");
  fflush  (stdout);


  for (i=0; i<ARRAY_SIZE; i++)
  { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

  fprintf (stdout, "\n"); fflush (stdout);

  /* ------------------------------------------------------------ */
  /* Remove some elements                                         */
  /* ------------------------------------------------------------ */

  fprintf (stdout, "\nRemoving elements from the array");
  fprintf (stdout, "\n--------------------------------");
  fprintf (stdout, "\n\n");
  fflush  (stdout);

  /* ----> */

     fprintf (stdout, "Removing element which index is 1 (%d elements):\n", array_number_of_elements(my_array));
     if (array_rem (&my_array, 1) == -1)
     {
       fprintf (stderr, "\nERROR: Can not remove the element which index is 1!\n");
       return 1;
     }

     for (i=0; i<array_number_of_elements(my_array); i++)
     { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }
 
     fprintf (stdout, "\n\n"); fflush (stdout);

  /* ----> */

     fprintf (stdout, "Removing element which index is 0 (%d elements):\n", array_number_of_elements(my_array));
     if (array_rem (&my_array, 0) == -1)
     {
       fprintf (stderr, "\nERROR: Can not remove the element which index is 0!\n");
       return 1;
     }

     for (i=0; i<array_number_of_elements(my_array); i++)
     { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

     fprintf (stdout, "\n\n"); fflush (stdout);

  /* ----> */

     fprintf (stdout, "Removing element which index is 2 (%d elements):\n", array_number_of_elements(my_array));
     if (array_rem (&my_array, 2) == -1)
     {
       fprintf (stderr, "\nERROR: Can not remove the element which index is 2!\n");
       return 1;
     }

     for (i=0; i<array_number_of_elements(my_array); i++)
     { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

     fprintf (stdout, "\n\n"); fflush (stdout);

  /* ----> */

     fprintf (stdout, "Removing element which index is 0 (%d elements):\n", array_number_of_elements(my_array));
     if (array_rem (&my_array, 0) == -1)
     {
       fprintf (stderr, "\nERROR: Can not remove the element which index is 0!\n");
       return 1;
     }

     for (i=0; i<array_number_of_elements(my_array); i++)
     { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

     fprintf (stdout, "\n\n"); fflush (stdout);

  /* ----> */

     fprintf (stdout, "Removing element which index is 0 (%d elements):\n", array_number_of_elements(my_array));
     if (array_rem (&my_array, 0) == -1)
     {
       fprintf (stderr, "\nERROR: Can not remove the element which index is 0!\n");
       return 1;
     }

     for (i=0; i<array_number_of_elements(my_array); i++)
     { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

     fprintf (stdout, "\n\n"); fflush (stdout);

  /* ------------------------------------------------------------ */
  /* Put data into the array                                      */
  /* ------------------------------------------------------------ */

  fprintf (stdout, "\nPutting data into the array");
  fprintf (stdout, "\n---------------------------");
  fprintf (stdout, "\n\n");
  fflush  (stdout);


  i = 1;
  sprintf (buffer, "%d", i);
  data.s    = buffer;
  data.size = strlen(data.s);


  while (array_add(&my_array, (void*)&data) != -1)
  {
    fprintf (stdout, "[%d:%s] ", array_number_of_elements(my_array), data.s); fflush (stdout);
    i *= 10;
    sprintf (buffer, "%d", i);
    data.s    = buffer;
    data.size = strlen(data.s);
  }

  fprintf (stdout, "\n"); fflush (stdout);

  /* ------------------------------------------------------------ */
  /* Insertion test                                               */
  /* ------------------------------------------------------------ */

  fprintf (stdout, "\nInserting data into the array");
  fprintf (stdout, "\n-----------------------------");
  fprintf (stdout, "\n\n");
  fflush  (stdout);

  /* ---->                                                                     */
  /* ----> Frist remove one element, so we make room for the new element <---- */
  /* ---->                                                                     */

     fprintf (stdout, "Removing element which index is 1 (%d elements):\n", array_number_of_elements(my_array));
     if (array_rem (&my_array, 1) == -1)
     {
       fprintf (stderr, "\nERROR: Can not remove the element which index is 1!\n");
       return 1;
     }

     for (i=0; i<array_number_of_elements(my_array); i++)
     { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

     fprintf (stdout, "\n\n"); fflush (stdout);

    /* ----> */
  
       fprintf (stdout, "inserting data (1) at index 0\n");
  
       i = 1;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);

       if (array_insert (&my_array, 0, (void*)&data) == -1)
       {
         fprintf (stderr, "\nERROR: Can not insert element at index 0!\n");
         return 1;
       }

       for (i=0; i<array_number_of_elements(my_array); i++)
       { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

       fprintf (stdout, "\n\n"); fflush (stdout);

  /* ---->                                                                     */
  /* ----> Frist remove one element, so we make room for the new element <---- */
  /* ---->                                                                     */

     fprintf (stdout, "Removing element which index is 1 (%d elements):\n", array_number_of_elements(my_array));
     if (array_rem (&my_array, 1) == -1)
     {
       fprintf (stderr, "\nERROR: Can not remove the element which index is 1!\n");
       return 1;
     }

     for (i=0; i<array_number_of_elements(my_array); i++)
     { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

     fprintf (stdout, "\n\n"); fflush (stdout);

    /* ----> */

       fprintf (stdout, "inserting data (15) at index 1\n");

       i = 15;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);

       if (array_insert (&my_array, 1, (void*)&data) == -1)
       {
         fprintf (stderr, "\nERROR: Can not insert element at index 0!\n");
         return 1;
       }

       for (i=0; i<array_number_of_elements(my_array); i++)
       { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

       fprintf (stdout, "\n\n"); fflush (stdout);

  /* ---->                                                                     */
  /* ----> Frist remove one element, so we make room for the new element <---- */
  /* ---->                                                                     */

     fprintf (stdout, "Removing element which index is 1 (%d elements):\n", array_number_of_elements(my_array));
     if (array_rem (&my_array, 1) == -1)
     {
       fprintf (stderr, "\nERROR: Can not remove the element which index is 1!\n");
       return 1;
     }

     for (i=0; i<array_number_of_elements(my_array); i++)
     { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

     fprintf (stdout, "\n\n"); fflush (stdout);

    /* ----> */

       fprintf (stdout, "inserting data (155) at index 3\n");

       i = 155;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);

       if (array_insert (&my_array, 3, (void*)&data) == -1)
       {
         fprintf (stderr, "\nERROR: Can not insert element at index 0!\n");
         return 1;
       }

       for (i=0; i<array_number_of_elements(my_array); i++)
       { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

       fprintf (stdout, "\n\n"); fflush (stdout);

  /* ---->                                                                     */
  /* ----> Frist remove one element, so we make room for the new element <---- */
  /* ---->                                                                     */

     fprintf (stdout, "Removing element which index is 1 (%d elements):\n", array_number_of_elements(my_array));
     if (array_rem (&my_array, 1) == -1)
     {
       fprintf (stderr, "\nERROR: Can not remove the element which index is 1!\n");
       return 1;
     }

     for (i=0; i<array_number_of_elements(my_array); i++)
     { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

     fprintf (stdout, "\n\n"); fflush (stdout);

    /* ----> */

       fprintf (stdout, "inserting data (157) at index 2\n");

       i = 157;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);

       if (array_insert (&my_array, 2, (void*)&data) == -1)
       {
         fprintf (stderr, "\nERROR: Can not insert element at index 0!\n");
         return 1;
       }

       for (i=0; i<array_number_of_elements(my_array); i++)
       { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

       fprintf (stdout, "\n\n"); fflush (stdout);

  /* ------------------------------------------------------------ */
  /* remove elements whitch indexes are 1,2,3                     */
  /* ------------------------------------------------------------ */

  fprintf (stdout, "\nRemoving a list of elements (elements index 1,2,3)");
  fprintf (stdout, "\n--------------------------------------------------");
  fprintf (stdout, "\n\n");
  fflush  (stdout);


  if (array_schedule_for_rem (&my_array, 1) == -1)
  {
    fprintf (stderr, "\nERROR: error while scheduling element [1] for removing!\n");
    return 1;
  }

  if (array_schedule_for_rem (&my_array, 2) == -1)
  {
    fprintf (stderr, "\nERROR: error while scheduling element [1] for removing!\n");
    return 1;
  }

  if (array_schedule_for_rem (&my_array, 3) == -1)
  {
    fprintf (stderr, "\nERROR: error while scheduling element [1] for removing!\n");
    return 1;
  }

  if (array_rem_all (&my_array) == -1)
  {
    fprintf (stderr, "\nERROR: error while removing scheduled elements!\n");
    return 1;
  }

  for (i=0; i<array_number_of_elements(my_array); i++)
  { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

  fprintf (stdout, "\n\n"); fflush (stdout);

  /* ------------------------------------------------------------ */
  /* Add three elements                                           */
  /* ------------------------------------------------------------ */

  fprintf (stdout, "\nAdding 3 elements (for more tests");
  fprintf (stdout, "\n---------------------------------");
  fprintf (stdout, "\n\n");
  fflush  (stdout);

    /* ----> */

       fprintf (stdout, "inserting data (157) at index 1\n");

       i = 157;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);

       if (array_insert (&my_array, 1, (void*)&data) == -1)
       {
         fprintf (stderr, "\nERROR: Can not insert element at index 0!\n");
         return 1;
       }

       for (i=0; i<array_number_of_elements(my_array); i++)
       { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

       fprintf (stdout, "\n\n"); fflush (stdout);

    /* ----> */

       fprintf (stdout, "inserting data (1571) at index 1\n");

       i = 1571;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);

       if (array_insert (&my_array, 1, (void*)&data) == -1)
       {
         fprintf (stderr, "\nERROR: Can not insert element at index 0!\n");
         return 1;
       }

       for (i=0; i<array_number_of_elements(my_array); i++)
       { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

       fprintf (stdout, "\n\n"); fflush (stdout);

    /* ----> */

       fprintf (stdout, "inserting data (1572) at index 1\n");

       i = 1572;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);

       if (array_insert (&my_array, 1, (void*)&data) == -1)
       {
         fprintf (stderr, "\nERROR: Can not insert element at index 0!\n");
         return 1;
       }

       for (i=0; i<array_number_of_elements(my_array); i++)
       { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

       fprintf (stdout, "\n\n"); fflush (stdout);

  /* ------------------------------------------------------------ */
  /* Searching for an element                                     */
  /* ------------------------------------------------------------ */

       fprintf (stdout, "\nSearcing for elements (for more tests");
       fprintf (stdout, "\n-------------------------------------");
       fprintf (stdout, "\n\n");
       fflush  (stdout);

       fprintf (stdout, "Searching for element 1572\n"); fflush (stdout);

       i = 1572;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);
 
       i = array_find (&my_array, &data);
       if (i == -1)
       { fprintf (stdout, "NOT found"); fflush (stdout); }
       else
       { fprintf (stdout, "found at index %d", i); fflush (stdout); }

       fprintf (stdout, "\n\n"); fflush (stdout);

    /* ----> */

       fprintf (stdout, "Searching for element 123\n"); fflush (stdout);

       i = 123;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);

       i = array_find (&my_array, &data);
       if (i == -1)
       { fprintf (stdout, "NOT found"); fflush (stdout); }
       else
       { fprintf (stdout, "found at index %d", i); fflush (stdout); }

       fprintf (stdout, "\n\n"); fflush (stdout);

    /* ----> */

       fprintf (stdout, "Searching for element 10000\n"); fflush (stdout);

       i = 10000;
       sprintf (buffer, "%d", i);
       data.s    = buffer;
       data.size = strlen(data.s);

       i = array_find (&my_array, &data);
       if (i == -1)
       { fprintf (stdout, "NOT found"); fflush (stdout); }
       else
       { fprintf (stdout, "found at index %d", i); fflush (stdout); }

       fprintf (stdout, "\n\n"); fflush (stdout);


  /* ------------------------------------------------------------ */
  /* remove elements whitch indexes are 1,2,3                     */
  /* ------------------------------------------------------------ */

  fprintf (stdout, "\nRemoving a list of elements (elements index 0,1,2,3,4)");
  fprintf (stdout, "\n------------------------------------------------------");
  fprintf (stdout, "\n\n");
  fflush  (stdout);


  if (array_schedule_for_rem (&my_array, 0) == -1)
  {
    fprintf (stderr, "\nERROR: error while scheduling element [0] for removing!\n");
    return 1;
  }

  if (array_schedule_for_rem (&my_array, 1) == -1)
  {
    fprintf (stderr, "\nERROR: error while scheduling element [1] for removing!\n");
    return 1;
  }

  if (array_schedule_for_rem (&my_array, 2) == -1)
  {
    fprintf (stderr, "\nERROR: error while scheduling element [2] for removing!\n");
    return 1;
  }

  if (array_schedule_for_rem (&my_array, 3) == -1)
  {
    fprintf (stderr, "\nERROR: error while scheduling element [3] for removing!\n");
    return 1;
  }

  if (array_schedule_for_rem (&my_array, 4) == -1)
  {
    fprintf (stderr, "\nERROR: error while scheduling element [4] for removing!\n");
    return 1;
  }

  if (array_rem_all (&my_array) == -1)
  {
    fprintf (stderr, "\nERROR: error while removing scheduled elements!\n");
    return 1;
  }

  for (i=0; i<array_number_of_elements(my_array); i++)
  { if (datas[i].s != NULL) { fprintf (stdout, "%s ", datas[i].s); fflush (stdout); } }

  fprintf (stdout, "\n\n"); fflush (stdout);

  /* ------------------------------------------------------------ */
  /* delete all allocated memory and exit                         */
  /* ------------------------------------------------------------ */

  array_free (&my_array);
  return 0;
}

Generated on Thu Apr 3 16:23:40 2003 for Common_C_libraries by doxygen1.3-rc1