wstdio.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/BitCast.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Types.h>
  10. #include <bits/stdio_file_implementation.h>
  11. #include <errno.h>
  12. #include <stdio.h>
  13. #include <wchar.h>
  14. static_assert(AssertSize<wchar_t, sizeof(u32)>());
  15. extern "C" {
  16. int fwide(FILE*, int mode)
  17. {
  18. // Nope Nope Nope.
  19. return mode;
  20. }
  21. wint_t fgetwc(FILE* stream)
  22. {
  23. VERIFY(stream);
  24. Array<u8, 4> underlying;
  25. auto underlying_bytes = underlying.span();
  26. size_t encoded_length = 0;
  27. size_t bytes_read = 0;
  28. do {
  29. size_t nread = fread(underlying_bytes.offset_pointer(bytes_read), 1, 1, stream);
  30. if (nread != 1) {
  31. errno = EILSEQ;
  32. return WEOF;
  33. }
  34. ++bytes_read;
  35. if (bytes_read == 1) {
  36. if (underlying[0] >> 7 == 0) {
  37. encoded_length = 1;
  38. } else if (underlying[0] >> 5 == 6) {
  39. encoded_length = 2;
  40. } else if (underlying[0] >> 4 == 0xe) {
  41. encoded_length = 3;
  42. } else if (underlying[0] >> 3 == 0x1e) {
  43. encoded_length = 4;
  44. } else {
  45. errno = EILSEQ;
  46. return WEOF;
  47. }
  48. }
  49. } while (bytes_read < encoded_length);
  50. wchar_t code_point;
  51. auto read_bytes = mbrtowc(&code_point, bit_cast<char const*>(underlying.data()), encoded_length, nullptr);
  52. VERIFY(read_bytes == encoded_length);
  53. return code_point;
  54. }
  55. wint_t getwc(FILE* stream)
  56. {
  57. return fgetwc(stream);
  58. }
  59. wint_t getwchar()
  60. {
  61. return getwc(stdin);
  62. }
  63. wint_t fputwc(wchar_t wc, FILE* stream)
  64. {
  65. VERIFY(stream);
  66. // Negative wide chars are weird
  67. if constexpr (IsSigned<wchar_t>) {
  68. if (wc < 0) {
  69. errno = EILSEQ;
  70. return WEOF;
  71. }
  72. }
  73. StringBuilder sb;
  74. sb.append_code_point(static_cast<u32>(wc));
  75. auto bytes = sb.string_view().bytes();
  76. ScopedFileLock lock(stream);
  77. size_t nwritten = stream->write(bytes.data(), bytes.size());
  78. if (nwritten < bytes.size())
  79. return WEOF;
  80. return wc;
  81. }
  82. wint_t putwc(wchar_t wc, FILE* stream)
  83. {
  84. return fputwc(wc, stream);
  85. }
  86. wint_t putwchar(wchar_t wc)
  87. {
  88. return fputwc(wc, stdout);
  89. }
  90. }