| 1 | /** |
| 2 | * \file src/cli.cc |
| 3 | * \ingroup cli |
| 4 | * |
| 5 | * Provide all the features of the opustags executable from a C++ API. The main point of separating |
| 6 | * this module from the main one is to allow easy testing. |
| 7 | */ |
| 8 | |
| 9 | #include <opustags.h>cxx . . $ `<file>/src/opustags.h`/ |
| 10 | |
| 11 | #include <errno.h> |
| 12 | #include <getopt.h> |
| 13 | #include <limits.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <unistd.h> |
| 18 | #include <algorithm> |
| 19 | |
| 20 | static const char help_messagecxx . . $ help_message.[] = |
| 21 | PROJECT_NAMEcxx . . $ `build/config.h:1:9`! " version " PROJECT_VERSIONcxx . . $ `build/config.h:2:9`! |
| 22 | R"raw( |
| 23 | |
| 24 | Usage: opustags --help |
| 25 | opustags [OPTIONS] FILE |
| 26 | opustags OPTIONS -i FILE... |
| 27 | opustags OPTIONS FILE -o FILE |
| 28 | |
| 29 | Options: |
| 30 | -h, --help print this help |
| 31 | -o, --output FILE specify the output file |
| 32 | -i, --in-place overwrite the input files |
| 33 | -y, --overwrite overwrite the output file if it already exists |
| 34 | -a, --add FIELD=VALUE add a comment |
| 35 | -d, --delete FIELD[=VALUE] delete previously existing comments |
| 36 | -D, --delete-all delete all the previously existing comments |
| 37 | -s, --set FIELD=VALUE replace a comment |
| 38 | -S, --set-all import comments from standard input |
| 39 | -e, --edit edit tags interactively in VISUAL/EDITOR |
| 40 | --output-cover FILE extract and save the cover art, if any |
| 41 | --set-cover FILE sets the cover art |
| 42 | --vendor print the vendor string |
| 43 | --set-vendor VALUE set the vendor string |
| 44 | --raw disable encoding conversion |
| 45 | -z delimit tags with NUL |
| 46 | |
| 47 | See the man page for extensive documentation. |
| 48 | )raw"; |
| 49 | |
| 50 | static struct option getopt_optionscxx . . $ getopt_options.[] = { |
| 51 | {"help", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'h'}, |
| 52 | {"output", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'o'}, |
| 53 | {"in-place", optional_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:64:9`!, 0, 'i'}, |
| 54 | {"overwrite", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'y'}, |
| 55 | {"delete", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'd'}, |
| 56 | {"add", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'a'}, |
| 57 | {"set", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 's'}, |
| 58 | {"delete-all", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'D'}, |
| 59 | {"set-all", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'S'}, |
| 60 | {"edit", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'e'}, |
| 61 | {"output-cover", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'c'}, |
| 62 | {"set-cover", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'C'}, |
| 63 | {"vendor", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'v'}, |
| 64 | {"set-vendor", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'V'}, |
| 65 | {"raw", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'r'}, |
| 66 | {NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, 0, 0, 0} |
| 67 | }; |
| 68 | |
| 69 | otcxx . . $ ot/::optionscxx . . $ ot/options# otcxx . . $ ot/::parse_optionscxx . . $ ot/parse_options(be20433a3686cf7a).(int argc, char** argv, FILE* comments_input) |
| 70 | { |
| 71 | optionscxx . . $ ot/options# opt; |
| 72 | const char* equal; |
| 73 | otcxx . . $ ot/::statuscxx . . $ ot/status# rccxx . . $ ot/status#status(570d951ce3a7f975).; |
| 74 | stdcxx . . $ std/::listcxx . . $ std/__cxx11/list#<stdcxx . . $ std/::stringcxx . . $ std/string#> local_to_addcxx . . $ std/__cxx11/list#list(49f6e7a06ebc5aa8).; // opt.to_add before UTF-8 conversion. |
| 75 | stdcxx . . $ std/::listcxx . . $ std/__cxx11/list#<stdcxx . . $ std/::stringcxx . . $ std/string#> local_to_deletecxx . . $ std/__cxx11/list#list(49f6e7a06ebc5aa8).; // opt.to_delete before UTF-8 conversion. |
| 76 | bool set_all = false; |
| 77 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<stdcxx . . $ std/::stringcxx . . $ std/string#> set_covercxx . . $ std/optional#optional(ced63f7c635d850d).; |
| 78 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<stdcxx . . $ std/::stringcxx . . $ std/string#> set_vendorcxx . . $ std/optional#optional(ced63f7c635d850d).; |
| 79 | opt =cxx . . $ ot/options#`operator=`(80e0391707d06066). {}; |
| 80 | if (argc == 1) |
| 81 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "No arguments specified. Use -h for help."}; |
| 82 | int c; |
| 83 | optind = 0; |
| 84 | while ((c = getopt_long(argc, argv, ":ho:iyd:a:s:DSez", getopt_optionscxx . . $ getopt_options., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)) != -1) { |
| 85 | switch (c) { |
| 86 | case 'h': |
| 87 | opt.print_helpcxx . . $ ot/options#print_help. = true; |
| 88 | break; |
| 89 | case 'o': |
| 90 | if (opt.path_outcxx . . $ ot/options#path_out.) |
| 91 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot specify --output more than once."}; |
| 92 | opt.path_outcxx . . $ ot/options#path_out. =cxx . . $ std/optional#`operator=`(a631871c47728645). optarg; |
| 93 | break; |
| 94 | case 'i': |
| 95 | opt.in_placecxx . . $ ot/options#in_place. = true; |
| 96 | opt.overwritecxx . . $ ot/options#overwrite. = true; |
| 97 | break; |
| 98 | case 'y': |
| 99 | opt.overwritecxx . . $ ot/options#overwrite. = true; |
| 100 | break; |
| 101 | case 'd': |
| 102 | local_to_delete.emplace_backcxx . . $ std/__cxx11/list#emplace_back(6e27df1505231885).(optarg); |
| 103 | break; |
| 104 | case 'a': |
| 105 | case 's': |
| 106 | equal = strchr(optarg, '='); |
| 107 | if (equal == nullptr) |
| 108 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Comment does not contain an equal sign: "scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). optarg +cxx . . $ std/operator+(900ec9581c410ad9). "."}; |
| 109 | if (c == 's') |
| 110 | local_to_delete.emplace_backcxx . . $ std/__cxx11/list#emplace_back(6e27df1505231885).(optarg, equal - optarg); |
| 111 | local_to_add.emplace_backcxx . . $ std/__cxx11/list#emplace_back(6e27df1505231885).(optarg); |
| 112 | break; |
| 113 | case 'S': |
| 114 | opt.delete_allcxx . . $ ot/options#delete_all. = true; |
| 115 | set_all = true; |
| 116 | break; |
| 117 | case 'D': |
| 118 | opt.delete_allcxx . . $ ot/options#delete_all. = true; |
| 119 | break; |
| 120 | case 'e': |
| 121 | opt.edit_interactivelycxx . . $ ot/options#edit_interactively. = true; |
| 122 | break; |
| 123 | case 'c': |
| 124 | if (opt.cover_outcxx . . $ ot/options#cover_out.) |
| 125 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot specify --output-cover more than once."}; |
| 126 | opt.cover_outcxx . . $ ot/options#cover_out. =cxx . . $ std/optional#`operator=`(a631871c47728645). optarg; |
| 127 | break; |
| 128 | case 'C': |
| 129 | if (set_cover) |
| 130 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot specify --set-cover more than once."}; |
| 131 | set_cover =cxx . . $ std/optional#`operator=`(a631871c47728645). optarg; |
| 132 | break; |
| 133 | case 'v': |
| 134 | opt.print_vendorcxx . . $ ot/options#print_vendor. = true; |
| 135 | break; |
| 136 | case 'V': |
| 137 | if (set_vendor) |
| 138 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot specify --set-vendor more than once."}; |
| 139 | set_vendor =cxx . . $ std/optional#`operator=`(a631871c47728645). optarg; |
| 140 | break; |
| 141 | case 'r': |
| 142 | opt.rawcxx . . $ ot/options#raw. = true; |
| 143 | break; |
| 144 | case 'z': |
| 145 | opt.tag_delimitercxx . . $ ot/options#tag_delimiter. = '\0'; |
| 146 | break; |
| 147 | case ':': |
| 148 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Missing value for option '"scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). argv[optind - 1] +cxx . . $ std/operator+(900ec9581c410ad9). "'."}; |
| 149 | default: |
| 150 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Unrecognized option '" +cxx . . $ std/operator+(39ed2c165380f78e). |
| 151 | (optopt ? "-"scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(cd4e1e16c2d05e54). static_cast<char>(optopt) : argvcxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).[optind - 1]) +cxx . . $ std/operator+(900ec9581c410ad9). "'."}; |
| 152 | } |
| 153 | } |
| 154 | if (opt.print_helpcxx . . $ ot/options#print_help.) |
| 155 | return opt; |
| 156 | |
| 157 | // All non-option arguments are input files. |
| 158 | size_t stdin_uses = 0; |
| 159 | for (int i = optind; i < argc; i++) { |
| 160 | if (strcmp(argv[i], "-") == 0) |
| 161 | ++stdin_uses; |
| 162 | opt.paths_incxx . . $ ot/options#paths_in..emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).(argv[i]); |
| 163 | } |
| 164 | bool stdin_as_input = stdin_uses > 0; |
| 165 | |
| 166 | if (set_cover ==cxx . . $ std/`operator==`(348876303c74c3ee). "-") |
| 167 | ++stdin_uses; |
| 168 | if (set_all) |
| 169 | ++stdin_uses; |
| 170 | if (stdin_uses > 1) |
| 171 | throw statuscxx . . $ ot/status#status(15c445588664906). { stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot use standard input more than once." }; |
| 172 | |
| 173 | // Convert arguments to UTF-8. |
| 174 | if (opt.rawcxx . . $ ot/options#raw.) { |
| 175 | // Cast the user data without any encoding conversion. |
| 176 | auto cast_to_utf8 = [](stdcxx . . $ std/::string_viewcxx . . $ std/string_view# in) |
| 177 | { return stdcxx . . $ std/__cxx11/basic_string#basic_string(6f2e210728d16ac0).::u8stringcxx . . $ std/u8string#(reinterpret_cast<const char8_t*>(in.datacxx . . $ std/basic_string_view#data(8e614c7de0b1573e).()), in.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).()); }; |
| 178 | stdcxx . . $ std/::transformcxx . . $ std/transform(da30451c4a040264).(local_to_add.begincxx . . $ std/__cxx11/list#begin(57ce810e58fbcab0).(), local_to_add.endcxx . . $ std/__cxx11/list#end(57ce810e58fbcab0).(), |
| 179 | stdcxx . . $ std/::back_insertercxx . . $ std/back_inserter(7b559d6bdd90ff74).(opt.to_addcxx . . $ ot/options#to_add.), cast_to_utf8); |
| 180 | stdcxx . . $ std/::transformcxx . . $ std/transform(da30451c4a040264).(local_to_delete.begincxx . . $ std/__cxx11/list#begin(57ce810e58fbcab0).(), local_to_delete.endcxx . . $ std/__cxx11/list#end(57ce810e58fbcab0).(), |
| 181 | stdcxx . . $ std/::back_insertercxx . . $ std/back_inserter(7b559d6bdd90ff74).(opt.to_deletecxx . . $ ot/options#to_delete.), cast_to_utf8); |
| 182 | if (set_vendor) |
| 183 | opt.set_vendorcxx . . $ ot/options#set_vendor. =cxx . . $ std/optional#`operator=`(a631871c47728645). cast_to_utf8(cxx . . $ ot/parse_options(be20433a3686cf7a).$anonymous_type_0#`operator()`(5e4947f81b0531ad).*cxx . . $ std/optional#`operator*`(47e1a15210ec6306).set_vendor); |
| 184 | } else { |
| 185 | try { |
| 186 | stdcxx . . $ std/::transformcxx . . $ std/transform(da30451c4a040264).(local_to_add.begincxx . . $ std/__cxx11/list#begin(57ce810e58fbcab0).(), local_to_add.endcxx . . $ std/__cxx11/list#end(57ce810e58fbcab0).(), |
| 187 | stdcxx . . $ std/::back_insertercxx . . $ std/back_inserter(7b559d6bdd90ff74).(opt.to_addcxx . . $ ot/options#to_add.), encode_utf8cxx . . $ ot/encode_utf8(4bb652cfea9d987a).); |
| 188 | stdcxx . . $ std/::transformcxx . . $ std/transform(da30451c4a040264).(local_to_delete.begincxx . . $ std/__cxx11/list#begin(57ce810e58fbcab0).(), local_to_delete.endcxx . . $ std/__cxx11/list#end(57ce810e58fbcab0).(), |
| 189 | stdcxx . . $ std/::back_insertercxx . . $ std/back_inserter(7b559d6bdd90ff74).(opt.to_deletecxx . . $ ot/options#to_delete.), encode_utf8cxx . . $ ot/encode_utf8(4bb652cfea9d987a).); |
| 190 | if (set_vendor) |
| 191 | opt.set_vendorcxx . . $ ot/options#set_vendor. =cxx . . $ std/optional#`operator=`(a631871c47728645). encode_utf8cxx . . $ ot/encode_utf8(4bb652cfea9d987a).(*cxx . . $ std/optional#`operator*`(47e1a15210ec6306).set_vendor); |
| 192 | } catch (const otcxx . . $ ot/::statuscxx . . $ ot/status#& rc) { |
| 193 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Could not encode argument into UTF-8: " +cxx . . $ std/operator+(42c652dfebe55ad7). rc.messagecxx . . $ ot/status#message.}; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | bool read_only = !opt.in_placecxx . . $ ot/options#in_place. && !opt.path_outcxx . . $ ot/options#path_out..has_valuecxx . . $ std/optional#has_value(3482b152b9333168).(); |
| 198 | |
| 199 | if (opt.in_placecxx . . $ ot/options#in_place. && opt.path_outcxx . . $ ot/options#path_out.) |
| 200 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot combine --in-place and --output."}; |
| 201 | |
| 202 | if (opt.in_placecxx . . $ ot/options#in_place. && stdin_as_input) |
| 203 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot modify standard input in place."}; |
| 204 | |
| 205 | if ((!opt.in_placecxx . . $ ot/options#in_place. || opt.edit_interactivelycxx . . $ ot/options#edit_interactively.) && opt.paths_incxx . . $ ot/options#paths_in..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() != 1) |
| 206 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Exactly one input file must be specified."}; |
| 207 | |
| 208 | if (opt.edit_interactivelycxx . . $ ot/options#edit_interactively. && (stdin_as_input || opt.path_outcxx . . $ ot/options#path_out. ==cxx . . $ std/`operator==`(348876303c74c3ee). "-" || opt.cover_outcxx . . $ ot/options#cover_out. ==cxx . . $ std/`operator==`(348876303c74c3ee). "-")) |
| 209 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot edit interactively when standard input or standard output are already used."}; |
| 210 | |
| 211 | if (opt.edit_interactivelycxx . . $ ot/options#edit_interactively. && read_only) |
| 212 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot edit interactively when no output is specified."}; |
| 213 | |
| 214 | if (opt.edit_interactivelycxx . . $ ot/options#edit_interactively. && (opt.delete_allcxx . . $ ot/options#delete_all. || !opt.to_addcxx . . $ ot/options#to_add..emptycxx . . $ std/__cxx11/list#empty(3482b152b9333168).() || !opt.to_deletecxx . . $ ot/options#to_delete..emptycxx . . $ std/__cxx11/list#empty(3482b152b9333168).())) |
| 215 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot mix --edit with -adDsS."}; |
| 216 | |
| 217 | if (opt.cover_outcxx . . $ ot/options#cover_out. ==cxx . . $ std/`operator==`(348876303c74c3ee). "-" && opt.path_outcxx . . $ ot/options#path_out. ==cxx . . $ std/`operator==`(348876303c74c3ee). "-") |
| 218 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot specify standard output for both --output and --output-cover."}; |
| 219 | |
| 220 | if (opt.cover_outcxx . . $ ot/options#cover_out. && opt.paths_incxx . . $ ot/options#paths_in..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() > 1) |
| 221 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "Cannot use --output-cover with multiple input files."}; |
| 222 | |
| 223 | if (opt.print_vendorcxx . . $ ot/options#print_vendor. && !read_only) |
| 224 | throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_argumentscxx . . $ ot/st#bad_arguments., "--vendor is only supported in read-only mode."}; |
| 225 | |
| 226 | if (set_cover) { |
| 227 | byte_stringcxx . . $ ot/byte_string# picture_data = otcxx . . $ ot/::slurp_binary_filecxx . . $ ot/slurp_binary_file(568fd295d03a71e1).(set_cover->cxx . . $ std/optional#`operator->`(258ee1c9e0a18a5c).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 228 | opt.to_deletecxx . . $ ot/options#to_delete..push_backcxx . . $ std/__cxx11/list#push_back(fe7a8a4d1eddb70).(u8"METADATA_BLOCK_PICTURE"scxx . . $ std/literals/string_literals/`operator""s`(11d0dd56d2de4d28).); |
| 229 | opt.to_addcxx . . $ ot/options#to_add..push_backcxx . . $ std/__cxx11/list#push_back(fe7a8a4d1eddb70).(otcxx . . $ ot/::make_covercxx . . $ ot/make_cover(86a0a94dd06bebac).(picture_data)); |
| 230 | } |
| 231 | |
| 232 | if (set_all) { |
| 233 | // Read comments from stdin and prepend them to opt.to_add. |
| 234 | stdcxx . . $ std/::listcxx . . $ std/__cxx11/list#<stdcxx . . $ std/::u8stringcxx . . $ std/u8string#> comments = read_commentscxx . . $ ot/read_comments(40038bce797cca1f).(comments_input, opt); |
| 235 | opt.to_addcxx . . $ ot/options#to_add..splicecxx . . $ std/__cxx11/list#splice(9421c014527efe69).(optcxx . . $ std/_List_const_iterator#_List_const_iterator(2c469dd624388c9f)..to_addcxx . . $ ot/options#to_add..begincxx . . $ std/__cxx11/list#begin(57ce810e58fbcab0).(), stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(comments)); |
| 236 | } |
| 237 | return opt; |
| 238 | } |
| 239 | |
| 240 | /** Format a UTF-8 string by adding tabulations (\t) after line feeds (\n) to mark continuation for |
| 241 | * multiline values. With -z, this behavior applies for embedded NUL characters instead of LF. */ |
| 242 | static stdcxx . . $ std/::u8stringcxx . . $ std/u8string# format_valuecxx . . $ format_value(10fe4bc753274456).(const stdcxx . . $ std/::u8stringcxx . . $ std/u8string#& source, const otcxx . . $ ot/::optionscxx . . $ ot/options#& opt) |
| 243 | { |
| 244 | auto newline_count = stdcxx . . $ std/::countcxx . . $ std/count(698bbaade62a0610).(source.begincxx . . $ std/__cxx11/basic_string#begin(a865d278ca20c908).(), source.endcxx . . $ std/__cxx11/basic_string#end(a865d278ca20c908).(), opt.tag_delimitercxx . . $ ot/options#tag_delimiter.); |
| 245 | |
| 246 | // General case: the value fits on a single line. Use std::string’s copy constructor for the |
| 247 | // most efficient copy we could hope for. |
| 248 | if (newline_count == 0) |
| 249 | return sourcecxx . . $ std/__cxx11/basic_string#basic_string(a47f53d4115f6ce5).; |
| 250 | |
| 251 | stdcxx . . $ std/::u8stringcxx . . $ std/u8string# formattedcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 252 | formatted.reservecxx . . $ std/__cxx11/basic_string#reserve(a21092e7312342c).(source.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() + newline_count); |
| 253 | for (auto c : source) { |
| 254 | formatted.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(c); |
| 255 | if (c == opt.tag_delimitercxx . . $ ot/options#tag_delimiter.) |
| 256 | formatted.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(u8'\t'); |
| 257 | } |
| 258 | return formattedcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Convert the comment from UTF-8 to the system encoding if relevant, and print it with a trailing |
| 263 | * line feed. |
| 264 | */ |
| 265 | static void puts_utf8cxx . . $ puts_utf8(46adee08c0ee8c3e).(stdcxx . . $ std/::u8string_viewcxx . . $ std/u8string_view# str, FILE* output, const otcxx . . $ ot/::optionscxx . . $ ot/options#& opt) |
| 266 | { |
| 267 | if (opt.rawcxx . . $ ot/options#raw.) { |
| 268 | fwrite(str.datacxx . . $ std/basic_string_view#data(8e614c7de0b1573e).(), 1, str.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).(), output); |
| 269 | } else { |
| 270 | try { |
| 271 | stdcxx . . $ std/::stringcxx . . $ std/string# local = otcxx . . $ ot/::decode_utf8cxx . . $ ot/decode_utf8(249f514546b71f2d).(strcxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).); |
| 272 | fwrite(local.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).(), 1, local.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).(), output); |
| 273 | } catch (otcxx . . $ ot/::statuscxx . . $ ot/status#& rc) { |
| 274 | rc.messagecxx . . $ ot/status#message. +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). " See --raw."; |
| 275 | throw; |
| 276 | } |
| 277 | } |
| 278 | putc(opt.tag_delimitercxx . . $ ot/options#tag_delimiter., output); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Print comments in a human readable format that can also be read back in by #read_comment. |
| 283 | * |
| 284 | * To disambiguate between a newline embedded in a comment and a newline representing the start of |
| 285 | * the next tag, continuation lines always have a single TAB (^I) character added to the beginning. |
| 286 | */ |
| 287 | void otcxx . . $ ot/::print_commentscxx . . $ ot/print_comments(21f5f64e4bbe8a68).(const stdcxx . . $ std/::listcxx . . $ std/__cxx11/list#<stdcxx . . $ std/::u8stringcxx . . $ std/u8string#>& comments, FILE* output, const otcxx . . $ ot/::optionscxx . . $ ot/options#& opt) |
| 288 | { |
| 289 | bool has_control = false; |
| 290 | for (const stdcxx . . $ std/::u8stringcxx . . $ std/u8string#& source_comment : comments) { |
| 291 | if (!has_control) { // Don’t bother analyzing comments if the flag is already up. |
| 292 | for (unsigned char c : source_comment) { |
| 293 | if (c < 0x20 && c != '\n') { |
| 294 | has_control = true; |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | stdcxx . . $ std/::u8stringcxx . . $ std/u8string# utf8_comment = format_valuecxx . . $ format_value(10fe4bc753274456).(source_comment, opt); |
| 300 | puts_utf8cxx . . $ puts_utf8(46adee08c0ee8c3e).(utf8_comment, output, opt); |
| 301 | } |
| 302 | if (has_control) |
| 303 | fputs("warning: Some tags contain control characters.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!); |
| 304 | } |
| 305 | |
| 306 | stdcxx . . $ std/::listcxx . . $ std/__cxx11/list#<stdcxx . . $ std/::u8stringcxx . . $ std/u8string#> otcxx . . $ ot/::read_commentscxx . . $ ot/read_comments(40038bce797cca1f).(FILE* input, const otcxx . . $ ot/::optionscxx . . $ ot/options#& opt) |
| 307 | { |
| 308 | stdcxx . . $ std/::listcxx . . $ std/__cxx11/list#<stdcxx . . $ std/::u8stringcxx . . $ std/u8string#> commentscxx . . $ std/__cxx11/list#list(49f6e7a06ebc5aa8).; |
| 309 | comments.clearcxx . . $ std/__cxx11/list#clear(ced63f7c635d850d).(); |
| 310 | char* source_line = nullptr; |
| 311 | size_t buflen = 0; |
| 312 | ssize_t nread; |
| 313 | stdcxx . . $ std/::u8stringcxx . . $ std/u8string#* previous_comment = nullptr; |
| 314 | while ((nread = getdelim(&source_line, &buflen, opt.tag_delimitercxx . . $ ot/options#tag_delimiter., input)) != -1) { |
| 315 | if (nread > 0 && source_line[nread - 1] == opt.tag_delimitercxx . . $ ot/options#tag_delimiter.) |
| 316 | --nread; // Chomp. |
| 317 | |
| 318 | stdcxx . . $ std/::u8stringcxx . . $ std/u8string# linecxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 319 | if (opt.rawcxx . . $ ot/options#raw.) { |
| 320 | line =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). stdcxx . . $ std/__cxx11/basic_string#basic_string(6f2e210728d16ac0).::u8stringcxx . . $ std/u8string#(reinterpret_cast<char8_t*>(source_line), nread); |
| 321 | } else { |
| 322 | try { |
| 323 | line =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). encode_utf8cxx . . $ ot/encode_utf8(4bb652cfea9d987a).(stdcxx . . $ std/basic_string_view#basic_string_view(f8b699e64a75d442).::string_viewcxx . . $ std/string_view#(source_line, nread)); |
| 324 | } catch (const otcxx . . $ ot/::statuscxx . . $ ot/status#& rc) { |
| 325 | freecxx . . $ free(bd21765a0afc8e3c).(source_line); |
| 326 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::badly_encodedcxx . . $ ot/st#badly_encoded., "UTF-8 conversion error: " +cxx . . $ std/operator+(42c652dfebe55ad7). rc.messagecxx . . $ ot/status#message.}; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if (line.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).()) { |
| 331 | // Ignore empty lines. |
| 332 | previous_comment = nullptr; |
| 333 | } else if (line[cxx . . $ std/__cxx11/basic_string#`operator[]`(69897ebe6c310f7e).0] == u8'#') { |
| 334 | // Ignore comments. |
| 335 | previous_comment = nullptr; |
| 336 | } else if (line[cxx . . $ std/__cxx11/basic_string#`operator[]`(69897ebe6c310f7e).0] == u8'\t') { |
| 337 | // Continuation line: append the current line to the previous tag. |
| 338 | if (previous_comment == nullptr) { |
| 339 | otcxx . . $ ot/::statuscxx . . $ ot/status# rc = {cxx . . $ ot/status#status(15c445588664906).otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Unexpected continuation line: " +cxx . . $ std/operator+(39ed2c165380f78e). stdcxx . . $ std/__cxx11/basic_string#basic_string(6f2e210728d16ac0).::stringcxx . . $ std/string#(source_line, nread)}; |
| 340 | freecxx . . $ free(bd21765a0afc8e3c).(source_line); |
| 341 | throw rc; |
| 342 | } else { |
| 343 | line[cxx . . $ std/__cxx11/basic_string#`operator[]`(69897ebe6c310f7e).0] = opt.tag_delimitercxx . . $ ot/options#tag_delimiter.; |
| 344 | previous_comment->appendcxx . . $ std/__cxx11/basic_string#append(c0b47be1fddc0e75).(line); |
| 345 | } |
| 346 | } else if (line.findcxx . . $ std/__cxx11/basic_string#find(bec4eca5a23fda83).(u8'=') == decltypecxx . . $ std/__cxx11/basic_string#(line)::nposcxx . . $ std/__cxx11/basic_string#npos.) { |
| 347 | otcxx . . $ ot/::statuscxx . . $ ot/status# rc = {cxx . . $ ot/status#status(15c445588664906).otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Malformed tag: " +cxx . . $ std/operator+(39ed2c165380f78e). stdcxx . . $ std/__cxx11/basic_string#basic_string(6f2e210728d16ac0).::stringcxx . . $ std/string#(source_line, nread)}; |
| 348 | freecxx . . $ free(bd21765a0afc8e3c).(source_line); |
| 349 | throw rc; |
| 350 | } else { |
| 351 | previous_comment = &comments.emplace_backcxx . . $ std/__cxx11/list#emplace_back(6e27df1505231885).(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(line)); |
| 352 | } |
| 353 | } |
| 354 | freecxx . . $ free(bd21765a0afc8e3c).(source_line); |
| 355 | return commentscxx . . $ std/__cxx11/list#list(dc54fa54483e74db).; |
| 356 | } |
| 357 | |
| 358 | void otcxx . . $ ot/::delete_commentscxx . . $ ot/delete_comments(e24a0c7c6463300e).(stdcxx . . $ std/::listcxx . . $ std/__cxx11/list#<stdcxx . . $ std/::u8stringcxx . . $ std/u8string#>& comments, const stdcxx . . $ std/::u8stringcxx . . $ std/u8string#& selector) |
| 359 | { |
| 360 | auto name = selector.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(); |
| 361 | auto equal = selector.findcxx . . $ std/__cxx11/basic_string#find(bec4eca5a23fda83).(u8'='); |
| 362 | auto value = (equal == stdcxx . . $ std/::u8stringcxx . . $ std/u8string#::nposcxx . . $ std/__cxx11/basic_string#npos. ? nullptr : name + equal + 1); |
| 363 | auto name_len = value ? equal : selector.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).(); |
| 364 | auto value_len = value ? selector.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() - equal - 1 : 0; |
| 365 | auto it = comments.begincxx . . $ std/__cxx11/list#begin(57ce810e58fbcab0).(), end = comments.endcxx . . $ std/__cxx11/list#end(57ce810e58fbcab0).(); |
| 366 | while (it != end) { |
| 367 | auto current = it++cxx . . $ std/_List_iterator#operator++(42220274f0bb161f).; |
| 368 | /** \todo Avoid using strncasecmp because it assumes the system locale is UTF-8. */ |
| 369 | bool name_match = current->cxx . . $ std/_List_iterator#`operator->`(34d6acfd341ba7ab).sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() > name_len + 1 && |
| 370 | (*cxx . . $ std/_List_iterator#`operator*`(d426bc7b625886b7).current)[cxx . . $ std/__cxx11/basic_string#`operator[]`(69897ebe6c310f7e).name_len] == '=' && |
| 371 | strncasecmp((const char*) current->cxx . . $ std/_List_iterator#`operator->`(34d6acfd341ba7ab).datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).(), (const char*) name, name_len) == 0; |
| 372 | if (!name_match) |
| 373 | continue; |
| 374 | bool value_match = value == nullptr || |
| 375 | (current->cxx . . $ std/_List_iterator#`operator->`(34d6acfd341ba7ab).sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() == selector.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() && |
| 376 | memcmp(current->cxx . . $ std/_List_iterator#`operator->`(34d6acfd341ba7ab).datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).() + equal + 1, value, value_len) == 0); |
| 377 | if (value_match) |
| 378 | comments.erasecxx . . $ std/__cxx11/list#erase(aceec42672b9127f).(currentcxx . . $ std/_List_const_iterator#_List_const_iterator(2c469dd624388c9f).); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** Apply the modifications requested by the user to the opustags packet. */ |
| 383 | static void edit_tagscxx . . $ edit_tags(c5c15f8165c39885).(otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags#& tags, const otcxx . . $ ot/::optionscxx . . $ ot/options#& opt) |
| 384 | { |
| 385 | if (opt.set_vendorcxx . . $ ot/options#set_vendor.) |
| 386 | tags.vendorcxx . . $ ot/opus_tags#vendor. =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). *cxx . . $ std/optional#`operator*`(a104ceb4a760916d).opt.set_vendorcxx . . $ ot/options#set_vendor.; |
| 387 | |
| 388 | if (opt.delete_allcxx . . $ ot/options#delete_all.) { |
| 389 | tags.commentscxx . . $ ot/opus_tags#comments..clearcxx . . $ std/__cxx11/list#clear(ced63f7c635d850d).(); |
| 390 | } else for (const stdcxx . . $ std/::u8stringcxx . . $ std/u8string#& name : opt.to_deletecxx . . $ ot/options#to_delete.) { |
| 391 | otcxx . . $ ot/::delete_commentscxx . . $ ot/delete_comments(e24a0c7c6463300e).(tags.commentscxx . . $ ot/opus_tags#comments., name); |
| 392 | } |
| 393 | |
| 394 | for (const stdcxx . . $ std/::u8stringcxx . . $ std/u8string#& comment : opt.to_addcxx . . $ ot/options#to_add.) |
| 395 | tags.commentscxx . . $ ot/opus_tags#comments..emplace_backcxx . . $ std/__cxx11/list#emplace_back(6e27df1505231885).(comment); |
| 396 | } |
| 397 | |
| 398 | /** Spawn VISUAL or EDITOR to edit the given tags. */ |
| 399 | static void edit_tags_interactivelycxx . . $ edit_tags_interactively(d73e2b678d7d6933).(otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags#& tags, const stdcxx . . $ std/::optionalcxx . . $ std/optional#<stdcxx . . $ std/::stringcxx . . $ std/string#>& base_path, const otcxx . . $ ot/::optionscxx . . $ ot/options#& opt) |
| 400 | { |
| 401 | const char* editor = nullptr; |
| 402 | if (getenvcxx . . $ getenv(c63359b659779399).("TERM") != nullptr) |
| 403 | editor = getenvcxx . . $ getenv(c63359b659779399).("VISUAL"); |
| 404 | if (editor == nullptr) // without a terminal, or if VISUAL is unset |
| 405 | editor = getenvcxx . . $ getenv(c63359b659779399).("EDITOR"); |
| 406 | if (editor == nullptr) |
| 407 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., |
| 408 | "No editor specified in environment variable VISUAL or EDITOR."}; |
| 409 | |
| 410 | // Building the temporary tags file. |
| 411 | otcxx . . $ ot/::statuscxx . . $ ot/status# rccxx . . $ ot/status#status(570d951ce3a7f975).; |
| 412 | stdcxx . . $ std/::stringcxx . . $ std/string# tags_path = base_path.value_orcxx . . $ std/optional#value_or(ad7e34b55e27c58e).("tags") +cxx . . $ std/operator+(900ec9581c410ad9). ".XXXXXX.opustags"; |
| 413 | int fd = mkstemps(const_cast<char*>(tags_path.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).()), 9); |
| 414 | otcxx . . $ ot/::filecxx . . $ ot/file# tags_filecxx . . $ ot/file#file(8d4262d45fc644ab).; |
| 415 | if (fd == -1 || (tags_file =cxx . . $ ot/file#`operator=`(8da8a9ac207569ce). fdopencxx . . $ ot/file#file(8d4262d45fc644ab).(fd, "w")) ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) |
| 416 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., |
| 417 | "Could not open '" +cxx . . $ std/operator+(42c652dfebe55ad7). tags_path +cxx . . $ std/operator+(900ec9581c410ad9). "': " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 418 | otcxx . . $ ot/::print_commentscxx . . $ ot/print_comments(21f5f64e4bbe8a68).(tags.commentscxx . . $ ot/opus_tags#comments., tags_file.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), opt); |
| 419 | tags_file.resetcxx . . $ std/unique_ptr#reset(c8c3025d73122d7d).(); |
| 420 | |
| 421 | // Spawn the editor, and watch the modification timestamps. |
| 422 | timespec before = otcxx . . $ ot/::get_file_timestampcxx . . $ ot/get_file_timestamp(afc89d0ecac88f66).(tags_path.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 423 | otcxx . . $ ot/::statuscxx . . $ ot/status# editor_rccxx . . $ ot/status#status(570d951ce3a7f975).; |
| 424 | try { |
| 425 | otcxx . . $ ot/::run_editorcxx . . $ ot/run_editor(9ebb09e227cac344).(editorcxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792)., tags_path); |
| 426 | editor_rc =cxx . . $ ot/status#`operator=`(a0ede868b080ffee). otcxx . . $ ot/status#status(570d951ce3a7f975).::stcxx . . $ ot/st#::okcxx . . $ ot/st#ok.; |
| 427 | } catch (const otcxx . . $ ot/::statuscxx . . $ ot/status#& rc) { |
| 428 | editor_rc =cxx . . $ ot/status#`operator=`(ede20b08b8729810). rc; |
| 429 | } |
| 430 | timespec after = otcxx . . $ ot/::get_file_timestampcxx . . $ ot/get_file_timestamp(afc89d0ecac88f66).(tags_path.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 431 | bool modified = (before.tv_sec != after.tv_sec || before.tv_nsec != after.tv_nsec); |
| 432 | if (editor_rc != otcxx . . $ ot/::stcxx . . $ ot/st#::okcxx . . $ ot/st#ok.) { |
| 433 | if (modified) |
| 434 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "warning: Leaving %s on the disk.\n", tags_path.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 435 | else |
| 436 | remove(tags_path.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 437 | throw editor_rc; |
| 438 | } else if (!modified) { |
| 439 | remove(tags_path.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 440 | fputs("Cancelling edition because the tags file was not modified.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!); |
| 441 | throw otcxx . . $ ot/status#status(570d951ce3a7f975).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::cancelcxx . . $ ot/st#cancel.}; |
| 442 | } |
| 443 | |
| 444 | // Applying the new tags. |
| 445 | tags_file =cxx . . $ ot/file#`operator=`(8da8a9ac207569ce). fopencxx . . $ ot/file#file(8d4262d45fc644ab).(tags_path.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), "re"); |
| 446 | if (tags_file ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) |
| 447 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., "Error opening " +cxx . . $ std/operator+(42c652dfebe55ad7). tags_path +cxx . . $ std/operator+(900ec9581c410ad9). ": " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 448 | try { |
| 449 | tags.commentscxx . . $ ot/opus_tags#comments. =cxx . . $ std/__cxx11/list#`operator=`(926e4d17a59d25e2). otcxx . . $ ot/::read_commentscxx . . $ ot/read_comments(40038bce797cca1f).(tags_file.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), opt); |
| 450 | } catch (const otcxx . . $ ot/::statuscxx . . $ ot/status#& rc) { |
| 451 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "warning: Leaving %s on the disk.\n", tags_path.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 452 | throw; |
| 453 | } |
| 454 | tags_file.resetcxx . . $ std/unique_ptr#reset(c8c3025d73122d7d).(); |
| 455 | |
| 456 | // Remove the temporary tags file only on success, because unlike the |
| 457 | // partial Ogg file that is irrecoverable, the edited tags file |
| 458 | // contains user data, so let’s leave users a chance to recover it. |
| 459 | remove(tags_path.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 460 | } |
| 461 | |
| 462 | static void output_covercxx . . $ output_cover(1d69d064047a1619).(const otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags#& tags, const otcxx . . $ ot/::optionscxx . . $ ot/options# &opt) |
| 463 | { |
| 464 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<otcxx . . $ ot/::picturecxx . . $ ot/picture#> cover = extract_covercxx . . $ ot/extract_cover(757f97eae4ffafe0).(tags); |
| 465 | if (!cover) { |
| 466 | fputs("warning: No cover found.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | otcxx . . $ ot/::filecxx . . $ ot/file# outputcxx . . $ ot/file#file(8d4262d45fc644ab).; |
| 471 | if (opt.cover_outcxx . . $ ot/options#cover_out. ==cxx . . $ std/`operator==`(348876303c74c3ee). "-") { |
| 472 | output =cxx . . $ ot/file#`operator=`(8da8a9ac207569ce). stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!; |
| 473 | } else { |
| 474 | struct stat output_info; |
| 475 | if (stat(opt.cover_outcxx . . $ ot/options#cover_out.->cxx . . $ std/optional#`operator->`(8e614c7de0b1573e).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), &output_info) == 0) { |
| 476 | if (S_ISREGcxx . . $ `<external>/531885356569766532/stat.h:128:9`!(output_info.st_mode) && !opt.overwritecxx . . $ ot/options#overwrite.) |
| 477 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "'" +cxx . . $ std/operator+(42c652dfebe55ad7). opt.cover_outcxx . . $ ot/options#cover_out..valuecxx . . $ std/optional#value(8cf0592b1484bc82).() +cxx . . $ std/operator+(900ec9581c410ad9). "' already exists. Use -y to overwrite."}; |
| 478 | } else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) { |
| 479 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Could not identify '" +cxx . . $ std/operator+(42c652dfebe55ad7). opt.cover_outcxx . . $ ot/options#cover_out..valuecxx . . $ std/optional#value(8cf0592b1484bc82).() +cxx . . $ std/operator+(900ec9581c410ad9). "': " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 480 | } |
| 481 | |
| 482 | output =cxx . . $ ot/file#`operator=`(8da8a9ac207569ce). fopencxx . . $ ot/file#file(8d4262d45fc644ab).(opt.cover_outcxx . . $ ot/options#cover_out.->cxx . . $ std/optional#`operator->`(8e614c7de0b1573e).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), "w"); |
| 483 | if (output ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) |
| 484 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., "Could not open '" +cxx . . $ std/operator+(42c652dfebe55ad7). opt.cover_outcxx . . $ ot/options#cover_out..valuecxx . . $ std/optional#value(8cf0592b1484bc82).() +cxx . . $ std/operator+(900ec9581c410ad9). "' for writing: " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 485 | } |
| 486 | |
| 487 | if (fwrite(cover->cxx . . $ std/optional#`operator->`(258ee1c9e0a18a5c).picture_datacxx . . $ ot/picture#picture_data..datacxx . . $ std/basic_string_view#data(8e614c7de0b1573e).(), 1, cover->cxx . . $ std/optional#`operator->`(258ee1c9e0a18a5c).picture_datacxx . . $ ot/picture#picture_data..sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).(), output.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) < cover->cxx . . $ std/optional#`operator->`(258ee1c9e0a18a5c).picture_datacxx . . $ ot/picture#picture_data..sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).()) |
| 488 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., "fwrite error: "scxx . . $ std/literals/string_literals/`operator""s`(aa2a803d3c5b7a78). +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Main loop of opustags. Read the packets from the reader, and forwards them to the writer. |
| 493 | * Transform the OpusTags packet on the fly. |
| 494 | * |
| 495 | * The writer is optional. When writer is nullptr, opustags runs in read-only mode. |
| 496 | */ |
| 497 | static void processcxx . . $ process(15eafbdeb72ec27e).(otcxx . . $ ot/::ogg_readercxx . . $ ot/ogg_reader#& reader, otcxx . . $ ot/::ogg_writercxx . . $ ot/ogg_writer#* writer, const otcxx . . $ ot/::optionscxx . . $ ot/options# &opt) |
| 498 | { |
| 499 | bool focused = false; /*< the stream on which we operate is defined */ |
| 500 | int focused_serialno; /*< when focused, the serialno of the focused stream */ |
| 501 | |
| 502 | /** When the number of pages the OpusTags packet takes differs from the input stream to the |
| 503 | * output stream, we need to renumber all the succeeding pages. If the input stream |
| 504 | * contains gaps, the offset will naively reproduce the gaps: page numbers 0 (1) 2 4 will |
| 505 | * become 0 (1 2) 3 5, where (…) is the OpusTags packet, and not 0 (1 2) 3 4. */ |
| 506 | int pageno_offset = 0; |
| 507 | |
| 508 | while (reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).()) { |
| 509 | auto serialno = ogg_page_serialno(&reader.pagecxx . . $ ot/ogg_reader#page.); |
| 510 | auto pageno = ogg_page_pageno(&reader.pagecxx . . $ ot/ogg_reader#page.); |
| 511 | if (!focused) { |
| 512 | focused = true; |
| 513 | focused_serialno = serialno; |
| 514 | } else if (serialno != focused_serialno) { |
| 515 | /** \todo Support mixed streams. */ |
| 516 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Muxed streams are not supported yet."}; |
| 517 | } |
| 518 | if (reader.absolute_page_nocxx . . $ ot/ogg_reader#absolute_page_no. == 0) { // Identification header |
| 519 | if (!otcxx . . $ ot/::is_opus_streamcxx . . $ ot/is_opus_stream(f406ab5a65ea8040).(reader.pagecxx . . $ ot/ogg_reader#page.)) |
| 520 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Not an Opus stream."}; |
| 521 | if (writer) |
| 522 | writer->write_pagecxx . . $ ot/ogg_writer#write_page(40553eaf79898ae3).(reader.pagecxx . . $ ot/ogg_reader#page.); |
| 523 | } else if (reader.absolute_page_nocxx . . $ ot/ogg_reader#absolute_page_no. == 1) { // Comment header |
| 524 | otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags# tags; |
| 525 | reader.process_header_packetcxx . . $ ot/ogg_reader#process_header_packet(4de049347c6f80eb).([cxx . . $ std/function#function(bdc3712e480e4d04).&tags](ogg_packet& p) { tags =cxx . . $ ot/opus_tags#`operator=`(b58e29ffabd83a96). otcxx . . $ ot/::parse_tagscxx . . $ ot/parse_tags(eaf0a27cbd141b32).(p); }); |
| 526 | if (opt.cover_outcxx . . $ ot/options#cover_out.) |
| 527 | output_covercxx . . $ output_cover(1d69d064047a1619).(tags, opt); |
| 528 | edit_tagscxx . . $ edit_tags(c5c15f8165c39885).(tags, opt); |
| 529 | if (writer) { |
| 530 | if (opt.edit_interactivelycxx . . $ ot/options#edit_interactively.) { |
| 531 | fflush(writer->filecxx . . $ ot/ogg_writer#file.); // flush before calling the subprocess |
| 532 | edit_tags_interactivelycxx . . $ edit_tags_interactively(d73e2b678d7d6933).(tags, writer->pathcxx . . $ ot/ogg_writer#path., opt); |
| 533 | } |
| 534 | auto packet = otcxx . . $ ot/::render_tagscxx . . $ ot/render_tags(67259994f473118f).(tags); |
| 535 | writer->write_header_packetcxx . . $ ot/ogg_writer#write_header_packet(39685ba16f0fc2d4).(serialno, pageno, packet); |
| 536 | pageno_offset = writer->next_page_nocxx . . $ ot/ogg_writer#next_page_no. - 1 - reader.absolute_page_nocxx . . $ ot/ogg_reader#absolute_page_no.; |
| 537 | } else { |
| 538 | if (opt.cover_outcxx . . $ ot/options#cover_out. !=cxx . . $ std/`operator!=`(cd853fd68ea5eee9). "-") { |
| 539 | if (opt.print_vendorcxx . . $ ot/options#print_vendor.) |
| 540 | puts_utf8cxx . . $ puts_utf8(46adee08c0ee8c3e).(tags.vendorcxx . . $ ot/opus_tags#vendor., stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, opt); |
| 541 | else |
| 542 | otcxx . . $ ot/::print_commentscxx . . $ ot/print_comments(21f5f64e4bbe8a68).(tags.commentscxx . . $ ot/opus_tags#comments., stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, opt); |
| 543 | } |
| 544 | break; |
| 545 | } |
| 546 | } else if (writer) { |
| 547 | otcxx . . $ ot/::renumber_pagecxx . . $ ot/renumber_page(99e4e6107b845fea).(reader.pagecxx . . $ ot/ogg_reader#page., pageno + pageno_offset); |
| 548 | writer->write_pagecxx . . $ ot/ogg_writer#write_page(40553eaf79898ae3).(reader.pagecxx . . $ ot/ogg_reader#page.); |
| 549 | } |
| 550 | } |
| 551 | if (reader.absolute_page_nocxx . . $ ot/ogg_reader#absolute_page_no. < 1) |
| 552 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Expected at least 2 Ogg pages."}; |
| 553 | } |
| 554 | |
| 555 | static void run_singlecxx . . $ run_single(86aacecc86c5310b).(const otcxx . . $ ot/::optionscxx . . $ ot/options#& opt, const stdcxx . . $ std/::stringcxx . . $ std/string#& path_in, const stdcxx . . $ std/::optionalcxx . . $ std/optional#<stdcxx . . $ std/::stringcxx . . $ std/string#>& path_out) |
| 556 | { |
| 557 | otcxx . . $ ot/::filecxx . . $ ot/file# inputcxx . . $ ot/file#file(8d4262d45fc644ab).; |
| 558 | if (path_in ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "-") |
| 559 | input =cxx . . $ ot/file#`operator=`(8da8a9ac207569ce). stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!; |
| 560 | else if ((input =cxx . . $ ot/file#`operator=`(8da8a9ac207569ce). fopencxx . . $ ot/file#file(8d4262d45fc644ab).(path_in.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), "re")) ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) |
| 561 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., |
| 562 | "Could not open '" +cxx . . $ std/operator+(42c652dfebe55ad7). path_in +cxx . . $ std/operator+(900ec9581c410ad9). "' for reading: " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 563 | otcxx . . $ ot/::ogg_readercxx . . $ ot/ogg_reader# readercxx . . $ ot/ogg_reader#ogg_reader(8d4262d45fc644ab).(input.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()); |
| 564 | |
| 565 | /* Read-only mode. */ |
| 566 | if (!path_out) { |
| 567 | processcxx . . $ process(15eafbdeb72ec27e).(reader, nullptr, opt); |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | /* Read-write mode. |
| 572 | * |
| 573 | * The output pointer is set to one of: |
| 574 | * - stdout for "-", |
| 575 | * - final_output.get() for special files like /dev/null, |
| 576 | * - temporary_output.get() for regular files. |
| 577 | * |
| 578 | * We use a temporary output file for the following reasons: |
| 579 | * 1. A partial .opus output would be seen by softwares like media players, but a .part |
| 580 | * (for partial) won’t. |
| 581 | * 2. If the process crashes badly, or the power cuts off, we don't want to leave a partial |
| 582 | * file at the final location. The temporary file is going to remain though. |
| 583 | * 3. If we're overwriting a regular file, we'd rather avoid wiping its content before we |
| 584 | * even started reading the input file. That way, the original file is always preserved |
| 585 | * on error or crash. |
| 586 | * 4. It is necessary for in-place editing. We can't reliably open the same file as both |
| 587 | * input and output. |
| 588 | */ |
| 589 | |
| 590 | FILE* output = nullptr; |
| 591 | otcxx . . $ ot/::partial_filecxx . . $ ot/partial_file# temporary_output; |
| 592 | otcxx . . $ ot/::filecxx . . $ ot/file# final_outputcxx . . $ ot/file#file(8d4262d45fc644ab).; |
| 593 | |
| 594 | struct stat output_info; |
| 595 | if (path_out ==cxx . . $ std/`operator==`(348876303c74c3ee). "-") { |
| 596 | output = stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!; |
| 597 | } else if (stat(path_out->cxx . . $ std/optional#`operator->`(8e614c7de0b1573e).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), &output_info) == 0) { |
| 598 | /* The output file exists. */ |
| 599 | if (!S_ISREGcxx . . $ `<external>/531885356569766532/stat.h:128:9`!(output_info.st_mode)) { |
| 600 | /* Special files are opened for writing directly. */ |
| 601 | if ((final_output =cxx . . $ ot/file#`operator=`(8da8a9ac207569ce). fopencxx . . $ ot/file#file(8d4262d45fc644ab).(path_out->cxx . . $ std/optional#`operator->`(8e614c7de0b1573e).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), "we")) ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) |
| 602 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::standard_errorcxx . . $ ot/st#standard_error., |
| 603 | "Could not open '" +cxx . . $ std/operator+(42c652dfebe55ad7). path_out.valuecxx . . $ std/optional#value(8cf0592b1484bc82).() +cxx . . $ std/operator+(900ec9581c410ad9). "' for writing: " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 604 | output = final_output.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(); |
| 605 | } else if (opt.overwritecxx . . $ ot/options#overwrite.) { |
| 606 | temporary_output.opencxx . . $ ot/partial_file#open(85c52e162fed56f9).(path_out->cxx . . $ std/optional#`operator->`(8e614c7de0b1573e).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 607 | output = temporary_output.getcxx . . $ ot/partial_file#get(3637adb0f06093ef).(); |
| 608 | } else { |
| 609 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "'" +cxx . . $ std/operator+(42c652dfebe55ad7). path_out.valuecxx . . $ std/optional#value(8cf0592b1484bc82).() +cxx . . $ std/operator+(900ec9581c410ad9). "' already exists. Use -y to overwrite."}; |
| 610 | } |
| 611 | } else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) { |
| 612 | temporary_output.opencxx . . $ ot/partial_file#open(85c52e162fed56f9).(path_out->cxx . . $ std/optional#`operator->`(8e614c7de0b1573e).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 613 | output = temporary_output.getcxx . . $ ot/partial_file#get(3637adb0f06093ef).(); |
| 614 | } else { |
| 615 | throw otcxx . . $ ot/status#status(15c445588664906).::statuscxx . . $ ot/status# {otcxx . . $ ot/::stcxx . . $ ot/st#::errorcxx . . $ ot/st#error., "Could not identify '" +cxx . . $ std/operator+(42c652dfebe55ad7). path_out.valuecxx . . $ std/optional#value(8cf0592b1484bc82).() +cxx . . $ std/operator+(900ec9581c410ad9). "': " +cxx . . $ std/operator+(900ec9581c410ad9). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)}; |
| 616 | } |
| 617 | |
| 618 | otcxx . . $ ot/::ogg_writercxx . . $ ot/ogg_writer# writercxx . . $ ot/ogg_writer#ogg_writer(8d4262d45fc644ab).(output); |
| 619 | writer.pathcxx . . $ ot/ogg_writer#path. =cxx . . $ std/optional#`operator=`(315b724577b3c69a). path_out; |
| 620 | processcxx . . $ process(15eafbdeb72ec27e).(reader, &writer, opt); |
| 621 | |
| 622 | // Close the input file and finalize the output. When --in-place is specified, some file |
| 623 | // systems like SMB require that the input is closed first. |
| 624 | input.resetcxx . . $ std/unique_ptr#reset(c8c3025d73122d7d).(); |
| 625 | temporary_output.commitcxx . . $ ot/partial_file#commit(49f6e7a06ebc5aa8).(); |
| 626 | } |
| 627 | |
| 628 | void otcxx . . $ ot/::runcxx . . $ ot/run(47bf986496f380f8).(const otcxx . . $ ot/::optionscxx . . $ ot/options#& opt) |
| 629 | { |
| 630 | if (opt.print_helpcxx . . $ ot/options#print_help.) { |
| 631 | fputs(help_messagecxx . . $ help_message., stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!); |
| 632 | return; |
| 633 | } |
| 634 | |
| 635 | otcxx . . $ ot/::statuscxx . . $ ot/status# global_rc = stcxx . . $ ot/status#status(570d951ce3a7f975).::okcxx . . $ ot/st#ok.; |
| 636 | for (const auto& path_in : opt.paths_incxx . . $ ot/options#paths_in.) { |
| 637 | try { |
| 638 | run_singlecxx . . $ run_single(86aacecc86c5310b).(opt, path_in, opt.in_placecxx . . $ ot/options#in_place. ? path_incxx . . $ std/optional#optional(e3f0d6382bd52f24). : opt.path_outcxx . . $ ot/options#path_out.); |
| 639 | } catch (const otcxx . . $ ot/::statuscxx . . $ ot/status#& rc) { |
| 640 | global_rc =cxx . . $ ot/status#`operator=`(a0ede868b080ffee). stcxx . . $ ot/status#status(570d951ce3a7f975).::errorcxx . . $ ot/st#error.; |
| 641 | if (!rc.messagecxx . . $ ot/status#message..emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).()) |
| 642 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: error: %s\n", path_in.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), rc.messagecxx . . $ ot/status#message..c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 643 | } |
| 644 | } |
| 645 | if (global_rc != stcxx . . $ ot/st#::okcxx . . $ ot/st#ok.) |
| 646 | throw global_rc; |
| 647 | } |