2 * Copyright (C) 2009,2010,2011 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #include <curl/curl.h>
34 #include "quvi/quvi.h"
36 #include "curl_wrap.h"
39 char *freprintf(char **dst
, const char *fmt
, ...)
46 vasprintf(dst
, fmt
, args
);
53 QUVIcode
to_utf8(_quvi_video_t video
)
55 static const char to
[] = "UTF-8";
56 size_t insize
, avail
, iconv_code
;
57 char inbuf
[1024], outbuf
[1024];
58 ICONV_CONST
char *inptr
;
63 assert(video
->quvi
!= 0);
64 assert(video
->title
!= 0);
65 assert(video
->charset
!= 0);
69 avail
= sizeof(outbuf
);
70 insize
= strlen(video
->title
);
72 if (insize
>= sizeof(inbuf
))
73 insize
= sizeof(inbuf
);
75 memset(wptr
, 0, sizeof(outbuf
));
77 snprintf(inbuf
, sizeof(inbuf
), "%s", video
->title
);
79 /* Try with TRANSLIT first. */
80 asprintf(&from
, "%s//TRANSLIT", video
->charset
);
81 cd
= iconv_open(to
, from
);
83 /* If that fails, then without TRANSLIT. */
84 if (cd
== (iconv_t
) - 1)
87 asprintf(&from
, "%s", video
->charset
);
88 cd
= iconv_open(to
, from
);
91 if (cd
== (iconv_t
) - 1)
95 freprintf(&video
->quvi
->errmsg
,
96 "conversion from %s to %s unavailable", from
, to
);
101 freprintf(&video
->quvi
->errmsg
, "iconv_open: %s",
104 perror("iconv_open");
113 iconv_code
= iconv(cd
, &inptr
, &insize
, &wptr
, &avail
);
117 if (iconv_code
== (size_t) - 1)
119 freprintf(&video
->quvi
->errmsg
,
120 "converting characters from '%s' to '%s' failed", from
,
127 freprintf(&video
->title
, "%s", outbuf
);
136 char *unescape(_quvi_t quvi
, char *s
)
141 assert(quvi
->curl
!= 0);
143 tmp
= curl_easy_unescape(quvi
->curl
, s
, 0, NULL
);
153 char *from_html_entities(char *src
)
161 static const struct lookup_s conv
[] =
178 for (i
= 0; conv
[i
].from
; ++i
)
179 src
= strepl(src
, conv
[i
].from
, conv
[i
].to
);
184 static int new_video_link(_quvi_video_link_t
* dst
)
186 struct _quvi_video_link_s
*qvl
;
188 qvl
= calloc(1, sizeof(*qvl
));
197 int add_video_link(llst_node_t
* lst
, const char *fmt
, ...)
199 _quvi_video_link_t qvl
;
203 rc
= new_video_link(&qvl
);
208 vasprintf((char **)&qvl
->url
, fmt
, args
);
217 return (llst_add(lst
, qvl
));
220 char *dirname_from(const char *s
)
223 char *p
= strdup(dirname(t
));
228 /* vim: set ts=2 sw=2 tw=72 expandtab: */