Implement new helpers to load preferences
This commit is contained in:
parent
641b945617
commit
a2a3630e56
10
config.h
Normal file
10
config.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#define PACKAGE "libSystem"
|
||||
#define VERSION "0.2.3"
|
||||
|
||||
#ifndef PREFIX
|
||||
# define PREFIX "/usr/local"
|
||||
#endif
|
||||
|
||||
#ifndef LIBDIR
|
||||
# define LIBDIR PREFIX "/lib"
|
||||
#endif
|
|
@ -47,7 +47,16 @@ void config_foreach_section(Config * config, String const * section,
|
|||
ConfigForeachSectionCallback callback, void * priv);
|
||||
|
||||
int config_load(Config * config, String const * filename);
|
||||
|
||||
int config_load_preferences(Config * config, String const * vendor,
|
||||
String const * package, String const * filename);
|
||||
int config_load_preferences_system(Config * config, String const * vendor,
|
||||
String const * package, String const * filename);
|
||||
int config_load_preferences_user(Config * config, String const * vendor,
|
||||
String const * package, String const * filename);
|
||||
|
||||
int config_reset(Config * config);
|
||||
|
||||
int config_save(Config * config, String const * filename);
|
||||
|
||||
#endif /* !LIBSYSTEM_SYSTEM_CONFIG_H */
|
||||
|
|
|
@ -2,7 +2,7 @@ package=libSystem
|
|||
version=0.2.3
|
||||
|
||||
subdirs=data,doc,include,src,tests,tools
|
||||
config=sh
|
||||
config=h,sh
|
||||
dist=Makefile,COPYING,config.sh,README.md
|
||||
|
||||
[README.md]
|
||||
|
|
79
src/config.c
79
src/config.c
|
@ -25,8 +25,16 @@
|
|||
#include "System/error.h"
|
||||
#include "System/mutator.h"
|
||||
#include "System/config.h"
|
||||
#include "../config.h"
|
||||
|
||||
/* constants */
|
||||
#ifndef PREFIX
|
||||
# define PREFIX "/usr/local"
|
||||
#endif
|
||||
#ifndef SYSCONFDIR
|
||||
# define SYSCONFDIR PREFIX "/etc"
|
||||
#endif
|
||||
|
||||
#define CONFIG_COMMENT '#'
|
||||
|
||||
|
||||
|
@ -342,6 +350,77 @@ static String * _load_value(FILE * fp)
|
|||
}
|
||||
|
||||
|
||||
/* config_load_preferences */
|
||||
int config_load_preferences(Config * config, String const * vendor,
|
||||
String const * package, String const * filename)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if((ret = config_load_preferences_system(config, vendor, package,
|
||||
filename)) != 0
|
||||
&& ret != -ENOENT)
|
||||
return ret;
|
||||
if((ret = config_load_preferences_user(config, vendor, package,
|
||||
filename)) != 0
|
||||
&& ret != -ENOENT)
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* config_load_preferences_system */
|
||||
int config_load_preferences_system(Config * config, String const * vendor,
|
||||
String const * package, String const * filename)
|
||||
{
|
||||
int ret;
|
||||
String * f;
|
||||
|
||||
if(filename == NULL)
|
||||
return error_set_code(-EINVAL, "%s", strerror(EINVAL));
|
||||
if(vendor != NULL && string_find(vendor, "/") != NULL)
|
||||
return error_set_code(-EPERM, "%s", strerror(EPERM));
|
||||
if(package != NULL && string_find(package, "/") != NULL)
|
||||
return error_set_code(-EPERM, "%s", strerror(EPERM));
|
||||
if((f = string_new_append(SYSCONFDIR, "/",
|
||||
(vendor != NULL) ? vendor : "", "/",
|
||||
(package != NULL) ? package : "", "/",
|
||||
filename, NULL)) == NULL)
|
||||
return error_get_code();
|
||||
ret = config_load(config, f);
|
||||
string_delete(f);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* config_load_preferences_user */
|
||||
int config_load_preferences_user(Config * config, String const * vendor,
|
||||
String const * package, String const * filename)
|
||||
{
|
||||
int ret;
|
||||
String const * homedir;
|
||||
String * f;
|
||||
|
||||
if(filename == NULL)
|
||||
return error_set_code(-EINVAL, "%s", strerror(EINVAL));
|
||||
if(vendor != NULL && string_find(vendor, "/") != NULL)
|
||||
return error_set_code(-EPERM, "%s", strerror(EPERM));
|
||||
if(package != NULL && string_find(package, "/") != NULL)
|
||||
return error_set_code(-EPERM, "%s", strerror(EPERM));
|
||||
if(filename != NULL && string_find(filename, "/") != NULL)
|
||||
return error_set_code(-EPERM, "%s", strerror(EPERM));
|
||||
if((homedir = getenv("HOME")) == NULL)
|
||||
return error_set_code(-errno, "%s", strerror(errno));
|
||||
if((f = string_new_append(homedir, "/.config/",
|
||||
(vendor != NULL) ? vendor : "", "/",
|
||||
(package != NULL) ? package : "", "/",
|
||||
filename, NULL)) == NULL)
|
||||
return error_get_code();
|
||||
ret = config_load(config, f);
|
||||
string_delete(f);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* config_reset */
|
||||
static void _delete_foreach(String const * key, void * value, void * data);
|
||||
static void _delete_foreach_section(String const * key, void * value,
|
||||
|
|
|
@ -13,6 +13,9 @@ sources=array.c,buffer.c,config.c,error.c,event.c,hash.c,mutator.c,object.c,pars
|
|||
ldflags=-ldl -lws2_32
|
||||
install=$(LIBDIR)
|
||||
|
||||
[config.c]
|
||||
depends=../config.h
|
||||
|
||||
[parser.c]
|
||||
depends=token.h
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user