_glob_scripts: LIBQUVI_SCRIPTS_DIR: Ignore _glob_scripts_dir return value
[libquvi.git] / examples / script.c
blob2fbbf1f24b93b43a58426ba0a7f5b2f7554c2c7c
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (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, 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 static void usage()
32 g_print("Usage: script [options]\n"
33 "Type\n"
34 " -p .. Dump playlist scripts\n"
35 " -m .. Dump media scripts [default]\n"
36 " -s .. Dump scan scripts\n"
37 "Printing\n"
38 " -t<arg> =pd .. Property mask, possible values: 'd','p','n','s'\n"
40 exit(0);
43 static QuviScriptType type = QUVI_SCRIPT_TYPE_MEDIA;
44 static GString *mask = NULL;
46 static void parse_mask(gchar *arg)
48 gint i=2;
50 if (strlen(arg) < 3)
51 return;
53 mask = g_string_new(NULL);
55 for (; arg[i]; ++i)
57 switch (arg[i])
59 case 'd':
60 case 'p':
61 case 'n':
62 case 's':
63 g_string_append_c(mask, arg[i]);
64 break;
65 default:
66 g_printerr("[%s] Unknown property mask character `%c'\n",
67 __func__, arg[i]);
72 static void parse_args(gint argc, gchar **argv)
74 gint i=1;
75 for (; i<argc; ++i)
77 if (strlen(argv[i]) <2 || argv[i][0] != '-')
78 continue;
80 switch (argv[i][1])
82 /* Type */
83 case 'p':
84 type = QUVI_SCRIPT_TYPE_PLAYLIST;
85 break;
86 case 'm':
87 type = QUVI_SCRIPT_TYPE_MEDIA;
88 break;
89 case 's':
90 type = QUVI_SCRIPT_TYPE_SCAN;
91 break;
92 /* Property mask */
93 case 't':
94 parse_mask(argv[i]);
95 break;
96 default:
97 usage();
102 static void dump_script(quvi_t q)
104 GString *o = g_string_new("Properties:\n");
105 gchar *prop = mask->str;
106 gint i=0;
108 for (; prop[i]; ++i)
110 QuviScriptProperty qsp;
111 switch (prop[i])
113 case 'd':
114 qsp = QUVI_SCRIPT_PROPERTY_DOMAINS;
115 g_string_append(o, " d=");
116 break;
117 case 'p':
118 qsp = QUVI_SCRIPT_PROPERTY_FILEPATH;
119 g_string_append(o, " p=");
120 break;
121 case 'n':
122 qsp = QUVI_SCRIPT_PROPERTY_FILENAME;
123 g_string_append(o, " n=");
124 break;
125 case 's':
126 qsp = QUVI_SCRIPT_PROPERTY_SHA1;
127 g_string_append(o, " s=");
128 break;
129 default:
130 g_warning("Invalid property mask character `%c'\n", prop[i]);
131 continue;
134 gchar *s = NULL;
135 quvi_script_get(q, type, qsp, &s);
136 g_string_append_printf(o, "%s\n", s);
139 g_print("%s", o->str);
140 g_string_free(o, TRUE);
143 gint main(gint argc, gchar **argv)
145 setlocale(LC_ALL, "");
146 parse_args(argc, argv);
148 if (mask == NULL)
149 mask = g_string_new("pd"); /* Default. */
151 g_printerr("[%s] mask=%s, type=%d\n", __func__, mask->str, type);
153 g_assert(q == NULL);
154 q = quvi_new();
155 exit_if_error();
157 while (quvi_script_next(q, type) == QUVI_TRUE)
158 dump_script(q);
160 g_string_free(mask, TRUE);
161 cleanup();
163 g_assert(q == NULL);
165 return (0);
168 /* vim: set ts=2 sw=2 tw=72 expandtab: */