2 * Copyright (C) 2009,2010 Toni Gundogdu.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <curl/curl.h>
31 #include "quvi/quvi.h"
33 #include "curl_wrap.h"
37 QUVIcode
to_utf8(_quvi_video_t video
)
39 static const char to
[] = "UTF-8";
40 size_t insize
, avail
, iconv_code
;
41 char inbuf
[1024], outbuf
[1024];
42 ICONV_CONST
char *inptr
;
48 assert(video
->quvi
!= 0);
49 assert(video
->title
!= 0);
50 assert(video
->charset
!= 0);
55 avail
= sizeof(outbuf
);
56 insize
= strlen(video
->title
);
58 if (insize
>= sizeof(inbuf
))
59 insize
= sizeof(inbuf
);
61 memset(wptr
, 0, sizeof(outbuf
));
63 snprintf(inbuf
, sizeof(inbuf
), "%s", video
->title
);
65 /* Try with TRANSLIT first. */
66 asprintf(&from
, "%s//TRANSLIT", video
->charset
);
67 cd
= iconv_open(to
, from
);
69 /* If that fails, then without TRANSLIT. */
70 if (cd
== (iconv_t
) - 1) {
72 asprintf(&from
, "%s", video
->charset
);
73 cd
= iconv_open(to
, from
);
76 if (cd
== (iconv_t
) - 1) {
78 seterr("conversion from %s to %s unavailable", from
, to
);
81 seterr("iconv_open: %s", strerror(errno
));
92 iconv_code
= iconv(cd
, &inptr
, &insize
, &wptr
, &avail
);
96 if (iconv_code
== (size_t) - 1) {
97 seterr("converting characters from '%s' to '%s' failed", from
, to
);
101 setvid(video
->title
, "%s", outbuf
);
109 char *unescape(_quvi_t quvi
, char *s
)
114 assert(quvi
->curl
!= 0);
116 tmp
= curl_easy_unescape(quvi
->curl
, s
, 0, NULL
);
126 char *from_html_entities(char *src
)
133 static const struct lookup_s conv
[] = {
149 for (i
= 0; conv
[i
].from
; ++i
)
150 src
= strepl(src
, conv
[i
].from
, conv
[i
].to
);
155 static int new_video_link(_quvi_video_link_t
* dst
)
157 struct _quvi_video_link_s
*qvl
;
159 qvl
= calloc(1, sizeof(*qvl
));
168 int add_video_link(llst_node_t
* lst
, const char *fmt
, ...)
170 _quvi_video_link_t qvl
;
174 rc
= new_video_link(&qvl
);
179 vasprintf((char **)&qvl
->url
, fmt
, args
);
187 return (llst_add(lst
, qvl
));