From 07e21ed65e8e544a11067c16bc3d386e59651c17 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Fri, 23 May 2003 06:34:58 +0000 Subject: [PATCH] Switching from fwrite/fread etc. to mm_io_c. --- common.h | 4 +-- mkvinfo.cpp | 8 +++--- mkvmerge.cpp | 73 +++++++++++++++++++++++++------------------------- mm_io.cpp | 42 ++++++++++++++++++----------- mm_io.h | 18 +++++++------ r_aac.cpp | 69 +++++++++++++++++++++++------------------------ r_aac.h | 11 ++++---- r_ac3.cpp | 44 +++++++++++++++--------------- r_ac3.h | 7 ++--- r_avi.cpp | 37 +++++++++++++------------ r_avi.h | 5 ++-- r_dts.cpp | 44 +++++++++++++++--------------- r_dts.h | 7 ++--- r_matroska.cpp | 15 ++++++----- r_matroska.h | 5 ++-- r_microdvd.cpp | 12 ++++----- r_microdvd.h | 4 +-- r_mp3.cpp | 44 +++++++++++++++--------------- r_mp3.h | 7 ++--- r_mp4.cpp | 17 +++++++----- r_mp4.h | 6 +++-- r_ogm.cpp | 36 +++++++++++++------------ r_ogm.h | 7 ++--- r_srt.cpp | 53 ++++++++++++++++++++---------------- r_srt.h | 7 ++--- r_vobsub.cpp | 24 ++++++++--------- r_vobsub.h | 4 +-- r_wav.cpp | 45 ++++++++++++++++--------------- r_wav.h | 7 ++--- 29 files changed, 350 insertions(+), 312 deletions(-) diff --git a/common.h b/common.h index eae2dab65..7464fe175 100644 --- a/common.h +++ b/common.h @@ -13,7 +13,7 @@ /*! \file - \version \$Id: common.h,v 1.28 2003/05/21 21:05:47 mosu Exp $ + \version \$Id: common.h,v 1.29 2003/05/23 06:34:57 mosu Exp $ \brief definitions used in all programs, helper functions \author Moritz Bunkus */ @@ -26,7 +26,7 @@ #elif defined WIN32 #include #define PACKAGE "mkvtoolnix" -#define VERSION 0.3.2 +#define VERSION 0.4.0 #endif #include diff --git a/mkvinfo.cpp b/mkvinfo.cpp index 18f7b11cc..086685df8 100644 --- a/mkvinfo.cpp +++ b/mkvinfo.cpp @@ -12,7 +12,7 @@ /*! \file - \version \$Id: mkvinfo.cpp,v 1.46 2003/05/22 17:54:43 mosu Exp $ + \version \$Id: mkvinfo.cpp,v 1.47 2003/05/23 06:34:57 mosu Exp $ \brief retrieves and displays information about a Matroska file \author Moritz Bunkus */ @@ -67,7 +67,7 @@ extern "C" { #include "common.h" #include "matroska.h" -#include "mm_io_callback.h" +#include "mm_io.h" #define NAME "MKVInfo" @@ -82,7 +82,7 @@ typedef struct { mkv_track_t **tracks = NULL; int num_tracks = 0; -mm_io_callback *in = NULL; +mm_io_c *in = NULL; void add_track(mkv_track_t *s) { tracks = (mkv_track_t **)saferealloc(tracks, sizeof(mkv_track_t *) * @@ -151,7 +151,7 @@ static void parse_args(int argc, char **argv) { /* open input file */ try { - in = new mm_io_callback(infile, MODE_READ); + in = new mm_io_c(infile, MODE_READ); } catch (std::exception &ex) { fprintf(stderr, "Error: Couldn't open input file %s (%s).\n", infile, strerror(errno)); diff --git a/mkvmerge.cpp b/mkvmerge.cpp index dea52f029..ec7af2633 100644 --- a/mkvmerge.cpp +++ b/mkvmerge.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: mkvmerge.cpp,v 1.78 2003/05/22 17:54:43 mosu Exp $ + \version \$Id: mkvmerge.cpp,v 1.79 2003/05/23 06:34:57 mosu Exp $ \brief command line parameter parsing, looping, output handling \author Moritz Bunkus */ @@ -59,7 +59,7 @@ #include "KaxVersion.h" #include "mkvmerge.h" -#include "mm_io_callback.h" +#include "mm_io.h" #include "cluster_helper.h" #include "common.h" #include "iso639.h" @@ -87,7 +87,7 @@ typedef struct { typedef struct filelist_tag { char *name; - FILE *fp; + mm_io_c *fp; int type; @@ -134,7 +134,7 @@ int64_t meta_seek_size = 0; // Specs say that track numbers should start at 1. int track_number = 1; -mm_io_callback *out; +mm_io_c *out; file_type_t file_types[] = {{"---", TYPEUNKNOWN, ""}, @@ -224,59 +224,55 @@ static void usage(void) { } static int get_type(char *filename) { - FILE *f = fopen(filename, "rb"); + mm_io_c *mm_io; off_t size; int type; - if (f == NULL) { - fprintf(stderr, "Error: could not open source file (%s).\n", filename); + try { + mm_io = new mm_io_c(filename, MODE_READ); + mm_io->setFilePointer(0, seek_end); + size = mm_io->getFilePointer(); + mm_io->setFilePointer(0, seek_current); + } catch (exception &ex) { + fprintf(stderr, "Error: could not open source file or seek properly in it " + "(%s).\n", filename); exit(1); } - if (fseeko(f, 0, SEEK_END) != 0) { - fprintf(stderr, "Error: could not seek to end of file (%s).\n", filename); - exit(1); - } - size = ftello(f); - if (fseeko(f, 0, SEEK_SET) != 0) { - fprintf(stderr, "Error: could not seek to beginning of file (%s).\n", - filename); - exit(1); - } - if (avi_reader_c::probe_file(f, size)) + if (avi_reader_c::probe_file(mm_io, size)) type = TYPEAVI; - else if (mkv_reader_c::probe_file(f, size)) + else if (mkv_reader_c::probe_file(mm_io, size)) type = TYPEMATROSKA; - else if (wav_reader_c::probe_file(f, size)) + else if (wav_reader_c::probe_file(mm_io, size)) type = TYPEWAV; - else if (mp4_reader_c::probe_file(f, size)) { + else if (mp4_reader_c::probe_file(mm_io, size)) { fprintf(stderr, "Error: MP4/Quicktime files are not supported (%s).\n", filename); exit(1); } #ifdef HAVE_OGGVORBIS - else if (ogm_reader_c::probe_file(f, size)) + else if (ogm_reader_c::probe_file(mm_io, size)) type = TYPEOGM; #endif // HAVE_OGGVORBIS - else if (srt_reader_c::probe_file(f, size)) + else if (srt_reader_c::probe_file(mm_io, size)) type = TYPESRT; - else if (mp3_reader_c::probe_file(f, size)) + else if (mp3_reader_c::probe_file(mm_io, size)) type = TYPEMP3; - else if (ac3_reader_c::probe_file(f, size)) + else if (ac3_reader_c::probe_file(mm_io, size)) type = TYPEAC3; - else if (dts_reader_c::probe_file(f, size)) + else if (dts_reader_c::probe_file(mm_io, size)) type = TYPEDTS; - else if (aac_reader_c::probe_file(f, size)) + else if (aac_reader_c::probe_file(mm_io, size)) type = TYPEAAC; -// else if (microdvd_reader_c::probe_file(f, size)) +// else if (microdvd_reader_c::probe_file(mm_io, size)) // type = TYPEMICRODVD; -// else if (vobsub_reader_c::probe_file(f, size)) +// else if (vobsub_reader_c::probe_file(mm_io, size)) // type = TYPEVOBSUB; -// else if (chapter_information_probe(f, size)) +// else if (chapter_information_probe(mm_io, size)) // type = TYPECHAPTERS; else type = TYPEUNKNOWN; - fclose(f); + delete mm_io; file_sizes += size; @@ -479,7 +475,7 @@ static float parse_aspect_ratio(char *s) { // return seconds; // } -static void render_headers(mm_io_callback *out) { +static void render_headers(mm_io_c *out) { EbmlHead head; filelist_t *file; @@ -970,17 +966,18 @@ static char *strip(char *s) { } static char **read_args_from_file(int &num_args, char **args, char *filename) { - FILE *f; + mm_io_c *mm_io; char buffer[8192], *space; - f = fopen(filename, "r"); - if (f == NULL) { + try { + mm_io = new mm_io_c(filename, MODE_READ); + } catch (exception &ex) { fprintf(stderr, "Error: Could not open file '%s' for reading command line " "arguments from.", filename); exit(1); } - while (!feof(f) && (fgets(buffer, 8191, f) != NULL)) { + while (!mm_io->eof() && (mm_io->gets(buffer, 8191) != NULL)) { buffer[8191] = 0; strip(buffer); if ((buffer[0] == '#') || (buffer[0] == 0)) @@ -997,6 +994,8 @@ static char **read_args_from_file(int &num_args, char **args, char *filename) { args = add_string(num_args, args, space); } + delete mm_io; + return args; } @@ -1059,7 +1058,7 @@ int main(int argc, char **argv) { // Open the output file. try { - out = new mm_io_callback(outfile, MODE_CREATE); + out = new mm_io_c(outfile, MODE_CREATE); } catch (std::exception &ex) { fprintf(stderr, "Error: Couldn't open output file %s (%s).\n", outfile, strerror(errno)); diff --git a/mm_io.cpp b/mm_io.cpp index 63f9582fd..865483b03 100644 --- a/mm_io.cpp +++ b/mm_io.cpp @@ -2,7 +2,7 @@ mkvmerge -- utility for splicing together matroska files from component media subtypes - mm_io_callback.cpp + mm_io_c.cpp Written by Moritz Bunkus @@ -13,19 +13,23 @@ /*! \file - \version \$Id: mm_io.cpp,v 1.1 2003/05/22 17:54:43 mosu Exp $ + \version \$Id: mm_io.cpp,v 1.2 2003/05/23 06:34:57 mosu Exp $ \brief IO callback class implementation \author Moritz Bunkus */ +#include + #include #include #include #include -#include "mm_io_callback.h" +#include "mm_io.h" -mm_io_callback::mm_io_callback(const char *path, const open_mode mode) { +using namespace std; + +mm_io_c::mm_io_c(const char *path, const open_mode mode) { char *cmode; switch (mode) { @@ -44,22 +48,19 @@ mm_io_callback::mm_io_callback(const char *path, const open_mode mode) { file = fopen(path, cmode); - if (file == NULL) { - fprintf(stderr, "Error: Could not open the file: %d (%s)\n", errno, - strerror(errno)); - exit(1); - } + if (file == NULL) + throw exception(); } -mm_io_callback::~mm_io_callback() { +mm_io_c::~mm_io_c() { close(); } -uint64 mm_io_callback::getFilePointer() { +uint64 mm_io_c::getFilePointer() { return ftello(file); } -void mm_io_callback::setFilePointer(int64 offset, seek_mode mode) { +void mm_io_c::setFilePointer(int64 offset, seek_mode mode) { int whence; if (mode == seek_beginning) @@ -69,10 +70,11 @@ void mm_io_callback::setFilePointer(int64 offset, seek_mode mode) { else whence = SEEK_CUR; - fseeko(file, offset, whence); + if (fseeko(file, offset, whence) != 0) + throw exception(); } -size_t mm_io_callback::write(const void *buffer, size_t size) { +size_t mm_io_c::write(const void *buffer, size_t size) { size_t bwritten; bwritten = fwrite(buffer, 1, size, file); @@ -85,14 +87,22 @@ size_t mm_io_callback::write(const void *buffer, size_t size) { return bwritten; } -uint32 mm_io_callback::read(void *buffer, size_t size) { +uint32 mm_io_c::read(void *buffer, size_t size) { return fread(buffer, 1, size, file); } -void mm_io_callback::close() { +void mm_io_c::close() { fclose(file); } +bool mm_io_c::eof() { + return feof(file) != 0 ? true : false; +} + +char *mm_io_c::gets(char *buffer, size_t max_size) { + return fgets(buffer, max_size, file); +} + /* * Dummy class for output to /dev/null. Needed for two pass stuff. */ diff --git a/mm_io.h b/mm_io.h index 7eafb81bc..171ba7c18 100644 --- a/mm_io.h +++ b/mm_io.h @@ -2,7 +2,7 @@ mkvmerge -- utility for splicing together matroska files from component media subtypes - mm_io_callback.h + mm_io_c.h Written by Moritz Bunkus @@ -13,13 +13,13 @@ /*! \file - \version \$Id: mm_io.h,v 1.1 2003/05/22 17:54:43 mosu Exp $ + \version \$Id: mm_io.h,v 1.2 2003/05/23 06:34:57 mosu Exp $ \brief IO callback class definitions \author Moritz Bunkus */ -#ifndef __MM_IO_CALLBACK_H -#define __MM_IO_CALLBACK_H +#ifndef __MM_IO_H +#define __MM_IO_H #include #include @@ -28,19 +28,21 @@ using namespace LIBEBML_NAMESPACE; -class mm_io_callback: public IOCallback { +class mm_io_c: public IOCallback { protected: FILE *file; public: - mm_io_callback(const char *path, const open_mode mode); - virtual ~mm_io_callback(); + mm_io_c(const char *path, const open_mode mode); + virtual ~mm_io_c(); virtual uint64 getFilePointer(); virtual void setFilePointer(int64 offset, seek_mode mode=seek_beginning); virtual uint32 read(void *buffer, size_t size); virtual size_t write(const void *buffer, size_t size); virtual void close(); + virtual bool eof(); + virtual char *gets(char *buffer, size_t max_size); }; class mm_devnull_io_callback: public IOCallback { @@ -57,4 +59,4 @@ public: virtual void close(); }; -#endif // __MM_IO_CALLBACK_H +#endif // __MM_IO_H diff --git a/r_aac.cpp b/r_aac.cpp index 96292679a..113c68eaa 100644 --- a/r_aac.cpp +++ b/r_aac.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_aac.cpp,v 1.6 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_aac.cpp,v 1.7 2003/05/23 06:34:57 mosu Exp $ \brief AAC demultiplexer module \author Moritz Bunkus */ @@ -29,20 +29,20 @@ #include "r_aac.h" #include "p_aac.h" -int aac_reader_c::probe_file(FILE *file, int64_t size) { +int aac_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { char buf[4096]; aac_header_t aacheader; if (size < 4096) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) - return 0; - if (fread(buf, 1, 4096, file) != 4096) { - fseeko(file, 0, SEEK_SET); + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->read(buf, 4096) != 4096) + mm_io->setFilePointer(0, seek_beginning); + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; } - fseeko(file, 0, SEEK_SET); - if (parse_aac_adif_header((unsigned char *)buf, 4096, &aacheader)) return 1; if (find_aac_header((unsigned char *)buf, 4096, &aacheader) < 0) @@ -56,38 +56,37 @@ aac_reader_c::aac_reader_c(track_info_t *nti) throw (error_c): int adif; aac_header_t aacheader; - if ((file = fopen(ti->fname, "rb")) == NULL) - throw error_c("aac_reader: Could not open source file."); - if (fseeko(file, 0, SEEK_END) != 0) - throw error_c("aac_reader: Could not seek to end of file."); - size = ftello(file); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("aac_reader: Could not seek to beginning of file."); - chunk = (unsigned char *)safemalloc(4096); - if (fread(chunk, 1, 4096, file) != 4096) - throw error_c("aac_reader: Could not read 4096 bytes."); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("aac_reader: Could not seek to beginning of file."); - if (parse_aac_adif_header(chunk, 4096, &aacheader)) { - throw error_c("aac_reader: ADIF header files are not supported."); - adif = 1; - } else if (find_aac_header(chunk, 4096, &aacheader) < 0) - throw error_c("aac_reader: No valid AAC packet found in the first " \ - "4096 bytes.\n"); - else - adif = 0; - bytes_processed = 0; - aacpacketizer = new aac_packetizer_c(this, aacheader.id, aacheader.profile, - aacheader.sample_rate, - aacheader.channels, ti); + try { + mm_io = new mm_io_c(ti->fname, MODE_READ); + mm_io->setFilePointer(0, seek_end); + size = mm_io->getFilePointer(); + mm_io->setFilePointer(0, seek_beginning); + chunk = (unsigned char *)safemalloc(4096); + if (mm_io->read(chunk, 4096) != 4096) + throw error_c("aac_reader: Could not read 4096 bytes."); + mm_io->setFilePointer(0, seek_beginning); + if (parse_aac_adif_header(chunk, 4096, &aacheader)) { + throw error_c("aac_reader: ADIF header files are not supported."); + adif = 1; + } else if (find_aac_header(chunk, 4096, &aacheader) < 0) + throw error_c("aac_reader: No valid AAC packet found in the first " \ + "4096 bytes.\n"); + else + adif = 0; + bytes_processed = 0; + aacpacketizer = new aac_packetizer_c(this, aacheader.id, aacheader.profile, + aacheader.sample_rate, + aacheader.channels, ti); + } catch (exception &ex) { + throw error_c("aac_reader: Could not open the file."); + } if (verbose) fprintf(stdout, "Using AAC demultiplexer for %s.\n+-> Using " \ "AAC output module for audio stream.\n", ti->fname); } aac_reader_c::~aac_reader_c() { - if (file != NULL) - fclose(file); + delete mm_io; if (chunk != NULL) safefree(chunk); if (aacpacketizer != NULL) @@ -97,7 +96,7 @@ aac_reader_c::~aac_reader_c() { int aac_reader_c::read() { int nread; - nread = fread(chunk, 1, 4096, file); + nread = mm_io->read(chunk, 4096); if (nread <= 0) return 0; diff --git a/r_aac.h b/r_aac.h index 6ed777e60..03d1fd26a 100644 --- a/r_aac.h +++ b/r_aac.h @@ -13,7 +13,7 @@ /*! \file r_avi.h - \version \$Id: r_aac.h,v 1.3 2003/05/20 06:30:24 mosu Exp $ + \version \$Id: r_aac.h,v 1.4 2003/05/23 06:34:57 mosu Exp $ \brief class definitions for the AVI demultiplexer module \author Moritz Bunkus */ @@ -23,17 +23,18 @@ #include +#include "mm_io.h" #include "pr_generic.h" #include "common.h" #include "error.h" -//#include "p_aac.h" +#include "p_aac.h" #include "aac_common.h" class aac_reader_c: public generic_reader_c { private: unsigned char *chunk; - FILE *file; - class aac_packetizer_c *aacpacketizer; + mm_io_c *mm_io; + aac_packetizer_c *aacpacketizer; int64_t bytes_processed, size; public: @@ -46,7 +47,7 @@ public: virtual void display_progress(); virtual void set_headers(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); }; #endif // __R_AAC_H diff --git a/r_ac3.cpp b/r_ac3.cpp index 6353602c4..3cd52183a 100644 --- a/r_ac3.cpp +++ b/r_ac3.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_ac3.cpp,v 1.23 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_ac3.cpp,v 1.24 2003/05/23 06:34:57 mosu Exp $ \brief AC3 demultiplexer module \author Moritz Bunkus */ @@ -33,20 +33,21 @@ extern "C" { #include "r_ac3.h" #include "p_ac3.h" -int ac3_reader_c::probe_file(FILE *file, int64_t size) { +int ac3_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { char buf[4096]; int pos; ac3_header_t ac3header; if (size < 4096) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) - return 0; - if (fread(buf, 1, 4096, file) != 4096) { - fseeko(file, 0, SEEK_SET); + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->read(buf, 4096) != 4096) + return 0; + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; } - fseeko(file, 0, SEEK_SET); pos = find_ac3_header((unsigned char *)buf, 4096, &ac3header); if (pos < 0) @@ -60,18 +61,18 @@ ac3_reader_c::ac3_reader_c(track_info_t *nti) throw (error_c): int pos; ac3_header_t ac3header; - if ((file = fopen(ti->fname, "rb")) == NULL) - throw error_c("ac3_reader: Could not open source file."); - if (fseeko(file, 0, SEEK_END) != 0) - throw error_c("ac3_reader: Could not seek to end of file."); - size = ftello(file); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("ac3_reader: Could not seek to beginning of file."); - chunk = (unsigned char *)safemalloc(4096); - if (fread(chunk, 1, 4096, file) != 4096) - throw error_c("ac3_reader: Could not read 4096 bytes."); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("ac3_reader: Could not seek to beginning of file."); + try { + mm_io = new mm_io_c(ti->fname, MODE_READ); + mm_io->setFilePointer(0, seek_end); + size = mm_io->getFilePointer(); + mm_io->setFilePointer(0, seek_beginning); + chunk = (unsigned char *)safemalloc(4096); + if (mm_io->read(chunk, 4096) != 4096) + throw error_c("ac3_reader: Could not read 4096 bytes."); + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { + throw error_c("ac3_reader: Could not open the source file."); + } pos = find_ac3_header(chunk, 4096, &ac3header); if (pos < 0) throw error_c("ac3_reader: No valid AC3 packet found in the first " \ @@ -85,8 +86,7 @@ ac3_reader_c::ac3_reader_c(track_info_t *nti) throw (error_c): } ac3_reader_c::~ac3_reader_c() { - if (file != NULL) - fclose(file); + delete mm_io; if (chunk != NULL) safefree(chunk); if (ac3packetizer != NULL) @@ -96,7 +96,7 @@ ac3_reader_c::~ac3_reader_c() { int ac3_reader_c::read() { int nread; - nread = fread(chunk, 1, 4096, file); + nread = mm_io->read(chunk, 4096); if (nread <= 0) return 0; diff --git a/r_ac3.h b/r_ac3.h index 33e0908e0..2d50484eb 100644 --- a/r_ac3.h +++ b/r_ac3.h @@ -13,7 +13,7 @@ /*! \file r_avi.h - \version \$Id: r_ac3.h,v 1.11 2003/05/20 06:30:24 mosu Exp $ + \version \$Id: r_ac3.h,v 1.12 2003/05/23 06:34:57 mosu Exp $ \brief class definitions for the AVI demultiplexer module \author Moritz Bunkus */ @@ -23,6 +23,7 @@ #include +#include "mm_io.h" #include "pr_generic.h" #include "common.h" #include "error.h" @@ -32,7 +33,7 @@ class ac3_reader_c: public generic_reader_c { private: unsigned char *chunk; - FILE *file; + mm_io_c *mm_io; class ac3_packetizer_c *ac3packetizer; int64_t bytes_processed, size; @@ -46,7 +47,7 @@ public: virtual void display_progress(); virtual void set_headers(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); }; #endif // __R_AC3_H diff --git a/r_avi.cpp b/r_avi.cpp index bdb5b8e7f..6ec2df7db 100644 --- a/r_avi.cpp +++ b/r_avi.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_avi.cpp,v 1.33 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_avi.cpp,v 1.34 2003/05/23 06:34:57 mosu Exp $ \brief AVI demultiplexer module \author Moritz Bunkus */ @@ -36,17 +36,19 @@ extern "C" { #include "p_mp3.h" #include "p_ac3.h" -int avi_reader_c::probe_file(FILE *file, int64_t size) { +int avi_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { unsigned char data[12]; if (size < 12) return 0; - fseeko(file, 0, SEEK_SET); - if (fread(data, 1, 12, file) != 12) { - fseeko(file, 0, SEEK_SET); + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->read(data, 12) != 12) + return 0; + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; } - fseeko(file, 0, SEEK_SET); if(strncasecmp((char *)data, "RIFF", 4) || strncasecmp((char *)data+8, "AVI ", 4)) return 0; @@ -61,20 +63,21 @@ avi_reader_c::avi_reader_c(track_info_t *nti) throw (error_c): generic_reader_c(nti) { int fsize, i, extract_video = 1; int64_t size; - FILE *f; + mm_io_c *mm_io; avi_demuxer_t *demuxer; char *codec; - if ((f = fopen(ti->fname, "rb")) == NULL) - throw error_c("avi_reader: Could not open source file."); - if (fseeko(f, 0, SEEK_END) != 0) - throw error_c("avi_reader: Could not seek to end of file."); - size = ftello(f); - if (fseeko(f, 0, SEEK_SET) != 0) - throw error_c("avi_reader: Could not seek to beginning of file."); - if (!avi_reader_c::probe_file(f, size)) - throw error_c("avi_reader: Source is not a valid AVI file."); - fclose(f); + try { + mm_io = new mm_io_c(ti->fname, MODE_READ); + mm_io->setFilePointer(0, seek_end); + size = mm_io->getFilePointer(); + mm_io->setFilePointer(0, seek_beginning); + if (!avi_reader_c::probe_file(mm_io, size)) + throw error_c("avi_reader: Source is not a valid AVI file."); + delete mm_io; + } catch (exception &ex) { + throw error_c("avi_reader: Could not read the source file."); + } if (verbose) fprintf(stdout, "Using AVI demultiplexer for %s. Opening file. This " diff --git a/r_avi.h b/r_avi.h index 0da6074f8..b81315c67 100644 --- a/r_avi.h +++ b/r_avi.h @@ -13,7 +13,7 @@ /*! \file r_avi.h - \version \$Id: r_avi.h,v 1.15 2003/05/20 06:30:24 mosu Exp $ + \version \$Id: r_avi.h,v 1.16 2003/05/23 06:34:57 mosu Exp $ \brief class definitions for the AVI demultiplexer module \author Moritz Bunkus */ @@ -27,6 +27,7 @@ extern "C" { #include } +#include "mm_io.h" #include "pr_generic.h" #include "common.h" #include "error.h" @@ -65,7 +66,7 @@ public: virtual void display_progress(); virtual void set_headers(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); private: virtual int add_audio_demuxer(avi_t *avi, int aid); diff --git a/r_dts.cpp b/r_dts.cpp index e9b3b5a2e..a01a66d0c 100644 --- a/r_dts.cpp +++ b/r_dts.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_dts.cpp,v 1.5 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_dts.cpp,v 1.6 2003/05/23 06:34:57 mosu Exp $ \brief DTS demultiplexer module \author Peter Niemayer \author Moritz Bunkus @@ -30,20 +30,21 @@ #include "r_dts.h" #include "p_dts.h" -int dts_reader_c::probe_file(FILE *file, int64_t size) { +int dts_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { char buf[max_dts_packet_size]; int pos; dts_header_t dtsheader; if (size < max_dts_packet_size) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) - return 0; - if (fread(buf, 1, max_dts_packet_size, file) != max_dts_packet_size) { - fseeko(file, 0, SEEK_SET); + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->read(buf, max_dts_packet_size) != max_dts_packet_size) + return 0; + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; } - fseeko(file, 0, SEEK_SET); pos = find_dts_header((unsigned char *)buf, max_dts_packet_size, &dtsheader); if (pos < 0) @@ -57,18 +58,18 @@ dts_reader_c::dts_reader_c(track_info_t *nti) throw (error_c): int pos; dts_header_t dtsheader; - if ((file = fopen(ti->fname, "rb")) == NULL) - throw error_c("dts_reader: Could not open source file."); - if (fseeko(file, 0, SEEK_END) != 0) - throw error_c("dts_reader: Could not seek to end of file."); - size = ftello(file); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("dts_reader: Could not seek to beginning of file."); - chunk = (unsigned char *)safemalloc(max_dts_packet_size); - if (fread(chunk, 1, max_dts_packet_size, file) != max_dts_packet_size) - throw error_c("dts_reader: Could not read max_dts_packet_size bytes."); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("dts_reader: Could not seek to beginning of file."); + try { + mm_io = new mm_io_c(ti->fname, MODE_READ); + mm_io->setFilePointer(0, seek_end); + size = mm_io->getFilePointer(); + mm_io->setFilePointer(0, seek_beginning); + chunk = (unsigned char *)safemalloc(max_dts_packet_size); + if (mm_io->read(chunk, max_dts_packet_size) != max_dts_packet_size) + throw error_c("dts_reader: Could not read max_dts_packet_size bytes."); + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { + throw error_c("dts_reader: Could not open the source file."); + } pos = find_dts_header(chunk, max_dts_packet_size, &dtsheader); @@ -87,8 +88,7 @@ dts_reader_c::dts_reader_c(track_info_t *nti) throw (error_c): } dts_reader_c::~dts_reader_c() { - if (file != NULL) - fclose(file); + delete mm_io; safefree(chunk); if (dtspacketizer != NULL) delete dtspacketizer; @@ -97,7 +97,7 @@ dts_reader_c::~dts_reader_c() { int dts_reader_c::read() { int nread; - nread = fread(chunk, 1, max_dts_packet_size, file); + nread = mm_io->read(chunk, max_dts_packet_size); if (nread <= 0) return 0; diff --git a/r_dts.h b/r_dts.h index ef7845608..72c6d0018 100644 --- a/r_dts.h +++ b/r_dts.h @@ -13,7 +13,7 @@ /*! \file r_avi.h - \version \$Id: r_dts.h,v 1.3 2003/05/20 06:30:24 mosu Exp $ + \version \$Id: r_dts.h,v 1.4 2003/05/23 06:34:57 mosu Exp $ \brief class definitions for the AVI demultiplexer module \author Moritz Bunkus */ @@ -23,6 +23,7 @@ #include +#include "mm_io.h" #include "pr_generic.h" #include "common.h" #include "error.h" @@ -32,7 +33,7 @@ class dts_reader_c: public generic_reader_c { private: unsigned char *chunk; - FILE *file; + mm_io_c *mm_io; class dts_packetizer_c *dtspacketizer; int64_t bytes_processed, size; @@ -46,7 +47,7 @@ public: virtual void display_progress(); virtual void set_headers(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); }; #endif // __R_DTS_H diff --git a/r_matroska.cpp b/r_matroska.cpp index f7acff7aa..49c0c9928 100644 --- a/r_matroska.cpp +++ b/r_matroska.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_matroska.cpp,v 1.38 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_matroska.cpp,v 1.39 2003/05/23 06:34:57 mosu Exp $ \brief Matroska reader \author Moritz Bunkus */ @@ -74,18 +74,19 @@ using namespace LIBMATROSKA_NAMESPACE; * Probes a file by simply comparing the first four bytes to the EBML * head signature. */ -int mkv_reader_c::probe_file(FILE *file, int64_t size) { +int mkv_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { unsigned char data[4]; if (size < 4) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) - return 0; - if (fread(data, 1, 4, file) != 4) { - fseeko(file, 0, SEEK_SET); + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->read(data, 4) != 4) + return 0; + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; } - fseeko(file, 0, SEEK_SET); if ((data[0] != 0x1A) || (data[1] != 0x45) || (data[2] != 0xDF) || (data[3] != 0xA3)) return 0; diff --git a/r_matroska.h b/r_matroska.h index 5098ddc2c..76a7e089e 100644 --- a/r_matroska.h +++ b/r_matroska.h @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_matroska.h,v 1.14 2003/05/22 11:11:05 mosu Exp $ + \version \$Id: r_matroska.h,v 1.15 2003/05/23 06:34:57 mosu Exp $ \brief class definitions for the Matroska reader \author Moritz Bunkus */ @@ -23,6 +23,7 @@ #include +#include "mm_io.h" #include "common.h" #include "pr_generic.h" #include "error.h" @@ -107,7 +108,7 @@ public: virtual void display_progress(); virtual void set_headers(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); private: virtual int demuxing_requested(mkv_track_t *t); diff --git a/r_microdvd.cpp b/r_microdvd.cpp index a67ef282e..b0d6972d9 100644 --- a/r_microdvd.cpp +++ b/r_microdvd.cpp @@ -27,13 +27,13 @@ #include "r_microdvd.h" #include "subtitles.h" -int microdvd_reader_c::probe_file(FILE *file, int64_t size) { +int microdvd_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { char chunk[2048]; int i; - if (fseeko(file, 0, SEEK_SET) != 0) + if (mm_io->setFilePointer(0, seek_beginning) != 0) return 0; - if (fgets(chunk, 2047, file) == NULL) + if (fgets(chunk, 2047) == NULL) return 0; if ((chunk[0] != '{') || !isdigit(chunk[1])) return 0; @@ -48,7 +48,7 @@ int microdvd_reader_c::probe_file(FILE *file, int64_t size) { if ((chunk[i] != '}') || (chunk[i + 1] == 0) || (chunk[i + 1] == '\n') || (chunk[i + 1] == '\r')) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) + if (mm_io->setFilePointer(0, seek_beginning) != 0) return 0; return 1; } @@ -57,7 +57,7 @@ microdvd_reader_c::microdvd_reader_c(char *fname, audio_sync_t *nasync, char **ncomments) throw (error_c) { if ((file = fopen(fname, "r")) == NULL) throw error_c("microdvd_reader: Could not open source file."); - if (!microdvd_reader_c::probe_file(file, 0)) + if (!microdvd_reader_c::probe_file(0)) throw error_c("microdvd_reader: Source is not a valid MicroDVD file."); textsubspacketizer = new textsubs_packetizer_c(nasync); if (verbose) @@ -85,7 +85,7 @@ int microdvd_reader_c::read() { lineno = 0; while (1) { - if (fgets(chunk, 2047, file) == NULL) + if (fgets(chunk, 2047) == NULL) break; lineno++; diff --git a/r_microdvd.h b/r_microdvd.h index f74af4783..f4d5d02c8 100644 --- a/r_microdvd.h +++ b/r_microdvd.h @@ -28,7 +28,7 @@ class microdvd_reader_c: public generic_reader_c { private: char chunk[2048]; - FILE *file; + mm_io_c *mm_io; textsubs_packetizer_c *textsubspacketizer; int act_wchar; @@ -46,7 +46,7 @@ public: virtual void display_progress(); virtual void set_headers(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); }; #endif // __R_MICRODVD_H diff --git a/r_mp3.cpp b/r_mp3.cpp index a9fb6d227..a83e70664 100644 --- a/r_mp3.cpp +++ b/r_mp3.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_mp3.cpp,v 1.21 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_mp3.cpp,v 1.22 2003/05/23 06:34:57 mosu Exp $ \brief MP3 reader module \author Moritz Bunkus */ @@ -29,7 +29,7 @@ #include "r_mp3.h" #include "p_mp3.h" -int mp3_reader_c::probe_file(FILE *file, int64_t size) { +int mp3_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { unsigned char buf[4096]; int pos; unsigned long header; @@ -37,13 +37,14 @@ int mp3_reader_c::probe_file(FILE *file, int64_t size) { if (size < 4096) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) - return 0; - if (fread(buf, 1, 4096, file) != 4096) { - fseeko(file, 0, SEEK_SET); + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->read(buf, 4096) != 4096) + return 0; + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; } - fseeko(file, 0, SEEK_SET); pos = find_mp3_header(buf, 4096, &header); if (pos < 0) @@ -67,18 +68,18 @@ mp3_reader_c::mp3_reader_c(track_info_t *nti) throw (error_c): unsigned long header; mp3_header_t mp3header; - if ((file = fopen(ti->fname, "rb")) == NULL) - throw error_c("mp3_reader: Could not open source file."); - if (fseeko(file, 0, SEEK_END) != 0) - throw error_c("mp3_reader: Could not seek to end of file."); - size = ftello(file); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("mp3_reader: Could not seek to beginning of file."); - chunk = (unsigned char *)safemalloc(4096); - if (fread(chunk, 1, 4096, file) != 4096) - throw error_c("mp3_reader: Could not read 4096 bytes."); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("mp3_reader: Could not seek to beginning of file."); + try { + mm_io = new mm_io_c(ti->fname, MODE_READ); + mm_io->setFilePointer(0, seek_end); + size = mm_io->getFilePointer(); + mm_io->setFilePointer(0, seek_beginning); + chunk = (unsigned char *)safemalloc(4096); + if (mm_io->read(chunk, 4096) != 4096) + throw error_c("mp3_reader: Could not read 4096 bytes."); + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { + throw error_c("mp3_reader: Could not open the source file."); + } pos = find_mp3_header(chunk, 4096, &header); if (pos < 0) throw error_c("mp3_reader: No valid MP3 packet found in the first " \ @@ -95,8 +96,7 @@ mp3_reader_c::mp3_reader_c(track_info_t *nti) throw (error_c): } mp3_reader_c::~mp3_reader_c() { - if (file != NULL) - fclose(file); + delete mm_io; if (chunk != NULL) safefree(chunk); if (mp3packetizer != NULL) @@ -106,7 +106,7 @@ mp3_reader_c::~mp3_reader_c() { int mp3_reader_c::read() { int nread; - nread = fread(chunk, 1, 4096, file); + nread = mm_io->read(chunk, 4096); if (nread <= 0) return 0; diff --git a/r_mp3.h b/r_mp3.h index c9b1837af..2cfcd3bae 100644 --- a/r_mp3.h +++ b/r_mp3.h @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_mp3.h,v 1.13 2003/05/20 06:30:24 mosu Exp $ + \version \$Id: r_mp3.h,v 1.14 2003/05/23 06:34:57 mosu Exp $ \brief class definitions for the MP3 reader module \author Moritz Bunkus */ @@ -23,6 +23,7 @@ #include +#include "mm_io.h" #include "common.h" #include "error.h" @@ -31,7 +32,7 @@ class mp3_reader_c: public generic_reader_c { private: unsigned char *chunk; - FILE *file; + mm_io_c *mm_io; class mp3_packetizer_c *mp3packetizer; int64_t bytes_processed, size; @@ -46,7 +47,7 @@ public: virtual int display_priority(); virtual void display_progress(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); }; #endif // __R_MP3_H diff --git a/r_mp4.cpp b/r_mp4.cpp index 109619d64..e1a599f0b 100644 --- a/r_mp4.cpp +++ b/r_mp4.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_mp4.cpp,v 1.2 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_mp4.cpp,v 1.3 2003/05/23 06:34:57 mosu Exp $ \brief MP4 identification class \author Moritz Bunkus */ @@ -24,18 +24,21 @@ #include "r_mp4.h" -int mp4_reader_c::probe_file(FILE *file, int64_t size) { +using namespace std; + +int mp4_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { unsigned char data[20]; if (size < 20) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) - return 0; - if (fread(data, 1, 20, file) != 20) { - fseeko(file, 0, SEEK_SET); + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->read(data, 20) != 20) + return 0; + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; } - fseeko(file, 0, SEEK_SET); if ((data[4] != 'f') || (data[5] != 't') || (data[6] != 'y') || (data[7] != 'p')) return 0; diff --git a/r_mp4.h b/r_mp4.h index 9f5ce7168..10c9ef024 100644 --- a/r_mp4.h +++ b/r_mp4.h @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_mp4.h,v 1.1 2003/05/22 15:37:53 mosu Exp $ + \version \$Id: r_mp4.h,v 1.2 2003/05/23 06:34:57 mosu Exp $ \brief class definitions for the dummy MP4 reader \author Moritz Bunkus */ @@ -23,9 +23,11 @@ #include +#include "mm_io.h" + class mp4_reader_c { public: - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); }; #endif // __R_MP4_H diff --git a/r_ogm.cpp b/r_ogm.cpp index eb07318ab..d14f7cdde 100644 --- a/r_ogm.cpp +++ b/r_ogm.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_ogm.cpp,v 1.33 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_ogm.cpp,v 1.34 2003/05/23 06:34:57 mosu Exp $ \brief OGG media stream reader \author Moritz Bunkus */ @@ -47,18 +47,19 @@ extern "C" { // for BITMAPINFOHEADER /* * Probes a file by simply comparing the first four bytes to 'OggS'. */ -int ogm_reader_c::probe_file(FILE *file, int64_t size) { +int ogm_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { unsigned char data[4]; if (size < 4) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) - return 0; - if (fread(data, 1, 4, file) != 4) { - fseeko(file, 0, SEEK_SET); + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->read(data, 4) != 4) + return 0; + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; } - fseeko(file, 0, SEEK_SET); if (strncmp((char *)data, "OggS", 4)) return 0; return 1; @@ -72,14 +73,15 @@ ogm_reader_c::ogm_reader_c(track_info_t *nti) throw (error_c): generic_reader_c(nti) { int64_t size; - if ((file = fopen(ti->fname, "rb")) == NULL) - throw error_c("ogm_reader: Could not open source file."); - if (fseeko(file, 0, SEEK_END) != 0) - throw error_c("ogm_reader: Could not seek to end of file."); - size = ftello(file); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("ogm_reader: Could not seek to beginning of file."); - if (!ogm_reader_c::probe_file(file, size)) + try { + mm_io = new mm_io_c(ti->fname, MODE_READ); + mm_io->setFilePointer(0, seek_end); + size = mm_io->getFilePointer(); + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { + throw error_c("ogm_reader: Could not open the source file."); + } + if (!ogm_reader_c::probe_file(mm_io, size)) throw error_c("ogm_reader: Source is not a valid OGG media file."); ogg_sync_init(&oy); @@ -187,7 +189,7 @@ int ogm_reader_c::read_page(ogg_page *og) { exit(1); } - if ((nread = fread(buf, 1, BUFFER_SIZE, file)) <= 0) + if ((nread = mm_io->read(buf, BUFFER_SIZE)) <= 0) return 0; ogg_sync_wrote(&oy, nread); @@ -659,7 +661,7 @@ int ogm_reader_c::read_headers() { } } - fseeko(file, 0, SEEK_SET); + mm_io->setFilePointer(0, seek_beginning); ogg_sync_clear(&oy); ogg_sync_init(&oy); diff --git a/r_ogm.h b/r_ogm.h index 7c1f41b38..517b69c92 100644 --- a/r_ogm.h +++ b/r_ogm.h @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_ogm.h,v 1.14 2003/05/20 06:30:24 mosu Exp $ + \version \$Id: r_ogm.h,v 1.15 2003/05/23 06:34:58 mosu Exp $ \brief class definitions for the OGG media stream reader \author Moritz Bunkus */ @@ -25,6 +25,7 @@ #include +#include "mm_io.h" #include "common.h" #include "pr_generic.h" @@ -47,7 +48,7 @@ typedef struct { class ogm_reader_c: public generic_reader_c { private: ogg_sync_state oy; - FILE *file; + mm_io_c *mm_io; int act_wchar, num_sdemuxers, nastreams, nvstreams, ntstreams, numstreams; ogm_demuxer_t **sdemuxers; char **comments; @@ -64,7 +65,7 @@ public: virtual int display_priority(); virtual void display_progress(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); private: virtual ogm_demuxer_t *find_demuxer(int serialno); diff --git a/r_srt.cpp b/r_srt.cpp index 320df956b..c79081916 100644 --- a/r_srt.cpp +++ b/r_srt.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_srt.cpp,v 1.14 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_srt.cpp,v 1.15 2003/05/23 06:34:58 mosu Exp $ \brief Subripper subtitle reader \author Moritz Bunkus */ @@ -29,6 +29,8 @@ #include "r_srt.h" #include "subtitles.h" +using namespace std; + #define iscolon(s) (*(s) == ':') #define iscomma(s) (*(s) == ',') #define istwodigits(s) (isdigit(*(s)) && isdigit(*(s + 1))) @@ -42,33 +44,38 @@ #define issrttimecode(s) (istimecode(s) && isarrow(s + 12) && \ istimecode(s + 17)) -int srt_reader_c::probe_file(FILE *file, int64_t size) { +int srt_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { char chunk[2048]; - if (fseeko(file, 0, SEEK_SET) != 0) - return 0; - if (fgets(chunk, 2047, file) == NULL) - return 0; - if ((chunk[0] != '1') || ((chunk[1] != '\n') && (chunk[1] != '\r'))) - return 0; - if (fgets(chunk, 2047, file) == NULL) - return 0; - if ((strlen(chunk) < 29) || !issrttimecode(chunk)) - return 0; - if (fgets(chunk, 2047, file) == NULL) - return 0; - if (fseeko(file, 0, SEEK_SET) != 0) + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->gets(chunk, 2047) == NULL) + return 0; + if ((chunk[0] != '1') || ((chunk[1] != '\n') && (chunk[1] != '\r'))) + return 0; + if (mm_io->gets(chunk, 2047) == NULL) + return 0; + if ((strlen(chunk) < 29) || !issrttimecode(chunk)) + return 0; + if (mm_io->gets(chunk, 2047) == NULL) + return 0; + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; + } return 1; } srt_reader_c::srt_reader_c(track_info_t *nti) throw (error_c): generic_reader_c(nti) { - if ((file = fopen(ti->fname, "r")) == NULL) - throw error_c("srt_reader: Could not open source file."); - if (!srt_reader_c::probe_file(file, 0)) - throw error_c("srt_reader: Source is not a valid SRT file."); - textsubs_packetizer = new textsubs_packetizer_c(this, ti); + try { + mm_io = new mm_io_c(ti->fname, MODE_READ); + if (!srt_reader_c::probe_file(mm_io, 0)) + throw error_c("srt_reader: Source is not a valid SRT file."); + textsubs_packetizer = new textsubs_packetizer_c(this, ti); + } catch (exception &ex) { + throw error_c("srt_reader: Could not open the source file."); + } if (verbose) fprintf(stdout, "Using SRT subtitle reader for %s.\n+-> Using " \ "text subtitle output module for subtitles.\n", ti->fname); @@ -85,9 +92,9 @@ int srt_reader_c::read() { subtitles_c subs; while (1) { - if (fgets(chunk, 2047, file) == NULL) + if (mm_io->gets(chunk, 2047) == NULL) break; - if (fgets(chunk, 2047, file) == NULL) + if (mm_io->gets(chunk, 2047) == NULL) break; if ((strlen(chunk) < 29) || !issrttimecode(chunk)) break; @@ -111,7 +118,7 @@ int srt_reader_c::read() { atol(&chunk[23]) * 1000 + atol(&chunk[26]); subtitles = NULL; while (1) { - if (fgets(chunk, 2047, file) == NULL) + if (mm_io->gets(chunk, 2047) == NULL) break; chunk[2047] = 0; if ((*chunk == '\n') || (*chunk == '\r')) diff --git a/r_srt.h b/r_srt.h index 20c1acb62..732f96197 100644 --- a/r_srt.h +++ b/r_srt.h @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_srt.h,v 1.11 2003/05/20 06:30:25 mosu Exp $ + \version \$Id: r_srt.h,v 1.12 2003/05/23 06:34:58 mosu Exp $ \brief class definition for the Subripper subtitle reader \author Moritz Bunkus */ @@ -23,6 +23,7 @@ #include +#include "mm_io.h" #include "common.h" #include "pr_generic.h" @@ -31,7 +32,7 @@ class srt_reader_c: public generic_reader_c { private: char chunk[2048]; - FILE *file; + mm_io_c *mm_io; textsubs_packetizer_c *textsubs_packetizer; int act_wchar; @@ -46,7 +47,7 @@ public: virtual int display_priority(); virtual void display_progress(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); }; #endif // __R_SRT_H diff --git a/r_vobsub.cpp b/r_vobsub.cpp index bd85c14cb..4bf95e040 100644 --- a/r_vobsub.cpp +++ b/r_vobsub.cpp @@ -52,17 +52,17 @@ iscommafileposstr(s + 23) && \ isfilepos(s + 34)) -int vobsub_reader_c::probe_file(FILE *file, int64_t size) { +int vobsub_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { char chunk[2048]; - if (fseeko(file, 0, SEEK_SET) != 0) + if (mm_io->setFilePointer(0, seek_beginning) != 0) return 0; - if (fgets(chunk, 2047, file) == NULL) + if (fgets(chunk, 2047) == NULL) return 0; if (strncmp(chunk, "# VobSub index file, v7", strlen("# VobSub index file, v7"))) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) + if (mm_io->setFilePointer(0, seek_beginning) != 0) return 0; return 1; } @@ -73,7 +73,7 @@ vobsub_reader_c::vobsub_reader_c(char *fname, audio_sync_t *nasync) if ((file = fopen(fname, "r")) == NULL) throw error_c("vobsub_reader: Could not open source file."); - if (!vobsub_reader_c::probe_file(file, 0)) + if (!vobsub_reader_c::probe_file(0)) throw error_c("vobsub_reader: Source is not a valid VobSub index file."); name = safestrdup(fname); @@ -142,7 +142,7 @@ int vobsub_reader_c::read() { last_start = -1; last_filepos = -1; while (1) { - if (fgets(chunk, 2047, file) == NULL) + if (fgets(chunk, 2047) == NULL) break; lineno++; if ((*chunk == 0) || (strchr("#\n\r", *chunk) != NULL)) @@ -249,7 +249,7 @@ int vobsub_reader_c::read() { filepos = strtoll(&chunk[34], NULL, 16); if ((last_start != -1) && (last_filepos != -1)) { - if (fseeko(subfile, last_filepos, SEEK_SET) != 0) + if (mm_io->setFilePointer(subfile, last_filepos, seek_beginning) != 0) fprintf(stderr, "Warning: vobsub_reader: Could not seek to position " "%lld. Ignoring this entry.\n", last_filepos); else if (last_filepos == filepos) @@ -257,7 +257,7 @@ int vobsub_reader_c::read() { "entry start at the same position in the file. Ignored.\n"); else { s = (char *)safemalloc(filepos - last_filepos); - if (fread(s, 1, filepos - last_filepos, subfile) != + if (mm_io->read(s, 1, filepos - last_filepos, subfile) != (filepos - last_filepos)) fprintf(stderr, "Warning: vobsub_reader: Could not read entry " "from the sub file. Ignored.\n"); @@ -276,14 +276,14 @@ int vobsub_reader_c::read() { } if ((last_start != -1) && (last_filepos != -1) && (vobsub_packetizer != NULL)) { - if (fseeko(subfile, 0, SEEK_END) != 0) { + if (mm_io->setFilePointer(subfile, 0, seek_end) != 0) { fprintf(stderr, "Warning: vobsub_reader: Could not seek to end of " "the sub file. Ignoring last entry.\n"); vobsub_packetizer->produce_eos_packet(); return 0; } - filepos = ftello(subfile); - if (fseeko(subfile, last_filepos, SEEK_SET) != 0) + filepos = mm_io->getFilePointer(); + if (mm_io->setFilePointer(subfile, last_filepos, seek_beginning) != 0) fprintf(stderr, "Warning: vobsub_reader: Could not seek to position " "%lld. Ignoring this entry.\n", last_filepos); else if (last_filepos == filepos) @@ -291,7 +291,7 @@ int vobsub_reader_c::read() { "entry start at the same position in the file. Ignored.\n"); else { s = (char *)safemalloc(filepos - last_filepos); - if (fread(s, 1, filepos - last_filepos, subfile) != + if (mm_io->read(s, 1, filepos - last_filepos, subfile) != (filepos - last_filepos)) fprintf(stderr, "Warning: vobsub_reader: Could not read entry " "from the sub file. Ignored.\n"); diff --git a/r_vobsub.h b/r_vobsub.h index d2926ccb6..aaa9c4093 100644 --- a/r_vobsub.h +++ b/r_vobsub.h @@ -29,7 +29,7 @@ class vobsub_reader_c: public generic_reader_c { private: char chunk[2048]; - FILE *file, *subfile; + mm_io_c *mm_io, *subfile; vobsub_packetizer_c *vobsub_packetizer, **all_packetizers; int num_packetizers, act_wchar; char **comments; @@ -49,7 +49,7 @@ public: virtual void display_progress(); virtual void set_headers(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); private: virtual void add_vobsub_packetizer(int width, int height, diff --git a/r_wav.cpp b/r_wav.cpp index 8ad3e63b3..e2655e140 100644 --- a/r_wav.cpp +++ b/r_wav.cpp @@ -13,7 +13,7 @@ /*! \file - \version \$Id: r_wav.cpp,v 1.26 2003/05/22 16:14:29 mosu Exp $ + \version \$Id: r_wav.cpp,v 1.27 2003/05/23 06:34:58 mosu Exp $ \brief MP3 reader module \author Moritz Bunkus \author Peter Niemayer @@ -86,18 +86,19 @@ void dts_14_to_dts_16(unsigned short * src, const unsigned long srcwords, } } -int wav_reader_c::probe_file(FILE *file, int64_t size) { +int wav_reader_c::probe_file(mm_io_c *mm_io, int64_t size) { wave_header wheader; if (size < sizeof(wave_header)) return 0; - if (fseeko(file, 0, SEEK_SET) != 0) - return 0; - if (fread((char *)&wheader, 1, sizeof(wheader), file) != sizeof(wheader)) { - fseeko(file, 0, SEEK_SET); + try { + mm_io->setFilePointer(0, seek_beginning); + if (mm_io->read((char *)&wheader, sizeof(wheader)) != sizeof(wheader)) + return 0; + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { return 0; } - fseeko(file, 0, SEEK_SET); if (strncmp((char *)wheader.riff.id, "RIFF", 4) || strncmp((char *)wheader.riff.wave_id, "WAVE", 4) || strncmp((char *)wheader.data.id, "data", 4)) @@ -113,16 +114,17 @@ wav_reader_c::wav_reader_c(track_info_t *nti) throw (error_c): pcmpacketizer = 0; dtspacketizer = 0; - if ((file = fopen(ti->fname, "rb")) == NULL) - throw error_c("wav_reader: Could not open source file."); - if (fseeko(file, 0, SEEK_END) != 0) - throw error_c("wav_reader: Could not seek to end of file."); - size = ftello(file); - if (fseeko(file, 0, SEEK_SET) != 0) - throw error_c("wav_reader: Could not seek to beginning of file."); - if (!wav_reader_c::probe_file(file, size)) + try { + mm_io = new mm_io_c(ti->fname, MODE_READ); + mm_io->setFilePointer(0, seek_end); + size = mm_io->getFilePointer(); + mm_io->setFilePointer(0, seek_beginning); + } catch (exception &ex) { + throw error_c("wav_reader: Could not open the source file."); + } + if (!wav_reader_c::probe_file(mm_io, size)) throw error_c("wav_reader: Source is not a valid WAVE file."); - if (fread(&wheader, 1, sizeof(wheader), file) != sizeof(wheader)) + if (mm_io->read(&wheader, sizeof(wheader)) != sizeof(wheader)) throw error_c("wav_reader: could not read WAVE header."); bps = wheader.common.wChannels * wheader.common.wBitsPerSample * wheader.common.dwSamplesPerSec / 8; @@ -135,8 +137,8 @@ wav_reader_c::wav_reader_c(track_info_t *nti) throw (error_c): unsigned short buf[2][max_dts_packet_size/2]; int cur_buf = 0; - long rlen = fread(obuf, 1, max_dts_packet_size, file); - fseeko(file, sizeof(wheader), SEEK_SET); + long rlen = mm_io->read(obuf, max_dts_packet_size); + mm_io->setFilePointer(sizeof(wheader), seek_beginning); for (dts_swap_bytes = 0; dts_swap_bytes < 2; dts_swap_bytes++) { memcpy(buf[cur_buf], obuf, rlen); @@ -192,8 +194,7 @@ wav_reader_c::wav_reader_c(track_info_t *nti) throw (error_c): } wav_reader_c::~wav_reader_c() { - if (file != NULL) - fclose(file); + delete mm_io; if (chunk != NULL) safefree(chunk); if (pcmpacketizer != NULL) @@ -206,7 +207,7 @@ int wav_reader_c::read() { if (pcmpacketizer) { int nread; - nread = fread(chunk, 1, bps, file); + nread = mm_io->read(chunk, bps); if (nread <= 0) return 0; @@ -223,7 +224,7 @@ int wav_reader_c::read() { if (dtspacketizer) { unsigned short buf[2][max_dts_packet_size/2]; int cur_buf = 0; - long rlen = fread(buf[cur_buf], 1, max_dts_packet_size, file); + long rlen = mm_io->read(buf[cur_buf], max_dts_packet_size); if (rlen <= 0) return 0; diff --git a/r_wav.h b/r_wav.h index 17ac2f6c9..fb1a4d8a7 100644 --- a/r_wav.h +++ b/r_wav.h @@ -14,7 +14,7 @@ /*! \file - \version \$Id: r_wav.h,v 1.13 2003/05/20 06:30:25 mosu Exp $ + \version \$Id: r_wav.h,v 1.14 2003/05/23 06:34:58 mosu Exp $ \brief class definitions for the WAV reader module \author Moritz Bunkus */ @@ -24,6 +24,7 @@ #include +#include "mm_io.h" #include "common.h" #include "error.h" @@ -37,7 +38,7 @@ extern "C" { class wav_reader_c: public generic_reader_c { private: unsigned char *chunk; - FILE *file; + mm_io_c *mm_io; class pcm_packetizer_c *pcmpacketizer; class dts_packetizer_c *dtspacketizer; int dts_swap_bytes, dts_14_16; @@ -56,7 +57,7 @@ public: virtual int display_priority(); virtual void display_progress(); - static int probe_file(FILE *file, int64_t size); + static int probe_file(mm_io_c *mm_io, int64_t size); }; #endif // __R_WAV_H