@@ -1170,14 +1170,15 @@ struct MCMemoryMappedFileHandle: public MCMemoryFileHandle
11701170
11711171struct MCStdioFileHandle : public MCSystemFileHandle
11721172{
1173- MCStdioFileHandle (MCWinSysHandle p_handle, bool p_is_pipe = false )
1173+ MCStdioFileHandle (MCWinSysHandle p_handle, bool p_is_pipe = false , bool p_buffered_write = false )
11741174 {
11751175 m_handle = p_handle;
11761176 m_is_eof = false ;
11771177 m_is_pipe = p_is_pipe;
11781178 m_putback = -1 ;
11791179
11801180 /* The write buffer starts off unallocated. */
1181+ m_should_buffer_writes = p_buffered_write;
11811182 m_write_buffer = nullptr ;
11821183 m_write_buffer_frontier = 0 ;
11831184 }
@@ -1570,12 +1571,27 @@ struct MCStdioFileHandle: public MCSystemFileHandle
15701571 return 0 ;
15711572 }
15721573
1573- /* As the API call returns the size in two pieces, combine them into a single 64-bit value. */
1574+ /* As the API call returns the size in two pieces, combine them into
1575+ * a single 64-bit value. */
15741576 uint64_t t_file_size =
15751577 (uint64_t )t_low_word | (uint64_t )t_high_word << 32 ;
15761578
1577- /* Increase the offset by any currently (unwritten) buffered data. */
1578- t_file_size += GetBufferedWriteSize ();
1579+ /* If there is buffered data to write, then we must take the current
1580+ * file pointer into account. */
1581+ uint32_t t_buffered_write_size =
1582+ GetBufferedWriteSize ();
1583+ if (t_buffered_write_size != 0 )
1584+ {
1585+ /* Fetch the current file pointer - including any additional offset
1586+ * pending data to write. */
1587+ int64_t t_file_pointer =
1588+ Tell ();
1589+
1590+ /* The file size is either the current file pointer, or the current
1591+ * actual file size adjusted by the size of pending data. */
1592+ t_file_size = MCMax (t_file_size + t_buffered_write_size,
1593+ t_file_pointer);
1594+ }
15791595
15801596 /* Return the final, computed, file size. */
15811597 return t_file_size;
@@ -1593,7 +1609,8 @@ struct MCStdioFileHandle: public MCSystemFileHandle
15931609 {
15941610 /* If there is no write buffer, or the write buffer is empty then there
15951611 * is nothing to do. */
1596- if (m_write_buffer == nullptr ||
1612+ if (!m_should_buffer_writes ||
1613+ m_write_buffer == nullptr ||
15971614 m_write_buffer_frontier == 0 )
15981615 {
15991616 return true ;
@@ -1663,6 +1680,7 @@ struct MCStdioFileHandle: public MCSystemFileHandle
16631680 /* The maximum single write size which will be buffered. */
16641681 kMaximumSingleBufferedWriteSize = 16384 ,
16651682 };
1683+ bool m_should_buffer_writes;
16661684 byte_t *m_write_buffer;
16671685 uint32_t m_write_buffer_frontier;
16681686};
@@ -2343,7 +2361,7 @@ struct MCWindowsDesktop: public MCSystemInterface, public MCWindowsSystemService
23432361 omode = GENERIC_READ;
23442362 createmode = OPEN_EXISTING;
23452363 }
2346- if (p_mode== kMCOpenFileModeWrite || p_mode == kMCOpenFileModeCreate )
2364+ if (p_mode == kMCOpenFileModeBufferedWrite || p_mode == kMCOpenFileModeWrite || p_mode == kMCOpenFileModeCreate )
23472365 {
23482366 omode = GENERIC_WRITE;
23492367 createmode = CREATE_ALWAYS;
@@ -2452,7 +2470,9 @@ struct MCWindowsDesktop: public MCSystemInterface, public MCWindowsSystemService
24522470 }
24532471 else
24542472 {
2455- t_handle = new (nothrow) MCStdioFileHandle ((MCWinSysHandle)t_file_handle);
2473+ t_handle = new (nothrow) MCStdioFileHandle ((MCWinSysHandle)t_file_handle,
2474+ false ,
2475+ p_mode == kMCOpenFileModeBufferedWrite );
24562476 t_close_file_handler = t_handle == NULL ;
24572477 }
24582478
0 commit comments