diff options
author | Grigori Goronzy <greg@blackbox> | 2009-07-11 02:18:51 +0200 |
---|---|---|
committer | Grigori Goronzy <greg@blackbox> | 2009-07-11 02:22:18 +0200 |
commit | 2c412cdab94a7bb27c5a1e04ab902295215de888 (patch) | |
tree | c1372ebf5e6473b287e152a40c88587f3470d237 /libass/ass.c | |
parent | 613a22ab9b96453c10de6d75b43067652ad6d7db (diff) | |
download | libass-2c412cdab94a7bb27c5a1e04ab902295215de888.tar.bz2 libass-2c412cdab94a7bb27c5a1e04ab902295215de888.tar.xz |
Message callback funtionality
Introduce functionality for providing a message callback that is used
for passing messages to the controlling application instead of simply
printing them to standard output. The function pointer to the callback
is stored in the ass_library_t instance. ass_msg needs access to it, so
in many places the library instance needs to be passed around now.
The default behavior is the old one: messages of MSGL_INFO or lower
are printed to the standard output, prefixed with "[ass]".
Diffstat (limited to 'libass/ass.c')
-rw-r--r-- | libass/ass.c | 119 |
1 files changed, 66 insertions, 53 deletions
diff --git a/libass/ass.c b/libass/ass.c index 135232f..9eaa7b0 100644 --- a/libass/ass.c +++ b/libass/ass.c @@ -182,26 +182,26 @@ static int lookup_style(ass_track_t *track, char *name) return i; } i = track->default_style; - ass_msg(MSGL_WARN, + ass_msg(track->library, MSGL_WARN, "[%p]: Warning: no style named '%s' found, using '%s'", track, name, track->styles[i].Name); return i; // use the first style } -static uint32_t string2color(char *p) +static uint32_t string2color(ass_library_t *library, char *p) { uint32_t tmp; - (void) strtocolor(&p, &tmp); + (void) strtocolor(library, &p, &tmp); return tmp; } -static long long string2timecode(char *p) +static long long string2timecode(ass_library_t *library, char *p) { unsigned h, m, s, ms; long long tm; int res = sscanf(p, "%1d:%2d:%2d.%2d", &h, &m, &s, &ms); if (res < 4) { - ass_msg(MSGL_WARN, "Bad timestamp"); + ass_msg(library, MSGL_WARN, "Bad timestamp"); return 0; } tm = ((h * 60 + m) * 60 + s) * 1000 + ms * 10; @@ -229,22 +229,30 @@ static int numpad2align(int val) #define ANYVAL(name,func) \ } else if (strcasecmp(tname, #name) == 0) { \ target->name = func(token); \ - ass_msg(MSGL_DBG2, "%s = %s", #name, token); + ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token); #define STRVAL(name) \ } else if (strcasecmp(tname, #name) == 0) { \ if (target->name != NULL) free(target->name); \ target->name = strdup(token); \ - ass_msg(MSGL_DBG2, "%s = %s", #name, token); + ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token); + +#define COLORVAL(name) \ + } else if (strcasecmp(tname, #name) == 0) { \ + target->name = string2color(track->library, token); \ + ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token); -#define COLORVAL(name) ANYVAL(name,string2color) #define INTVAL(name) ANYVAL(name,atoi) #define FPVAL(name) ANYVAL(name,atof) -#define TIMEVAL(name) ANYVAL(name,string2timecode) +#define TIMEVAL(name) \ + } else if (strcasecmp(tname, #name) == 0) { \ + target->name = string2timecode(track->library, token); \ + ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token); + #define STYLEVAL(name) \ } else if (strcasecmp(tname, #name) == 0) { \ target->name = lookup_style(track, token); \ - ass_msg(MSGL_DBG2, "%s = %s", #name, token); + ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token); #define ALIAS(alias,name) \ if (strcasecmp(tname, #alias) == 0) {tname = #name;} @@ -318,7 +326,7 @@ static int process_event_tail(ass_track_t *track, ass_event_t *event, if (last >= event->Text && *last == '\r') *last = 0; } - ass_msg(MSGL_DBG2, "Text = %s", event->Text); + ass_msg(track->library, MSGL_DBG2, "Text = %s", event->Text); event->Duration -= event->Start; free(format); return 0; // "Text" is always the last @@ -458,7 +466,7 @@ static int process_style(ass_track_t *track, char *str) q = format = strdup(track->style_format); - ass_msg(MSGL_V, "[%p] Style: %s", track, str); + ass_msg(track->library, MSGL_V, "[%p] Style: %s", track, str); sid = ass_alloc_style(track); @@ -535,7 +543,7 @@ static int process_styles_line(ass_track_t *track, char *str) char *p = str + 7; skip_spaces(&p); track->style_format = strdup(p); - ass_msg(MSGL_DBG2, "Style format: %s", + ass_msg(track->library, MSGL_DBG2, "Style format: %s", track->style_format); } else if (!strncmp(str, "Style:", 6)) { char *p = str + 6; @@ -565,14 +573,13 @@ static void event_format_fallback(ass_track_t *track) { track->parser_priv->state = PST_EVENTS; if (track->track_type == TRACK_TYPE_SSA) - track->event_format = - strdup - ("Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"); + track->event_format = strdup("Format: Marked, Start, End, Style, " + "Name, MarginL, MarginR, MarginV, Effect, Text"); else - track->event_format = - strdup - ("Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text"); - ass_msg(MSGL_V, "No event format found, using fallback"); + track->event_format = strdup("Format: Layer, Start, End, Style, " + "Actor, MarginL, MarginR, MarginV, Effect, Text"); + ass_msg(track->library, MSGL_V, + "No event format found, using fallback"); } static int process_events_line(ass_track_t *track, char *str) @@ -581,7 +588,7 @@ static int process_events_line(ass_track_t *track, char *str) char *p = str + 7; skip_spaces(&p); track->event_format = strdup(p); - ass_msg(MSGL_DBG2, "Event format: %s", track->event_format); + ass_msg(track->library, MSGL_DBG2, "Event format: %s", track->event_format); } else if (!strncmp(str, "Dialogue:", 9)) { // This should never be reached for embedded subtitles. // They have slightly different format and are parsed in ass_process_chunk, @@ -601,7 +608,7 @@ static int process_events_line(ass_track_t *track, char *str) process_event_tail(track, event, str, 0); } else { - ass_msg(MSGL_V, "Not understood: '%s'", str); + ass_msg(track->library, MSGL_V, "Not understood: '%s'", str); } return 0; } @@ -636,11 +643,11 @@ static int decode_font(ass_track_t *track) int dsize; // decoded size unsigned char *buf = 0; - ass_msg(MSGL_V, "Font: %d bytes encoded data", - track->parser_priv->fontdata_used); + ass_msg(track->library, MSGL_V, "Font: %d bytes encoded data", + track->parser_priv->fontdata_used); size = track->parser_priv->fontdata_used; if (size % 4 == 1) { - ass_msg(MSGL_ERR, "Bad encoded data size"); + ass_msg(track->library, MSGL_ERR, "Bad encoded data size"); goto error_decode_font; } buf = malloc(size / 4 * 3 + 2); @@ -686,19 +693,20 @@ static int process_fonts_line(ass_track_t *track, char *str) decode_font(track); } track->parser_priv->fontname = strdup(p); - ass_msg(MSGL_V, "Fontname: %s", + ass_msg(track->library, MSGL_V, "Fontname: %s", track->parser_priv->fontname); return 0; } if (!track->parser_priv->fontname) { - ass_msg(MSGL_V, "Not understood: '%s'", str); + ass_msg(track->library, MSGL_V, "Not understood: '%s'", str); return 0; } len = strlen(str); if (len > 80) { - ass_msg(MSGL_WARN, "Font line too long: %d, %s", len, str); + ass_msg(track->library, MSGL_WARN, "Font line too long: %d, %s", + len, str); return 0; } if (track->parser_priv->fontdata_used + len > @@ -801,7 +809,7 @@ void ass_process_data(ass_track_t *track, char *data, int size) memcpy(str, data, size); str[size] = '\0'; - ass_msg(MSGL_V, "Event: %s", str); + ass_msg(track->library, MSGL_V, "Event: %s", str); process_text(track, str); free(str); } @@ -852,14 +860,14 @@ void ass_process_chunk(ass_track_t *track, char *data, int size, ass_event_t *event; if (!track->event_format) { - ass_msg(MSGL_WARN, "Event format header missing"); + ass_msg(track->library, MSGL_WARN, "Event format header missing"); return; } str = malloc(size + 1); memcpy(str, data, size); str[size] = '\0'; - ass_msg(MSGL_V, "Event at %" PRId64 ", +%" PRId64 ": %s", + ass_msg(track->library, MSGL_V, "Event at %" PRId64 ", +%" PRId64 ": %s", (int64_t) timecode, (int64_t) duration, str); eid = ass_alloc_event(track); @@ -898,7 +906,8 @@ void ass_process_chunk(ass_track_t *track, char *data, int size, * \param size buffer size * \return a pointer to recoded buffer, caller is responsible for freeing it **/ -static char *sub_recode(char *data, size_t size, char *codepage) +static char *sub_recode(ass_library_t *library, char *data, size_t size, + char *codepage) { iconv_t icdsc; char *tocp = "UTF-8"; @@ -913,14 +922,14 @@ static char *sub_recode(char *data, size_t size, char *codepage) || sscanf(codepage, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) { cp_tmp = - ass_guess_buffer_cp((unsigned char *) data, size, enca_lang, - enca_fallback); + ass_guess_buffer_cp(library, (unsigned char *) data, size, + enca_lang, enca_fallback); } #endif if ((icdsc = iconv_open(tocp, cp_tmp)) != (iconv_t) (-1)) { - ass_msg(MSGL_V, "Opened iconv descriptor"); + ass_msg(library, MSGL_V, "Opened iconv descriptor"); } else - ass_msg(MSGL_ERR, "Error opening iconv descriptor"); + ass_msg(library, MSGL_ERR, "Error opening iconv descriptor"); } { @@ -951,7 +960,7 @@ static char *sub_recode(char *data, size_t size, char *codepage) osize += size; oleft += size; } else { - ass_msg(MSGL_WARN, "Error recoding file"); + ass_msg(library, MSGL_WARN, "Error recoding file"); return NULL; } } else if (clear) @@ -963,7 +972,7 @@ static char *sub_recode(char *data, size_t size, char *codepage) if (icdsc != (iconv_t) (-1)) { (void) iconv_close(icdsc); icdsc = (iconv_t) (-1); - ass_msg(MSGL_V, "Closed iconv descriptor"); + ass_msg(library, MSGL_V, "Closed iconv descriptor"); } return outbuf; @@ -976,7 +985,7 @@ static char *sub_recode(char *data, size_t size, char *codepage) * \param bufsize out: file size * \return pointer to file contents. Caller is responsible for its deallocation. */ -static char *read_file(char *fname, size_t *bufsize) +static char *read_file(ass_library_t *library, char *fname, size_t *bufsize) { int res; long sz; @@ -985,12 +994,14 @@ static char *read_file(char *fname, size_t *bufsize) FILE *fp = fopen(fname, "rb"); if (!fp) { - ass_msg(MSGL_WARN, "ass_read_file(%s): fopen failed", fname); + ass_msg(library, MSGL_WARN, + "ass_read_file(%s): fopen failed", fname); return 0; } res = fseek(fp, 0, SEEK_END); if (res == -1) { - ass_msg(MSGL_WARN, "ass_read_file(%s): fseek failed", fname); + ass_msg(library, MSGL_WARN, + "ass_read_file(%s): fseek failed", fname); fclose(fp); return 0; } @@ -999,14 +1010,14 @@ static char *read_file(char *fname, size_t *bufsize) rewind(fp); if (sz > 10 * 1024 * 1024) { - ass_msg(MSGL_INFO, + ass_msg(library, MSGL_INFO, "ass_read_file(%s): Refusing to load subtitles " "larger than 10MiB", fname); fclose(fp); return 0; } - ass_msg(MSGL_V, "File size: %ld", sz); + ass_msg(library, MSGL_V, "File size: %ld", sz); buf = malloc(sz + 1); assert(buf); @@ -1014,7 +1025,7 @@ static char *read_file(char *fname, size_t *bufsize) do { res = fread(buf + bytes_read, 1, sz - bytes_read, fp); if (res <= 0) { - ass_msg(MSGL_INFO, "Read failed, %d: %s", errno, + ass_msg(library, MSGL_INFO, "Read failed, %d: %s", errno, strerror(errno)); fclose(fp); free(buf); @@ -1080,7 +1091,7 @@ ass_track_t *ass_read_memory(ass_library_t *library, char *buf, #ifdef CONFIG_ICONV if (codepage) - buf = sub_recode(buf, bufsize, codepage); + buf = sub_recode(library, buf, bufsize, codepage); if (!buf) return 0; else @@ -1092,23 +1103,24 @@ ass_track_t *ass_read_memory(ass_library_t *library, char *buf, if (!track) return 0; - ass_msg(MSGL_INFO, "Added subtitle file: " + ass_msg(library, MSGL_INFO, "Added subtitle file: " "<memory> (%d styles, %d events)", track->n_styles, track->n_events); return track; } -static char *read_file_recode(char *fname, char *codepage, size_t *size) +static char *read_file_recode(ass_library_t *library, char *fname, + char *codepage, size_t *size) { char *buf; size_t bufsize; - buf = read_file(fname, &bufsize); + buf = read_file(library, fname, &bufsize); if (!buf) return 0; #ifdef CONFIG_ICONV if (codepage) { - char *tmpbuf = sub_recode(buf, bufsize, codepage); + char *tmpbuf = sub_recode(library, buf, bufsize, codepage); free(buf); buf = tmpbuf; } @@ -1133,7 +1145,7 @@ ass_track_t *ass_read_file(ass_library_t *library, char *fname, ass_track_t *track; size_t bufsize; - buf = read_file_recode(fname, codepage, &bufsize); + buf = read_file_recode(library, fname, codepage, &bufsize); if (!buf) return 0; track = parse_memory(library, buf); @@ -1143,7 +1155,8 @@ ass_track_t *ass_read_file(ass_library_t *library, char *fname, track->name = strdup(fname); - ass_msg(MSGL_INFO, "Added subtitle file: '%s' (%d styles, %d events)", + ass_msg(library, MSGL_INFO, + "Added subtitle file: '%s' (%d styles, %d events)", fname, track->n_styles, track->n_events); return track; @@ -1158,13 +1171,13 @@ int ass_read_styles(ass_track_t *track, char *fname, char *codepage) parser_state_t old_state; size_t sz; - buf = read_file(fname, &sz); + buf = read_file(track->library, fname, &sz); if (!buf) return 1; #ifdef CONFIG_ICONV if (codepage) { char *tmpbuf; - tmpbuf = sub_recode(buf, sz, codepage); + tmpbuf = sub_recode(track->library, buf, sz, codepage); free(buf); buf = tmpbuf; } |