home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / i18nv104.zip / SAMPLE / ICONV / CONVERT.C < prev    next >
C/C++ Source or Header  |  1996-02-13  |  2KB  |  67 lines

  1. #include <errno.h>
  2. #include <iconv.h>
  3. #include <stdio.h>
  4.  
  5. main(int argc, char** argv)
  6. {
  7.     iconv_t cd;
  8.     size_t inbytesleft, outbytesleft;
  9.     char inbuf[BUFSIZ], outbuf[BUFSIZ];
  10.     char *inptr, *outptr;
  11.     int bytesread, inputlen;
  12.     int rc = 0;
  13.  
  14.  
  15.     cd = iconv_open(argv[2], argv[1]);
  16.     fprintf(stderr, "iconv_open returned %d\n", cd);
  17.     fprintf(stderr, "errno = %d\n", errno);
  18.  
  19.     if (!cd || (cd == (iconv_t)-1)) {
  20.         exit(1);
  21.     }
  22.  
  23.     inbytesleft = 0;
  24.  
  25.     while (bytesread = _read(0, inbuf + inbytesleft, BUFSIZ - inbytesleft)) {
  26.  
  27.         inptr = inbuf;
  28.         outptr = outbuf;
  29.         outbytesleft = BUFSIZ;
  30.         inbytesleft += bytesread;
  31.         inputlen = inbytesleft;
  32.  
  33.         errno = 0;
  34.         rc = iconv(cd, &inptr, &inbytesleft, &outptr, &outbytesleft);
  35.         fprintf(stderr, "iconv returned %d\n", rc);
  36.         fprintf(stderr, "errno = %d\n", errno);
  37.  
  38.         _write(1, outbuf, BUFSIZ - outbytesleft);
  39.  
  40.         if (rc == -1) {
  41.  
  42.             if (errno == EILSEQ) { /* Illegal sequence */
  43.                 break;
  44.             }
  45.             else if (errno == EINVAL) { /* Truncated input */
  46.         /* Do nothing. Should get handled on next interation. */    
  47.             }
  48.             else if (errno == E2BIG) { /* Output buffer too small. */
  49.         /* Do nothing. Should get handled on next interation. */    
  50.             }
  51.  
  52.             /* Copy any unconverted input to front of buffer. */
  53.             memcpy(inbuf, inptr, inbytesleft);
  54.         }
  55.  
  56.     } /* while */
  57.  
  58.     errno = 0;
  59.     rc = iconv_close(cd);
  60.     fprintf(stderr, "iconv_close returned %d\n", rc);
  61.     fprintf(stderr, "errno = %d\n", errno);
  62.  
  63.  
  64.     exit(rc);
  65. }
  66.  
  67.