diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-07-11 11:45:17 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-07-11 11:45:17 +0300 |
commit | c3052454045484d22ba4d94722016162c222fb17 (patch) | |
tree | af70ba549577d8a508afff53e565c47d479b06a0 /src | |
parent | 3eca38aa7fa33362df0acc9c17493ad027809437 (diff) | |
download | idest-c3052454045484d22ba4d94722016162c222fb17.tar.gz idest-c3052454045484d22ba4d94722016162c222fb17.tar.bz2 |
Include comment classes (short descriptions) to the output.
* src/idest.h (ed_item): New members lang & class.
(field_to_string): New proto.
(ed_list_add_item): Change signature.
* src/idop.c (add_stringlist): Change signature. Optionally return the
total length of strings added to the list. All uses changed.
(field_to_string): New function.
(ed_item_set_comment_fields): New function.
* src/main.c (ed_list_add_item): Add comment classes.
Diffstat (limited to 'src')
-rw-r--r-- | src/idest.h | 5 | ||||
-rw-r--r-- | src/idop.c | 106 | ||||
-rw-r--r-- | src/main.c | 29 |
3 files changed, 127 insertions, 13 deletions
diff --git a/src/idest.h b/src/idest.h index 1270045..4fe1906 100644 --- a/src/idest.h +++ b/src/idest.h @@ -49,6 +49,8 @@ enum item_ids { struct ed_item { char *name; /* Printable name */ char id[5]; /* Item ID */ + char *lang; /* Comment language */ + char *class; /* Comment class */ union { gl_list_t vlist; /* List of strings, used with --query */ char *value; /* New value, used with --set */ @@ -80,6 +82,7 @@ char *idest_ucs4_cvt(id3_ucs4_t const *ucs4); void query_tags(const char *name); void info_id3(const char *name); +char *field_to_string(union id3_field *field, int isgenre); /* slist.c */ @@ -92,7 +95,7 @@ void print_string_list(FILE *fp, gl_list_t list); int frame_id(const char *arg); -void ed_list_add_item(const char *id, gl_list_t list); +void ed_list_add_item(struct id3_frame *frame, gl_list_t list); void ed_list_print(void); void ed_list_add_assignment(const char *name, const char *value); void ed_list_clear(void); @@ -54,7 +54,7 @@ set_frame_value(struct id3_frame *frame, const char *value) field = id3_frame_field(frame, 0); id3_field_settextencoding(field, encoding); /* FIXME: Field 1: Language */ - /* FIXME: Field 2: String */ + /* FIXME: Field 2: Short descr */ /* Field 3: Comment */ field = id3_frame_field(frame, 3); if (latin1_option) @@ -202,10 +202,12 @@ idest_ucs4_cvt(id3_ucs4_t const *ucs4) } static void -add_stringlist(gl_list_t list, struct id3_frame *frame, - union id3_field *field) +add_stringlist(gl_list_t list, union id3_field *field, int isgenre, + size_t *psize) { unsigned i, nstrings = id3_field_getnstrings(field); + size_t size = 0; + for (i = 0; i < nstrings; i++) { id3_ucs4_t const *ucs4; char *str; @@ -213,11 +215,84 @@ add_stringlist(gl_list_t list, struct id3_frame *frame, ucs4 = id3_field_getstrings(field, i); if (!ucs4) continue; - if (strcmp(frame->id, ID3_FRAME_GENRE) == 0) + if (isgenre) ucs4 = id3_genre_name(ucs4); str = idest_ucs4_cvt(ucs4); + size += strlen(str); gl_list_add_last(list, str); } + if (psize) + *psize = size; +} + +char * +field_to_string(union id3_field *field, int isgenre) +{ + id3_ucs4_t const *ucs4; + char *ret = NULL; + char buf[128]; + + switch (id3_field_type(field)) { + case ID3_FIELD_TYPE_TEXTENCODING: + case ID3_FIELD_TYPE_INT8: + case ID3_FIELD_TYPE_INT16: + case ID3_FIELD_TYPE_INT24: + case ID3_FIELD_TYPE_INT32: + case ID3_FIELD_TYPE_INT32PLUS: + snprintf(buf, sizeof(buf), "%ld", field->number.value); + ret = xstrdup(buf); + break; + + case ID3_FIELD_TYPE_LATIN1: + case ID3_FIELD_TYPE_LATIN1FULL: + /* FIXME */ + ret = xstrdup(id3_field_getlatin1(field)); + break; + + case ID3_FIELD_TYPE_LATIN1LIST: + /* FIXME */ + break; + + case ID3_FIELD_TYPE_STRING: + ucs4 = id3_field_getstring(field);; + if (ucs4) + ret = idest_ucs4_cvt(ucs4); + break; + + case ID3_FIELD_TYPE_STRINGFULL: + ucs4 = id3_field_getfullstring(field); + ret = idest_ucs4_cvt(ucs4); + break; + + case ID3_FIELD_TYPE_STRINGLIST: { + gl_list_t list; + size_t sz; + gl_list_iterator_t itr; + const void *p; + + list = new_string_list(true); + add_stringlist(list, field, isgenre, &sz); + ret = xmalloc(sz + 1); + ret[0] = 0; + itr = gl_list_iterator(list); + while (gl_list_iterator_next(&itr, &p, NULL)) + strcat(ret, p); + gl_list_iterator_free(&itr); + gl_list_free(list); + break; + } + + case ID3_FIELD_TYPE_LANGUAGE: + case ID3_FIELD_TYPE_FRAMEID: + case ID3_FIELD_TYPE_DATE: + ret = xstrdup(field->immediate.value); + break; + + case ID3_FIELD_TYPE_BINARYDATA: + /* FIXME */ + break; + } + return ret; } static void @@ -241,7 +316,9 @@ add_field(gl_list_t list, struct id3_frame *frame, union id3_field *field) gl_list_add_last(list, str); break; case ID3_FIELD_TYPE_STRINGLIST: - add_stringlist(list, frame, field); + add_stringlist(list, field, + strcmp(frame->id, ID3_FRAME_GENRE) == 0, + NULL); break; case ID3_FIELD_TYPE_LANGUAGE: case ID3_FIELD_TYPE_FRAMEID: @@ -255,6 +332,23 @@ add_field(gl_list_t list, struct id3_frame *frame, union id3_field *field) } } +void +ed_item_set_comment_fields(struct ed_item *itm, struct id3_frame *frame) +{ + union id3_field *field; + + if (strcmp(frame->id, ID3_FRAME_COMMENT) == 0) { + field = id3_frame_field(frame, 1); + if (!field) + return; + itm->lang = field_to_string(field, 0); + field = id3_frame_field(frame, 2); + if (!field) + return; + itm->class = field_to_string(field, 0); + } +} + static gl_list_t frame_to_list(struct id3_frame *frame) { @@ -277,7 +371,7 @@ show_tags(struct id3_tag *tag) for (i = 0; (frame = id3_tag_findframe(tag, NULL, i)); i++) { gl_list_t list = frame_to_list(frame); if (gl_list_size(list) > 0) - ed_list_add_item(frame->id, list); + ed_list_add_item(frame, list); else gl_list_free(list); } @@ -152,7 +152,7 @@ parse_ed_items(const char *arg) { ed_list_create(); while (*arg) { - size_t len = strcspn(arg, " \t,"); + size_t len = strcspn(arg, " \t,"); ed_list_new_item(arg, len); @@ -164,7 +164,7 @@ parse_ed_items(const char *arg) } void -ed_list_add_item(const char *id, gl_list_t list) +ed_list_add_item(struct id3_frame *frame, gl_list_t list) { if (all_frames) { const char *name; @@ -172,24 +172,37 @@ ed_list_add_item(const char *id, gl_list_t list) int i; ed_list_create(); - i = frame_id(id); + i = frame_id(frame->id); if (i >= 0) name = item_names[i]; else - name = id; - itm = ed_item_create(name, id); + name = frame->id; + itm = ed_item_create(name, frame->id); + ed_item_set_comment_fields(itm, frame); itm->v.vlist = list; gl_list_add_last(ed_list, itm); } else { gl_list_node_t node; struct ed_item item; - strncpy(item.id, id, sizeof(item.id)); + strncpy(item.id, frame->id, sizeof(item.id)); node = gl_list_search(ed_list, &item); if (node) { struct ed_item *itm = (struct ed_item *) gl_list_node_value(ed_list, node); if (itm->v.vlist) { + struct ed_item dummy; + memset(&dummy, 0, sizeof(dummy)); + ed_item_set_comment_fields(&dummy, frame); + if (dummy.class) { + char *str = + xmalloc(strlen(dummy.class) + + 5); + sprintf(str, "[%s]:", dummy.class); + gl_list_add_last(itm->v.vlist, str); + free(dummy.class); + free(dummy.lang); + } concat_string_list(itm->v.vlist, list); gl_list_free(list); } else @@ -220,6 +233,8 @@ ed_list_print() while (gl_list_iterator_next(&itr, &p, NULL)) { const struct ed_item *item = p; printf("%s:", item->name); + if (item->class) + printf(":%s:", item->class); if (item->v.vlist) print_string_list(stdout, item->v.vlist); putchar('\n'); @@ -237,6 +252,8 @@ ed_list_clear() itr = gl_list_iterator(ed_list); while (gl_list_iterator_next(&itr, &p, NULL)) { struct ed_item *item = (struct ed_item *)p; + free(item->lang); + free(item->class); if (item->v.vlist) { gl_list_free(item->v.vlist); item->v.vlist = NULL; |