39 lines · cpp
1//===-- Implementation of mbrtowc -----------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/wchar/mbrtowc.h"10 11#include "hdr/types/mbstate_t.h"12#include "hdr/types/size_t.h"13#include "hdr/types/wchar_t.h"14#include "src/__support/common.h"15#include "src/__support/libc_errno.h"16#include "src/__support/macros/config.h"17#include "src/__support/wchar/mbrtowc.h"18#include "src/__support/wchar/mbstate.h"19 20namespace LIBC_NAMESPACE_DECL {21 22LLVM_LIBC_FUNCTION(size_t, mbrtowc,23 (wchar_t *__restrict pwc, const char *__restrict s, size_t n,24 mbstate_t *__restrict ps)) {25 static internal::mbstate internal_mbstate;26 auto ret = internal::mbrtowc(pwc, s, n,27 ps == nullptr28 ? &internal_mbstate29 : reinterpret_cast<internal::mbstate *>(ps));30 if (!ret.has_value()) {31 // Encoding failure32 libc_errno = ret.error();33 return -1;34 }35 return ret.value();36}37 38} // namespace LIBC_NAMESPACE_DECL39