History

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.

Kacper Michajłow committed 2 years ago

Commit c389f9e1 parent 6177aa76 files · +25 −18Browse files

Unified · Split

6 files
  1. osdep/io.c +17 −9
  2. osdep/io.h +3 −4
  3. osdep/terminal-dummy.c +1 −1
  4. osdep/terminal-win.c +2 −2
  5. osdep/terminal.h +1 −1
  6. test/test_utils.c +1 −1
osdep/io.c+17−9
@@ -309,18 +309,26 @@ static inline HANDLE get_handle(FILE *stream) · 308 unmodified lines
309309 return wstream;
310310 }
311311
312−int mp_fputs(const char *str, FILE *stream)
312+size_t mp_fwrite(const void *restrict buffer, size_t size, size_t count,
313+ FILE *restrict stream)
313314 {
314− HANDLE wstream = get_handle(stream);
315− if (mp_check_console(wstream))
316− return mp_console_fputs(wstream, bstr0(str));
315+ if (!size || !count)
316+ return 0;
317317
318− return fputs(str, stream);
319−}
318+ HANDLE wstream = get_handle(stream);
319+ if (mp_check_console(wstream)) {
320+ unsigned char *start = (unsigned char *)buffer;
321+ size_t c = 0;
322+ for (; c < count; ++c) {
323+ if (mp_console_write(wstream, (bstr){start, size}) <= 0)
324+ break;
325+ start += size;
326+ }
327+ return c;
328+ }
320329
321−int mp_puts(const char *str)
322−{
323− return mp_fputs(str, stdout);
330+#undef fwrite
331+ return fwrite(buffer, size, count, stream);
324332 }
325333
326334 #if HAVE_UWP
osdep/io.h+3−4
@@ -106,8 +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);
109+size_t mp_fwrite(const void *restrict buffer, size_t size, size_t count,
110+ FILE *restrict stream);
111111 int mp_printf(const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
112112 int mp_fprintf(FILE *stream, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
113113 int mp_open(const char *filename, int oflag, ...);
@@ -176,8 +176,7 @@ int mp_glob(const char *restrict pattern, int flags, · 62 unmodified lines
176176 int (*errfunc)(const char*, int), mp_glob_t *restrict pglob);
177177 void mp_globfree(mp_glob_t *pglob);
178178
179−#define puts(...) mp_puts(__VA_ARGS__)
180−#define fputs(...) mp_fputs(__VA_ARGS__)
179+#define fwrite(...) mp_fwrite(__VA_ARGS__)
181180 #define printf(...) mp_printf(__VA_ARGS__)
182181 #define fprintf(...) mp_fprintf(__VA_ARGS__)
183182 #define open(...) mp_open(__VA_ARGS__)
osdep/terminal-dummy.c+1−1
@@ -32,7 +32,7 @@ int mp_console_vfprintf(void *wstream, const char *format, va_list args) · 31 unmodified lines
3232 return 0;
3333 }
3434
35−int mp_console_fputs(void *wstream, bstr str)
35+int mp_console_write(void *wstream, bstr str)
3636 {
3737 return 0;
3838 }
osdep/terminal-win.c+2−2
@@ -239,7 +239,7 @@ int mp_console_vfprintf(HANDLE wstream, const char *format, va_list args) · 238 unmodified lines
239239 buffers->write_console_buf.len = 0;
240240 bstr_xappend_vasprintf(buffers, &buffers->write_console_buf, format, args);
241241
242− int ret = mp_console_fputs(wstream, buffers->write_console_buf);
242+ int ret = mp_console_write(wstream, buffers->write_console_buf);
243243
244244 if (free_buf)
245245 talloc_free(buffers);
@@ -247,7 +247,7 @@ int mp_console_vfprintf(HANDLE wstream, const char *format, va_list args) · 1 unmodified line
247247 return ret;
248248 }
249249
250−int mp_console_fputs(HANDLE wstream, bstr str)
250+int mp_console_write(HANDLE wstream, bstr str)
251251 {
252252 struct tmp_buffers *buffers = FlsGetValue(tmp_buffers_key);
253253 bool free_buf = false;
osdep/terminal.h+1−1
@@ -57,7 +57,7 @@ void terminal_get_size2(int *rows, int *cols, int *px_width, int *px_height); · 56 unmodified lines
5757
5858 // Windows only.
5959 int mp_console_vfprintf(void *wstream, const char *format, va_list args);
60−int mp_console_fputs(void *wstream, bstr str);
60+int mp_console_write(void *wstream, bstr str);
6161 bool mp_check_console(void *handle);
6262
6363 /* Windows-only function to attach to the parent process's console */
test/test_utils.c+1−1
@@ -114,7 +114,7 @@ int mp_msg_find_level(const char *s) {return 0;}; · 113 unmodified lines
114114 int mp_msg_level(struct mp_log *log) {return 0;};
115115 void mp_msg_set_max_level(struct mp_log *log, int lev) {};
116116 int mp_console_vfprintf(void *wstream, const char *format, va_list args) {return 0;};
117−int mp_console_fputs(void *wstream, bstr str) {return 0;};
117+int mp_console_write(void *wstream, bstr str) {return 0;};
118118 bool mp_check_console(void *handle) { return false; };
119119 void mp_set_avdict(AVDictionary **dict, char **kv) {};
120120 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