tests/verify.c: chk_val: Test for >0
[libquvi.git] / examples / supports.c
blobd79e042bf856713d88ed2bade69809acf70e7d1e
1 /* libquvi
2 * Copyright (C) 2012 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 <locale.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <quvi.h>
28 #include "examples.h"
30 struct _opts_s
32 gboolean autoproxy;
33 gboolean verbose;
34 gboolean online;
35 gchar *type;
36 gchar **url;
39 static struct _opts_s opts;
41 static const GOptionEntry entries[] =
44 "type", 't', 0, G_OPTION_ARG_STRING, &opts.type,
45 "Script type", "TYPE"
48 "online", 's', 0, G_OPTION_ARG_NONE, &opts.online,
49 "Check online", NULL
52 "autoproxy", 'a', 0, G_OPTION_ARG_NONE, &opts.autoproxy,
53 "Enable the autoproxy feature", NULL
56 "verbose", 'v', 0, G_OPTION_ARG_NONE, &opts.verbose,
57 "Verbose libcurl output", NULL
60 G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts.url, "URL"
62 {NULL, 0, 0, 0, NULL, NULL, NULL}
65 struct _type_lookup_s
67 QuviSupportsType to;
68 const gchar *from;
71 static const struct _type_lookup_s type_conv[] =
73 {QUVI_SUPPORTS_TYPE_PLAYLIST, "playlist"},
74 {QUVI_SUPPORTS_TYPE_MEDIA, "media"},
75 {QUVI_SUPPORTS_TYPE_ANY, "any"},
76 {0, NULL}
79 static gchar **type_sv()
81 gchar **r;
82 gint i,j;
84 i=0;
85 while (type_conv[i].from != NULL) ++i;
86 r = g_new(gchar*, i+1);
88 i=j=0;
89 while (type_conv[j].from != NULL)
90 r[i++] = g_strdup(type_conv[j++].from);
91 r[i] = NULL;
93 return (r);
96 static gboolean chk_type_values()
98 gchar **v, *s;
99 gboolean r;
101 v = type_sv();
102 r = examples_chk_val_s(opts.type, v, &s);
103 if (r == FALSE)
105 g_printerr(
106 "error: invalid value (`%s') for the option `--type'\n", s);
109 g_strfreev(v);
110 v = NULL;
112 return (r);
115 static QuviSupportsType type_n()
117 gint i;
118 for (i=0; type_conv[i].from != NULL; ++i)
120 if (g_strcmp0(opts.type, type_conv[i].from) == 0)
121 return (type_conv[i].to);
123 return (QUVI_SCRIPT_TYPE_MEDIA);
126 static gint opts_new(gint argc, gchar **argv)
128 GOptionContext *c;
129 GOptionGroup *g;
130 GError *e;
131 gint r;
133 c = g_option_context_new("URL");
134 r = EXIT_SUCCESS;
135 g = NULL;
136 e = NULL;
138 g_option_context_set_help_enabled(c, TRUE);
139 g_option_context_add_main_entries(c, entries, NULL);
141 if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
143 g_printerr("error: %s\n", e->message);
144 g_error_free(e);
145 r = EXIT_FAILURE;
146 e = NULL;
149 g_option_context_free(c);
150 c = NULL;
152 /* Set default values. */
154 if (opts.type == NULL)
155 opts.type = g_strdup("any");
157 /* Check input. */
159 if (chk_type_values() == FALSE)
160 return (EXIT_FAILURE);
162 if (opts.url == NULL)
164 g_printerr("error: no input URL\n");
165 return (EXIT_FAILURE);
168 return (r);
171 static void opts_free()
173 g_strfreev(opts.url);
174 opts.url = NULL;
176 g_free(opts.type);
177 opts.type = NULL;
180 static QuviSupportsMode mode = QUVI_SUPPORTS_MODE_OFFLINE;
181 static QuviSupportsType type = QUVI_SUPPORTS_TYPE_ANY;
183 static void chk_support(const gchar *url)
185 const QuviBoolean r = quvi_supports(q, url, mode, type);
187 /* Always check for any network errors with QUVI_SUPPORTS_MODE_ONLINE. */
188 if (r == FALSE && mode == QUVI_SUPPORTS_MODE_ONLINE)
190 glong ec = 0;
191 quvi_get(q, QUVI_INFO_ERROR_CODE, &ec);
193 if (ec != QUVI_ERROR_NO_SUPPORT)
195 g_printerr("\nerror: %s\n", quvi_errmsg(q));
196 return;
199 g_print("%s: %s\n", url, (r == QUVI_TRUE) ? "yes":"no");
202 typedef quvi_callback_status qcs;
204 gint main(gint argc, gchar **argv)
206 gint i,r;
208 g_assert(q == NULL);
210 memset(&opts, 0, sizeof(struct _opts_s));
211 setlocale(LC_ALL, "");
213 r = opts_new(argc, argv);
214 if (r != EXIT_SUCCESS)
215 return (r);
217 q = quvi_new();
218 examples_exit_if_error();
220 if (opts.autoproxy == TRUE)
221 examples_enable_autoproxy();
223 if (opts.verbose == TRUE)
224 examples_enable_verbose();
226 if (opts.online == TRUE)
227 mode = QUVI_SUPPORTS_MODE_ONLINE;
229 quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) examples_status);
230 type = type_n();
232 g_printerr("[%s] type=%s (0x%x), mode=0x%x\n",
233 __func__, opts.type, type, mode);
235 for (i=0; opts.url[i] != NULL; ++i)
236 chk_support(opts.url[i]);
238 opts_free();
239 examples_cleanup();
241 g_assert(q == NULL);
242 return (r);
245 /* vim: set ts=2 sw=2 tw=72 expandtab: */