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

test_cyclic_buffer.c

This file shows how to use the function create_cyclic_buffer().

#include <stdio.h>
#include "buffers.h"



int main (int argc, char *argv[])
{
  cyclic_buffer   my_buffer;
  unsigned long   bsize;
  unsigned long   i;
  void*           e;

  if (argc != 2)
  {
    fprintf (stderr, "\nUsage: cyclic_buffer.test <buffer size>\n");
    return 1;
  }

  bsize = (unsigned long)atoi(argv[1]);


  /* =========================================================== */
  /* Create the cyclic buffer                                    */
  /* =========================================================== */

  my_buffer = create_cyclic_buffer(bsize);
  if (my_buffer == NULL)
  {
    fprintf (stderr, "\nCan not allocate memory!\n");
    return 1;
  }

  /* =========================================================== */
  /* Add and display buffer's content                            */
  /* =========================================================== */

  for (i=0; i<(3*bsize); i++)
  {
    if (cyclic_buffer_add_element (my_buffer, (void*)&i, sizeof(unsigned long)) == NULL)
    {
      fprintf (stderr, "\nCan not allocate memory!\n");
      return 1;
    }

    rewind_cyclic_buffer (my_buffer);
    fprintf (stdout, "\n-> [%lu to %lu - %lu] ", cyclic_buffer_get_start(my_buffer), cyclic_buffer_get_stop(my_buffer), cyclic_buffer_get_current_size(my_buffer));
    while ((e = cyclic_buffer_get_element(my_buffer)) != NULL)
    { fprintf (stdout, "<%lu> ",*((unsigned long*)e)); }
    fprintf (stdout, "\n");
  }

  /* =========================================================== */
  /* Destroy the cyclic buffer                                   */
  /* =========================================================== */

  destroy_cyclic_buffer (my_buffer);

  return 0;
}

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