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

test_dstring.c

This file shows how to use the entire API for dynamic string manipulation.

#include <stdio.h>
#include <stdlib.h>
#include "dstring.h"


#define MAX_BUFF 2048
#define MAX_LOOP 10000
#define DS_INIT  100


int main()
{
  dstring ds;
  char    buff[MAX_BUFF], *s;
  int     size, loop, i, j;


  /* --------------------------------------------------- */
  /* Initialize the dynamic string                       */
  /* --------------------------------------------------- */

  fprintf (stdout, "\nInitialize dynamic string");
  fflush (stdout);

  if (dstring_init (&ds, DS_INIT) == 1)
  {
    fprintf (stderr, "\n\nCan not allocate memory!\n\n");
    return 1;
  }
  
  /* --------------------------------------------------- */
  /* Fill the dynamic string with data                   */
  /* --------------------------------------------------- */

  fprintf (stdout, "\nFill dynamic string with data");
  fflush (stdout);

  loop = rand() % MAX_LOOP;
  
  for (i=0; i<loop; i++)
  {
    size = rand() % MAX_BUFF;

    for (j=0; j<size; j++) { buff[j] = (int)((int)'A' + (rand() % 26)); }

    if (dstring_add (&ds, buff, size) == 1)
    {
      fprintf (stderr, "\n\nCan not allocate memory!\n\n");
      return 1;
    }
  }

  /* --------------------------------------------------- */
  /* Extract data                                        */
  /* --------------------------------------------------- */

  s = dstring_get_data (&ds, &size);
  if (s == NULL)
  {
    fprintf (stderr, "\n\nCan not allocate memory!\n\n");
    return 1;
  }

  fprintf (stdout, "\n\nData:\n\n%s\n\n", s);
  fflush (stdout);

  /* --------------------------------------------------- */
  /* Free allocated all memory                           */
  /* --------------------------------------------------- */

  fprintf (stdout, "\nFree all allocated memory");
  fflush (stdout);

  dstring_free (&ds);
  free (s);


  fprintf (stdout, "\n\n");
  return 0;

}

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