ref: 92b806c9c3f5b3a201c373299835953605a2a2a3
dir: /examples/readtags.c/
#include <fcntl.h> #include <stdio.h> #include <sys/types.h> #include <unistd.h> #define USED(x) (void)x #include "tags.h" typedef struct Aux Aux; struct Aux { int fd; }; static const char *t2s[] = { [Talbum] = "album", [Talbumgain] = "albumgain", [Talbumpeak] = "albumpeak", [Tartist] = "artist", [Tcomment] = "comment", [Tcomposer] = "composer", [Tdate] = "date", [Tgenre] = "genre", [Timage] = "image", [Ttitle] = "title", [Ttrack] = "track", [Ttrackgain] = "trackgain", [Ttrackpeak] = "trackpeak", }; static void tag(Tagctx *ctx, int t, const char *k, const char *v, int offset, int size, Tagread f) { USED(ctx); USED(k); USED(f); if(t == Timage) printf("%-12s %s %d %d\n", t2s[t], v, offset, size); else if(t == Tunknown) printf("%-12s %s\n", k, v); else printf("%-12s %s\n", t2s[t], v); } static void toc(Tagctx *ctx, int ms, int offset) { USED(ctx); USED(ms); USED(offset); } static int ctxread(Tagctx *ctx, void *buf, int cnt) { Aux *aux = ctx->aux; return read(aux->fd, buf, cnt); } static int ctxseek(Tagctx *ctx, int offset, int whence) { Aux *aux = ctx->aux; return lseek(aux->fd, offset, whence); } int main(int argc, char **argv) { int i; char buf[256]; Aux aux; Tagctx ctx = { .read = ctxread, .seek = ctxseek, .tag = tag, .toc = toc, .buf = buf, .bufsz = sizeof(buf), .aux = &aux, }; if(argc < 2){ printf("usage: readtags FILE...\n"); return -1; } for(i = 1; i < argc; i++){ printf("*** %s\n", argv[i]); if((aux.fd = open(argv[i], O_RDONLY)) < 0) perror("failed to open"); else{ if(tagsget(&ctx) != 0) printf("no tags or failed to read tags\n"); else{ if(ctx.duration > 0) printf("%-12s %d ms\n", "duration", ctx.duration); if(ctx.samplerate > 0) printf("%-12s %d\n", "samplerate", ctx.samplerate); if(ctx.channels > 0) printf("%-12s %d\n", "channels", ctx.channels); if(ctx.bitrate > 0) printf("%-12s %d\n", "bitrate", ctx.bitrate); } close(aux.fd); } printf("\n"); } return 0; }