/*
* Subtitle reader with format autodetection
*
* Copyright (c) 2001 laaz
* Some code cleanup & realloc() by A'rpi/ESP-team
*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include "config.h"
#include "core/mp_msg.h"
#include "subreader.h"
#include "core/mp_common.h"
#include "core/options.h"
#include "stream/stream.h"
#include "libavutil/common.h"
#include "libavutil/avstring.h"
#ifdef CONFIG_ENCA
#include <enca.h>
#endif
#define ERR ((void *) -1)
#ifdef CONFIG_ICONV
#include <iconv.h>
#endif
// Parameter struct for the format-specific readline functions
struct readline_args {
int utf16;
struct MPOpts *opts;
// subtitle reader state used by some formats
float mpsub_multiplier;
float mpsub_position;
int sub_slacktime;
/*
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.
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;
};
/* Maximal length of line of a subtitle */
#define LINE_LEN 1000
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';
}
static char *stristr(const char *haystack, const char *needle) {
int len = 0;
const char *p = haystack;
if (!(haystack && needle)) return NULL;
len=strlen(needle);
while (*p != '\0') {
if (strncasecmp(p, needle, len) == 0) return (char*)p;
p++;
}
return NULL;
}
static void sami_add_line(subtitle *current, char *buffer, char **pos) {
char *p = *pos;
*p = 0;
trail_space(buffer);
if (*buffer && current->lines < SUB_MAX_TEXT)
current->text[current->lines++] = strdup(buffer);
*pos = buffer;
}
static subtitle *sub_read_line_sami(stream_t* st, subtitle *current,
struct readline_args *args)
{
int utf16 = args->utf16;
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;
current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
state = 0;
/* read the first line */
if (!s)
if (!(s = stream_read_line(st, line, LINE_LEN, utf16))) return 0;
do {
swit
|