~ubuntu-branches/ubuntu/saucy/libquvi/saucy-proposed

« back to all changes in this revision

Viewing changes to lib/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Alejandro Garrido Mota
  • Date: 2011-04-25 01:07:41 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110425010741-nalzd12y180qs082
Tags: 0.2.15-1
* New upstream release (Closes: #622253). 
* Update d/watch to sf.net
* Update homepage field to quvi.sourceforge.net (Closes: #615554) 
* d/rules: Add dh_auto_test before NO_INTERNET=1 in override_dh_auto_test 
* Update Standards-Version field to 3.9.2 
* Add symbol (d/libquvi0.symbols) file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* quvi
2
 
 * Copyright (C) 2009,2010  Toni Gundogdu <legatvs@gmail.com>
 
2
 * Copyright (C) 2009,2010,2011  Toni Gundogdu <legatvs@gmail.com>
3
3
 *
4
4
 * This library is free software; you can redistribute it and/or
5
5
 * modify it under the terms of the GNU Lesser General Public
22
22
#include <string.h>
23
23
#include <errno.h>
24
24
#include <stdarg.h>
 
25
#include <libgen.h>
25
26
#include <assert.h>
26
27
 
27
28
#include <curl/curl.h>
35
36
#include "curl_wrap.h"
36
37
#include "util.h"
37
38
 
 
39
char *freprintf(char **dst, const char *fmt, ...)
 
40
{
 
41
  va_list args;
 
42
 
 
43
  _free(*dst);
 
44
 
 
45
  va_start(args, fmt);
 
46
  vasprintf(dst, fmt, args);
 
47
  va_end(args);
 
48
 
 
49
  return (*dst);
 
50
}
 
51
 
38
52
#ifdef HAVE_ICONV
39
 
QUVIcode to_utf8(_quvi_video_t video)
 
53
QUVIcode to_utf8(_quvi_media_t video)
40
54
{
41
55
  static const char to[] = "UTF-8";
42
56
  size_t insize, avail, iconv_code;
44
58
  ICONV_CONST char *inptr;
45
59
  char *from, *wptr;
46
60
  iconv_t cd;
47
 
  _quvi_t quvi;
48
61
 
49
62
  assert(video != 0);
50
63
  assert(video->quvi != 0);
51
64
  assert(video->title != 0);
52
65
  assert(video->charset != 0);
53
66
 
54
 
  quvi = video->quvi;
55
67
  wptr = outbuf;
56
68
  inptr = inbuf;
57
69
  avail = sizeof(outbuf);
69
81
  cd = iconv_open(to, from);
70
82
 
71
83
  /* If that fails, then without TRANSLIT. */
72
 
  if (cd == (iconv_t) - 1) {
73
 
    _free(from);
74
 
    asprintf(&from, "%s", video->charset);
75
 
    cd = iconv_open(to, from);
76
 
  }
 
84
  if (cd == (iconv_t) - 1)
 
85
    {
 
86
      _free(from);
 
87
      asprintf(&from, "%s", video->charset);
 
88
      cd = iconv_open(to, from);
 
89
    }
77
90
 
78
 
  if (cd == (iconv_t) - 1) {
79
 
    if (errno == EINVAL)
80
 
      seterr("conversion from %s to %s unavailable", from, to);
81
 
    else {
 
91
  if (cd == (iconv_t) - 1)
 
92
    {
 
93
      if (errno == EINVAL)
 
94
        {
 
95
          freprintf(&video->quvi->errmsg,
 
96
                    "conversion from %s to %s unavailable", from, to);
 
97
        }
 
98
      else
 
99
        {
82
100
#ifdef HAVE_STRERROR
83
 
      seterr("iconv_open: %s", strerror(errno));
 
101
          freprintf(&video->quvi->errmsg, "iconv_open: %s",
 
102
                    strerror(errno));
84
103
#else
85
 
      perror("iconv_open");
 
104
          perror("iconv_open");
86
105
#endif
 
106
        }
 
107
 
 
108
      _free(from);
 
109
 
 
110
      return (QUVI_ICONV);
87
111
    }
88
112
 
89
 
    _free(from);
90
 
 
91
 
    return (QUVI_ICONV);
92
 
  }
93
 
 
94
113
  iconv_code = iconv(cd, &inptr, &insize, &wptr, &avail);
95
114
  iconv_close(cd);
96
115
  cd = 0;
97
116
 
98
 
  if (iconv_code == (size_t) - 1) {
99
 
    seterr("converting characters from '%s' to '%s' failed", from, to);
100
 
    _free(from);
101
 
    return (QUVI_ICONV);
102
 
  } else
103
 
    setvid(video->title, "%s", outbuf);
 
117
  if (iconv_code == (size_t) - 1)
 
118
    {
 
119
      freprintf(&video->quvi->errmsg,
 
120
                "converting characters from '%s' to '%s' failed", from,
 
121
                to);
 
122
      _free(from);
 
123
      return (QUVI_ICONV);
 
124
    }
 
125
  else
 
126
    {
 
127
      freprintf(&video->title, "%s", outbuf);
 
128
    }
104
129
 
105
130
  _free(from);
106
131
 
127
152
 
128
153
char *from_html_entities(char *src)
129
154
{
130
 
  struct lookup_s {
 
155
  struct lookup_s
 
156
  {
131
157
    const char *from;
132
158
    const char *to;
133
159
  };
134
160
 
135
 
  static const struct lookup_s conv[] = {
 
161
  static const struct lookup_s conv[] =
 
162
  {
136
163
    {"&quot;", "\""},
137
164
    {"&#34;", "\""},
 
165
    {"&#034;", "\""},
138
166
    {"&amp;", "&"},
139
167
    {"&#38;", "&"},
 
168
    {"&#038;", "&"},
140
169
    {"&apos;", "'"},
141
170
    {"&#39;", "'"},
 
171
    {"&#039;", "'"},
142
172
    {"&lt;", "<"},
143
173
    {"&#60;", "<"},
 
174
    {"&#060;", "<"},
144
175
    {"&gt;", ">"},
145
176
    {"&#62;", ">"},
 
177
    {"&#062;", ">"},
146
178
    {0, 0}
147
179
  };
148
180
 
181
213
  vasprintf((char **)&qvl->url, fmt, args);
182
214
  va_end(args);
183
215
 
184
 
  if (!qvl->url) {
185
 
    _free(qvl);
186
 
    return (QUVI_MEM);
187
 
  }
 
216
  if (!qvl->url)
 
217
    {
 
218
      _free(qvl);
 
219
      return (QUVI_MEM);
 
220
    }
188
221
 
189
222
  return (llst_add(lst, qvl));
190
223
}
 
224
 
 
225
char *dirname_from(const char *s)
 
226
{
 
227
  char *t = strdup(s);
 
228
  char *p = strdup(dirname(t));
 
229
  _free(t);
 
230
  return (p);
 
231
}
 
232
 
 
233
/* vim: set ts=2 sw=2 tw=72 expandtab: */