mirror of
https://github.com/reactos/reactos.git
synced 2025-06-12 08:18:31 +00:00
[UCRT] Import Microsoft.Windows.SDK.CRTSource version 10.0.22621.3
Imported from https://www.nuget.org/packages/Microsoft.Windows.SDK.CRTSource/10.0.22621.3 License: MIT
This commit is contained in:
parent
f1b60c66f0
commit
04e0dc4a7a
568 changed files with 115483 additions and 0 deletions
92
sdk/lib/ucrt/stdio/ungetc.cpp
Normal file
92
sdk/lib/ucrt/stdio/ungetc.cpp
Normal file
|
@ -0,0 +1,92 @@
|
|||
//
|
||||
// ungetc.cpp
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// Defines ungetc(), which pushes a character back into a stream.
|
||||
//
|
||||
#include <corecrt_internal_stdio.h>
|
||||
|
||||
|
||||
|
||||
// Pushes a character ("ungets" it) back into a stream. It is possible to push
|
||||
// back one character. It may not be possible to push back more than one
|
||||
// character in a row. Returns the pushed-back character on success; returns
|
||||
// EOF on failure. Ungetting EOF is expressly forbidden.
|
||||
extern "C" int __cdecl ungetc(int const c, FILE* const stream)
|
||||
{
|
||||
_VALIDATE_RETURN(stream != nullptr, EINVAL, EOF);
|
||||
|
||||
int return_value = EOF;
|
||||
|
||||
_lock_file(stream);
|
||||
__try
|
||||
{
|
||||
return_value = _ungetc_nolock(c, stream);
|
||||
}
|
||||
__finally
|
||||
{
|
||||
_unlock_file(stream);
|
||||
}
|
||||
|
||||
return return_value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
extern "C" int __cdecl _ungetc_nolock(int const c, FILE* public_stream)
|
||||
{
|
||||
__crt_stdio_stream const stream(public_stream);
|
||||
|
||||
_VALIDATE_STREAM_ANSI_RETURN(stream, EINVAL, EOF);
|
||||
|
||||
// Ungetting EOF is expressly forbidden:
|
||||
if (c == EOF)
|
||||
return EOF;
|
||||
|
||||
// The stream must either be open in read-only mode, or must be open in
|
||||
// read-write mode and must not currently be in write mode:
|
||||
bool const is_in_read_only_mode = stream.has_all_of(_IOREAD);
|
||||
bool const is_in_rw_write_mode = stream.has_all_of(_IOUPDATE | _IOWRITE);
|
||||
|
||||
if (!is_in_read_only_mode && !is_in_rw_write_mode)
|
||||
return EOF;
|
||||
|
||||
// If the stream is currently unbuffered, buffer it:
|
||||
if (stream->_base == nullptr)
|
||||
__acrt_stdio_allocate_buffer_nolock(stream.public_stream());
|
||||
|
||||
// At this point, we know that _base is not null, since the file is buffered.
|
||||
|
||||
if (stream->_ptr == stream->_base)
|
||||
{
|
||||
// If we've already buffered a pushed-back character, there's no room for
|
||||
// another, and there's nothing we can do:
|
||||
if (stream->_cnt)
|
||||
return EOF;
|
||||
|
||||
++stream->_ptr;
|
||||
}
|
||||
|
||||
// If the stream is string-backed (and not file-backed), do not modify the buffer:
|
||||
if (stream.is_string_backed())
|
||||
{
|
||||
--stream->_ptr;
|
||||
if (*stream->_ptr != static_cast<char>(c))
|
||||
{
|
||||
++stream->_ptr;
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
--stream->_ptr;
|
||||
*stream->_ptr = static_cast<char>(c);
|
||||
}
|
||||
|
||||
++stream->_cnt;
|
||||
stream.unset_flags(_IOEOF);
|
||||
stream.set_flags(_IOREAD);
|
||||
|
||||
return c & 0xff;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue