History

win32: add puts/fputs wrappers

Kacper Michajłow committed 2 years ago

Commit fc55f351 parent 3372e176 files · +66 −15Browse files

Unified · Split

6 files
  1. osdep/io.c +27 −8
  2. osdep/io.h +4 −0
  3. osdep/terminal-dummy.c +6 −1
  4. osdep/terminal-win.c +25 −4
  5. osdep/terminal.h +2 −1
  6. test/test_utils.c +2 −1
osdep/io.c+27−8
@@ -298,6 +298,31 @@ int mp_fstat(int fd, struct mp_stat *buf) · 297 unmodified lines
298298 return hstat(h, buf);
299299 }
300300
301+static inline HANDLE get_handle(FILE *stream)
302+{
303+ HANDLE wstream = INVALID_HANDLE_VALUE;
304+
305+ if (stream == stdout || stream == stderr) {
306+ wstream = GetStdHandle(stream == stdout ?
307+ STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
308+ }
309+ return wstream;
310+}
311+
312+int mp_fputs(const char *str, FILE *stream)
313+{
314+ HANDLE wstream = get_handle(stream);
315+ if (mp_check_console(wstream))
316+ return mp_console_fputs(wstream, bstr0(str));
317+
318+ return fputs(str, stream);
319+}
320+
321+int mp_puts(const char *str)
322+{
323+ return mp_fputs(str, stdout);
324+}
325+
301326 #if HAVE_UWP
302327 static int mp_vfprintf(FILE *stream, const char *format, va_list args)
303328 {
@@ -307,15 +332,9 @@ static int mp_vfprintf(FILE *stream, const char *format, va_list args) · 3 unmodified lines
307332
308333 static int mp_vfprintf(FILE *stream, const char *format, va_list args)
309334 {
310− HANDLE wstream = INVALID_HANDLE_VALUE;
311−
312− if (stream == stdout || stream == stderr) {
313− wstream = GetStdHandle(stream == stdout ?
314− STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
315− }
316−
335+ HANDLE wstream = get_handle(stream);
317336 if (mp_check_console(wstream))
318− return mp_write_console_ansi(wstream, format, args);
337+ return mp_console_vfprintf(wstream, format, args);
319338
320339 return vfprintf(stream, format, args);
321340 }
osdep/io.h+4−0
@@ -106,6 +106,8 @@ char *mp_to_utf8(void *talloc_ctx, const wchar_t *s); · 105 unmodified lines
106106 #include <sys/stat.h>
107107 #include <fcntl.h>
108108
109+int mp_puts(const char *str);
110+int mp_fputs(const char *str, FILE *stream);
109111 int mp_printf(const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
110112 int mp_fprintf(FILE *stream, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
111113 int mp_open(const char *filename, int oflag, ...);
@@ -174,6 +176,8 @@ int mp_glob(const char *restrict pattern, int flags, · 62 unmodified lines
174176 int (*errfunc)(const char*, int), mp_glob_t *restrict pglob);
175177 void mp_globfree(mp_glob_t *pglob);
176178
179+#define puts(...) mp_puts(__VA_ARGS__)
180+#define fputs(...) mp_fputs(__VA_ARGS__)
177181 #define printf(...) mp_printf(__VA_ARGS__)
178182 #define fprintf(...) mp_fprintf(__VA_ARGS__)
179183 #define open(...) mp_open(__VA_ARGS__)
osdep/terminal-dummy.c+6−1
@@ -27,7 +27,12 @@ void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height) · 26 unmodified lines
2727 {
2828 }
2929
30−int mp_write_console_ansi(void *wstream, const char *format, va_list args)
30+int mp_console_vfprintf(void *wstream, const char *format, va_list args)
31+{
32+ return 0;
33+}
34+
35+int mp_console_fputs(void *wstream, bstr str)
3136 {
3237 return 0;
3338 }
osdep/terminal-win.c+25−4
@@ -226,15 +226,36 @@ bool terminal_in_background(void) · 225 unmodified lines
226226 return false;
227227 }
228228
229−int mp_write_console_ansi(HANDLE wstream, const char *format, va_list args)
229+int mp_console_vfprintf(HANDLE wstream, const char *format, va_list args)
230230 {
231231 struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key);
232− if (!buffers)
232+ bool free_buf = false;
233+ if (!buffers) {
233234 buffers = talloc_zero(NULL, struct tmp_buffers);
235+ free_buf = !FlsSetValue(tmp_buffers_key, buffers);
236+ }
234237
235238 buffers->write_console_buf.len = 0;
236239 bstr_xappend_vasprintf(buffers, &buffers->write_console_buf, format, args);
237− int wlen = bstr_to_wchar(buffers, buffers->write_console_buf, &buffers->write_console_wbuf);
240+
241+ int ret = mp_console_fputs(wstream, buffers->write_console_buf);
242+
243+ if (free_buf)
244+ talloc_free(buffers);
245+
246+ return ret;
247+}
248+
249+int mp_console_fputs(HANDLE wstream, bstr str)
250+{
251+ struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key);
252+ bool free_buf = false;
253+ if (!buffers) {
254+ buffers = talloc_zero(NULL, struct tmp_buffers);
255+ free_buf = !FlsSetValue(tmp_buffers_key, buffers);
256+ }
257+
258+ int wlen = bstr_to_wchar(buffers, str, &buffers->write_console_wbuf);
238259 wchar_t *pos = buffers->write_console_wbuf;
239260
240261 while (*pos) {
@@ -380,7 +401,7 @@ int mp_write_console_ansi(HANDLE wstream, const char *format, va_list args) · 139 unmodified lines
380401
381402 int ret = buffers->write_console_buf.len;
382403
383− if (!FlsSetValue(tmp_buffers_key, buffers))
404+ if (free_buf)
384405 talloc_free(buffers);
385406
386407 return ret;
osdep/terminal.h+2−1
@@ -54,7 +54,8 @@ void terminal_get_size(int *w, int *h); · 53 unmodified lines
5454 void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height);
5555
5656 // Windows only.
57−int mp_write_console_ansi(void *wstream, const char *format, va_list args);
57+int mp_console_vfprintf(void *wstream, const char *format, va_list args);
58+int mp_console_fputs(void *wstream, bstr str);
5859 bool mp_check_console(void *handle);
5960
6061 /* Windows-only function to attach to the parent process's console */
test/test_utils.c+2−1
@@ -107,7 +107,8 @@ void mp_msg(struct mp_log *log, int lev, const char *format, ...) {}; · 106 unmodified lines
107107 int mp_msg_find_level(const char *s) {return 0;};
108108 int mp_msg_level(struct mp_log *log) {return 0;};
109109 void mp_msg_set_max_level(struct mp_log *log, int lev) {};
110−int mp_write_console_ansi(void *wstream, const char *format, va_list args) {return 0;};
110+int mp_console_vfprintf(void *wstream, const char *format, va_list args) {return 0;};
111+int mp_console_fputs(void *wstream, bstr str) {return 0;};
111112 bool mp_check_console(void *handle) { return false; };
112113 void mp_set_avdict(AVDictionary **dict, char **kv) {};
113114 struct mp_log *mp_log_new(void *talloc_ctx, struct mp_log *parent,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t