summaryrefslogtreecommitdiffabout
path: root/src/eyekinfig.cc
blob: 6dde7b2cb101e871ea4cb8e9faad0a567787c57b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cassert>
#include <stdexcept>
#include <autosprintf.h>
#include "eyekinfig.h"

#include "config.h"

eyekinfig_t::eyekinfig_t(const std::string& ma) : macaddress(ma) {
    try {
	static cfg_opt_t opts[] = {
	    CFG_STR((char*)"targetdir",(char*)"/var/lib/" PACKAGE "/%s",CFGF_NONE),
	    CFG_STR((char*)"uploadkey",(char*)"",CFGF_NONE),
	    CFG_STR((char*)"on-start-session",(char*)"",CFGF_NONE),
	    CFG_STR((char*)"on-upload-photo",(char*)"",CFGF_NONE),
	    CFG_STR((char*)"on-mark-last-photo-in-roll",(char*)"",CFGF_NONE),
	    CFG_INT((char*)"umask",022,CFGF_NONE),
	    CFG_END()
	};
	cfg = cfg_init(opts,CFGF_NONE);
	if(!cfg)
	    throw std::runtime_error("failed to cfg_init()");
	std::string::size_type ls = macaddress.rfind('/');
	std::string cf = gnu::autosprintf( EYEKIN_CONF_DIR "/%s.conf",
		macaddress.c_str()+((ls==std::string::npos)?0:ls+1) );
	int r = cfg_parse(cfg,cf.c_str());
	if(r != CFG_SUCCESS) {
	    cfg_free(cfg); cfg=0;
	    if(CFG_FILE_ERROR) throw std::runtime_error(gnu::autosprintf("failed to open configuration file '%s'",cf.c_str()));
	    throw std::runtime_error(gnu::autosprintf("failed to parse configuration file '%s'",cf.c_str()));
	}
    }catch(...) {
	if(cfg) cfg_free(cfg), cfg=0;
	throw;
    }
}

eyekinfig_t::~eyekinfig_t() {
    if(cfg) cfg_free(cfg);
}

std::string eyekinfig_t::get_targetdir() {
    assert(cfg);
    return gnu::autosprintf(cfg_getstr(cfg,"targetdir"),macaddress.c_str());
}

std::string eyekinfig_t::get_upload_key() {
    assert(cfg);
    return cfg_getstr(cfg,"uploadkey");
}

std::string eyekinfig_t::get_on_start_session() {
    assert(cfg);
    return cfg_getstr(cfg,"on-start-session");
}
std::string eyekinfig_t::get_on_upload_photo() {
    assert(cfg);
    return cfg_getstr(cfg,"on-upload-photo");
}

std::string eyekinfig_t::get_on_mark_last_photo_in_roll() {
    assert(cfg);
    return cfg_getstr(cfg,"on-mark-last-photo-in-roll");
}


int eyekinfig_t::get_umask() {
    assert(cfg);
    return 0777&cfg_getint(cfg,"umask");
}