2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
15 #include <vorbis/vorbisfile.h>
23 static int fill_comments(struct ogg_context
*ctx
, struct ogg_oggfile
*ogg
);
24 static void do_iconv(struct ogg_context
*ctx
, const char *in
, char *out
, size_t outlen
);
27 ogg_oggfile_create(struct ogg_context
*ctx
, struct ogg_oggfile
*ogg
, char *filename
)
31 assert(filename
!= NULL
);
33 strncpy(ogg
->filename
, filename
, sizeof(ogg
->filename
) - 1);
34 ogg
->filename
[sizeof(ogg
->filename
) - 1] = '\0';
36 if (fill_comments(ctx
, ogg
) != 0)
43 fill_comments(struct ogg_context
*ctx
, struct ogg_oggfile
*ogg
)
53 if (ov_fopen(ogg
->filename
, &ovf
) != 0) {
54 warnx("could not open file: %s", ogg
->filename
);
57 if ((ovc
= ov_comment(&ovf
, -1)) == NULL
) {
58 warnx("could not read comments for file: %s", ogg
->filename
);
61 for (i
= 0; i
< ovc
->comments
; i
++) {
62 value
= ovc
->user_comments
[i
];
63 key
= strsep(&value
, "=");
64 if (strcasecmp(key
, "artist") == 0)
65 do_iconv(ctx
, value
, ogg
->artist
, sizeof(ogg
->artist
));
66 else if (strcasecmp(key
, "album") == 0)
67 do_iconv(ctx
, value
, ogg
->album
, sizeof(ogg
->album
));
68 else if (strcasecmp(key
, "title") == 0)
69 do_iconv(ctx
, value
, ogg
->title
, sizeof(ogg
->title
));
70 if (ogg
->artist
== NULL
|| ogg
->album
== NULL
|| ogg
->title
== NULL
) {
71 warnx("insufficient comments for file: %s", ogg
->filename
);
76 if (ov_clear(&ovf
) != 0)
77 warnx("could not close file: %s", ogg
->filename
);
85 struct ogg_context
*ctx
;
88 if ((cd
= iconv_open("char", "UTF-8")) == (iconv_t
) (-1))
90 if ((ctx
= malloc(sizeof(*ctx
))) == NULL
)
99 ogg_context_close(struct ogg_context
*ctx
)
103 if (iconv_close(ctx
->cd
) == -1)
111 do_iconv(struct ogg_context
*ctx
, const char *in
, char *out
, size_t outlen
)
115 size_t inlen
= strlen(in
);
125 if (iconv(ctx
->cd
, NULL
, NULL
, &outp
, &outlen
) == (size_t) (-1))
126 errx(EX_SOFTWARE
, "could not set initial conversion state");
129 if (iconv(ctx
->cd
, &inp
, &inlen
, &outp
, &outlen
) == (size_t) (-1))
130 errx(EX_SOFTWARE
, "string conversion failed");