Add function for flushing file buffers

This commit is contained in:
Vas Crabb 2016-03-01 07:38:14 +11:00
parent a830ea7627
commit ba960afb5f
8 changed files with 114 additions and 4 deletions

View file

@ -625,6 +625,17 @@ int emu_file::puts(const char *s)
}
//-------------------------------------------------
// vfprintf - vfprintf to a text file
//-------------------------------------------------
int emu_file::vprintf(const char *fmt, va_list va)
{
// write the data if we can
return (m_file != nullptr) ? core_vfprintf(m_file, fmt, va) : 0;
}
//-------------------------------------------------
// printf - vfprintf to a text file
//-------------------------------------------------
@ -641,13 +652,14 @@ int CLIB_DECL emu_file::printf(const char *fmt, ...)
//-------------------------------------------------
// mame_vfprintf - vfprintf to a text file
// flush - flush file buffers
//-------------------------------------------------
int emu_file::vprintf(const char *fmt, va_list va)
void emu_file::flush()
{
// write the data if we can
return (m_file != nullptr) ? core_vfprintf(m_file, fmt, va) : 0;
// flush the buffers if we can
if (m_file != nullptr)
core_fflush(m_file);
}

View file

@ -120,6 +120,8 @@ public:
// control
file_error compress(int compress);
// position
int seek(INT64 offset, int whence);
UINT64 tell();
bool eof();
@ -137,6 +139,9 @@ public:
int vprintf(const char *fmt, va_list va);
int printf(const char *fmt, ...) ATTR_PRINTF(2,3);
// buffers
void flush();
private:
bool compressed_file_ready(void);

View file

@ -898,6 +898,16 @@ file_error core_truncate(core_file *f, UINT64 offset)
}
/*-------------------------------------------------
core_truncate - flush write buffer
-------------------------------------------------*/
file_error core_fflush(core_file *f)
{
return osd_fflush(f->file);
}
/***************************************************************************
FILENAME UTILITIES

View file

@ -120,6 +120,9 @@ int CLIB_DECL core_fprintf(core_file *f, const char *fmt, ...) ATTR_PRINTF(2,3);
/* file truncation */
file_error core_truncate(core_file *f, UINT64 offset);
/* flush file buffers */
file_error core_fflush(core_file *f);
/* ----- filename utilities ----- */

View file

@ -202,6 +202,21 @@ file_error osd_openpty(osd_file **file, char *name, size_t name_len);
file_error osd_truncate(osd_file *file, UINT64 offset);
/*-----------------------------------------------------------------------------
osd_fflus: flush file buffers
Parameters:
file - handle to a file previously opened via osd_open
Return value:
a file_error describing any error that occurred while writing to
the file, or FILERR_NONE if no error occurred
-----------------------------------------------------------------------------*/
file_error osd_fflush(osd_file *file);
/*-----------------------------------------------------------------------------
osd_rmfile: deletes a file

View file

@ -128,6 +128,22 @@ file_error osd_truncate(osd_file *file, UINT64 offset)
return FILERR_NONE;
}
//============================================================
// osd_fflush
//============================================================
file_error osd_fflush(osd_file *file)
{
if (!file)
return FILERR_FAILURE;
int result = fflush((FILE *)file);
if (result == EOF)
return FILERR_FAILURE;
return FILERR_NONE;
}
//============================================================
// osd_rmfile

View file

@ -369,6 +369,30 @@ file_error osd_truncate(osd_file *file, UINT64 offset)
}
}
//============================================================
// osd_truncate
//============================================================
file_error osd_fflush(osd_file *file)
{
UINT32 result;
if (!file || !file->handle)
return FILERR_FAILURE;
switch (file->type)
{
case SDLFILE_FILE:
result = fflush(file->handle, offset);
if (result == EOF)
return error_to_file_error(errno);
return FILERR_NONE;
default:
return FILERR_FAILURE;
}
}
//============================================================
// osd_close

View file

@ -281,6 +281,31 @@ file_error osd_truncate(osd_file *file, UINT64 offset)
return FILERR_NONE;
}
//============================================================
// osd_fflush
//============================================================
file_error osd_fflush(osd_file *file)
{
if (!file || !file->handle)
return FILERR_FAILURE;
switch (file->type)
{
case WINFILE_FILE:
// attempt to flush file buffers
if (!FlushFileBuffers(file->handle))
return win_error_to_mame_file_error(GetLastError());
break;
case WINFILE_SOCKET:
return FILERR_FAILURE;
case WINFILE_PTTY:
return FILERR_FAILURE;
}
return FILERR_NONE;
}
//============================================================
// osd_close