/*
* Subtitle reader with format autodetection
*
* Written by laaz
* Some code cleanup & realloc() by A'rpi/ESP-team
* dunnowhat sub format by szabi
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
#include "mp_msg.h"
#include "subreader.h"
#define ERR ((void *) -1)
#ifdef USE_ICONV
#include <iconv.h>
char *sub_cp=NULL;
#endif
/* Maximal length of line of a subtitle */
#define LINE_LEN 1000
static float mpsub_position=0;
int sub_uses_time=0;
int sub_errs=0;
int sub_num=0; // number of subtitle structs
int sub_slacktime=2000; // 20 seconds
/* Use the SUB_* constant defined in the header file */
int sub_format=SUB_INVALID;
#ifdef USE_SORTSUB
/*
Some subtitling formats, namely AQT and Subrip09, define the end of a
subtitle as the beginning of the following. Since currently we read one
subtitle at time, for these format we keep two global *subtitle,
previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
so we can change its end when we read current subtitle starting time.
When USE_SORTSUB is defined, we use a single global unsigned long,
previous_sub_end, for both (and even future) formats, to store the end of
the previous sub: it is initialized to 0 in sub_read_file and eventually
modified by sub_read_aqt_line or sub_read_subrip09_line.
*/
unsigned long previous_sub_end;
#endif
static int eol(char p) {
return (p=='\r' || p=='\n' || p=='\0');
}
/* Remove leading and trailing space */
static void trail_space(char *s) {
int i = 0;
while (isspace(s[i])) ++i;
if (i) strcpy(s, s + i);
i = strlen(s) - 1;
while (i > 0 && isspace(s[i])) s[i--] = '\0';
}
subtitle *sub_read_line_sami(FILE *fd, subtitle *current) {
static char line[LINE_LEN+1];
static char *s = NULL, *slacktime_s;
char text[LINE_LEN+1], *p=NULL, *q;
int state;
current->lines = current->start = current->end = 0;
state = 0;
/* read the first line */
if (!s)
if (!(s = fgets(line, LINE_LEN, fd))) return 0;
do {
switch (state) {
case 0: /* find "START=" or "Slacktime:" */
slacktime_s = strstr (s, "Slacktime:");
if (slacktime_s) sub_slacktime = strtol (slacktime_s + 10, NULL, 0) / 10;
s = strstr (s, "Start=");
if (s) {
current->start = strtol (s + 6, &s, 0) / 10;
state = 1; continue;
}
break;
case 1: /* find "<P" */
if ((s = strstr (s, "<P"))) { s += 2; state = 2; continue; }
break;
case 2: /* find ">" */
if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; }
break;
case 3: /* get all text until '<' appears */
if (*s == '\0') break;
else if (!strncasecmp (s, "<br>", 4)) {
*p = '\0'; p = text; trail_space (text);
if (text[0] != '\0')
curre
|