+#include <stdlib.h>
+#include "genint1.h"
#include "AppConfiguration.h"
-void AppConfiguration::error(config_t *cfg)
+AppConfiguration::AppConfiguration(const char * fname)
{
+ config_init(&cfg);
+
+ /* Read the file. If there is an error, report it and exit. */
+ if(!config_read_file(&cfg, fname))
+ {
+ error();
+ config_destroy(&cfg);
+ exit(3);/* from original code. */
+ }
+}
+int AppConfiguration::getInt(const char * path)
+{
+ int value = 0;
+ if (config_lookup_int(&cfg, path, &value) == CONFIG_FALSE)
+ {
+ fprintf(stderr, "Warning: %s was not found, or bad type requested\n", path);
+ }
+ return value;
+}
+
+const char * AppConfiguration::getString(const char * path)
+{
+ const char * value;
+ if (config_lookup_string(&cfg, path, &value) == CONFIG_FALSE)
+ {
+ fprintf(stderr, "Warning: %s was not found, or bad type requested\n", path);
+ }
+ return value;
+}
+
+int AppConfiguration::error()
+{
+ if (config_error_type(&cfg) == CONFIG_ERR_NONE)
+ {
+ return FALSE;
+ }
+
fprintf(stderr, "%s: In file %s, line %d\n",
- config_error_text(cfg),
- config_error_file(cfg),
- config_error_line(cfg));
+ config_error_text(&cfg),
+ config_error_file(&cfg),
+ config_error_line(&cfg));
+ return TRUE;
+}
+
+void AppConfiguration::release()
+{
+ config_destroy(&cfg);
}
*/
class AppConfiguration {
protected:
+ config_t cfg;
public:
- static void error(config_t *cfg);
+ AppConfiguration(const char * fname);
+ int getInt(const char * path);
+ const char * getString(const char * path);
+ int error();
+ void release();
};
void QKernel::LoadConfig(char * fname)
{
- config_t cfg;
- const char *str;
-
- config_init(&cfg);
-
/* Hack for checking if file exists without using external libs.*/
FILE * file = fopen(fname, "r");
if (!file) {
/* File exists, so file has been locked. Release it. */
fclose(file);
- /* Read the file. If there is an error, report it and exit. */
- if(!config_read_file(&cfg, fname))
- {
- AppConfiguration::error(&cfg);
- config_destroy(&cfg);
- exit(3);/* from original code. */
- }
-
- if(!config_lookup_int(&cfg, "node_number", &NodeNumber))
- {
- AppConfiguration::error(&cfg);
- config_destroy(&cfg);
- exit(3);
- }
+ AppConfiguration config(fname);
- if(config_lookup_string(&cfg, "type", &str)){
- ConType = (strcmp(str, "explicit") == 0) ? 1 : 2;
- }
- else {
- AppConfiguration::error(&cfg);
+ NodeNumber = config.getInt("node_number");
+ if (config.error() == TRUE) {
+ config.release();
+ exit(3);
}
+ ConType = (strcmp("explicit", config.getString("type")) == 0) ? 1 : 2;
+ config.error();
- if(config_lookup_string(&cfg, "host", &str)) {
- char host[255];
- strcpy(host, str);//FIXME: buffer overflow
- ConnectList.append(new ConnectEntry(host));
- }
- else {
- AppConfiguration::error(&cfg);
+ char host[255];
+ strcpy(host, config.getString("host"));//FIXME: buffer overflow
+ if (config.error() == FALSE) {
+ ConnectList.append(new ConnectEntry(config.getString("host")));
}
- if(config_lookup_string(&cfg, "progdir", &str)){
- strcpy(progdir, str);//FIXME: buffer overflow
- }
- else {
- AppConfiguration::error(&cfg);
- }
+ strcpy(progdir, config.getString("progdir"));//FIXME: buffer overflow
+ config.error();
- if(config_lookup_string(&cfg, "homedir", &str)){
- strcpy(HomeDir, str);//FIXME: buffer overflow
- }
- else {
- AppConfiguration::error(&cfg);
- }
+ strcpy(HomeDir, config.getString("homedir"));//FIXME: buffer overflow
+ config.error();
- config_destroy(&cfg);
+ config.release();
}
/* +++++++++++++++++++++++++++++++++++++++++++++++ */
void NETMOD::load_config(char *fname)
{
- config_t cfg;
- const char *str;
int on,k=0;
NETlink *pomlink;
-
- config_init(&cfg);
/* Hack for checking if file exists without using external libs.*/
/* File exists, so file has been locked. Release it. */
fclose(file);
- /* Read the file. If there is an error, report it and exit. */
- if(!config_read_file(&cfg, fname))
- {
- AppConfiguration::error(&cfg);
- config_destroy(&cfg);
- exit(3);/* from original code. */
- }
+ AppConfiguration config(fname);
- if(config_lookup_int(&cfg, "node_number", &MyNode))
- {
- if (MyNode==-1) {
- write_at_console("Node number must be specified");
- config_destroy(&cfg);
- exit(1);
- };
- }
- else
- {
- AppConfiguration::error(&cfg);
- config_destroy(&cfg);
+ MyNode = config.getInt("node_number");
+ if (config.error() == TRUE) {
+ write_at_console("Node number must be specified");
+ config.release();
exit(1);
}
- if(config_lookup_string(&cfg, "host", &str)) {
+ char host[255];
+ strcpy(host, config.getString("host"));
+ if(config.error() == FALSE)
+ {
k++;
pomlink = new NETlink;
- strcpy(pomlink->addr, str);
+ strcpy(pomlink->addr, host);
pomlink->connected = FALSE;
pomlink->sock = socket(AF_INET, SOCK_STREAM, 0);
fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0));
Links.append(pomlink);
to_connect++;
}
- else {
- AppConfiguration::error(&cfg);
- }
-
- config_destroy(&cfg);
+ config.release();
if (k==0) all_connected=TRUE;
}