Subversion Repositories heating

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | 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
{
5 andreas 51
char confFile[512], line[512];
52
char *home, *p;
53
char hv0[64], hv1[128];
54
bool gid = false;
55
ifstream cfile;
3 andreas 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");
5 andreas 63
	strcpy(Configure.serial, "/dev/ttyAMA0");
3 andreas 64
	Configure.port = 11006;
17 andreas 65
	Configure.html_port = 8080;
3 andreas 66
	Configure.debug = true;
67
	enableDebug(Configure.debug);
68
	// Try to find a config file
69
	home = getenv("HOME");
5 andreas 70
 
3 andreas 71
	if (!access("/etc/heating.conf",R_OK))
72
		strcpy(confFile,"/etc/heating.conf");
73
	else if (!access("/etc/heating/heating.conf",R_OK))
74
		strcpy(confFile,"/etc/heating/heating.conf");
75
	else if (!access("/usr/etc/heating.conf",R_OK))
76
		strcpy(confFile,"/usr/etc/heating.conf");
77
	else if (home)
78
	{
79
		sprintf(confFile, "%s/.heating", home);
5 andreas 80
 
3 andreas 81
		if (access(confFile,R_OK))
82
			confFile[0] = 0;
83
	}
84
	else
85
	{
86
		confFile[0] = 0;
87
		syslog(LOG_WARNING, "No config file was found!");
88
		initialized = false;
89
		return;
90
	}
5 andreas 91
 
3 andreas 92
	// If there's no config file, inform the user about it.
93
	cfile.open(confFile, ios::in);
5 andreas 94
 
3 andreas 95
	if ((cfile.rdstate() & std::ifstream::failbit) != 0)
96
	{
97
		syslog(LOG_WARNING,"Error opening the config file: %s", strerror(errno));
98
		initialized = false;
99
		return;
100
	}
101
 
102
	syslog(LOG_INFO, "Found config file %s.", confFile);
103
	// Here we've found a config file. We'll read it and set the
104
	// contents to the structure
105
	while (cfile.getline(&line[0], sizeof(line)))
106
	{
107
		int len;
108
 
109
		trim (&line[0]);
110
 
111
		if (line[0] == '#' || !char_traits<char>::length(line))
112
			continue;
113
 
114
		if ((p = findc(line, char_traits<char>::length(line), '=')) == NULL)
115
			continue;
116
 
117
		*p = 0;
118
		p++;
119
		len = char_traits<char>::length(line);
120
 
121
		if (len > (int)sizeof(hv0))
122
			len = (int)sizeof(hv0) - 1;
123
 
124
		strncpy(hv0, line, len);
125
		hv0[len] = 0;
126
		trim (hv0);
127
		len = char_traits<char>::length(p);
128
 
129
		if (len > (int)sizeof(hv1))
130
			len = (int)sizeof(hv0) - 1;
131
 
132
		strncpy(hv1, p, len);
133
		hv1[len] = 0;
134
		trim (hv1);
135
 
136
		if (!compcase(hv0, "port"))
137
			Configure.port = atoi (hv1);
138
 
139
		if (!compcase(hv0, "pidfile"))
140
			strncpy(Configure.pidfile, hv1, sizeof(Configure.pidfile));
141
 
142
		if (!compcase(hv0, "User"))
143
			strncpy (Configure.user, hv1, sizeof(Configure.user));
144
 
145
		if (!compcase(hv0, "Group"))
146
			strncpy (Configure.group, hv1, sizeof(Configure.group));
147
 
17 andreas 148
		if (!compcase(hv0, "HTMLport"))
149
			Configure.html_port = atoi(hv1);
150
 
3 andreas 151
		if (!compcase(hv0, "HeatConf"))
152
			strncpy(Configure.heatconf, hv1, sizeof(Configure.heatconf));
153
 
154
		if (!compcase(hv0, "Home"))
155
			strncpy(Configure.home, hv1, sizeof(Configure.home));
156
 
5 andreas 157
		if (!compcase(hv0, "Serial"))
158
			strncpy(Configure.serial, hv1, sizeof(Configure.serial));
159
 
3 andreas 160
		if (!compcase(hv0, "debug"))
161
		{
162
			Configure.debug = atoi (hv1);
163
			enableDebug(Configure.debug);
164
		}
165
	}
5 andreas 166
 
3 andreas 167
	cfile.close ();
168
	initialized = true;
169
}
170
 
171
CONFIGURE config::getConfig()
172
{
173
	return Configure;
174
}