mirror of https://github.com/mpv-player/mpv
win32: optimize mp_vfprintf a little
- remove redundant strlen/wcslen - reuse allocated temporary buffers The difference is not big, but it satisfies me to remove those redundancies.
This commit is contained in:
parent
2ee0db4c5d
commit
3372e17d51
18
osdep/io.c
18
osdep/io.c
|
@ -307,8 +307,6 @@ static int mp_vfprintf(FILE *stream, const char *format, va_list args)
|
|||
|
||||
static int mp_vfprintf(FILE *stream, const char *format, va_list args)
|
||||
{
|
||||
int done = 0;
|
||||
|
||||
HANDLE wstream = INVALID_HANDLE_VALUE;
|
||||
|
||||
if (stream == stdout || stream == stderr) {
|
||||
|
@ -316,20 +314,10 @@ static int mp_vfprintf(FILE *stream, const char *format, va_list args)
|
|||
STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
|
||||
}
|
||||
|
||||
if (mp_check_console(wstream)) {
|
||||
size_t len = vsnprintf(NULL, 0, format, args) + 1;
|
||||
char *buf = talloc_array(NULL, char, len);
|
||||
if (mp_check_console(wstream))
|
||||
return mp_write_console_ansi(wstream, format, args);
|
||||
|
||||
if (buf) {
|
||||
done = vsnprintf(buf, len, format, args);
|
||||
mp_write_console_ansi(wstream, buf);
|
||||
}
|
||||
talloc_free(buf);
|
||||
} else {
|
||||
done = vfprintf(stream, format, args);
|
||||
}
|
||||
|
||||
return done;
|
||||
return vfprintf(stream, format, args);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "terminal.h"
|
||||
|
||||
#include "misc/bstr.h"
|
||||
|
||||
void terminal_init(void)
|
||||
{
|
||||
}
|
||||
|
@ -25,8 +27,9 @@ void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height)
|
|||
{
|
||||
}
|
||||
|
||||
void mp_write_console_ansi(void *wstream, char *buf)
|
||||
int mp_write_console_ansi(void *wstream, const char *format, va_list args)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool terminal_try_attach(void)
|
||||
|
|
|
@ -204,6 +204,12 @@ void terminal_setup_getch(struct input_ctx *ictx)
|
|||
}
|
||||
}
|
||||
|
||||
DWORD tmp_buffers_key = FLS_OUT_OF_INDEXES;
|
||||
struct tmp_buffers {
|
||||
bstr write_console_buf;
|
||||
wchar_t *write_console_wbuf;
|
||||
};
|
||||
|
||||
void terminal_uninit(void)
|
||||
{
|
||||
if (running) {
|
||||
|
@ -212,6 +218,7 @@ void terminal_uninit(void)
|
|||
input_ctx = NULL;
|
||||
running = false;
|
||||
}
|
||||
FlsFree(tmp_buffers_key);
|
||||
}
|
||||
|
||||
bool terminal_in_background(void)
|
||||
|
@ -219,14 +226,20 @@ bool terminal_in_background(void)
|
|||
return false;
|
||||
}
|
||||
|
||||
void mp_write_console_ansi(HANDLE wstream, char *buf)
|
||||
int mp_write_console_ansi(HANDLE wstream, const char *format, va_list args)
|
||||
{
|
||||
wchar_t *wbuf = mp_from_utf8(NULL, buf);
|
||||
wchar_t *pos = wbuf;
|
||||
struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key);
|
||||
if (!buffers)
|
||||
buffers = talloc_zero(NULL, struct tmp_buffers);
|
||||
|
||||
buffers->write_console_buf.len = 0;
|
||||
bstr_xappend_vasprintf(buffers, &buffers->write_console_buf, format, args);
|
||||
int wlen = bstr_to_wchar(buffers, buffers->write_console_buf, &buffers->write_console_wbuf);
|
||||
wchar_t *pos = buffers->write_console_wbuf;
|
||||
|
||||
while (*pos) {
|
||||
if (is_native_out_vt(wstream)) {
|
||||
WriteConsoleW(wstream, pos, wcslen(pos), NULL, NULL);
|
||||
WriteConsoleW(wstream, pos, wlen, NULL, NULL);
|
||||
break;
|
||||
}
|
||||
wchar_t *next = wcschr(pos, '\033');
|
||||
|
@ -365,7 +378,12 @@ void mp_write_console_ansi(HANDLE wstream, char *buf)
|
|||
pos = next;
|
||||
}
|
||||
|
||||
talloc_free(wbuf);
|
||||
int ret = buffers->write_console_buf.len;
|
||||
|
||||
if (!FlsSetValue(tmp_buffers_key, buffers))
|
||||
talloc_free(buffers);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool is_a_console(HANDLE h)
|
||||
|
@ -455,4 +473,6 @@ void terminal_init(void)
|
|||
|
||||
GetConsoleScreenBufferInfo(hSTDOUT, &cinfo);
|
||||
stdoutAttrs = cinfo.wAttributes;
|
||||
|
||||
tmp_buffers_key = FlsAlloc((PFLS_CALLBACK_FUNCTION)talloc_free);
|
||||
}
|
||||
|
|
|
@ -20,8 +20,10 @@
|
|||
#ifndef MPLAYER_GETCH2_H
|
||||
#define MPLAYER_GETCH2_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "misc/bstr.h"
|
||||
|
||||
#define TERM_ESC_GOTO_YX "\033[%d;%df"
|
||||
#define TERM_ESC_HIDE_CURSOR "\033[?25l"
|
||||
|
@ -52,7 +54,7 @@ void terminal_get_size(int *w, int *h);
|
|||
void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height);
|
||||
|
||||
// Windows only.
|
||||
void mp_write_console_ansi(void *wstream, char *buf);
|
||||
int mp_write_console_ansi(void *wstream, const char *format, va_list args);
|
||||
bool mp_check_console(void *handle);
|
||||
|
||||
/* Windows-only function to attach to the parent process's console */
|
||||
|
|
|
@ -107,7 +107,7 @@ void mp_msg(struct mp_log *log, int lev, const char *format, ...) {};
|
|||
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) {};
|
||||
void mp_write_console_ansi(void *wstream, char *buf) {};
|
||||
int mp_write_console_ansi(void *wstream, const char *format, va_list args) {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,
|
||||
|
|
Loading…
Reference in New Issue