From 75c0a33ec5fcce72358c1d5625885890114d1889 Mon Sep 17 00:00:00 2001
From: Serge Ziryukin <ftrvxmtrx@gmail.com>
Date: Thu, 9 Jul 2009 17:13:09 +0300
Subject: [PATCH] flac: load external cue sheet when no internal one

External cue sheet file for "file.flac" should be named as "file.flac.cue".
---
 NEWS                      |  1 +
 src/decoder/flac_plugin.c | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/NEWS b/NEWS
index 39ae6bb21..dd52d8008 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,7 @@ ver 0.16 (20??/??/??)
 * decoders:
   - ffmpeg: support multiple tags
   - sndfile: new decoder plugin based on libsndfile
+  - flac: load external cue sheet when no internal one
 * mixers:
   - removed support for legacy mixer configuration
   - reimplemented software volume as mixer+filter plugin
diff --git a/src/decoder/flac_plugin.c b/src/decoder/flac_plugin.c
index 965a8b46b..b235469f2 100644
--- a/src/decoder/flac_plugin.c
+++ b/src/decoder/flac_plugin.c
@@ -300,6 +300,8 @@ flac_cue_tag_load(const char *file)
 	FLAC__uint64 track_time = 0;
 #ifdef HAVE_CUE /* libcue */
 	FLAC__StreamMetadata* vc = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
+	char* cs_filename;
+	FILE* cs_file;
 #endif /* libcue */
 	FLAC__StreamMetadata* si = FLAC__metadata_object_new(FLAC__METADATA_TYPE_STREAMINFO);
 	FLAC__StreamMetadata* cs = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
@@ -328,6 +330,18 @@ flac_cue_tag_load(const char *file)
 		}
 		FLAC__metadata_object_delete(vc);
 	}
+
+	if (tag == NULL) {
+		cs_filename = g_strconcat(file, ".cue", NULL);
+
+		cs_file = fopen(cs_filename, "rt");
+		g_free(cs_filename);
+
+		if (cs_file != NULL) {
+			tag = cue_tag_file(cs_file, tnum);
+			fclose(cs_file);
+		}
+	}
 #endif /* libcue */
 
 	if (tag == NULL)