_wfwrite.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include "_stdio.h"
  8. #include <wchar.h>
  9. #ifndef __UCLIBC_HAS_WCHAR__
  10. #error wide function when no wide support!
  11. #endif
  12. size_t attribute_hidden _wstdio_fwrite(const wchar_t *__restrict ws, size_t n,
  13. register FILE *__restrict stream)
  14. {
  15. size_t r, count;
  16. char buf[64];
  17. const wchar_t *pw;
  18. __STDIO_STREAM_VALIDATE(stream);
  19. #ifdef __STDIO_BUFFERS
  20. if (__STDIO_STREAM_IS_FAKE_VSWPRINTF(stream)) {
  21. /* We know buffer is wchar aligned for fake streams. */
  22. count = (((wchar_t *)(stream->__bufend))
  23. - ((wchar_t *)(stream->__bufpos)));
  24. if (count > n) {
  25. count = n;
  26. }
  27. if (count) {
  28. wmemcpy((wchar_t *)(stream->__bufpos), ws, count);
  29. stream->__bufpos = (unsigned char *)(((wchar_t *)(stream->__bufpos)) + count);
  30. }
  31. __STDIO_STREAM_VALIDATE(stream);
  32. return n;
  33. }
  34. #endif
  35. count = 0;
  36. if (__STDIO_STREAM_IS_WIDE_WRITING(stream)
  37. || !__STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_WIDE)
  38. ) {
  39. pw = ws;
  40. while (n > count) {
  41. r = wcsnrtombs(buf, &pw, n-count, sizeof(buf), &stream->__state);
  42. if (r != ((size_t) -1)) { /* No encoding errors */
  43. if (!r) {
  44. ++r; /* 0 is returned when nul is reached. */
  45. pw = ws + count + r; /* pw was set to NULL, so correct. */
  46. }
  47. if (__stdio_fwrite((const unsigned char *)buf, r, stream) == r) {
  48. count = pw - ws;
  49. continue;
  50. }
  51. }
  52. break;
  53. }
  54. /* Note: The count is incorrect if 0 < __stdio_fwrite return < r!!! */
  55. }
  56. __STDIO_STREAM_VALIDATE(stream);
  57. return count;
  58. }