warn about newlines and control characters

This commit is contained in:
Frédéric Mangano-Tarumi 2018-12-16 12:36:37 -05:00
parent 70e9b576cf
commit 46cd25f744
2 changed files with 22 additions and 1 deletions

View File

@ -146,6 +146,8 @@ void ot::print_comments(const std::list<std::string>& comments, FILE* output)
std::string local;
bool info_lost = false;
bool bad_comments = false;
bool has_newline = false;
bool has_control = false;
for (const std::string& comment : comments) {
ot::status rc = from_utf8(comment, local);
if (rc == ot::st::information_lost) {
@ -154,6 +156,12 @@ void ot::print_comments(const std::list<std::string>& comments, FILE* output)
bad_comments = true;
continue;
}
for (unsigned char c : comment) {
if (c == '\n')
has_newline = true;
else if (c < 0x20)
has_control = true;
}
fwrite(local.data(), 1, local.size(), output);
putchar('\n');
}
@ -161,6 +169,11 @@ void ot::print_comments(const std::list<std::string>& comments, FILE* output)
fputs("warning: Some tags have been transliterated to your system encoding.\n", stderr);
if (bad_comments)
fputs("warning: Some tags are not properly encoded and have not been displayed.\n", stderr);
if (has_newline)
fputs("warning: Some tags contain newline characters. "
"These are not supported by --set-all.\n", stderr);
if (has_control)
fputs("warning: Some tags contain control characters.\n", stderr);
}
/**

View File

@ -8,7 +8,7 @@ use strict;
use warnings;
use utf8;
use Test::More tests => 33;
use Test::More tests => 34;
use Digest::MD5;
use File::Basename;
@ -143,6 +143,14 @@ error: Invalid comment 'FOO'.
EOF
is(md5('out.opus'), '66780307a6081523dc9040f3c47b0448', 'the file did not change');
is_deeply(opustags('out.opus', '-D', '-a', "X=foo\nbar\tquux"), [<<'END_OUT', <<'END_ERR', 0], 'control characters');
X=foo
bar quux
END_OUT
warning: Some tags contain newline characters. These are not supported by --set-all.
warning: Some tags contain control characters.
END_ERR
is_deeply(opustags(qw(-i out.opus -s fatal=yes -s FOO -s BAR)), ['', <<'EOF', 256], 'bad tag with --set');
error: Invalid comment 'FOO'.
EOF