source: roaraudio/libroar/config.c @ 3219:9f57256fb56a

Last change on this file since 3219:9f57256fb56a was 3219:9f57256fb56a, checked in by phi, 14 years ago

typo

File size: 9.6 KB
RevLine 
[2477]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
[2895]37static struct roar_libroar_config_codec *
38           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config);
39
40
[2478]41struct roar_libroar_config * roar_libroar_get_config_ptr(void) {
42 static struct roar_libroar_config config;
[3218]43 static int    inited = 0;
44 static char   authfile[1024];
45 char        * home = getenv("HOME");
[2478]46
47 if ( !inited ) {
48  memset(&config, 0, sizeof(config));
[2567]49
[3218]50  config.server   = NULL;
51  config.authfile = NULL;
52
53  if ( home != NULL ) {
54   snprintf(authfile, 1023, "%s/.roarauth", home);
55   authfile[1023]  = 0;
56   config.authfile = authfile;
57  }
[2567]58
[2478]59  inited++;
60 }
61
62 return &config;
63}
64
65struct roar_libroar_config * roar_libroar_get_config(void) {
66 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
67 static int inited = 0;
[2884]68 char * next;
[2478]69
70 if ( !inited ) {
[2480]71  next = getenv("ROAR_OPTIONS");
72
[2884]73  if ( next != NULL ) {
74   roar_libroar_config_parse(next, " ");
[2480]75  }
76
[2478]77  inited++;
78 }
79
80 return config;
81}
82
[2890]83#define _P_FP(v)   ((int)(atof((v))*256.0))
84#define _P_INT(v)  (atoi((v)))
85#define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0)
86
87static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) {
88 struct roar_libroar_config_codec * codec_cfg;
89 int codec;
90 char * codec_str, * option_str, * value_str;
91
[2912]92 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
93
[2890]94 if ( config == NULL || txt == NULL )
95  return -1;
96
[2912]97 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
98
[2892]99 codec_str = strtok(txt, ":");
[2890]100
[2892]101 if ( codec_str == NULL )
102  return -1;
103
104 option_str = strtok(NULL, ":");
[2890]105
106 if ( option_str == NULL )
107  return -1;
108
[2892]109 value_str = strtok(NULL, ":");
[2890]110
111 if ( value_str == NULL )
112  return -1;
113
[2912]114 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
115
[2890]116 if ( (codec = roar_str2codec(codec_str)) == -1 ) {
117  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str);
118  return -1;
119 }
120
[2912]121 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec);
122
[2893]123 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
[2890]124  return -1;
125
[2912]126 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg);
127
[2890]128 if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) {
129  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q;
130  codec_cfg->q = _P_FP(value_str);
131 } else if ( !strcmp(option_str, "complexity") ) {
132  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY;
133  codec_cfg->complexity = _P_FP(value_str);
134 } else if ( !strcmp(option_str, "dtx") ) {
135  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX;
136  codec_cfg->dtx = _P_BOOL(value_str);
137 } else if ( !strcmp(option_str, "cc-max") ) {
138  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC;
139  codec_cfg->max_cc = _P_INT(value_str);
[2926]140 } else if ( !strcmp(option_str, "vbr") ) {
141  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR;
142  codec_cfg->vbr = _P_BOOL(value_str);
[2962]143 } else if ( !strcmp(option_str, "mode") ) {
144  if ( !strcmp(value_str, "nb") ) {
145   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
146   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_NB;
147  } else if ( !strcmp(value_str, "wb") ) {
148   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
149   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_WB;
150  } else if ( !strcmp(value_str, "uwb") ) {
151   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
152   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_UWB;
153  } else {
154   ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec mode: %s", value_str);
155   return -1;
156  }
[2890]157 } else {
[2891]158  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
[2892]159  return -1;
[2890]160 }
161
[2892]162 return 0;
[2890]163}
164
[2884]165int    roar_libroar_config_parse(char * txt, char * delm) {
166 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
167 char * k, * v, * next = txt;
168
169 while (next != NULL) {
170  k = next;
[2886]171
172  if ( delm == NULL ) {
173   // no delm -> we have only one option
174   next = NULL;
175  } else {
176   next = strstr(next, delm);
177  }
178
[2884]179  if ( next != NULL ) {
180   *next = 0;
181    next++;
182  }
183
[2888]184  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
185
[2887]186  // strip leading spaces:
187  while ( *k == ' ' ) k++;
188
[2888]189  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
190
[2887]191  // strip tailing new lions:
192  v = strtok(k, "\r\n");
193  if ( v != NULL ) {
194   if ( *v == '\r' || *v == '\n' )
195    *v = 0;
196  }
197
[2888]198  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
199
[2887]200  // comments
201  if ( *k == '#' )
202   continue;
203
[2888]204  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
205
[2887]206  // empty options:
207  if ( *k == 0 )
208   continue;
209
[2884]210  if ( (v = strstr(k, ":")) != NULL ) {
211   *v = 0;
212    v++;
213  }
214
[2888]215  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
216
[2884]217  if ( !strcmp(k, "workaround") ) {
218   if ( !strcmp(v, "use-execed") ) {
219    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
220   } else {
[2888]221    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
[2884]222   }
223  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
224   if ( !strcmp(v, "sysio") ) {
225    config->warnings.sysio = ROAR_WARNING_ALWAYS;
226   } else {
[2888]227    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
[2884]228   }
[3136]229  } else if ( !strcmp(k, "force-rate") ) {
230   config->info.rate = atoi(v);
231  } else if ( !strcmp(k, "force-bits") ) {
232   config->info.bits = atoi(v);
233  } else if ( !strcmp(k, "force-channels") ) {
234   config->info.channels = atoi(v);
235  } else if ( !strcmp(k, "force-codec") ) {
236   config->info.codec = roar_str2codec(v);
[2890]237  } else if ( !strcmp(k, "codec") ) {
238   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
239    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
240   }
[2896]241  } else if ( !strcmp(k, "set-server") ) {
[2899]242   if ( roar_libroar_get_server() == NULL )
243    roar_libroar_set_server(v);
[3218]244  } else if ( !strcmp(k, "set-authfile") ) {
[3219]245   strncpy(config->authfile, v, 1023);
246   config->authfile[1023] = 0;
[2884]247  } else {
[2888]248   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
[2884]249  }
250 }
251
252 return 0;
253}
254
[2886]255struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
[2893]256 struct roar_libroar_config * config = roar_libroar_get_config();
257 return roar_libroar_config_codec_get_conf(codec, create, config);
258}
259
260static struct roar_libroar_config_codec *
261           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) {
[2886]262 int i;
263 int need_new = 1;
264
[2912]265 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
266
[2886]267 if ( codec < 0 || create < 0 )
268  return NULL;
269
[2912]270 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
271
[2886]272 if ( config->codecs.num == 0 ) {
273  // no match case:
274  if ( !create )
275   return NULL;
276 } else {
277  for (i = 0; i < config->codecs.num; i++) {
278   if ( config->codecs.codec[i].codec == codec )
279    return &(config->codecs.codec[i]);
280   if ( config->codecs.codec[i].codec == -1 )
281    need_new = 0;
282  }
283 }
284
[2912]285 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
286
[2886]287 if ( !create )
288  return NULL;
289
290 if ( !need_new ) {
291  for (i = 0; i < config->codecs.num; i++) {
292   if ( config->codecs.codec[i].codec == -1 ) {
293    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
294    config->codecs.codec[i].codec = codec;
295    return &(config->codecs.codec[i]);
296   }
297  }
298 }
299
300 if ( config->codecs.num == 0 ) {
301  config->codecs.codec = malloc(16*sizeof(struct roar_libroar_config_codec));
302 } else {
303  config->codecs.codec = realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
304 }
305
306 if ( config->codecs.codec == NULL )
307  return NULL;
308
309 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
310 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
311  config->codecs.codec[i].codec = -1;
312 }
313
314 i = config->codecs.num;
315 config->codecs.num += 16;
316
[2913]317 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
318 config->codecs.codec[i].codec = codec;
319
[2886]320 return &(config->codecs.codec[i]);
321}
322
[2567]323int    roar_libroar_set_server(char * server) {
324 roar_libroar_get_config_ptr()->server = server;
325 return 0;
326}
327
328char * roar_libroar_get_server(void) {
329 return roar_libroar_get_config_ptr()->server;
330}
[2478]331
[2477]332//ll
Note: See TracBrowser for help on using the repository browser.