source: roaraudio/libroar/config.c @ 2926:ef036c16f643

Last change on this file since 2926:ef036c16f643 was 2926:ef036c16f643, checked in by phi, 15 years ago

added ROAR_LIBROAR_CONFIG_PSET_VBR, added some other consts in order to void collissions with FLAG IDs

File size: 8.4 KB
Line 
1//config.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *
6 *  This file is part of libroar 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 *  libroar 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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37static struct roar_libroar_config_codec *
38           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config);
39
40
41struct roar_libroar_config * roar_libroar_get_config_ptr(void) {
42 static struct roar_libroar_config config;
43 static int inited = 0;
44
45 if ( !inited ) {
46  memset(&config, 0, sizeof(config));
47
48  config.server = NULL;
49
50  inited++;
51 }
52
53 return &config;
54}
55
56struct roar_libroar_config * roar_libroar_get_config(void) {
57 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
58 static int inited = 0;
59 char * next;
60
61 if ( !inited ) {
62  next = getenv("ROAR_OPTIONS");
63
64  if ( next != NULL ) {
65   roar_libroar_config_parse(next, " ");
66  }
67
68  inited++;
69 }
70
71 return config;
72}
73
74#define _P_FP(v)   ((int)(atof((v))*256.0))
75#define _P_INT(v)  (atoi((v)))
76#define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0)
77
78static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) {
79 struct roar_libroar_config_codec * codec_cfg;
80 int codec;
81 char * codec_str, * option_str, * value_str;
82
83 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
84
85 if ( config == NULL || txt == NULL )
86  return -1;
87
88 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
89
90 codec_str = strtok(txt, ":");
91
92 if ( codec_str == NULL )
93  return -1;
94
95 option_str = strtok(NULL, ":");
96
97 if ( option_str == NULL )
98  return -1;
99
100 value_str = strtok(NULL, ":");
101
102 if ( value_str == NULL )
103  return -1;
104
105 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
106
107 if ( (codec = roar_str2codec(codec_str)) == -1 ) {
108  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str);
109  return -1;
110 }
111
112 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec);
113
114 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
115  return -1;
116
117 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg);
118
119 if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) {
120  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q;
121  codec_cfg->q = _P_FP(value_str);
122 } else if ( !strcmp(option_str, "complexity") ) {
123  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY;
124  codec_cfg->complexity = _P_FP(value_str);
125 } else if ( !strcmp(option_str, "dtx") ) {
126  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX;
127  codec_cfg->dtx = _P_BOOL(value_str);
128 } else if ( !strcmp(option_str, "cc-max") ) {
129  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC;
130  codec_cfg->max_cc = _P_INT(value_str);
131 } else if ( !strcmp(option_str, "vbr") ) {
132  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR;
133  codec_cfg->vbr = _P_BOOL(value_str);
134 } else {
135  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
136  return -1;
137 }
138
139 return 0;
140}
141
142int    roar_libroar_config_parse(char * txt, char * delm) {
143 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
144 char * k, * v, * next = txt;
145
146 while (next != NULL) {
147  k = next;
148
149  if ( delm == NULL ) {
150   // no delm -> we have only one option
151   next = NULL;
152  } else {
153   next = strstr(next, delm);
154  }
155
156  if ( next != NULL ) {
157   *next = 0;
158    next++;
159  }
160
161  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
162
163  // strip leading spaces:
164  while ( *k == ' ' ) k++;
165
166  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
167
168  // strip tailing new lions:
169  v = strtok(k, "\r\n");
170  if ( v != NULL ) {
171   if ( *v == '\r' || *v == '\n' )
172    *v = 0;
173  }
174
175  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
176
177  // comments
178  if ( *k == '#' )
179   continue;
180
181  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
182
183  // empty options:
184  if ( *k == 0 )
185   continue;
186
187  if ( (v = strstr(k, ":")) != NULL ) {
188   *v = 0;
189    v++;
190  }
191
192  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
193
194  if ( !strcmp(k, "workaround") ) {
195   if ( !strcmp(v, "use-execed") ) {
196    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
197   } else {
198    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
199   }
200  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
201   if ( !strcmp(v, "sysio") ) {
202    config->warnings.sysio = ROAR_WARNING_ALWAYS;
203   } else {
204    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
205   }
206  } else if ( !strcmp(k, "codec") ) {
207   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
208    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
209   }
210  } else if ( !strcmp(k, "set-server") ) {
211   if ( roar_libroar_get_server() == NULL )
212    roar_libroar_set_server(v);
213  } else {
214   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
215  }
216 }
217
218 return 0;
219}
220
221struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
222 struct roar_libroar_config * config = roar_libroar_get_config();
223 return roar_libroar_config_codec_get_conf(codec, create, config);
224}
225
226static struct roar_libroar_config_codec *
227           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) {
228 int i;
229 int need_new = 1;
230
231 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
232
233 if ( codec < 0 || create < 0 )
234  return NULL;
235
236 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
237
238 if ( config->codecs.num == 0 ) {
239  // no match case:
240  if ( !create )
241   return NULL;
242 } else {
243  for (i = 0; i < config->codecs.num; i++) {
244   if ( config->codecs.codec[i].codec == codec )
245    return &(config->codecs.codec[i]);
246   if ( config->codecs.codec[i].codec == -1 )
247    need_new = 0;
248  }
249 }
250
251 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
252
253 if ( !create )
254  return NULL;
255
256 if ( !need_new ) {
257  for (i = 0; i < config->codecs.num; i++) {
258   if ( config->codecs.codec[i].codec == -1 ) {
259    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
260    config->codecs.codec[i].codec = codec;
261    return &(config->codecs.codec[i]);
262   }
263  }
264 }
265
266 if ( config->codecs.num == 0 ) {
267  config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec));
268 } else {
269  config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
270 }
271
272 if ( config->codecs.codec == NULL )
273  return NULL;
274
275 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
276 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
277  config->codecs.codec[i].codec = -1;
278 }
279
280 i = config->codecs.num;
281 config->codecs.num += 16;
282
283 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
284 config->codecs.codec[i].codec = codec;
285
286 return &(config->codecs.codec[i]);
287}
288
289int    roar_libroar_set_server(char * server) {
290 roar_libroar_get_config_ptr()->server = server;
291 return 0;
292}
293
294char * roar_libroar_get_server(void) {
295 return roar_libroar_get_config_ptr()->server;
296}
297
298//ll
Note: See TracBrowser for help on using the repository browser.