HowtoContributeCode: Cleanup
[quvi.git] / lib / util.c
blob3085aa64915741709003e7e32f532d817e941b95
1 /*
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/>.
18 #include "config.h"
20 #include <string.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <assert.h>
25 #include <curl/curl.h>
27 #ifdef HAVE_ICONV
28 #include <iconv.h>
29 #endif
31 #include "quvi/quvi.h"
32 #include "internal.h"
33 #include "curl_wrap.h"
34 #include "util.h"
36 #ifdef HAVE_ICONV
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;
43 char *from, *wptr;
44 iconv_t cd;
45 _quvi_t quvi;
47 assert(video != 0);
48 assert(video->quvi != 0);
49 assert(video->title != 0);
50 assert(video->charset != 0);
52 quvi = video->quvi;
53 wptr = outbuf;
54 inptr = inbuf;
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) {
71 _free(from);
72 asprintf(&from, "%s", video->charset);
73 cd = iconv_open(to, from);
76 if (cd == (iconv_t) - 1) {
77 if (errno == EINVAL)
78 seterr("conversion from %s to %s unavailable", from, to);
79 else {
80 #ifdef HAVE_STRERROR
81 seterr("iconv_open: %s", strerror(errno));
82 #else
83 perror("iconv_open");
84 #endif
87 _free(from);
89 return (QUVI_ICONV);
92 iconv_code = iconv(cd, &inptr, &insize, &wptr, &avail);
93 iconv_close(cd);
94 cd = 0;
96 if (iconv_code == (size_t) - 1) {
97 seterr("converting characters from '%s' to '%s' failed", from, to);
98 _free(from);
99 return (QUVI_ICONV);
100 } else
101 setvid(video->title, "%s", outbuf);
103 _free(from);
105 return (QUVI_OK);
107 #endif
109 char *unescape(_quvi_t quvi, char *s)
111 char *tmp, *ret;
113 assert(quvi != 0);
114 assert(quvi->curl != 0);
116 tmp = curl_easy_unescape(quvi->curl, s, 0, NULL);
117 assert(tmp != 0);
118 ret = strdup(tmp);
119 curl_free(tmp);
121 free(s);
123 return (ret);
126 char *from_html_entities(char *src)
128 struct lookup_s {
129 const char *from;
130 const char *to;
133 static const struct lookup_s conv[] = {
134 {"&quot;", "\""},
135 {"&#34;", "\""},
136 {"&amp;", "&"},
137 {"&#38;", "&"},
138 {"&apos;", "'"},
139 {"&#39;", "'"},
140 {"&lt;", "<"},
141 {"&#60;", "<"},
142 {"&gt;", ">"},
143 {"&#62;", ">"},
144 {0, 0}
147 int i;
149 for (i = 0; conv[i].from; ++i)
150 src = strepl(src, conv[i].from, conv[i].to);
152 return (src);
155 static int new_video_link(_quvi_video_link_t * dst)
157 struct _quvi_video_link_s *qvl;
159 qvl = calloc(1, sizeof(*qvl));
160 if (!qvl)
161 return (QUVI_MEM);
163 *dst = qvl;
165 return (QUVI_OK);
168 int add_video_link(llst_node_t * lst, const char *fmt, ...)
170 _quvi_video_link_t qvl;
171 va_list args;
172 int rc;
174 rc = new_video_link(&qvl);
175 if (rc != QUVI_OK)
176 return (rc);
178 va_start(args, fmt);
179 vasprintf((char **)&qvl->url, fmt, args);
180 va_end(args);
182 if (!qvl->url) {
183 _free(qvl);
184 return (QUVI_MEM);
187 return (llst_add(lst, qvl));