shithub: sox

Download patch

ref: d1a2f94089bff9e099fe72795b2328fb69a9b228
parent: 2c3c95d2b5554390590cba77ddf19ea76fc71785
author: Doug Cook <idigdoug@users.sourceforge.net>
date: Sat Mar 17 23:46:03 EDT 2012

Clean up additional uses of format_t.fp

--- a/src/effects_i.c
+++ b/src/effects_i.c
@@ -343,7 +343,7 @@
   return result < 0 ? -1 : result;
 }
 
-FILE * lsx_open_input_file(sox_effect_t * effp, char const * filename)
+FILE * lsx_open_input_file(sox_effect_t * effp, char const * filename, sox_bool text_mode)
 {
   FILE * file;
 
@@ -355,7 +355,7 @@
     effp->global_info->global_info->stdin_in_use_by = effp->handler.name;
     file = stdin;
   }
-  else if (!(file = fopen(filename, "r"))) {
+  else if (!(file = fopen(filename, text_mode ? "r" : "rb"))) {
     lsx_fail("couldn't open file %s: %s", filename, strerror(errno));
     return NULL;
   }
--- a/src/fir.c
+++ b/src/fir.c
@@ -56,7 +56,7 @@
 
   if (!f->num_taps) {
     if (!p->n && p->filename) {
-      FILE * file = lsx_open_input_file(effp, p->filename);
+      FILE * file = lsx_open_input_file(effp, p->filename, sox_true);
       if (!file)
         return SOX_EOF;
       while ((i = fscanf(file, " #%*[^\n]%c", &c)) >= 0) {
--- a/src/firfit.c
+++ b/src/firfit.c
@@ -82,7 +82,7 @@
 static sox_bool read_knots(sox_effect_t * effp)
 {
   priv_t * p = (priv_t *) effp->priv;
-  FILE * file = lsx_open_input_file(effp, p->filename);
+  FILE * file = lsx_open_input_file(effp, p->filename, sox_true);
   sox_bool result = sox_false;
   int num_converted = 1;
   char c;
--- a/src/formats_i.c
+++ b/src/formats_i.c
@@ -141,7 +141,7 @@
 uint64_t lsx_filelength(sox_format_t * ft)
 {
   struct stat st;
-  int ret = fstat(fileno((FILE*)ft->fp), &st);
+  int ret = ft->fp ? fstat(fileno((FILE*)ft->fp), &st) : 0;
 
   return (!ret && (st.st_mode & S_IFREG))? (uint64_t)st.st_size : 0;
 }
--- a/src/libsox_i.c
+++ b/src/libsox_i.c
@@ -107,7 +107,7 @@
     lsx_debug(FAKE_MKSTEMP "mkstemp, name=%s (O_TEMPORARY)", name);
 #endif
     free(name);
-    return fildes == -1? NULL : fdopen(fildes, "w+");
+    return fildes == -1? NULL : fdopen(fildes, "w+b");
   }
 
   /* Use standard tmpfile (delete on close); tmp dir is undefined: */
--- a/src/maud.c
+++ b/src/maud.c
@@ -84,8 +84,6 @@
                             return(SOX_EOF);
                         }
 
-                        /* fseeko(ft->fp,12,SEEK_CUR); */
-
                         /* number of samples stored in MDAT */
                         lsx_readdw(ft, &(p->nsamples));
 
--- a/src/mp3-util.h
+++ b/src/mp3-util.h
@@ -258,7 +258,6 @@
 static size_t mp3_duration_ms(sox_format_t * ft)
 {
   priv_t              * p = (priv_t *) ft->priv;
-  FILE                * fp = ft->fp;
   struct mad_stream   mad_stream;
   struct mad_header   mad_header;
   struct mad_frame    mad_frame;
@@ -276,7 +275,7 @@
     size_t leftover = mad_stream.bufend - mad_stream.next_frame;
 
     memcpy(p->mp3_buffer, mad_stream.this_frame, leftover);
-    read = fread(p->mp3_buffer + leftover, (size_t) 1, p->mp3_buffer_size - leftover, fp);
+    read = lsx_readbuf(ft, p->mp3_buffer + leftover, p->mp3_buffer_size - leftover);
     if (read <= 0) {
       lsx_debug("got exact duration by scan to EOF (frames=%" PRIuPTR " leftover=%" PRIuPTR ")", frames, leftover);
       break;
@@ -299,7 +298,7 @@
           tagsize = tagtype(mad_stream.this_frame, (size_t) available);
           if (tagsize) {   /* It's some ID3 tags, so just skip */
             if (tagsize >= available) {
-              fseeko(fp, (off_t)(tagsize - available), SEEK_CUR);
+              lsx_seeki(ft, (off_t)(tagsize - available), SEEK_CUR);
               depadded = sox_false;
             }
             p->mad_stream_skip(&mad_stream, min(tagsize, available));
@@ -334,9 +333,7 @@
 
       /* If not VBR, we can time just a few frames then extrapolate */
       if (++frames == 25 && !vbr) {
-        struct stat filestat;
-        fstat(fileno(fp), &filestat);
-        mad_timer_mult(&time, (double)(filestat.st_size - tagsize) / consumed);
+        mad_timer_mult(&time, (double)(lsx_filelength(ft) - tagsize) / consumed);
         lsx_debug("got approx. duration by CBR extrapolation");
         break;
       }
@@ -346,7 +343,7 @@
   p->mad_frame_finish(&mad_frame);
   mad_header_finish(&mad_header);
   p->mad_stream_finish(&mad_stream);
-  rewind(fp);
+  lsx_rewind(ft);
   return p->mad_timer_count(time, MAD_UNITS_MILLISECONDS);
 }
 
--- a/src/noiseprof.c
+++ b/src/noiseprof.c
@@ -75,7 +75,7 @@
     effp->global_info->global_info->stdout_in_use_by = effp->handler.name;
     data->output_file = stdout;
   }
-  else if ((data->output_file = fopen(data->output_filename, "w")) == NULL) {
+  else if ((data->output_file = fopen(data->output_filename, "wb")) == NULL) {
     lsx_fail("Couldn't open profile file %s: %s", data->output_filename, strerror(errno));
     return SOX_EOF;
   }
--- a/src/noisered.c
+++ b/src/noisered.c
@@ -86,7 +86,7 @@
     size_t fchannels = 0;
     size_t channels = effp->in_signal.channels;
     size_t i;
-    FILE * ifp = lsx_open_input_file(effp, data->profile_filename);
+    FILE * ifp = lsx_open_input_file(effp, data->profile_filename, sox_false);
 
     if (!ifp)
       return SOX_EOF;
--- a/src/skelform.c
+++ b/src/skelform.c
@@ -82,9 +82,8 @@
   unsigned char sample;
 
   for (done = 0; done < len; done++) {
-    if (feof((FILE*)ft->fp)) /* no more samples */
+    if (lsx_eof(ft) || lsx_readb(ft, &sample)) /* no more samples */
       break;
-    sample = fgetc((FILE*)ft->fp);
     switch (ft->encoding.bits_per_sample) {
     case 8:
       switch (ft->encoding.encoding) {
--- a/src/sox.c
+++ b/src/sox.c
@@ -293,14 +293,12 @@
 
 static char const * size_and_bitrate(sox_format_t * ft, char const * * text)
 {
-  struct stat st;    /* ft->fp may validly be NULL, so stat not fstat */
-  if (stat(ft->filename, &st) || (st.st_mode & S_IFMT) != S_IFREG)
-    return NULL;
+  off_t size = lsx_filelength(ft);
   if (ft->signal.length && ft->signal.channels && ft->signal.rate && text) {
     double secs = ft->signal.length / ft->signal.channels / ft->signal.rate;
-    *text = lsx_sigfigs3(8. * st.st_size / secs);
+    *text = lsx_sigfigs3(8. * size / secs);
   }
-  return lsx_sigfigs3((double)st.st_size);
+  return lsx_sigfigs3((double)size);
 }
 
 static void play_file_info(sox_format_t * ft, file_t * f, sox_bool full)
@@ -905,7 +903,7 @@
 
 static void read_user_effects(char const *filename)
 {
-    FILE *file = fopen(filename, "rt");
+    FILE *file = fopen(filename, "r");
     const size_t buffer_size_step = 1024;
     size_t buffer_size = buffer_size_step;
     char *s = lsx_malloc(buffer_size); /* buffer for one input line */
@@ -2122,7 +2120,7 @@
   int c;
   size_t text_length = 100;
   char * text = lsx_malloc(text_length + 1);
-  FILE * file = fopen(filename, "rt");
+  FILE * file = fopen(filename, "r");
 
   if (file == NULL) {
     lsx_fail("Cannot open comment file `%s'", filename);
--- a/src/sox.h
+++ b/src/sox.h
@@ -2601,6 +2601,17 @@
     LSX_PARAM_INOUT lsx_getopt_t * state /**< The getopt state pointer. */
     );
 
+/**
+Plugins API:
+Gets the file length, or 0 if the file is not seekable/normal.
+@returns The file length, or 0 if the file is not seekable/normal.
+*/
+sox_uint64_t
+LSX_API
+lsx_filelength(
+    LSX_PARAM_IN sox_format_t * ft
+    );
+
 /* WARNING END */
 
 #if defined(__cplusplus)
--- a/src/sox_i.h
+++ b/src/sox_i.h
@@ -77,7 +77,7 @@
 int lsx_parse_note(char const * text, char * * end_ptr);
 double lsx_parse_frequency_k(char const * text, char * * end_ptr, int key);
 #define lsx_parse_frequency(a, b) lsx_parse_frequency_k(a, b, INT_MAX)
-FILE * lsx_open_input_file(sox_effect_t * effp, char const * filename);
+FILE * lsx_open_input_file(sox_effect_t * effp, char const * filename, sox_bool text_mode);
 
 void lsx_prepare_spline3(double const * x, double const * y, int n,
     double start_1d, double end_1d, double * y_2d);
@@ -198,7 +198,7 @@
 int lsx_flush(sox_format_t * ft);
 int lsx_seeki(sox_format_t * ft, off_t offset, int whence);
 int lsx_unreadb(sox_format_t * ft, unsigned ub);
-uint64_t lsx_filelength(sox_format_t * ft);
+/* uint64_t lsx_filelength(sox_format_t * ft); Temporarily Moved to sox.h. */
 off_t lsx_tell(sox_format_t * ft);
 void lsx_clearerr(sox_format_t * ft);
 void lsx_rewind(sox_format_t * ft);