wstdio.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/StringBuilder.h>
  8. #include <AK/Types.h>
  9. #include <bits/stdio_file_implementation.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <wchar.h>
  13. static_assert(AssertSize<wchar_t, sizeof(u32)>());
  14. extern "C" {
  15. wint_t fgetwc(FILE* stream)
  16. {
  17. VERIFY(stream);
  18. Array<u8, 4> underlying;
  19. auto underlying_bytes = underlying.span();
  20. size_t encoded_length = 0;
  21. size_t bytes_read = 0;
  22. do {
  23. size_t nread = fread(underlying_bytes.offset_pointer(bytes_read), 1, 1, stream);
  24. if (nread != 1) {
  25. errno = EILSEQ;
  26. return WEOF;
  27. }
  28. ++bytes_read;
  29. if (bytes_read == 1) {
  30. if (underlying[0] >> 7 == 0) {
  31. encoded_length = 1;
  32. } else if (underlying[0] >> 5 == 6) {
  33. encoded_length = 2;
  34. } else if (underlying[0] >> 4 == 0xe) {
  35. encoded_length = 3;
  36. } else if (underlying[0] >> 3 == 0x1e) {
  37. encoded_length = 4;
  38. } else {
  39. errno = EILSEQ;
  40. return WEOF;
  41. }
  42. }
  43. } while (bytes_read < encoded_length);
  44. wchar_t code_point;
  45. auto read_bytes = mbrtowc(&code_point, bit_cast<char const*>(underlying.data()), encoded_length, nullptr);
  46. VERIFY(read_bytes == encoded_length);
  47. return code_point;
  48. }
  49. wint_t getwc(FILE* stream)
  50. {
  51. return fgetwc(stream);
  52. }
  53. wint_t getwchar()
  54. {
  55. return getwc(stdin);
  56. }
  57. }