win32: change fputs to fwrite wrapper

Removes mp_puts/mp_fputs and adds mp_fwrite.

In fact I wanted fwrite instead of puts, no need to make it more awkward
with the implicit new lines.

Fixes: fc55f355fc
This commit is contained in:
Kacper Michajłow 2024-03-21 04:38:00 +01:00
parent 6177aa7616
commit c389f9e75e
6 changed files with 25 additions and 18 deletions

View File

@ -309,18 +309,26 @@ static inline HANDLE get_handle(FILE *stream)
return wstream;
}
int mp_fputs(const char *str, FILE *stream)
size_t mp_fwrite(const void *restrict buffer, size_t size, size_t count,
FILE *restrict stream)
{
if (!size || !count)
return 0;
HANDLE wstream = get_handle(stream);
if (mp_check_console(wstream))
return mp_console_fputs(wstream, bstr0(str));
if (mp_check_console(wstream)) {
unsigned char *start = (unsigned char *)buffer;
size_t c = 0;
for (; c < count; ++c) {
if (mp_console_write(wstream, (bstr){start, size}) <= 0)
break;
start += size;
}
return c;
}
return fputs(str, stream);
}
int mp_puts(const char *str)
{
return mp_fputs(str, stdout);
#undef fwrite
return fwrite(buffer, size, count, stream);
}
#if HAVE_UWP

View File

@ -106,8 +106,8 @@ char *mp_to_utf8(void *talloc_ctx, const wchar_t *s);
#include <sys/stat.h>
#include <fcntl.h>
int mp_puts(const char *str);
int mp_fputs(const char *str, FILE *stream);
size_t mp_fwrite(const void *restrict buffer, size_t size, size_t count,
FILE *restrict stream);
int mp_printf(const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
int mp_fprintf(FILE *stream, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
int mp_open(const char *filename, int oflag, ...);
@ -176,8 +176,7 @@ int mp_glob(const char *restrict pattern, int flags,
int (*errfunc)(const char*, int), mp_glob_t *restrict pglob);
void mp_globfree(mp_glob_t *pglob);
#define puts(...) mp_puts(__VA_ARGS__)
#define fputs(...) mp_fputs(__VA_ARGS__)
#define fwrite(...) mp_fwrite(__VA_ARGS__)
#define printf(...) mp_printf(__VA_ARGS__)
#define fprintf(...) mp_fprintf(__VA_ARGS__)
#define open(...) mp_open(__VA_ARGS__)

View File

@ -32,7 +32,7 @@ int mp_console_vfprintf(void *wstream, const char *format, va_list args)
return 0;
}
int mp_console_fputs(void *wstream, bstr str)
int mp_console_write(void *wstream, bstr str)
{
return 0;
}

View File

@ -239,7 +239,7 @@ int mp_console_vfprintf(HANDLE wstream, const char *format, va_list args)
buffers->write_console_buf.len = 0;
bstr_xappend_vasprintf(buffers, &buffers->write_console_buf, format, args);
int ret = mp_console_fputs(wstream, buffers->write_console_buf);
int ret = mp_console_write(wstream, buffers->write_console_buf);
if (free_buf)
talloc_free(buffers);
@ -247,7 +247,7 @@ int mp_console_vfprintf(HANDLE wstream, const char *format, va_list args)
return ret;
}
int mp_console_fputs(HANDLE wstream, bstr str)
int mp_console_write(HANDLE wstream, bstr str)
{
struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key);
bool free_buf = false;

View File

@ -57,7 +57,7 @@ void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height);
// Windows only.
int mp_console_vfprintf(void *wstream, const char *format, va_list args);
int mp_console_fputs(void *wstream, bstr str);
int mp_console_write(void *wstream, bstr str);
bool mp_check_console(void *handle);
/* Windows-only function to attach to the parent process's console */

View File

@ -114,7 +114,7 @@ int mp_msg_find_level(const char *s) {return 0;};
int mp_msg_level(struct mp_log *log) {return 0;};
void mp_msg_set_max_level(struct mp_log *log, int lev) {};
int mp_console_vfprintf(void *wstream, const char *format, va_list args) {return 0;};
int mp_console_fputs(void *wstream, bstr str) {return 0;};
int mp_console_write(void *wstream, bstr str) {return 0;};
bool mp_check_console(void *handle) { return false; };
void mp_set_avdict(AVDictionary **dict, char **kv) {};
struct mp_log *mp_log_new(void *talloc_ctx, struct mp_log *parent,