source: roaraudio/libroar/config.c @ 2891:b89a0b2b92a4

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

typo

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