source: roaraudio/libroar/config.c @ 2892:aba43608e324

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

corredted parsing function

File size: 6.9 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
37struct roar_libroar_config * roar_libroar_get_config_ptr(void) {
38 static struct roar_libroar_config config;
39 static int inited = 0;
40
41 if ( !inited ) {
42  memset(&config, 0, sizeof(config));
43
44  config.server = NULL;
45
46  inited++;
47 }
48
49 return &config;
50}
51
52struct roar_libroar_config * roar_libroar_get_config(void) {
53 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
54 static int inited = 0;
55 char * next;
56
57 if ( !inited ) {
58  next = getenv("ROAR_OPTIONS");
59
60  if ( next != NULL ) {
61   roar_libroar_config_parse(next, " ");
62  }
63
64  inited++;
65 }
66
67 return config;
68}
69
70#define _P_FP(v)   ((int)(atof((v))*256.0))
71#define _P_INT(v)  (atoi((v)))
72#define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0)
73
74static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) {
75 struct roar_libroar_config_codec * codec_cfg;
76 int codec;
77 char * codec_str, * option_str, * value_str;
78
79 if ( config == NULL || txt == NULL )
80  return -1;
81
82 codec_str = strtok(txt, ":");
83
84 if ( codec_str == NULL )
85  return -1;
86
87 option_str = strtok(NULL, ":");
88
89 if ( option_str == NULL )
90  return -1;
91
92 value_str = strtok(NULL, ":");
93
94 if ( value_str == NULL )
95  return -1;
96
97 if ( (codec = roar_str2codec(codec_str)) == -1 ) {
98  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str);
99  return -1;
100 }
101
102 if ( (codec_cfg = roar_libroar_config_codec_get(codec, 1)) == NULL )
103  return -1;
104
105 if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) {
106  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q;
107  codec_cfg->q = _P_FP(value_str);
108 } else if ( !strcmp(option_str, "complexity") ) {
109  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY;
110  codec_cfg->complexity = _P_FP(value_str);
111 } else if ( !strcmp(option_str, "dtx") ) {
112  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX;
113  codec_cfg->dtx = _P_BOOL(value_str);
114 } else if ( !strcmp(option_str, "cc-max") ) {
115  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC;
116  codec_cfg->max_cc = _P_INT(value_str);
117 } else {
118  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
119  return -1;
120 }
121
122 return 0;
123}
124
125int    roar_libroar_config_parse(char * txt, char * delm) {
126 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
127 char * k, * v, * next = txt;
128
129 while (next != NULL) {
130  k = next;
131
132  if ( delm == NULL ) {
133   // no delm -> we have only one option
134   next = NULL;
135  } else {
136   next = strstr(next, delm);
137  }
138
139  if ( next != NULL ) {
140   *next = 0;
141    next++;
142  }
143
144  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
145
146  // strip leading spaces:
147  while ( *k == ' ' ) k++;
148
149  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
150
151  // strip tailing new lions:
152  v = strtok(k, "\r\n");
153  if ( v != NULL ) {
154   if ( *v == '\r' || *v == '\n' )
155    *v = 0;
156  }
157
158  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
159
160  // comments
161  if ( *k == '#' )
162   continue;
163
164  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
165
166  // empty options:
167  if ( *k == 0 )
168   continue;
169
170  if ( (v = strstr(k, ":")) != NULL ) {
171   *v = 0;
172    v++;
173  }
174
175  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
176
177  if ( !strcmp(k, "workaround") ) {
178   if ( !strcmp(v, "use-execed") ) {
179    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
180   } else {
181    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
182   }
183  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
184   if ( !strcmp(v, "sysio") ) {
185    config->warnings.sysio = ROAR_WARNING_ALWAYS;
186   } else {
187    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
188   }
189  } else if ( !strcmp(k, "codec") ) {
190   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
191    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
192   }
193  } else {
194   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
195  }
196 }
197
198 return 0;
199}
200
201struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
202 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
203 int i;
204 int need_new = 1;
205
206 if ( codec < 0 || create < 0 )
207  return NULL;
208
209 if ( config->codecs.num == 0 ) {
210  // no match case:
211  if ( !create )
212   return NULL;
213 } else {
214  for (i = 0; i < config->codecs.num; i++) {
215   if ( config->codecs.codec[i].codec == codec )
216    return &(config->codecs.codec[i]);
217   if ( config->codecs.codec[i].codec == -1 )
218    need_new = 0;
219  }
220 }
221
222 if ( !create )
223  return NULL;
224
225 if ( !need_new ) {
226  for (i = 0; i < config->codecs.num; i++) {
227   if ( config->codecs.codec[i].codec == -1 ) {
228    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
229    config->codecs.codec[i].codec = codec;
230    return &(config->codecs.codec[i]);
231   }
232  }
233 }
234
235 if ( config->codecs.num == 0 ) {
236  config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec));
237 } else {
238  config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
239 }
240
241 if ( config->codecs.codec == NULL )
242  return NULL;
243
244 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
245 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
246  config->codecs.codec[i].codec = -1;
247 }
248
249 i = config->codecs.num;
250 config->codecs.num += 16;
251
252 return &(config->codecs.codec[i]);
253}
254
255int    roar_libroar_set_server(char * server) {
256 roar_libroar_get_config_ptr()->server = server;
257 return 0;
258}
259
260char * roar_libroar_get_server(void) {
261 return roar_libroar_get_config_ptr()->server;
262}
263
264//ll
Note: See TracBrowser for help on using the repository browser.