From 8006a1de47d492a2fc15e314a3851fb40e6622bd Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Fri, 29 Jul 2011 14:33:27 +0300 Subject: Add more tests. * src/idest.h (source_vopt): New extern. (guess_file_tag_options): New proto. * src/idop.c (parse_ed_items): Use only commas as separators. (set_tag_options,guess_file_tag_options): New function. (find_matching_frame): Use the above functions. (del_tags): make sure tag version remains unchanged, unless requested so on the command line. * src/main.c (source_vopt): New variable. (main): Set source_vopt. * tests/Makefile.am: Add new testcases. * tests/testsuite.at: Add new testcases. * tests/copy-v1-00.at: New testcase. * tests/copy-v12-00.at: New testcase. * tests/copy-v2-00.at: New testcase. * tests/del-all-v1.at: New testcase. * tests/del-all-v12.at: New testcase. * tests/del-all-v2.at: New testcase. * tests/del-frame-v1.at: New testcase. * tests/del-frame-v2.at: New testcase. * tests/set-v1-01.at: New testcase. * tests/set-v2-01.at: New testcase. --- src/idop.c | 94 +++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 34 deletions(-) (limited to 'src/idop.c') diff --git a/src/idop.c b/src/idop.c index e549bcc..353bd46 100644 --- a/src/idop.c +++ b/src/idop.c @@ -42,14 +42,14 @@ parse_ed_items(gl_list_t *plist, const char *arg) list = *plist; while (*arg) { struct ed_item *itm; - size_t len = strcspn(arg, " \t,"); + size_t len = strcspn(arg, ","); itm = ed_item_from_frame_spec(arg, len); gl_list_add_last(list, itm); arg += len; if (!*arg) break; - arg += strspn(arg, " \t,"); + arg += strspn(arg, ","); } } @@ -368,6 +368,48 @@ build_tag_options(struct id3_tag *tag, unsigned long location, return 0; } +static void +set_tag_options(struct id3_tag *tag, int vopt) +{ + int opts = 0; + + if (vopt & IDEST_ID3V_1) + opts |= ID3_TAG_OPTION_ID3V1; + if (!(vopt & IDEST_ID3V_2)) + opts |= ID3_TAG_OPTION_NO_ID3V2; + if (opts) + id3_tag_options(tag, + ID3_TAG_OPTION_ID3V1|ID3_TAG_OPTION_NO_ID3V2, + opts); +} + +int +guess_file_tag_options(struct id3_file *file, int *modified) +{ + int vopt = version_option; + int m = 0; + + if (!modified) + modified = &m; + if (convert_version == 0) { + if (*modified && !vopt) { + if (id3_file_struct_ntags(file)) { + id3_file_struct_iterate(file, + build_tag_options, + &vopt); + } else + vopt = default_version_option; + } + } else if (id3_file_struct_ntags(file)) { + id3_file_struct_iterate(file, build_tag_options, &vopt); + if (vopt != convert_version) { + vopt = convert_version; + *modified |= 1; + } + } + return vopt; +} + struct id3_frame * find_matching_frame(struct id3_tag *tag, const struct ed_item *item, idest_frame_cmp_t cmp) @@ -452,11 +494,13 @@ set_tags(const char *name) struct id3_file *file; struct id3_tag *tag; int modified = 0; - int vopt = version_option; - + int vopt; + int has_tags; + file = id3_file_open(name, ID3_FILE_MODE_READWRITE); if (!file) error(1, errno, "cannot open file %s", name); + has_tags = id3_file_struct_ntags(file); tag = id3_file_tag(file); if (!tag) abort(); /* FIXME */ @@ -482,38 +526,14 @@ set_tags(const char *name) /* FIXME */ modified |= guile_transform(name, tag); - - if (convert_version == 0) { - if (modified && !vopt) { - if (id3_file_struct_ntags(file)) { - id3_file_struct_iterate(file, - build_tag_options, - &vopt); - } else - vopt = default_version_option; - } - } else if (id3_file_struct_ntags(file)) { - id3_file_struct_iterate(file, build_tag_options, &vopt); - if (vopt != convert_version) { - vopt = convert_version; - modified |= 1; - } - } - - if (modified && vopt) { - int opts = 0; - if (vopt & IDEST_ID3V_1) - opts |= ID3_TAG_OPTION_ID3V1; - if (!(vopt & IDEST_ID3V_2)) - opts |= ID3_TAG_OPTION_NO_ID3V2; - id3_tag_options(tag, - ID3_TAG_OPTION_ID3V1|ID3_TAG_OPTION_NO_ID3V2, - opts); - } - if (modified) + vopt = (source_tag && !has_tags) ? source_vopt : + guess_file_tag_options(file, &modified); + + if (modified) { + set_tag_options(tag, vopt); safe_id3_file_update_and_close(file); - else + } else id3_file_close(file); } @@ -532,6 +552,8 @@ del_tags(const char *name) if (filter_list) { int i; struct id3_frame *frame; + int modified = 0; + int vopt; for (i = 0; (frame = id3_tag_findframe(tag, NULL, i)); ) { const struct idest_frametab *ft = @@ -540,9 +562,13 @@ del_tags(const char *name) frame, ft ? ft->cmp : NULL)) { id3_tag_detachframe(tag, frame); id3_frame_delete(frame); + modified = 1; } else i++; } + vopt = guess_file_tag_options(file, &modified); + if (modified) + set_tag_options(tag, vopt); } else id3_tag_clearframes(tag); safe_id3_file_update_and_close(file); -- cgit v1.2.1