Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include <stdlib.h>
  2. #include <wchar.h>
  3. #include "local.h"
  4.  
  5. size_t
  6. _DEFUN (_wcstombs_r, (reent, s, pwcs, n, state),
  7.         struct _reent *r    _AND        
  8.         char          *__restrict s    _AND
  9.         const wchar_t *__restrict pwcs _AND
  10.         size_t         n    _AND
  11.         mbstate_t     *state)
  12. {
  13.   char *ptr = s;
  14.   size_t max = n;
  15.   char buff[8];
  16.   int i, bytes, num_to_copy;
  17.  
  18.   if (s == NULL)
  19.     {
  20.       size_t num_bytes = 0;
  21.       while (*pwcs != 0)
  22.         {
  23.           bytes = __wctomb (r, buff, *pwcs++, __locale_charset (), state);
  24.           if (bytes == -1)
  25.             return -1;
  26.           num_bytes += bytes;
  27.         }
  28.       return num_bytes;
  29.     }
  30.   else
  31.     {
  32.       while (n > 0)
  33.         {
  34.           bytes = __wctomb (r, buff, *pwcs, __locale_charset (), state);
  35.           if (bytes == -1)
  36.             return -1;
  37.           num_to_copy = (n > bytes ? bytes : (int)n);
  38.           for (i = 0; i < num_to_copy; ++i)
  39.             *ptr++ = buff[i];
  40.          
  41.           if (*pwcs == 0x00)
  42.             return ptr - s - (n >= bytes);
  43.           ++pwcs;
  44.           n -= num_to_copy;
  45.         }
  46.       return max;
  47.     }
  48. }
  49.