/*****************************************************************************
- * subtitles.c
+ * subtitles.c : subtitles detection
*****************************************************************************
- * Copyright (C) 2003-2006 the VideoLAN team
+ * Copyright (C) 2003-2009 VLC authors and VideoLAN
* $Id$
*
* Authors: Derk-Jan Hartman <hartman at videolan.org>
* This is adapted code from the GPL'ed MPlayer (http://mplayerhq.hu)
*
- * This program 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
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program 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.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/**
# include "config.h"
#endif
-#include <vlc_common.h>
-#include <vlc_input.h>
-#include <vlc_charset.h>
-
-#ifdef HAVE_DIRENT_H
-# include <dirent.h>
-#endif
-
-#include <limits.h>
-
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
+#include <ctype.h> /* isalnum() */
+#include <unistd.h>
#include <sys/stat.h>
-#include <ctype.h>
+#include <vlc_common.h>
+#include <vlc_fs.h>
+#include <vlc_url.h>
+
#include "input_internal.h"
/**
*/
#define MAX_SUBTITLE_FILES 128
-
/**
* The possible extensions for subtitle files we support
*/
-static const char const sub_exts[][6] = {
+static const char sub_exts[][6] = {
+ "idx", "sub", "srt",
+ "ssa", "ass", "smi",
"utf", "utf8", "utf-8",
- "sub", "srt", "smi",
- "txt", "ssa", "idx",
-
- "cdg",
-
- ""
+ "rt", "aqt", "txt",
+ "usf", "jss", "cdg",
+ "psb", "mpsub","mpl2",
+ "pjs", "dks", "stl",
+ "vtt",""
};
-/* extensions from unsupported types */
-/* rt, aqt, jss, js, ass */
-
static void strcpy_trim( char *d, const char *s )
{
+ unsigned char c;
+
/* skip leading whitespace */
- while( *s && !isalnum(*s) )
+ while( ((c = *s) != '\0') && !isalnum(c) )
{
s++;
}
for(;;)
{
/* copy word */
- while( *s && isalnum(*s) )
+ while( ((c = *s) != '\0') && isalnum(c) )
{
- *d = tolower(*s);
+ *d = tolower(c);
s++; d++;
}
if( *s == 0 ) break;
/* trim excess whitespace */
- while( *s && !isalnum(*s) )
+ while( ((c = *s) != '\0') && !isalnum(c) )
{
s++;
}
static void strcpy_strip_ext( char *d, const char *s )
{
+ unsigned char c;
+
const char *tmp = strrchr(s, '.');
if( !tmp )
{
}
else
strlcpy(d, s, tmp - s + 1 );
- while( *d )
+ while( (c = *d) != '\0' )
{
- *d = tolower(*d);
+ *d = tolower(c);
d++;
}
}
static int whiteonly( const char *s )
{
- while( *s )
+ unsigned char c;
+
+ while( (c = *s) != '\0' )
{
- if( isalnum( *s ) )
+ if( isalnum( c ) )
return 0;
s++;
}
enum
{
- SUB_PRIORITY_NONE = 0,
- SUB_PRIORITY_MATCH_NONE = 1,
+ SUB_PRIORITY_NONE = 0,
+ SUB_PRIORITY_MATCH_NONE = 1,
SUB_PRIORITY_MATCH_RIGHT = 2,
- SUB_PRIORITY_MATCH_LEFT = 3,
- SUB_PRIORITY_MATCH_ALL = 4,
+ SUB_PRIORITY_MATCH_LEFT = 3,
+ SUB_PRIORITY_MATCH_ALL = 4,
};
typedef struct
{
if( p0->priority < p1->priority )
return 1;
-#ifndef UNDER_CE
+#ifdef HAVE_STRCOLL
return strcoll( p0->psz_fname, p1->psz_fname);
#else
return strcmp( p0->psz_fname, p1->psz_fname);
int subtitles_Filter( const char *psz_dir_content )
{
const char *tmp = strrchr( psz_dir_content, '.');
- int i;
if( !tmp )
return 0;
tmp++;
- for( i = 0; sub_exts[i][0]; i++ )
+ for( int i = 0; sub_exts[i][0]; i++ )
if( strcasecmp( sub_exts[i], tmp ) == 0 )
return 1;
return 0;
if( *psz_subdir == '\0' )
continue;
- if( asprintf( &subdirs[i++], "%s%s%c",
+ if( asprintf( &subdirs[i++], "%s%s",
psz_subdir[0] == '.' ? psz_dir : "",
- psz_subdir,
- psz_subdir[strlen(psz_subdir) - 1] == DIR_SEP_CHAR ?
- '\0' : DIR_SEP_CHAR ) == -1 )
+ psz_subdir ) == -1 )
break;
}
subdirs[i] = NULL;
char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
const char *psz_name_org )
{
- vlc_value_t fuzzy;
+ int i_fuzzy = var_GetInteger( p_this, "sub-autodetect-fuzzy" );
+ if ( i_fuzzy == 0 )
+ return NULL;
int j, i_result2, i_sub_count, i_fname_len;
- char *f_dir = NULL, *f_fname = NULL, *f_fname_noext = NULL, *f_fname_trim = NULL;
- char *tmp = NULL;
+ char *f_fname_noext = NULL, *f_fname_trim = NULL;
char **subdirs; /* list of subdirectories to look in */
vlc_subfn_t *result = NULL; /* unsorted results */
char **result2; /* sorted results */
- const char *psz_fname = psz_name_org;
- if( !psz_fname )
+ if( !psz_name_org )
return NULL;
- if( !strncmp( psz_fname, "file://", 7 ) )
- psz_fname += 7;
+ char *psz_fname = make_path( psz_name_org );
+ if( !psz_fname )
+ return NULL;
/* extract filename & dirname from psz_fname */
- tmp = strrchr( psz_fname, DIR_SEP_CHAR );
- if( tmp )
+ char *f_dir = strdup( psz_fname );
+ if( f_dir == NULL )
{
- const int i_dirlen = strlen(psz_fname)-strlen(tmp)+1; /* include the separator */
- f_fname = strdup( &tmp[1] ); /* skip the separator */
- f_dir = strndup( psz_fname, i_dirlen );
+ free( psz_fname );
+ return NULL;
}
- else
- {
-#ifdef HAVE_UNISTD_H
- /* Get the current working directory */
- char *psz_cwd = getcwd( NULL, 0 );
-#else
- char *psz_cwd = NULL;
-#endif
- if( !psz_cwd )
- return NULL;
- f_fname = strdup( psz_fname );
- if( asprintf( &f_dir, "%s%c", psz_cwd, DIR_SEP_CHAR ) == -1 )
- f_dir = NULL; /* Assure that function will return in next test */
- free( psz_cwd );
- }
- if( !f_fname || !f_dir )
+ const char *f_fname = strrchr( psz_fname, DIR_SEP_CHAR );
+ if( !f_fname )
{
- free( f_fname );
free( f_dir );
+ free( psz_fname );
return NULL;
}
+ f_fname++; /* Skip the '/' */
+ f_dir[f_fname - psz_fname] = 0; /* keep dir separator in f_dir */
i_fname_len = strlen( f_fname );
f_fname_trim = malloc(i_fname_len + 1 );
if( !f_fname_noext || !f_fname_trim )
{
- free( f_fname );
free( f_dir );
free( f_fname_noext );
free( f_fname_trim );
+ free( psz_fname );
return NULL;
}
strcpy_strip_ext( f_fname_noext, f_fname );
strcpy_trim( f_fname_trim, f_fname_noext );
- var_Get( p_this, "sub-autodetect-fuzzy", &fuzzy );
-
result = calloc( MAX_SUBTITLE_FILES+1, sizeof(vlc_subfn_t) ); /* We check it later (simplify code) */
subdirs = paths_to_list( f_dir, psz_path );
for( j = -1, i_sub_count = 0; (j == -1) || ( j >= 0 && subdirs != NULL && subdirs[j] != NULL ); j++ )
{
- const char *psz_dir = j < 0 ? f_dir : subdirs[j];
- char **ppsz_dir_content;
- int i_dir_content;
- int a;
-
+ const char *psz_dir = (j < 0) ? f_dir : subdirs[j];
if( psz_dir == NULL || ( j >= 0 && !strcmp( psz_dir, f_dir ) ) )
continue;
/* parse psz_src dir */
- i_dir_content = utf8_scandir( psz_dir, &ppsz_dir_content,
- subtitles_Filter, NULL );
- if( i_dir_content < 0 )
+ DIR *dir = vlc_opendir( psz_dir );
+ if( dir == NULL )
continue;
msg_Dbg( p_this, "looking for a subtitle file in %s", psz_dir );
- for( a = 0; a < i_dir_content && i_sub_count < MAX_SUBTITLE_FILES ; a++ )
+
+ const char *psz_name;
+ while( (psz_name = vlc_readdir( dir )) && i_sub_count < MAX_SUBTITLE_FILES )
{
- char *psz_name = ppsz_dir_content[a];
+ if( psz_name[0] == '.' || !subtitles_Filter( psz_name ) )
+ continue;
+
char tmp_fname_noext[strlen( psz_name ) + 1];
char tmp_fname_trim[strlen( psz_name ) + 1];
char tmp_fname_ext[strlen( psz_name ) + 1];
-
- int i_prio;
-
- if( psz_name == NULL )
- continue;
+ const char *tmp;
+ int i_prio = SUB_PRIORITY_NONE;
/* retrieve various parts of the filename */
strcpy_strip_ext( tmp_fname_noext, psz_name );
strcpy_get_ext( tmp_fname_ext, psz_name );
strcpy_trim( tmp_fname_trim, tmp_fname_noext );
- i_prio = SUB_PRIORITY_NONE;
- if( i_prio == SUB_PRIORITY_NONE && !strcmp( tmp_fname_trim, f_fname_trim ) )
+ if( !strcmp( tmp_fname_trim, f_fname_trim ) )
{
/* matches the movie name exactly */
i_prio = SUB_PRIORITY_MATCH_ALL;
}
- if( i_prio == SUB_PRIORITY_NONE &&
- ( tmp = strstr( tmp_fname_trim, f_fname_trim ) ) )
+ else if( (tmp = strstr( tmp_fname_trim, f_fname_trim )) )
{
/* contains the movie name */
tmp += strlen( f_fname_trim );
i_prio = SUB_PRIORITY_MATCH_LEFT;
}
}
- if( i_prio == SUB_PRIORITY_NONE &&
- j == 0 )
+ else if( j == -1 )
{
/* doesn't contain the movie name, prefer files in f_dir over subdirs */
i_prio = SUB_PRIORITY_MATCH_NONE;
}
- if( i_prio >= fuzzy.i_int )
+ if( i_prio >= i_fuzzy )
{
- char psz_path[strlen( psz_dir ) + strlen( psz_name ) + 1];
struct stat st;
+ char *path;
- sprintf( psz_path, "%s%s", psz_dir, psz_name );
- if( !strcmp( psz_path, psz_fname ) )
+ if( asprintf( &path, "%s"DIR_SEP"%s", psz_dir, psz_name ) < 0 )
continue;
- if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) && result )
+ if( strcmp( path, psz_fname )
+ && vlc_stat( path, &st ) == 0
+ && S_ISREG( st.st_mode ) && result )
{
msg_Dbg( p_this,
"autodetected subtitle: %s with priority %d",
- psz_path, i_prio );
+ path, i_prio );
result[i_sub_count].priority = i_prio;
- result[i_sub_count].psz_fname = strdup( psz_path );
+ result[i_sub_count].psz_fname = path;
+ path = NULL;
result[i_sub_count].psz_ext = strdup(tmp_fname_ext);
i_sub_count++;
}
- else
- {
- msg_Dbg( p_this, "stat failed (autodetecting subtitle: %s with priority %d)",
- psz_path, i_prio );
- }
+ free( path );
}
}
- if( ppsz_dir_content )
- {
- for( a = 0; a < i_dir_content; a++ )
- free( ppsz_dir_content[a] );
- free( ppsz_dir_content );
- }
+ closedir( dir );
}
if( subdirs )
{
free( subdirs[j] );
free( subdirs );
}
- free( f_fname );
free( f_dir );
free( f_fname_trim );
free( f_fname_noext );
+ free( psz_fname );
if( !result )
return NULL;