From 80e9b3c0f277824c4223b2f3dac91e86b2333f77 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 16 Feb 2013 22:51:10 +0100 Subject: cleanup: remove duplicated function, move escape parsing function --- core/input/input.c | 63 ++++++------------------------------------------------ core/mp_common.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ core/mp_common.h | 6 ++++++ sub/osd_libass.c | 29 ++++++++++--------------- 4 files changed, 82 insertions(+), 75 deletions(-) diff --git a/core/input/input.c b/core/input/input.c index c12dba29b6..6f611a8ef3 100644 --- a/core/input/input.c +++ b/core/input/input.c @@ -31,6 +31,9 @@ #include #include +#include +#include + #include "osdep/io.h" #include "osdep/getch2.h" @@ -38,8 +41,6 @@ #include "core/mp_fifo.h" #include "keycodes.h" #include "osdep/timer.h" -#include "libavutil/avstring.h" -#include "libavutil/common.h" #include "core/mp_msg.h" #include "core/m_config.h" #include "core/m_option.h" @@ -48,6 +49,7 @@ #include "core/options.h" #include "core/bstr.h" #include "stream/stream.h" +#include "core/mp_common.h" #include "joystick.h" @@ -546,17 +548,6 @@ static const char builtin_input_conf[] = #include "core/input/input_conf.h" ; -// Encode the unicode codepoint as UTF-8, and append to the end of the -// talloc'ed buffer. -static char *append_utf8_buffer(char *buffer, uint32_t codepoint) -{ - char data[8]; - uint8_t tmp; - char *output = data; - PUT_UTF8(codepoint, tmp, *output++ = tmp;); - return talloc_strndup_append_buffer(buffer, data, output - data); -} - static char *get_key_name(int key, char *ret) { for (int i = 0; modifier_names[i].name; i++) { @@ -573,7 +564,7 @@ static char *get_key_name(int key, char *ret) // printable, and valid unicode range if (key >= 32 && key <= 0x10FFFF) - return append_utf8_buffer(ret, key); + return mp_append_utf8_buffer(ret, key); // Print the hex key code return talloc_asprintf_append_buffer(ret, "%#-8x", key); @@ -779,48 +770,6 @@ static bool eat_token(bstr *str, const char *tok) return false; } -static bool append_escape(bstr *code, char **str) -{ - if (code->len < 1) - return false; - char replace = 0; - switch (code->start[0]) { - case '"': replace = '"'; break; - case '\\': replace = '\\'; break; - case 'b': replace = '\b'; break; - case 'f': replace = '\f'; break; - case 'n': replace = '\n'; break; - case 'r': replace = '\r'; break; - case 't': replace = '\t'; break; - case 'e': replace = '\x1b'; break; - case '\'': replace = '\''; break; - } - if (replace) { - *str = talloc_strndup_append_buffer(*str, &replace, 1); - *code = bstr_cut(*code, 1); - return true; - } - if (code->start[0] == 'x' && code->len >= 3) { - bstr num = bstr_splice(*code, 1, 3); - char c = bstrtoll(num, &num, 16); - if (!num.len) - return false; - *str = talloc_strndup_append_buffer(*str, &c, 1); - *code = bstr_cut(*code, 3); - return true; - } - if (code->start[0] == 'u' && code->len >= 5) { - bstr num = bstr_splice(*code, 1, 5); - int c = bstrtoll(num, &num, 16); - if (num.len) - return false; - *str = append_utf8_buffer(*str, c); - *code = bstr_cut(*code, 5); - return true; - } - return false; -} - static bool read_escaped_string(void *talloc_ctx, bstr *str, bstr *literal) { bstr t = *str; @@ -830,7 +779,7 @@ static bool read_escaped_string(void *talloc_ctx, bstr *str, bstr *literal) break; if (t.start[0] == '\\') { t = bstr_cut(t, 1); - if (!append_escape(&t, &new)) + if (!mp_parse_escape(&t, &new)) goto error; } else { new = talloc_strndup_append_buffer(new, t.start, 1); diff --git a/core/mp_common.c b/core/mp_common.c index 611cb22bcf..03e3e988fd 100644 --- a/core/mp_common.c +++ b/core/mp_common.c @@ -19,6 +19,7 @@ #include #include "talloc.h" +#include "core/bstr.h" #include "core/mp_common.h" char *mp_format_time(double time, bool fractions) @@ -64,3 +65,61 @@ bool mp_rect_intersection(struct mp_rect *rc, const struct mp_rect *rc2) return rc->x1 > rc->x0 && rc->y1 > rc->y0; } + +// Encode the unicode codepoint as UTF-8, and append to the end of the +// talloc'ed buffer. +char *mp_append_utf8_buffer(char *buffer, uint32_t codepoint) +{ + char data[8]; + uint8_t tmp; + char *output = data; + PUT_UTF8(codepoint, tmp, *output++ = tmp;); + return talloc_strndup_append_buffer(buffer, data, output - data); +} + +// Parse a C-style escape beginning at code, and append the result to *str +// using talloc. The input string (*code) must point to the first character +// after the initial '\', and after parsing *code is set to the first character +// after the current escape. +// On error, false is returned, and all input remains unchanged. +bool mp_parse_escape(bstr *code, char **str) +{ + if (code->len < 1) + return false; + char replace = 0; + switch (code->start[0]) { + case '"': replace = '"'; break; + case '\\': replace = '\\'; break; + case 'b': replace = '\b'; break; + case 'f': replace = '\f'; break; + case 'n': replace = '\n'; break; + case 'r': replace = '\r'; break; + case 't': replace = '\t'; break; + case 'e': replace = '\x1b'; break; + case '\'': replace = '\''; break; + } + if (replace) { + *str = talloc_strndup_append_buffer(*str, &replace, 1); + *code = bstr_cut(*code, 1); + return true; + } + if (code->start[0] == 'x' && code->len >= 3) { + bstr num = bstr_splice(*code, 1, 3); + char c = bstrtoll(num, &num, 16); + if (!num.len) + return false; + *str = talloc_strndup_append_buffer(*str, &c, 1); + *code = bstr_cut(*code, 3); + return true; + } + if (code->start[0] == 'u' && code->len >= 5) { + bstr num = bstr_splice(*code, 1, 5); + int c = bstrtoll(num, &num, 16); + if (num.len) + return false; + *str = mp_append_utf8_buffer(*str, c); + *code = bstr_cut(*code, 5); + return true; + } + return false; +} diff --git a/core/mp_common.h b/core/mp_common.h index 62c314d95b..6cb1da373d 100644 --- a/core/mp_common.h +++ b/core/mp_common.h @@ -21,6 +21,7 @@ #include #include +#include #include "compat/compiler.h" #include "core/mp_talloc.h" @@ -53,4 +54,9 @@ struct mp_rect { void mp_rect_union(struct mp_rect *rc, const struct mp_rect *src); bool mp_rect_intersection(struct mp_rect *rc, const struct mp_rect *rc2); +char *mp_append_utf8_buffer(char *buffer, uint32_t codepoint); + +struct bstr; +bool mp_parse_escape(struct bstr *code, char **str); + #endif /* MPLAYER_MPCOMMON_H */ diff --git a/sub/osd_libass.c b/sub/osd_libass.c index 051383f78e..bbd925f135 100644 --- a/sub/osd_libass.c +++ b/sub/osd_libass.c @@ -20,12 +20,14 @@ #include #include +#include + #include "config.h" #include "talloc.h" +#include "core/mp_common.h" #include "core/mp_msg.h" #include "sub.h" -#include "libavutil/common.h" static const char osd_font_pfb[] = #include "sub/osd_font.h" @@ -102,15 +104,6 @@ static void clear_obj(struct osd_object *obj) ass_flush_events(obj->osd_track); } -static char *append_utf8_buffer(char *buffer, uint32_t codepoint) -{ - char data[8]; - uint8_t tmp; - char *output = data; - PUT_UTF8(codepoint, tmp, *output++ = tmp;); - return talloc_strndup_append_buffer(buffer, data, output - data); -} - void osd_get_function_sym(char *buffer, size_t buffer_size, int osd_function) { // 0xFF is never valid UTF-8, so we can use it to escape OSD symbols. @@ -124,7 +117,7 @@ static char *mangle_ass(const char *in) // As used by osd_get_function_sym(). if (in[0] == '\xFF' && in[1]) { res = talloc_strdup_append_buffer(res, ASS_USE_OSD_FONT); - res = append_utf8_buffer(res, OSD_CODEPOINTS + in[1]); + res = mp_append_utf8_buffer(res, OSD_CODEPOINTS + in[1]); res = talloc_strdup_append_buffer(res, "{\\r}"); in += 2; continue; @@ -134,7 +127,7 @@ static char *mangle_ass(const char *in) res = talloc_strndup_append_buffer(res, in, 1); // Break ASS escapes with U+2060 WORD JOINER if (*in == '\\') - res = append_utf8_buffer(res, 0x2060); + res = mp_append_utf8_buffer(res, 0x2060); in++; } return res; @@ -207,10 +200,10 @@ static void update_progbar(struct osd_state *osd, struct osd_object *obj) if (osd->progbar_type == 0 || osd->progbar_type >= 256) { // no sym } else if (osd->progbar_type >= 32) { - text = append_utf8_buffer(text, osd->progbar_type); + text = mp_append_utf8_buffer(text, osd->progbar_type); } else { text = talloc_strdup_append_buffer(text, ASS_USE_OSD_FONT); - text = append_utf8_buffer(text, OSD_CODEPOINTS + osd->progbar_type); + text = mp_append_utf8_buffer(text, OSD_CODEPOINTS + osd->progbar_type); text = talloc_strdup_append_buffer(text, "{\\r}"); } @@ -218,12 +211,12 @@ static void update_progbar(struct osd_state *osd, struct osd_object *obj) text = talloc_strdup_append_buffer(text, "\\h"); text = talloc_strdup_append_buffer(text, ASS_USE_OSD_FONT); - text = append_utf8_buffer(text, OSD_CODEPOINTS + OSD_PB_START); + text = mp_append_utf8_buffer(text, OSD_CODEPOINTS + OSD_PB_START); for (int n = 0; n < active; n++) - text = append_utf8_buffer(text, OSD_CODEPOINTS + OSD_PB_0); + text = mp_append_utf8_buffer(text, OSD_CODEPOINTS + OSD_PB_0); for (int n = 0; n < OSDBAR_ELEMS - active; n++) - text = append_utf8_buffer(text, OSD_CODEPOINTS + OSD_PB_1); - text = append_utf8_buffer(text, OSD_CODEPOINTS + OSD_PB_END); + text = mp_append_utf8_buffer(text, OSD_CODEPOINTS + OSD_PB_1); + text = mp_append_utf8_buffer(text, OSD_CODEPOINTS + OSD_PB_END); ASS_Event *event = get_osd_ass_event(obj->osd_track); event->Text = strdup(text); -- cgit v1.2.3