|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_stdio.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| _Success_(return != 0)
|
| static Character* __cdecl common_fgets(
|
| _Out_writes_z_(count) Character* const string,
|
| int const count,
|
| __crt_stdio_stream const stream
|
| ) throw()
|
| {
|
| typedef __acrt_stdio_char_traits<Character> stdio_traits;
|
|
|
| _VALIDATE_RETURN(string != nullptr || count == 0, EINVAL, nullptr);
|
| _VALIDATE_RETURN(count >= 0, EINVAL, nullptr);
|
| _VALIDATE_RETURN(stream.valid(), EINVAL, nullptr);
|
|
|
| if (count == 0)
|
| return nullptr;
|
|
|
| Character* return_value = nullptr;
|
|
|
| _lock_file(stream.public_stream());
|
| __try
|
| {
|
| if (!stdio_traits::validate_stream_is_ansi_if_required(stream.public_stream()))
|
| __leave;
|
|
|
|
|
|
|
| Character* it = string;
|
| for (int i = 1; i != count; ++i)
|
| {
|
| int const c = stdio_traits::getc_nolock(stream.public_stream());
|
| if (c == stdio_traits::eof)
|
| {
|
|
|
|
|
|
|
|
|
| if (it == string)
|
| __leave;
|
|
|
|
|
| break;
|
| }
|
|
|
|
|
| *it++ = static_cast<Character>(c);
|
| if (static_cast<Character>(c) == '\n')
|
| break;
|
| }
|
|
|
| *it = '\0';
|
| return_value = string;
|
| }
|
| __finally
|
| {
|
| _unlock_file(stream.public_stream());
|
| }
|
|
|
| return return_value;
|
| }
|
|
|
|
|
|
|
| extern "C" char* __cdecl fgets(
|
| char* const string,
|
| int const count,
|
| FILE* const stream
|
| )
|
| {
|
| return common_fgets(string, count, __crt_stdio_stream(stream));
|
| }
|
|
|
| extern "C" wchar_t* __cdecl fgetws(
|
| wchar_t* const string,
|
| int const count,
|
| FILE* const stream
|
| )
|
| {
|
| return common_fgets(string, count, __crt_stdio_stream(stream));
|
| }
|
|
|