Subversion Repositories heating

Rev

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