Advertisement
rfmonk

multibyte_conversion_ex.c

Jul 23rd, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. int
  2. file_mbsrtowcs (int input, int output)
  3. {
  4.     /* Note the use of MB_LEN_MAX.
  5.      * MB_CUR_MAX cannot portably be used here */
  6.     char buffer[BUFSIZE + MB_LEN_MAX];
  7.     mbstate_t state;
  8.     int filled = 0;
  9.     int eof = 0;
  10.  
  11.     /* Initialize the state  */
  12.     memset (&state, '\0, sizeof (state));
  13.  
  14.    while (!eof)
  15.    {
  16.        ssize_t nread;
  17.        ssize_t nwrite;
  18.        char *inp = buffer;
  19.        wchar_t outbuf[BUFSIZ];
  20.        wchar_t *outp = outbuf;
  21.  
  22.        /* Fill up the buffer from the input file.  */
  23.        nread = read (input, buffer + filled, BUFSIZ);
  24.        if (nread < 0)
  25.        {
  26.            perror("read");
  27.            return 0;
  28.        }
  29.        /* If we reach end of file, make a note to read no more.  */
  30.        if (nread == 0)
  31.            eof = 1;
  32.  
  33.        /* filled is now the number of bytes in buffer.  */
  34.        filled += nread;
  35.  
  36.        /* Convert those bytes to wide characters--as many as we can */
  37.        while (1)
  38.        {
  39.            size_t thislen = mbrtowc (outp, inp, filled, &state);
  40.            /* stop converting at invalid character;
  41.             * this can mean we have read just the first part
  42.             * of a valid character.  */
  43.            if (thislen == (size_t) -1)
  44.                break;
  45.            /* we want to handle embedded NUL bytes
  46.             * but the return value is 0. Correct this.  */
  47.            if (thislen == 0)
  48.                thislen = 1;
  49.            /* Advance past this character. */
  50.            inp += thislen;
  51.            filled -= thislen;
  52.            ++outp;
  53.        }
  54.  
  55.        /* Write the wide characters we just made.  */
  56.        nwrite = write(output, outbuf,
  57.                (outp - outbuf) * sizeof (wchar_t));
  58.        if (nwrite < 0)
  59.        {
  60.            perror ("write");
  61.            return 0;
  62.        }
  63.  
  64.        /* see if we have a real invalid character. */
  65.        if ((eol && filled > 0) || filled >= MB_CUR_MAX)
  66.        {
  67.            error (0, 0, "invalid multibyte character");
  68.            return 0;
  69.        }
  70.  
  71.        /* if any characters must be carried forward,
  72.         * put them at the beginning of the buffer. */
  73.  
  74.        if (filled > 0)
  75.            memmove (buffer, inp, filled);
  76.    }
  77.    return 1;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement