Back to index

glibc  2.9
Classes | Defines | Functions | Variables
tst-iconv5.c File Reference
#include <iconv.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

Go to the source code of this file.

Classes

struct  unalign
struct  convcode

Defines

#define SIZE   256 /* enough room for conversion */
#define SAMPLESTR   "abc"

Functions

int convert (const char *tocode, const char *fromcode, char *inbufp, size_t inbytesleft, char *outbufp, size_t outbytesleft)
int test_unalign (struct convcode *codes, char *str, int len)
int main (int argc, char *argv[])

Variables

int number = (int) sizeof (testcode) / sizeof (struct convcode)

Class Documentation

struct unalign

Definition at line 29 of file tst-iconv5.c.

Class Members
char str1
char str2
struct convcode

Definition at line 35 of file tst-iconv5.c.

Class Members
const char * fromcode
const char * tocode

Define Documentation

#define SAMPLESTR   "abc"

Definition at line 27 of file tst-iconv5.c.

#define SIZE   256 /* enough room for conversion */

Definition at line 26 of file tst-iconv5.c.


Function Documentation

int convert ( const char *  tocode,
const char *  fromcode,
char *  inbufp,
size_t  inbytesleft,
char *  outbufp,
size_t  outbytesleft 
)

Definition at line 54 of file tst-iconv5.c.

{
  iconv_t *ic;
  size_t outbytes = outbytesleft;
  int ret;

  ic = iconv_open (tocode, fromcode);
  if (ic == (iconv_t *) - 1)
    {
      printf ("iconv_open failed: from: %s, to: %s: %s",
             fromcode, tocode, strerror (errno));
      return -1;
    }

  while (inbytesleft > 0)
    {
      ret = iconv (ic, &inbufp, &inbytesleft, &outbufp, &outbytes);
      if (ret == -1)
       {
         printf ("iconv failed: from: %s, to: %s: %s",
                fromcode, tocode, strerror (errno));
         return -1;
       }
    }

  ret = iconv_close (ic);
  if (ret == -1)
    {
      printf ("iconv_close failed: from: %s, to: %s: %s",
             fromcode, tocode, strerror (errno));
      return -1;
    }

  return outbytesleft - outbytes;
}

Here is the call graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 142 of file tst-iconv5.c.

{
  int i;
  int ret = 0;

  for (i = 0; i < number; i++)
    {
      ret = test_unalign (&testcode[i], (char *) SAMPLESTR, sizeof (SAMPLESTR));
      if (ret)
       break;
      printf ("iconv: %s <-> %s: ok\n",
             testcode[i].fromcode, testcode[i].tocode);
    }
  printf ("Succeeded.\n");

  return ret;
}

Here is the call graph for this function:

int test_unalign ( struct convcode codes,
char *  str,
int  len 
)

Definition at line 93 of file tst-iconv5.c.

{
  struct unalign *inbufp, *outbufp;
  char *inbuf, *outbuf;
  size_t inbytesleft, outbytesleft;
  int retlen;

  /* allocating unaligned buffer for both inbuf and outbuf */
  inbufp = (struct unalign *) malloc (sizeof (struct unalign));
  if (!inbufp)
    {
      printf ("no memory available\n");
      exit (1);
    }
  inbuf = inbufp->str2;

  outbufp = (struct unalign *) malloc (sizeof (struct unalign));
  if (!outbufp)
    {
      printf ("no memory available\n");
      exit (1);
    }
  outbuf = outbufp->str2;

  /* first iconv phase */
  memcpy (inbuf, str, len);
  inbytesleft = len;
  outbytesleft = sizeof (struct unalign);
  retlen = convert (codes->tocode, codes->fromcode, inbuf, inbytesleft,
                  outbuf, outbytesleft);
  if (retlen == -1)  /* failed */
    return 1;

  /* second round trip iconv phase */
  memcpy (inbuf, outbuf, retlen);
  inbytesleft = retlen;
  outbytesleft = sizeof (struct unalign);
  retlen = convert (codes->fromcode, codes->tocode, inbuf, inbytesleft,
                  outbuf, outbytesleft);
  if (retlen == -1)  /* failed */
    return 1;

  free (inbufp);
  free (outbufp);

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int number = (int) sizeof (testcode) / sizeof (struct convcode)

Definition at line 51 of file tst-iconv5.c.