Manual: Add newline note
[quvi.git] / lib / util.c
blob2b596a4a21a3fbeb59fc9bbae9f92ae544fd539c
1 /* quvi
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
17 * 02110-1301 USA
20 #include "config.h"
22 #include <string.h>
23 #include <errno.h>
24 #include <stdarg.h>
25 #include <libgen.h>
26 #include <assert.h>
28 #ifdef HAVE_ICONV
29 #include <iconv.h>
30 #endif
32 #include "quvi/quvi.h"
33 #include "quvi/net.h"
34 #include "quvi/llst.h"
36 #include "internal.h"
37 #include "curl_wrap.h"
38 #include "util.h"
40 char *vafreprintf(char **dst, const char *fmt, va_list args)
42 _free(*dst);
43 assert(*dst == NULL);
45 vasprintf(dst, fmt, args);
46 va_end(args);
48 return (*dst);
51 char *freprintf(char **dst, const char *fmt, ...)
53 va_list args;
55 va_start(args, fmt);
56 vafreprintf(dst, fmt, args);
58 return (*dst);
61 #ifdef HAVE_ICONV
62 QUVIcode to_utf8(_quvi_media_t media)
64 static const char to[] = "UTF-8";
65 size_t insize, avail, iconv_code;
66 char inbuf[1024], outbuf[1024];
67 ICONV_CONST char *inptr;
68 char *from, *wptr;
69 iconv_t cd;
71 assert(media != 0);
72 assert(media->quvi != 0);
73 assert(media->title != 0);
74 assert(media->charset != 0);
76 wptr = outbuf;
77 inptr = inbuf;
78 avail = sizeof(outbuf);
79 insize = strlen(media->title);
81 if (insize >= sizeof(inbuf))
82 insize = sizeof(inbuf);
84 memset(wptr, 0, sizeof(outbuf));
86 snprintf(inbuf, sizeof(inbuf), "%s", media->title);
88 /* Try with TRANSLIT first. */
89 asprintf(&from, "%s//TRANSLIT", media->charset);
90 cd = iconv_open(to, from);
92 /* If that fails, then without TRANSLIT. */
93 if (cd == (iconv_t) - 1)
95 _free(from);
96 asprintf(&from, "%s", media->charset);
97 cd = iconv_open(to, from);
100 if (cd == (iconv_t) - 1)
102 if (errno == EINVAL)
104 freprintf(&media->quvi->errmsg,
105 "conversion from %s to %s unavailable", from, to);
107 else
109 #ifdef HAVE_STRERROR
110 freprintf(&media->quvi->errmsg, "iconv_open: %s",
111 strerror(errno));
112 #else
113 perror("iconv_open");
114 #endif
117 _free(from);
119 return (QUVI_ICONV);
122 iconv_code = iconv(cd, &inptr, &insize, &wptr, &avail);
123 iconv_close(cd);
124 cd = 0;
126 if (iconv_code == (size_t) - 1)
128 freprintf(&media->quvi->errmsg,
129 "converting characters from '%s' to '%s' failed", from,
130 to);
131 _free(from);
132 return (QUVI_ICONV);
134 else
136 freprintf(&media->title, "%s", outbuf);
139 _free(from);
141 return (QUVI_OK);
143 #endif /* HAVE_ICONV */
145 char *unescape(_quvi_t quvi, char *s)
147 return curl_unescape_url(quvi, s);
150 char *from_html_entities(char *src)
152 struct lookup_s
154 const char *from;
155 const char *to;
158 static const struct lookup_s conv[] =
160 {"&quot;", "\""},
161 {"&#34;", "\""},
162 {"&#034;", "\""},
163 {"&amp;", "&"},
164 {"&#38;", "&"},
165 {"&#038;", "&"},
166 {"&apos;", "'"},
167 {"&#39;", "'"},
168 {"&#039;", "'"},
169 {"&lt;", "<"},
170 {"&#60;", "<"},
171 {"&#060;", "<"},
172 {"&gt;", ">"},
173 {"&#62;", ">"},
174 {"&#062;", ">"},
175 {0, 0}
178 int i;
180 for (i = 0; conv[i].from; ++i)
181 src = strepl(src, conv[i].from, conv[i].to);
183 return (src);
186 static int new_media_url(_quvi_media_url_t * dst)
188 struct _quvi_media_url_s *qvl;
190 qvl = calloc(1, sizeof(*qvl));
191 if (!qvl)
192 return (QUVI_MEM);
194 *dst = qvl;
196 return (QUVI_OK);
199 int add_media_url(_quvi_llst_node_t * lst, const char *fmt, ...)
201 _quvi_media_url_t qvl;
202 va_list args;
203 int rc;
205 rc = new_media_url(&qvl);
206 if (rc != QUVI_OK)
207 return (rc);
209 va_start(args, fmt);
210 vasprintf((char **)&qvl->url, fmt, args);
211 va_end(args);
213 if (!qvl->url)
215 _free(qvl);
216 return (QUVI_MEM);
219 return (quvi_llst_append((quvi_llst_node_t*)lst, qvl));
222 char *dirname_from(const char *s)
224 char *t = strdup(s);
225 char *p = strdup(dirname(t));
226 _free(t);
227 return (p);
230 /* vim: set ts=2 sw=2 tw=72 expandtab: */