Subversion Repositories heating

Rev

Rev 5 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 * 
16
 * Author: Andreas Theofilu <andreas@theosys.at>
17
 */
18
#include <iostream>
19
#include <fstream>
20
#include <string>
21
#include <cstring>
22
#include <cctype>
23
#include <cstddef>
24
#include <cstdio>
25
#include <cstdlib>
26
#include <fstream>
27
#include <cerrno>
28
#include <unistd.h>
29
#include <syslog.h>
30
#include <sys/stat.h>
31
#include <sys/types.h>
32
#include <fcntl.h>
33
#include "config.h"
34
 
35
CONFIGURE Configure;
36
 
37
using namespace std;
38
 
39
config::config()
40
{
41
	initialized = false;
42
	readConf();
43
}
44
 
45
/*
46
 * The following functions read a config file and put the
47
 * contents into a structure.
48
 */
49
void config::readConf(void)
50
{
51
	char confFile[512], line[512];
52
	char *home, *p;
53
	char hv0[64], hv1[128];
54
	bool gid = false;
55
	ifstream cfile;
56
 
57
	// Initialize structure with default values
58
	strcpy(Configure.user, "nouser");
59
	strcpy(Configure.group, "nogroup");
60
	strcpy(Configure.heatconf, "/etc/heating.d/heatconf.conf");
61
	strcpy(Configure.home, "/var/lib/heating/heating.db");
62
	strcpy(Configure.pidfile, "/var/run/heating.run");
63
	Configure.port = 11006;
64
	Configure.debug = true;
65
	enableDebug(Configure.debug);
66
	// Try to find a config file
67
	home = getenv("HOME");
68
 
69
	if (!access("/etc/heating.conf",R_OK))
70
		strcpy(confFile,"/etc/heating.conf");
71
	else if (!access("/etc/heating/heating.conf",R_OK))
72
		strcpy(confFile,"/etc/heating/heating.conf");
73
	else if (!access("/usr/etc/heating.conf",R_OK))
74
		strcpy(confFile,"/usr/etc/heating.conf");
75
	else if (home)
76
	{
77
		sprintf(confFile, "%s/.heating", home);
78
 
79
		if (access(confFile,R_OK))
80
			confFile[0] = 0;
81
	}
82
	else
83
	{
84
		confFile[0] = 0;
85
		syslog(LOG_WARNING, "No config file was found!");
86
		initialized = false;
87
		return;
88
	}
89
 
90
	// If there's no config file, inform the user about it.
91
	cfile.open(confFile, ios::in);
92
 
93
	if ((cfile.rdstate() & std::ifstream::failbit) != 0)
94
	{
95
		syslog(LOG_WARNING,"Error opening the config file: %s", strerror(errno));
96
		initialized = false;
97
		return;
98
	}
99
 
100
	syslog(LOG_INFO, "Found config file %s.", confFile);
101
	// Here we've found a config file. We'll read it and set the
102
	// contents to the structure
103
	while (cfile.getline(&line[0], sizeof(line)))
104
	{
105
		int len;
106
 
107
		trim (&line[0]);
108
 
109
		if (line[0] == '#' || !char_traits<char>::length(line))
110
			continue;
111
 
112
		if ((p = findc(line, char_traits<char>::length(line), '=')) == NULL)
113
			continue;
114
 
115
		*p = 0;
116
		p++;
117
		len = char_traits<char>::length(line);
118
 
119
		if (len > (int)sizeof(hv0))
120
			len = (int)sizeof(hv0) - 1;
121
 
122
		strncpy(hv0, line, len);
123
		hv0[len] = 0;
124
		trim (hv0);
125
		len = char_traits<char>::length(p);
126
 
127
		if (len > (int)sizeof(hv1))
128
			len = (int)sizeof(hv0) - 1;
129
 
130
		strncpy(hv1, p, len);
131
		hv1[len] = 0;
132
		trim (hv1);
133
 
134
		if (!compcase(hv0, "port"))
135
			Configure.port = atoi (hv1);
136
 
137
		if (!compcase(hv0, "pidfile"))
138
			strncpy(Configure.pidfile, hv1, sizeof(Configure.pidfile));
139
 
140
		if (!compcase(hv0, "User"))
141
			strncpy (Configure.user, hv1, sizeof(Configure.user));
142
 
143
		if (!compcase(hv0, "Group"))
144
			strncpy (Configure.group, hv1, sizeof(Configure.group));
145
 
146
		if (!compcase(hv0, "HeatConf"))
147
			strncpy(Configure.heatconf, hv1, sizeof(Configure.heatconf));
148
 
149
		if (!compcase(hv0, "Home"))
150
			strncpy(Configure.home, hv1, sizeof(Configure.home));
151
 
152
		if (!compcase(hv0, "debug"))
153
		{
154
			Configure.debug = atoi (hv1);
155
			enableDebug(Configure.debug);
156
		}
157
	}
158
 
159
	cfile.close ();
160
	initialized = true;
161
}
162
 
163
CONFIGURE config::getConfig()
164
{
165
	return Configure;
166
}