source: roaraudio/plugins/universal/filter-slfi-file2filter.c @ 5990:d903e4b823cd

Last change on this file since 5990:d903e4b823cd was 5990:d903e4b823cd, checked in by phi, 10 years ago

added SLFI filter "file2filter"

File size: 5.8 KB
Line 
1//filter-slfi-file2filter.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2012-2014
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include <roaraudio.h>
27#include <libroarlight/libroarlight.h>
28
29#define MAX_STACKSIZE 16
30
31static struct roar_slfi_inst * __parse_filter(char * p) {
32 struct roar_slfi_inst * ret;
33 struct roar_keyval * para = NULL;
34 ssize_t paralen = -1;
35 char * args;
36 int err;
37
38 args = strstr(p, " ");
39 if ( args != NULL ) {
40  *args = 0;
41  args++;
42  // strip spaces:
43  for (; *p == ' '; p++);
44
45  paralen = roar_keyval_split(&para, args, NULL, NULL, 1);
46 }
47
48 ret = roar_slfi_new(p, 1, para, paralen);
49 err = roar_error;
50
51 if ( para != NULL )
52  roar_mm_free(para);
53
54 roar_err_set(err);
55 return ret;
56}
57
58static struct roar_slfi_inst * __parse_vio(struct roar_vio_calls * vio) {
59 struct roar_slfi_inst * stack[MAX_STACKSIZE] = {NULL};
60 size_t stackpointer = 0;
61 size_t i;
62 char buf[1024];
63 char * p;
64 int err = ROAR_ERROR_NONE;
65 char * end;
66
67 // create root node.
68 stack[stackpointer] = roar_slfi_new("chain", 1, NULL, -1);
69 if ( stack[stackpointer] == NULL )
70  return NULL;
71 stackpointer++;
72
73 while ((p = roar_vio_fgets(vio, buf, sizeof(buf))) != NULL) {
74  // strip spaces:
75  for (; *p == ' '; p++);
76
77  // strip newlines.
78  for (end = p; *end; end++) {
79   if ( *end == '\n' || *end == '\r' ) {
80    *end = 0;
81    break;
82   }
83  }
84  // skip comments:
85  if ( *p == '#' )
86   continue;
87  if ( *p == '{' ) {
88   if ( stackpointer == (MAX_STACKSIZE-1) ) {
89    err = ROAR_ERROR_NOSPC;
90    break;
91   }
92   stackpointer++;
93   stack[stackpointer] = NULL;
94  } else if ( *p == '}' ) {
95   if ( stackpointer == 1 ) {
96    err = ROAR_ERROR_ILLSEQ;
97    break;
98   }
99   if ( stack[stackpointer] != NULL ) {
100    roar_slfi_unref(stack[stackpointer]);
101    stack[stackpointer] = NULL;
102   }
103   stackpointer--;
104  } else {
105   if ( stack[stackpointer] != NULL ) {
106    roar_slfi_unref(stack[stackpointer]);
107    stack[stackpointer] = NULL;
108   }
109   stack[stackpointer] = __parse_filter(p);
110   if ( stack[stackpointer] == NULL ) {
111    err = roar_error;
112    break;
113   }
114   if ( roar_slfi_ctl(stack[stackpointer-1], ROAR_SLFI_CMD_PUSH, stack[stackpointer]) == -1 ) {
115    err = roar_error;
116    break;
117   }
118  }
119 }
120
121 for (i = stackpointer; i; i--)
122  roar_slfi_unref(stack[i]);
123
124 if (err != ROAR_ERROR_NONE ) {
125  roar_slfi_unref(stack[0]);
126  stack[0] = NULL;
127 }
128
129 roar_err_set(err);
130 return stack[0];
131}
132
133static struct roar_slfi_inst * __parse_file(const char * file) {
134 struct roar_vio_calls vio;
135 struct roar_slfi_inst * ret;
136 int err;
137
138 if ( file == NULL ) {
139  roar_err_set(ROAR_ERROR_INVAL);
140  return NULL;
141 }
142
143 if ( roar_vio_open_dstr_simple(&vio, file, ROAR_VIOF_READ) == -1 )
144  return NULL;
145
146 ret = __parse_vio(&vio);
147 err = roar_error;
148
149 roar_vio_close(&vio);
150
151 roar_err_set(err);
152 return ret;
153}
154
155static int __init(struct roar_slfi_inst * inst, const struct roar_keyval * para, ssize_t paralen) {
156 const struct roar_keyval * kv;
157 const char * filename = NULL;
158 ssize_t i;
159
160 for (i = 0; i < paralen; i++) {
161  kv = &(para[i]);
162  if ( kv->key == NULL || kv->value == NULL )
163   continue;
164  if ( !strcmp(kv->key, "file") ) {
165   filename = kv->value;
166  } else {
167   ROAR_WARN("__init(*): Unknown parameter: %s", kv->key);
168  }
169 }
170
171 inst->userdata = __parse_file(filename);
172 if ( inst->userdata == NULL )
173  return -1;
174
175 return 0;
176}
177
178static int __uninit(struct roar_slfi_inst * inst) {
179 roar_slfi_unref(inst->userdata);
180 inst->userdata = NULL;
181 return 0;
182}
183
184static int __update(struct roar_slfi_inst * inst, uint8_t * universe, ssize_t size_of_universe, int32_t usecspassed, const uint8_t * event, size_t eventlen) {
185 return roar_slfi_update(inst->userdata, universe, size_of_universe, usecspassed, event, eventlen);
186}
187
188static const struct roar_slfi_filter filter[1] = {
189 {
190  .name = "file2filter",
191  .description = "SLFI filter structure builder",
192  .flags = ROAR_SLFI_FLAG_ON_UPDATE,
193  .init = __init,
194  .uninit = __uninit,
195  .update = __update,
196  .ctl = NULL
197 }
198};
199
200ROAR_DL_PLUGIN_REG_SLFI(filter);
201
202// This is the plugin control block.
203ROAR_DL_PLUGIN_START(filter_slfi_file2filter) {
204 // Here we set the name and vendor of our plugin.
205 // If you have no Vendor ID you need to use ROAR_DL_PLUGIN_META_PRODUCT_NV().
206 ROAR_DL_PLUGIN_META_PRODUCT_NIV("filter-slfi-file2filter", ROAR_VID_ROARAUDIO, ROAR_VNAME_ROARAUDIO);
207
208 // This sets the version of your plugin.
209 ROAR_DL_PLUGIN_META_VERSION(ROAR_VERSION_STRING);
210
211 // This sets the license of your plugin.
212 // If there is no tag for the license you use you can just
213 // use ROAR_DL_PLUGIN_META_LICENSE().
214 ROAR_DL_PLUGIN_META_LICENSE_TAG(GPLv3_0);
215
216 // This sets the author and contact infos.
217 // There are several other macros to do this with other parameters.
218 // See ROAR_DL_PLUGIN_META_CONTACT*() in the header or documentation.
219 ROAR_DL_PLUGIN_META_CONTACT_FLNE("Philipp", "Schafft", "ph3-der-loewe", "lion@lion.leolix.org");
220
221 // This sets the description for your plugin.
222 ROAR_DL_PLUGIN_META_DESC("This plugin loads SLFI filter structures based on informations store in a file.");
223
224 // Load filters.
225 ROAR_DL_PLUGIN_REG_FNFUNC(ROAR_DL_FN_FILTER);
226
227// This is the end of the control block.
228} ROAR_DL_PLUGIN_END
229
230//ll
Note: See TracBrowser for help on using the repository browser.