Back to index

glibc  2.9
Functions | Variables
iconv_prog.h File Reference
#include <stdio.h>
#include <charmap.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int charmap_conversion (const char *from_code, struct charmap_t *from_charmap, const char *to_code, struct charmap_t *to_charmap, int argc, int remaining, char *argv[], const char *output_file)

Variables

int verbose
int omit_invalid

Function Documentation

int charmap_conversion ( const char *  from_code,
struct charmap_t from_charmap,
const char *  to_code,
struct charmap_t to_charmap,
int  argc,
int  remaining,
char *  argv[],
const char *  output_file 
)

Definition at line 95 of file iconv_charmap.c.

{
  struct convtable *cvtbl;
  int status = EXIT_SUCCESS;

  /* We have three different cases to handle:

     - both, from_charmap and to_charmap, are available.  This means we
       can assume that the symbolic names match and use them to create
       the mapping.

     - only from_charmap is available.  In this case we can only hope that
       the symbolic names used are of the <Uxxxx> form in which case we
       can use a UCS4->"to_code" iconv() conversion for the second step.

     - only to_charmap is available.  This is similar, only that we would
       use iconv() for the "to_code"->UCS4 conversion.

       We first create a table which maps input bytes into output bytes.
       Once this is done we can handle all three of the cases above
       equally.  */
  if (from_charmap != NULL)
    {
      if (to_charmap == NULL)
       cvtbl = use_from_charmap (from_charmap, to_code);
      else
       cvtbl = use_both_charmaps (from_charmap, to_charmap);
    }
  else
    {
      assert (to_charmap != NULL);
      cvtbl = use_to_charmap (from_code, to_charmap);
    }

  /* If we couldn't generate a table stop now.  */
  if (cvtbl == NULL)
    return EXIT_FAILURE;

  /* Determine output file.  */
  FILE *output;
  if (output_file != NULL && strcmp (output_file, "-") != 0)
    {
      output = fopen (output_file, "w");
      if (output == NULL)
       error (EXIT_FAILURE, errno, _("cannot open output file"));
    }
  else
    output = stdout;

  /* We can now start the conversion.  */
  if (remaining == argc)
    {
      if (process_file (cvtbl, stdin, output) != 0)
       status = EXIT_FAILURE;
    }
  else
    do
      {
       struct stat st;
       char *addr;
       int fd;

       if (verbose)
         printf ("%s:\n", argv[remaining]);
       if (strcmp (argv[remaining], "-") == 0)
         fd = 0;
       else
         {
           fd = open (argv[remaining], O_RDONLY);

           if (fd == -1)
             {
              error (0, errno, _("cannot open input file `%s'"),
                     argv[remaining]);
              status = EXIT_FAILURE;
              continue;
             }
         }

#ifdef _POSIX_MAPPED_FILES
       /* We have possibilities for reading the input file.  First try
          to mmap() it since this will provide the fastest solution.  */
       if (fstat (fd, &st) == 0
           && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE,
                           fd, 0)) != MAP_FAILED))
         {
           /* Yes, we can use mmap().  The descriptor is not needed
              anymore.  */
           if (close (fd) != 0)
             error (EXIT_FAILURE, errno,
                   _("error while closing input `%s'"), argv[remaining]);

           if (process_block (cvtbl, addr, st.st_size, output) < 0)
             {
              /* Something went wrong.  */
              status = EXIT_FAILURE;

              /* We don't need the input data anymore.  */
              munmap ((void *) addr, st.st_size);

              /* We cannot go on with producing output since it might
                 lead to problem because the last output might leave
                 the output stream in an undefined state.  */
              break;
             }

           /* We don't need the input data anymore.  */
           munmap ((void *) addr, st.st_size);
         }
       else
#endif /* _POSIX_MAPPED_FILES */
         {
           /* Read the file in pieces.  */
           if (process_fd (cvtbl, fd, output) != 0)
             {
              /* Something went wrong.  */
              status = EXIT_FAILURE;

              /* We don't need the input file anymore.  */
              close (fd);

              /* We cannot go on with producing output since it might
                 lead to problem because the last output might leave
                 the output stream in an undefined state.  */
              break;
             }

           /* Now close the file.  */
           close (fd);
         }
      }
    while (++remaining < argc);

  /* All done.  */
  return status;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 108 of file iconv_prog.c.

Definition at line 102 of file iconv_prog.c.