From 135c0eb64732c9e9c26624723499355a3069a9e2 Mon Sep 17 00:00:00 2001 From: Urs Fleisch Date: Sat, 21 Oct 2023 15:57:26 +0200 Subject: [PATCH] tagreader: Fix displaying of seconds in length, UTF-8 for ostream The padding was wrong, 8 seconds were displayed as 0:80 instead of 0:08. Also make the output of tagreader and tagreader_c the same, including the encoding, which was still ISO-8859-1 for std::ostream. --- examples/tagreader.cpp | 23 ++++++++++++----------- examples/tagreader_c.c | 4 ++-- taglib/toolkit/tstring.cpp | 2 +- taglib/toolkit/tvariant.cpp | 2 +- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/examples/tagreader.cpp b/examples/tagreader.cpp index 61ed5747..54c2d4c3 100644 --- a/examples/tagreader.cpp +++ b/examples/tagreader.cpp @@ -56,18 +56,19 @@ int main(int argc, char *argv[]) cout << "genre - \"" << tag->genre() << "\"" << endl; TagLib::PropertyMap tags = f.properties(); - - unsigned int longest = 0; - for(auto i = tags.cbegin(); i != tags.cend(); ++i) { - if (i->first.size() > longest) { - longest = i->first.size(); + if(!tags.isEmpty()) { + unsigned int longest = 0; + for(auto i = tags.cbegin(); i != tags.cend(); ++i) { + if (i->first.size() > longest) { + longest = i->first.size(); + } } - } - cout << "-- TAG (properties) --" << endl; - for(auto i = tags.cbegin(); i != tags.cend(); ++i) { - for(auto j = i->second.begin(); j != i->second.end(); ++j) { - cout << left << std::setfill(' ') << std::setw(longest) << i->first << " - " << '"' << *j << '"' << endl; + cout << "-- TAG (properties) --" << endl; + for(auto i = tags.cbegin(); i != tags.cend(); ++i) { + for(auto j = i->second.begin(); j != i->second.end(); ++j) { + cout << left << std::setfill(' ') << std::setw(longest) << i->first << " - " << '"' << *j << '"' << endl; + } } } @@ -113,7 +114,7 @@ int main(int argc, char *argv[]) cout << "bitrate - " << properties->bitrate() << endl; cout << "sample rate - " << properties->sampleRate() << endl; cout << "channels - " << properties->channels() << endl; - cout << "length - " << minutes << ":" << setfill('0') << setw(2) << seconds << endl; + cout << "length - " << minutes << ":" << setfill('0') << setw(2) << right << seconds << endl; } } return 0; diff --git a/examples/tagreader_c.c b/examples/tagreader_c.c index fc55eb35..9f49f440 100644 --- a/examples/tagreader_c.c +++ b/examples/tagreader_c.c @@ -98,11 +98,11 @@ int main(int argc, char *argv[]) while(*keyPtr) { TagLib_Complex_Property_Attribute*** properties = taglib_complex_property_get(file, *keyPtr); - printf("%s:\n", *keyPtr); if(properties != NULL) { TagLib_Complex_Property_Attribute*** propPtr = properties; while(*propPtr) { TagLib_Complex_Property_Attribute** attrPtr = *propPtr; + printf("%s:\n", *keyPtr); while(*attrPtr) { TagLib_Complex_Property_Attribute *attr = *attrPtr; TagLib_Variant_Type type = attr->value.type; @@ -130,7 +130,7 @@ int main(int argc, char *argv[]) printf("%f\n", attr->value.value.doubleValue); break; case TagLib_Variant_String: - printf("%s\n", attr->value.value.stringValue); + printf("\"%s\"\n", attr->value.value.stringValue); break; case TagLib_Variant_StringList: if(attr->value.value.stringListValue) { diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 3c0830b4..6f70e1f2 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -706,6 +706,6 @@ TagLib::String operator+(const TagLib::String &s1, const char *s2) std::ostream &operator<<(std::ostream &s, const TagLib::String &str) { - s << str.to8Bit(); + s << str.to8Bit(true); return s; } diff --git a/taglib/toolkit/tvariant.cpp b/taglib/toolkit/tvariant.cpp index 98ab6848..53926205 100644 --- a/taglib/toolkit/tvariant.cpp +++ b/taglib/toolkit/tvariant.cpp @@ -116,7 +116,7 @@ public: void operator()(const TagLib::String &v) { s << '"'; - for (char c : v.to8Bit()) { + for (char c : v.to8Bit(true)) { if(c == '"') { s << "\\\""; }