Declare UTF-8 encoding for changelog
[platform/upstream/flac.git] / src / test_libs_common / file_utils_flac.c
index 6dbf3aa..892e2bd 100644 (file)
@@ -1,5 +1,6 @@
 /* test_libFLAC - Unit tester for libFLAC
- * Copyright (C) 2002,2003,2004,2005,2006  Josh Coalson
+ * Copyright (C) 2002-2009  Josh Coalson
+ * Copyright (C) 2011-2013  Xiph.Org Foundation
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -11,9 +12,9 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * 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.
  */
 
 #if HAVE_CONFIG_H
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h> /* for stat() */
+#include "share/compat.h"
 
 #ifdef min
 #undef min
 #endif
 #define min(a,b) ((a)<(b)?(a):(b))
 
+const long file_utils__ogg_serial_number = 12345;
+
 #ifdef FLAC__VALGRIND_TESTING
 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
 {
@@ -48,7 +52,7 @@ typedef struct {
        FILE *file;
 } encoder_client_struct;
 
-static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
+static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
 {
        encoder_client_struct *ecd = (encoder_client_struct*)client_data;
 
@@ -65,10 +69,11 @@ static void encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const
        (void)encoder, (void)metadata, (void)client_data;
 }
 
-FLAC__bool file_utils__generate_flacfile(const char *output_filename, off_t *output_filesize, unsigned length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, unsigned num_metadata)
+FLAC__bool file_utils__generate_flacfile(FLAC__bool is_ogg, const char *output_filename, FLAC__off_t *output_filesize, unsigned length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, unsigned num_metadata)
 {
        FLAC__int32 samples[1024];
        FLAC__StreamEncoder *encoder;
+       FLAC__StreamEncoderInitStatus init_status;
        encoder_client_struct encoder_client_data;
        unsigned i, n;
 
@@ -77,7 +82,7 @@ FLAC__bool file_utils__generate_flacfile(const char *output_filename, off_t *out
        FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO);
        FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0));
 
-       if(0 == (encoder_client_data.file = fopen(output_filename, "wb")))
+       if(0 == (encoder_client_data.file = flac_fopen(output_filename, "wb")))
                return false;
 
        encoder = FLAC__stream_encoder_new();
@@ -86,6 +91,7 @@ FLAC__bool file_utils__generate_flacfile(const char *output_filename, off_t *out
                return false;
        }
 
+       FLAC__stream_encoder_set_ogg_serial_number(encoder, file_utils__ogg_serial_number);
        FLAC__stream_encoder_set_verify(encoder, true);
        FLAC__stream_encoder_set_streamable_subset(encoder, true);
        FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
@@ -105,7 +111,12 @@ FLAC__bool file_utils__generate_flacfile(const char *output_filename, off_t *out
        FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples);
        FLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
 
-       if(FLAC__stream_encoder_init_stream(encoder, encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, encoder_metadata_callback_, &encoder_client_data) != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
+       if(is_ogg)
+               init_status = FLAC__stream_encoder_init_ogg_stream(encoder, /*read_callback=*/0, encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, encoder_metadata_callback_, &encoder_client_data);
+       else
+               init_status = FLAC__stream_encoder_init_stream(encoder, encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, encoder_metadata_callback_, &encoder_client_data);
+
+       if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
                fclose(encoder_client_data.file);
                return false;
        }
@@ -125,16 +136,16 @@ FLAC__bool file_utils__generate_flacfile(const char *output_filename, off_t *out
                length -= n;
        }
 
-       FLAC__stream_encoder_finish(encoder);
+       (void)FLAC__stream_encoder_finish(encoder);
 
        fclose(encoder_client_data.file);
 
        FLAC__stream_encoder_delete(encoder);
 
        if(0 != output_filesize) {
-               struct stat filestats;
+               struct flac_stat_s filestats;
 
-               if(stat(output_filename, &filestats) != 0)
+               if(flac_stat(output_filename, &filestats) != 0)
                        return false;
                else
                        *output_filesize = filestats.st_size;