owntone-server/src/config.c

145 lines
3.5 KiB
C
Raw Normal View History

2006-01-24 23:41:05 +00:00
/*
* $Id$
* Functions for reading and writing the config file
*
* Copyright (C) 2006 Ron Pedde (ron@pedde.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* \file config.c
*
* Config file reading and writing
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
2006-02-05 00:22:46 +00:00
#include "err.h"
#include "ll.h"
#include "daapd.h"
2006-01-24 23:41:05 +00:00
/** Globals */
2006-01-25 22:31:53 +00:00
int ecode;
2006-02-05 00:22:46 +00:00
LL_HANDLE config_main=NULL;
#define CONFIG_LINEBUFFER 128
2006-01-24 23:41:05 +00:00
/**
* read a configfile into a bag
*
* @param file file to read
* @returns TRUE if successful, FALSE otherwise
*/
int config_read(char *file) {
2006-01-25 22:31:53 +00:00
FILE *fin;
int err;
2006-02-05 00:22:46 +00:00
LL_HANDLE pllnew, plltemp;
char linebuffer[CONFIG_LINEBUFFER+1];
char *comment, *term, *value, *delim;
int compat_mode=1;
2006-01-25 22:31:53 +00:00
fin=fopen(file,"r");
if(!fin) {
return CONFIG_E_FOPEN;
}
2006-02-05 00:22:46 +00:00
if((err=ll_create(&pllnew)) != LL_E_SUCCESS) {
DPRINTF(E_LOG,L_CONF,"Error creating linked list: %d\n",err);
fclose(fin);
2006-01-25 22:31:53 +00:00
return CONFIG_E_UNKNOWN;
}
2006-01-24 23:41:05 +00:00
2006-02-05 00:22:46 +00:00
/* got what will be the root of the config tree, now start walking through
* the input file, populating the tree
*/
while(fgets(linebuffer,CONFIG_LINEBUFFER,fin)) {
linebuffer[CONFIG_LINEBUFFER] = '\0';
comment=strchr(linebuffer,'#');
if(comment) {
/* we should really preserve these in another tree*/
*comment = '\0';
}
while(strlen(linebuffer) && (strchr("\n\r ",linebuffer[strlen(linebuffer)-1])))
linebuffer[strlen(linebuffer)-1] = '\0';
if(linebuffer[0] == '[') {
/* section header */
compat_mode=0;
term=&linebuffer[1];
value = strchr(term,']');
if(!value) {
ll_destroy(pllnew);
fclose(fin);
return CONFIG_E_BADHEADER;
}
*value = '\0';
if((err = ll_create(&plltemp)) != LL_E_SUCCESS) {
ll_destroy(pllnew);
fclose(fin);
return CONFIG_E_UNKNOWN;
}
ll_add_ll(pllnew,term,plltemp);
} else {
/* k/v pair */
term=&linebuffer[0];
while((*term=='\t') || (*term==' '))
term++;
value=term;
if(compat_mode) {
delim="\t ";
} else {
delim="=";
}
strsep(&value,delim);
if((value) && (term) && (strlen(term))) {
while(strlen(value) && (strchr("\t ",*value)))
value++;
ll_add_string(pllnew,term,value);
}
}
}
2006-01-24 23:41:05 +00:00
2006-01-25 22:31:53 +00:00
fclose(fin);
2006-02-05 00:22:46 +00:00
/* Sanity check */
ll_dump(pllnew);
ll_destroy(pllnew);
2006-01-25 22:31:53 +00:00
return CONFIG_E_SUCCESS;
2006-01-24 23:41:05 +00:00
}
int config_close(void) {
2006-02-05 00:22:46 +00:00
if(config_main)
ll_destroy(config_main);
2006-01-24 23:41:05 +00:00
2006-02-05 00:22:46 +00:00
return CONFIG_E_SUCCESS;
2006-01-24 23:41:05 +00:00
}