[flac-dev] Metaflac UTF-8 fixes

Janne Hyvärinen cse at sci.fi
Tue Apr 23 11:40:41 PDT 2013


Hopefully the last patch from me to UTF-8 issues.
Metaflac can now print all console supported characters from tags on the 
screen. It also fixes metaflac to be able to import its own exports back 
without non-ascii characters getting mutilated. And --no-utf8-convert 
now works properly with import and export commands.

I updated my Windows binary archive with these changes for any 
interested party to test:
http://www.saunalahti.fi/~cse/temp/flac-1.3pre3-mod.zip

-------------- next part --------------
diff --git a/src/metaflac/operations.c b/src/metaflac/operations.c
index e01fa5c..529b0e5 100644
--- a/src/metaflac/operations.c
+++ b/src/metaflac/operations.c
@@ -551,7 +551,7 @@ void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned
 	unsigned i, j;
 
 /*@@@ yuck, should do this with a varargs function or something: */
-#define PPR if(filename)flac_printf("%s:",filename);
+#define PPR if(filename) if(raw) printf("%s:",filename); else flac_printf("%s:",filename);
 	PPR; printf("METADATA block #%u\n", block_number);
 	PPR; printf("  type: %u (%s)\n", (unsigned)block->type, block->type < FLAC__METADATA_TYPE_UNDEFINED? FLAC__MetadataTypeString[block->type] : "UNKNOWN");
 	PPR; printf("  is last: %s\n", block->is_last? "true":"false");
diff --git a/src/metaflac/operations_shorthand_vorbiscomment.c b/src/metaflac/operations_shorthand_vorbiscomment.c
index 3d381a5..19f9602 100644
--- a/src/metaflac/operations_shorthand_vorbiscomment.c
+++ b/src/metaflac/operations_shorthand_vorbiscomment.c
@@ -96,7 +96,11 @@ FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bo
 			ok = remove_vc_firstfield(filename, block, operation->argument.vc_field_name.value, needs_write);
 			break;
 		case OP__SET_VC_FIELD:
+#ifdef _WIN32 /* do not convert anything or things will break */
+			ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, true);
+#else
 			ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, raw);
+#endif
 			break;
 		case OP__IMPORT_VC_FROM:
 			ok = import_vc_from(filename, block, &operation->argument.filename, needs_write, raw);
@@ -245,9 +249,7 @@ FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const
 	}
 	else {
 		FLAC__bool needs_free = false;
-#ifdef _WIN32 /* do not convert anything or things will break */
 		entry.entry = (FLAC__byte *)field->field;
-#else
 		if(raw) {
 			entry.entry = (FLAC__byte *)field->field;
 		}
@@ -259,7 +261,6 @@ FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const
 			flac_fprintf(stderr, "%s: ERROR: converting comment '%s' to UTF-8\n", filename, field->field);
 			return false;
 		}
-#endif
 		entry.length = strlen((const char *)entry.entry);
 		if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
 			if(needs_free)
diff --git a/src/metaflac/utils.c b/src/metaflac/utils.c
index 8b91011..be5a3c1 100644
--- a/src/metaflac/utils.c
+++ b/src/metaflac/utils.c
@@ -229,13 +229,18 @@ void write_vc_field(const char *filename, const FLAC__StreamMetadata_VorbisComme
 {
 	if(0 != entry->entry) {
 		if(filename)
-			fprintf(f, "%s:", filename);
+			flac_fprintf(f, "%s:", filename);
 
 		if(!raw) {
 			/*
 			 * WATCHOUT: comments that contain an embedded null will
 			 * be truncated by utf_decode().
 			 */
+#ifdef _WIN32 /* if we are outputting to console, we need to use proper print functions to show unicode characters */
+			if (f == stdout || f == stderr) {
+				flac_fprintf(f, "%s", entry->entry);
+			} else {
+#endif
 			char *converted;
 
 			if(utf8_decode((const char *)entry->entry, &converted) >= 0) {
@@ -245,6 +250,9 @@ void write_vc_field(const char *filename, const FLAC__StreamMetadata_VorbisComme
 			else {
 				(void) local_fwrite(entry->entry, 1, entry->length, f);
 			}
+#ifdef _WIN32
+			}
+#endif
 		}
 		else {
 			(void) local_fwrite(entry->entry, 1, entry->length, f);
diff --git a/src/share/utf8/utf8.c b/src/share/utf8/utf8.c
index beb815a..18495fe 100644
--- a/src/share/utf8/utf8.c
+++ b/src/share/utf8/utf8.c
@@ -203,7 +203,7 @@ int utf8_decode(const char *from, char **to)
         return -1;
     }
 
-    chars = WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode,
+    chars = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, unicode,
             -1, NULL, 0, NULL, NULL);
 
     if(chars < 0) /* underflow check */
@@ -224,7 +224,7 @@ int utf8_decode(const char *from, char **to)
         return -1;
     }
 
-    err = WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode,
+    err = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, unicode,
             -1, *to, chars, NULL, NULL);
     if(err != chars)
     {


More information about the flac-dev mailing list