changeset 18936:b80b0c115a24

Split guess_cp() in two functions: - guess_buffer_cp() determines the encoding of text, given a pointer to a buffer and its length; - guess_cp() reads some data from stream and calls guess_buffer_cp()
author eugeni
date Fri, 07 Jul 2006 18:07:39 +0000
parents 880b99e9a891
children 9e95ac641e77
files subreader.c subreader.h
diffstat 2 files changed, 23 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/subreader.c	Fri Jul 07 18:04:56 2006 +0000
+++ b/subreader.c	Fri Jul 07 18:07:39 2006 +0000
@@ -1303,20 +1303,15 @@
 };
 
 #ifdef HAVE_ENCA
-#define MAX_GUESS_BUFFER_SIZE (256*1024)
-void* guess_cp(stream_t *st, char *preferred_language, char *fallback)
+void* guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_language, char *fallback)
 {
     const char **languages;
-    size_t langcnt, buflen;
+    size_t langcnt;
     EncaAnalyser analyser;
     EncaEncoding encoding;
-    unsigned char *buffer;
     char *detected_sub_cp = NULL;
     int i;
 
-    buffer = malloc(MAX_GUESS_BUFFER_SIZE);
-    buflen = stream_read(st,buffer, MAX_GUESS_BUFFER_SIZE);
-
     languages = enca_get_languages(&langcnt);
     mp_msg(MSGT_SUBREADER, MSGL_V, "ENCA supported languages: ");
     for (i = 0; i < langcnt; i++) {
@@ -1339,9 +1334,6 @@
     }
     
     free(languages);
-    free(buffer);
-    stream_reset(st);
-    stream_seek(st,0);
 
     if (!detected_sub_cp) {
 	detected_sub_cp = strdup(fallback);
@@ -1350,6 +1342,26 @@
 
     return detected_sub_cp;
 }
+
+#define MAX_GUESS_BUFFER_SIZE (256*1024)
+void* guess_cp(stream_t *st, char *preferred_language, char *fallback)
+{
+    size_t buflen;
+    unsigned char *buffer;
+    char *detected_sub_cp = NULL;
+
+    buffer = malloc(MAX_GUESS_BUFFER_SIZE);
+    buflen = stream_read(st,buffer, MAX_GUESS_BUFFER_SIZE);
+
+    detected_sub_cp = guess_buffer_cp(buffer, buflen, preferred_language, fallback);
+    
+    free(buffer);
+    stream_reset(st);
+    stream_seek(st,0);
+
+    return detected_sub_cp;
+}
+#undef MAX_GUESS_BUFFER_SIZE
 #endif
 
 sub_data* sub_read_file (char *filename, float fps) {
--- a/subreader.h	Fri Jul 07 18:04:56 2006 +0000
+++ b/subreader.h	Fri Jul 07 18:07:39 2006 +0000
@@ -73,6 +73,7 @@
 void subcp_open (struct stream_st *st); /* for demux_ogg.c */
 void subcp_close (void); /* for demux_ogg.c */
 #ifdef HAVE_ENCA
+void* guess_buffer_cp(unsigned char* buffer, int buflen, char *preferred_language, char *fallback);
 void* guess_cp(struct stream_st *st, char *preferred_language, char *fallback);
 #endif
 char ** sub_filenames(char *path, char *fname);