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

test_url_encoding.c

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

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



int main(int argc, char *argv[])
{
  char  *encoded, *decoded, *to_decode;

  if (argc != 2)
  {
    fprintf (stderr, "\nurl_encoding.test <string to URL encode>\n");
    return 1;
  }

  encoded = url_escape(argv[1]);
  if (encoded == NULL)
  {
    fprintf (stderr, "\nurl_escape() failed\n");
    return 1;
  }

  fprintf (stdout, "\n%s URL encoded is [%s]", argv[1], encoded);

  to_decode = (char*)malloc((strlen(encoded)+2)*sizeof(char));
  if (to_decode == NULL)
  {
    fprintf (stderr, "\nmalloc() failed\n");
    return 1;
  }

  sprintf (to_decode, "?%s", encoded);

  decoded = url_unescape(to_decode, strlen(to_decode));
  if (decoded == NULL)
  {
    fprintf (stderr, "\nurl_unescape() failed\n");
    return 1;
  }

  fprintf (stdout, "\nDecoding [%s] => [%s]\n\n", to_decode, decoded);

  free (decoded);
  free (to_decode);
  free (encoded);
  return 0;
}


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