Rename _flac_stat to flac_stat_s.
[platform/upstream/flac.git] / src / share / grabbag / replaygain.c
index 8fedce7..43f1be0 100644 (file)
@@ -1,5 +1,5 @@
 /* grabbag - Convenience lib for various routines common to several tools
- * Copyright (C) 2002,2003,2004,2005,2006,2007  Josh Coalson
+ * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 #  include <config.h>
 #endif
 
-#include "share/grabbag.h"
-#include "share/replaygain_analysis.h"
-#include "FLAC/assert.h"
-#include "FLAC/metadata.h"
-#include "FLAC/stream_decoder.h"
 #include <locale.h>
 #include <math.h>
 #include <stdio.h>
 #endif
 #include <sys/stat.h> /* for stat(), maybe chmod() */
 
+#include "FLAC/assert.h"
+#include "FLAC/metadata.h"
+#include "FLAC/stream_decoder.h"
+#include "share/grabbag.h"
+#include "share/replaygain_analysis.h"
+#include "share/safe_str.h"
+
 #ifdef local_min
 #undef local_min
 #endif
@@ -67,19 +69,19 @@ const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN = (const FLAC__byte
 const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK = (const FLAC__byte * const)"REPLAYGAIN_ALBUM_PEAK";
 
 
-static FLAC__bool get_file_stats_(const char *filename, struct stat *stats)
+static FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats)
 {
        FLAC__ASSERT(0 != filename);
        FLAC__ASSERT(0 != stats);
-       return (0 == stat(filename, stats));
+       return (0 == flac_stat(filename, stats));
 }
 
-static void set_file_stats_(const char *filename, struct stat *stats)
+static void set_file_stats_(const char *filename, struct flac_stat_s *stats)
 {
        FLAC__ASSERT(0 != filename);
        FLAC__ASSERT(0 != stats);
 
-       (void)chmod(filename, stats->st_mode);
+       (void)flac_chmod(filename, stats->st_mode);
 }
 
 static FLAC__bool append_tag_(FLAC__StreamMetadata *block, const char *format, const FLAC__byte *name, float value)
@@ -98,14 +100,13 @@ static FLAC__bool append_tag_(FLAC__StreamMetadata *block, const char *format, c
         * We need to save the old locale and switch to "C" because the locale
         * influences the formatting of %f and we want it a certain way.
         */
-       saved_locale = setlocale(LC_ALL, 0);
+       saved_locale = strdup(setlocale(LC_ALL, 0));
+       if (0 == saved_locale)
+               return false;
        setlocale(LC_ALL, "C");
-#if defined _MSC_VER || defined __MINGW32__
-       _snprintf(buffer, sizeof(buffer)-1, format, name, value);
-#else
-       snprintf(buffer, sizeof(buffer)-1, format, name, value);
-#endif
+       flac_snprintf(buffer, sizeof(buffer), format, name, value);
        setlocale(LC_ALL, saved_locale);
+       free(saved_locale);
 
        entry.entry = (FLAC__byte *)buffer;
        entry.length = strlen(buffer);
@@ -115,25 +116,7 @@ static FLAC__bool append_tag_(FLAC__StreamMetadata *block, const char *format, c
 
 FLAC__bool grabbag__replaygain_is_valid_sample_frequency(unsigned sample_frequency)
 {
-       static const unsigned valid_sample_rates[] = {
-               8000,
-               11025,
-               12000,
-               16000,
-               22050,
-               24000,
-               32000,
-               44100,
-               48000
-       };
-       static const unsigned n_valid_sample_rates = sizeof(valid_sample_rates) / sizeof(valid_sample_rates[0]);
-
-       unsigned i;
-
-       for(i = 0; i < n_valid_sample_rates; i++)
-               if(sample_frequency == valid_sample_rates[i])
-                       return true;
-       return false;
+        return ValidGainFrequency( sample_frequency );
 }
 
 FLAC__bool grabbag__replaygain_init(unsigned sample_frequency)
@@ -495,15 +478,17 @@ static const char *store_to_file_pre_(const char *filename, FLAC__Metadata_Chain
 
 static const char *store_to_file_post_(const char *filename, FLAC__Metadata_Chain *chain, FLAC__bool preserve_modtime)
 {
-       struct stat stats;
+       struct flac_stat_s stats;
        const FLAC__bool have_stats = get_file_stats_(filename, &stats);
 
        (void)grabbag__file_change_stats(filename, /*read_only=*/false);
 
        FLAC__metadata_chain_sort_padding(chain);
        if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, preserve_modtime)) {
+               const char *error;
+               error = FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)];
                FLAC__metadata_chain_delete(chain);
-               return FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)];
+               return error;
        }
 
        FLAC__metadata_chain_delete(chain);
@@ -608,8 +593,7 @@ static FLAC__bool parse_double_(const FLAC__StreamMetadata_VorbisComment_Entry *
        if(0 == q)
                return false;
        q++;
-       memset(s, 0, sizeof(s)-1);
-       strncpy(s, q, local_min(sizeof(s)-1, entry->length - (q-p)));
+       safe_strncpy(s, q, local_min(sizeof(s), (size_t) (entry->length - (q-p))));
 
        v = strtod(s, &end);
        if(end == s)