ReactOS 0.4.16-dev-852-gcfcc8d8
rewind.cpp
Go to the documentation of this file.
1//
2// rewind.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines rewind(), which rewinds a stream.
7//
10
11
12
13// Rewinds a stream back to the beginning, if the stream supports seeking. The
14// stream is flushed and errors are cleared before the rewind.
15static void __cdecl _rewind_internal(FILE* const public_stream, __crt_cached_ptd_host& ptd)
16{
17 __crt_stdio_stream const stream(public_stream);
18
20
21 int const fh = _fileno(stream.public_stream());
22
23 _lock_file(stream.public_stream());
24 __try
25 {
26 // Flush the streeam, reset the error state, and seek back to the
27 // beginning:
28 __acrt_stdio_flush_nolock(stream.public_stream(), ptd);
29 // If the stream is opened in update mode and is currently in use for reading,
30 // the buffer must be abandoned to ensure consistency when transitioning from
31 // reading to writing.
32 // __acrt_stdio_flush_nolock will not reset the buffer when _IOWRITE flag
33 // is not set.
35
36
37 stream.unset_flags(_IOERROR | _IOEOF);
38 _osfile_safe(fh) &= ~(FEOFLAG);
39
40 if (stream.has_all_of(_IOUPDATE))
41 {
42 stream.unset_flags(_IOREAD | _IOWRITE);
43 }
44
45 if (_lseek_internal(fh, 0, 0, ptd) == -1)
46 {
47 stream.set_flags(_IOERROR);
48 }
49 }
51 {
52 _unlock_file(stream.public_stream());
53 }
55}
56
57extern "C" void __cdecl rewind(FILE* const public_stream)
58{
59 __crt_cached_ptd_host ptd;
60 _rewind_internal(public_stream, ptd);
61}
#define EINVAL
Definition: acclib.h:90
#define __cdecl
Definition: accygwin.h:79
_Check_return_opt_ long __cdecl _lseek_internal(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin, _Inout_ __crt_cached_ptd_host &_Ptd)
#define _UCRT_VALIDATE_RETURN_VOID(ptd, expr, errorcode)
void __cdecl __acrt_stdio_reset_buffer(__crt_stdio_stream const stream)
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
int __cdecl __acrt_stdio_flush_nolock(FILE *const public_stream, __crt_cached_ptd_host &ptd)
Definition: fflush.cpp:222
_Check_return_ _CRTIMP int __cdecl _fileno(_In_ FILE *_File)
_CRTIMP void __cdecl _unlock_file(_Inout_ FILE *_File)
#define _IOEOF
Definition: stdio.h:132
#define _IOREAD
Definition: stdio.h:124
_CRTIMP void __cdecl _lock_file(_Inout_ FILE *_File)
#define __try
Definition: pseh2_64.h:172
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174
static void __cdecl _rewind_internal(FILE *const public_stream, __crt_cached_ptd_host &ptd)
Definition: rewind.cpp:15
void __cdecl rewind(FILE *const public_stream)
Definition: rewind.cpp:57
#define _osfile_safe(i)
Definition: internal.h:78
Definition: parse.h:23