Rev 17 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
*
* All rights reserved. No warranty, explicit or implicit, provided.
*
* NOTICE: All information contained herein is, and remains
* the property of Andreas Theofilu and his suppliers, if any.
* The intellectual and technical concepts contained
* herein are proprietary to Andreas Theofilu and its suppliers and
* may be covered by European and Foreign Patents, patents in process,
* and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Andreas Theofilu.
*
* Author: Andreas Theofilu <andreas@theosys.at>
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cctype>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <cerrno>
#include <unistd.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "config.h"
CONFIGURE Configure;
bool bcmInitialized;
using namespace std;
config::config()
{
initialized = false;
readConf();
}
/*
* The following functions read a config file and put the
* contents into a structure.
*/
void config::readConf(void)
{
char confFile[512], line[512];
char *home, *p;
char hv0[64], hv1[128];
bool gid = false;
ifstream cfile;
// Initialize structure with default values
strcpy(Configure.user, "nouser");
strcpy(Configure.group, "nogroup");
strcpy(Configure.heatconf, "/etc/heating.d/heatconf.conf");
strcpy(Configure.home, "/var/lib/heating/heating.db");
strcpy(Configure.pidfile, "/var/run/heating.run");
strcpy(Configure.serial, "/dev/ttyAMA0");
Configure.port = 11006;
Configure.html_port = 8080;
Configure.debug = true;
enableDebug(Configure.debug);
// Try to find a config file
home = getenv("HOME");
if (!access("/etc/heating.conf",R_OK))
strcpy(confFile,"/etc/heating.conf");
else if (!access("/etc/heating/heating.conf",R_OK))
strcpy(confFile,"/etc/heating/heating.conf");
else if (!access("/usr/etc/heating.conf",R_OK))
strcpy(confFile,"/usr/etc/heating.conf");
else if (home)
{
sprintf(confFile, "%s/.heating", home);
if (access(confFile,R_OK))
confFile[0] = 0;
}
else
{
confFile[0] = 0;
syslog(LOG_WARNING, "No config file was found!");
initialized = false;
return;
}
// If there's no config file, inform the user about it.
cfile.open(confFile, ios::in);
if ((cfile.rdstate() & std::ifstream::failbit) != 0)
{
syslog(LOG_WARNING,"Error opening the config file: %s", strerror(errno));
initialized = false;
return;
}
syslog(LOG_INFO, "Found config file %s.", confFile);
// Here we've found a config file. We'll read it and set the
// contents to the structure
while (cfile.getline(&line[0], sizeof(line)))
{
int len;
trim (&line[0]);
if (line[0] == '#' || !char_traits<char>::length(line))
continue;
if ((p = findc(line, char_traits<char>::length(line), '=')) == NULL)
continue;
*p = 0;
p++;
len = char_traits<char>::length(line);
if (len > (int)sizeof(hv0))
len = (int)sizeof(hv0) - 1;
strncpy(hv0, line, len);
hv0[len] = 0;
trim (hv0);
len = char_traits<char>::length(p);
if (len > (int)sizeof(hv1))
len = (int)sizeof(hv0) - 1;
strncpy(hv1, p, len);
hv1[len] = 0;
trim (hv1);
if (!compcase(hv0, "port"))
Configure.port = atoi (hv1);
if (!compcase(hv0, "pidfile"))
strncpy(Configure.pidfile, hv1, sizeof(Configure.pidfile));
if (!compcase(hv0, "User"))
strncpy (Configure.user, hv1, sizeof(Configure.user));
if (!compcase(hv0, "Group"))
strncpy (Configure.group, hv1, sizeof(Configure.group));
if (!compcase(hv0, "HTMLport"))
Configure.html_port = atoi(hv1);
if (!compcase(hv0, "HeatConf"))
strncpy(Configure.heatconf, hv1, sizeof(Configure.heatconf));
if (!compcase(hv0, "Home"))
strncpy(Configure.home, hv1, sizeof(Configure.home));
if (!compcase(hv0, "Serial"))
strncpy(Configure.serial, hv1, sizeof(Configure.serial));
if (!compcase(hv0, "debug"))
{
Configure.debug = atoi (hv1);
enableDebug(Configure.debug);
}
}
cfile.close ();
initialized = true;
}
CONFIGURE config::getConfig()
{
return Configure;
}