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

test_s_split_exact.c

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

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

int main(int argc, char *argv[])
{
  struct fields fd;
  int    n, i, crash, size;
  char   *st;


  crash = 0;

  if (argc == 1) { crash = 1; }
  else {
         if (argc != 3)
         {
           fprintf (stdout, "\nUsage s_split_exact.test string delimitor\n");
           return 1;
         }
       } 

  if (!crash)
  {
    n = s_split_exact(argv[1], argv[2], &fd);
    if (n == -1)
    {
      fprintf (stdout, "\nCan not allocate memory\n");
      return 1;
    }

    for (i=0; i<fd.number_of_fields; i++)
    { fprintf (stdout, "\n-> %s", fd.tabs[i]); }

    free_fields(&fd);

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


  while (1==1)
  {
    size = (rand() % 1024) + 1;
    st = (char*) malloc ((size+1) * sizeof(char));
    if (st == NULL)
    {
      fprintf (stdout, "\nCan not allocate memory for string\n");
      return 1;
    }

    for (i=0; i<size; i++)
    {
      if (rand() % 2) { st[i] = ';'; }
      else { st[i] = 'A' + rand() % 10; }
    }

    st[size] = 0;
    fprintf (stdout, "%d: [%s]\n", size, st); fflush(stdout);

    n = s_split_exact(st, ";", &fd);
    if (n == -1)
    {
      fprintf (stdout, "\nCan not allocate memory for s_split_exact\n");
      return 1;
    }

    for (i=0; i<fd.number_of_fields; i++)
    { fprintf (stdout, "\n-> %s", fd.tabs[i]); }

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

    free_fields(&fd);
    free (st);
  }





  return 0;
}

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