Skip to content

Commit baa22e2

Browse files
authored
Merge pull request #1 from Coderx7/Coderx7-patch-fix-unclosed-handle
Fix unclosed file handle when open mode is not ios_base::out
2 parents 0ddc618 + 7b52ac8 commit baa22e2

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

Release/include/cpprest/streams.h

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,22 @@ class basic_ostream
140140
}
141141

142142
/// <summary>
143-
/// Close the stream, preventing further write operations.
143+
/// Close the stream, preventing further write [or read] operations.
144144
/// </summary>
145-
pplx::task<void> close() const
145+
/// <param name="mode">The opening mode of the file</param>
146+
pplx::task<void> close(std::ios_base::openmode mode = std::ios_base::out) const
146147
{
147-
return is_valid() ? helper()->m_buffer.close(std::ios_base::out) : pplx::task_from_result();
148+
return is_valid() ? helper()->m_buffer.close(mode) : pplx::task_from_result();
148149
}
149150

150151
/// <summary>
151-
/// Close the stream with exception, preventing further write operations.
152+
/// Close the stream with exception, preventing further write [or read] operations.
152153
/// </summary>
153154
/// <param name="eptr">Pointer to the exception.</param>
154-
pplx::task<void> close(std::exception_ptr eptr) const
155+
/// <param name="mode">The opening mode of the file</param>
156+
pplx::task<void> close(std::exception_ptr eptr, std::ios_base::openmode mode = std::ios_base::out) const
155157
{
156-
return is_valid() ? helper()->m_buffer.close(std::ios_base::out, eptr) : pplx::task_from_result();
158+
return is_valid() ? helper()->m_buffer.close(mode, eptr) : pplx::task_from_result();
157159
}
158160

159161
/// <summary>
@@ -325,36 +327,39 @@ class basic_ostream
325327
}
326328

327329
/// <summary>
328-
/// Seeks to the specified write position.
330+
/// Seeks to the specified write[or read] position.
329331
/// </summary>
330332
/// <param name="pos">An offset relative to the beginning of the stream.</param>
333+
/// <param name="mode">The opening mode of the file</param>
331334
/// <returns>The new position in the stream.</returns>
332-
pos_type seek(pos_type pos) const
335+
pos_type seek(pos_type pos, std::ios_base::openmode mode = std::ios_base::out) const
333336
{
334337
_verify_and_throw(details::_out_stream_msg);
335-
return helper()->m_buffer.seekpos(pos, std::ios_base::out);
338+
return helper()->m_buffer.seekpos(pos, mode);
336339
}
337340

338341
/// <summary>
339-
/// Seeks to the specified write position.
342+
/// Seeks to the specified write[or read] position.
340343
/// </summary>
341-
/// <param name="off">An offset relative to the beginning, current write position, or the end of the stream.</param>
344+
/// <param name="off">An offset relative to the beginning, current write[or read] position, or the end of the stream.</param>
342345
/// <param name="way">The starting point (beginning, current, end) for the seek.</param>
346+
/// <param name="mode">The opening mode of the file</param>
343347
/// <returns>The new position in the stream.</returns>
344-
pos_type seek(off_type off, std::ios_base::seekdir way) const
348+
pos_type seek(off_type off, std::ios_base::seekdir way, std::ios_base::openmode mode = std::ios_base::out) const
345349
{
346350
_verify_and_throw(details::_out_stream_msg);
347-
return helper()->m_buffer.seekoff(off, way, std::ios_base::out);
351+
return helper()->m_buffer.seekoff(off, way, mode);
348352
}
349353

350354
/// <summary>
351-
/// Get the current write position, i.e. the offset from the beginning of the stream.
355+
/// Get the current write[or read] position, i.e. the offset from the beginning of the stream.
352356
/// </summary>
353-
/// <returns>The current write position.</returns>
354-
pos_type tell() const
357+
/// <param name="mode">The opening mode of the file</param>
358+
/// <returns>The current write[or read] position.</returns>
359+
pos_type tell(std::ios_base::openmode mode = std::ios_base::out) const
355360
{
356361
_verify_and_throw(details::_out_stream_msg);
357-
return helper()->m_buffer.getpos(std::ios_base::out);
362+
return helper()->m_buffer.getpos(mode);
358363
}
359364

360365
/// <summary>
@@ -379,7 +384,18 @@ class basic_ostream
379384
/// Test whether the stream is open for writing.
380385
/// </summary>
381386
/// <returns><c>true</c> if the stream is open for writing, <c>false</c> otherwise.</returns>
382-
bool is_open() const { return is_valid() && m_helper->m_buffer.can_write(); }
387+
bool is_open(std::ios_base::openmode mode = std::ios_base::out) const
388+
{
389+
bool is_able = false;
390+
if (mode == std::ios_base::out)
391+
is_able = m_helper->m_buffer.can_write();
392+
else if (mode == std::ios_base::in)
393+
is_able = m_helper->m_buffer.can_read();
394+
else
395+
is_able = m_helper->m_buffer.can_read() && m_helper->m_buffer.can_write();
396+
397+
return is_valid() && is_able;
398+
}
383399

384400
/// <summary>
385401
/// Get the underlying stream buffer.

0 commit comments

Comments
 (0)