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
32 #include "quvi/quvi.h"
34 #include "quvi/llst.h"
37 #include "curl_wrap.h"
40 char *vafreprintf(char **dst
, const char *fmt
, va_list args
)
45 vasprintf(dst
, fmt
, args
);
51 char *freprintf(char **dst
, const char *fmt
, ...)
56 vafreprintf(dst
, fmt
, args
);
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
;
72 assert(media
->quvi
!= 0);
73 assert(media
->title
!= 0);
74 assert(media
->charset
!= 0);
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)
96 asprintf(&from
, "%s", media
->charset
);
97 cd
= iconv_open(to
, from
);
100 if (cd
== (iconv_t
) - 1)
104 freprintf(&media
->quvi
->errmsg
,
105 "conversion from %s to %s unavailable", from
, to
);
110 freprintf(&media
->quvi
->errmsg
, "iconv_open: %s",
113 perror("iconv_open");
122 iconv_code
= iconv(cd
, &inptr
, &insize
, &wptr
, &avail
);
126 if (iconv_code
== (size_t) - 1)
128 freprintf(&media
->quvi
->errmsg
,
129 "converting characters from '%s' to '%s' failed", from
,
136 freprintf(&media
->title
, "%s", outbuf
);
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
)
158 static const struct lookup_s conv
[] =
180 for (i
= 0; conv
[i
].from
; ++i
)
181 src
= strepl(src
, conv
[i
].from
, conv
[i
].to
);
186 static int new_media_url(_quvi_media_url_t
* dst
)
188 struct _quvi_media_url_s
*qvl
;
190 qvl
= calloc(1, sizeof(*qvl
));
199 int add_media_url(_quvi_llst_node_t
* lst
, const char *fmt
, ...)
201 _quvi_media_url_t qvl
;
205 rc
= new_media_url(&qvl
);
210 vasprintf((char **)&qvl
->url
, fmt
, args
);
219 return (quvi_llst_append((quvi_llst_node_t
*)lst
, qvl
));
222 char *dirname_from(const char *s
)
225 char *p
= strdup(dirname(t
));
230 /* vim: set ts=2 sw=2 tw=72 expandtab: */