summaryrefslogtreecommitdiff
path: root/orkbasecxx/ConfigManager.cpp
blob: 85ad41ee8927de6d207735f261344a95f4f0275e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */
#pragma warning( disable: 4786 ) // disables truncated symbols in browse-info warning

#define _WINSOCKAPI_		// prevents the inclusion of winsock.h

#include "ace/OS_NS_dirent.h"
#include "Utils.h"
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOMWriter.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>
#include "serializers/DomSerializer.h"
#include "ConfigManager.h"

#define CONFIG_FILE_NAME "config.xml"
#define ETC_CONFIG_FILE_NAME "/etc/orkaudio/config.xml"

#ifdef WIN32
# define snprintf _snprintf
#endif

ConfigManager* ConfigManager::m_singleton = NULL;

ConfigManager* ConfigManager::Instance()
{
	if(m_singleton == NULL)
	{
		m_singleton = new ConfigManager();
	}
	return m_singleton;
}

void ConfigManager::Initialize()
{
	bool failed = false;
	m_configTopNode = NULL;

	try
	{
		char* cfgFilename = ""; 
		char* cfgEnvPath = "";
		int cfgAlloc = 0;

		cfgEnvPath = ACE_OS::getenv("ORKAUDIO_CONFIG_PATH");
		if(cfgEnvPath) {
			ACE_DIR* dir = ACE_OS::opendir(cfgEnvPath);
			if(dir) {
				int len = 0;

				ACE_OS::closedir(dir);
				len = strlen(cfgEnvPath)+1+strlen(CONFIG_FILE_NAME)+1;
				cfgFilename = (char*)malloc(len);

				if(cfgFilename) {
					cfgAlloc = 1;
					snprintf(cfgFilename, len, "%s/%s", cfgEnvPath, CONFIG_FILE_NAME);
				}
			}
		}

		if(!cfgFilename || !strlen(cfgFilename)) {
			FILE* file = ACE_OS::fopen(CONFIG_FILE_NAME, "r");
			if(file)
			{
				// config.xml exists in the current directory
				cfgFilename = CONFIG_FILE_NAME;
				fclose(file);
			}
			else
			{
				// config.xml could not be found in the current
				// directory, try to find it in system configuration directory
				cfgFilename = ETC_CONFIG_FILE_NAME;
			}
		}

        	XMLPlatformUtils::Initialize();

		// By default, the DOM document generated by the parser will be free() by the parser.
		// If we ever need to free the parser and the document separately, we need to do this:
		//		DOMNode *doc = parser->getDocument();
		//		...
		//		parser->adoptDocument();
		//		doc->release();  
		//		...
		//		delete parser;
		XercesDOMParser *m_parser = new XercesDOMParser;
		m_parser->parse(cfgFilename);
		DOMNode	*doc = NULL;
		doc = m_parser->getDocument();

		// XXX is it okay to free here?
		if(cfgAlloc) {
			free(cfgFilename);
		}

		if (doc)
		{
			DOMNode *firstChild = doc->getFirstChild();
			if (firstChild)
			{
				m_configTopNode = firstChild;
				m_config.DeSerializeDom(firstChild);

				/*
				// Write out config to a file
				DOMImplementation* impl =  DOMImplementationRegistry::getDOMImplementation(XStr("Core").unicodeForm());
				XERCES_CPP_NAMESPACE::DOMDocument* myDoc;
				   myDoc = impl->createDocument(
							   0,                    // root element namespace URI.
							   XStr("root").unicodeForm(),         // root element name
							   0);                   // document type object (DTD).
				m_config.SerializeDom(myDoc);
				CStdString toto = DomSerializer::DomNodeToString(myDoc);
				FILE* file = fopen("zzz.xml", "w");
				fwrite((PCSTR)toto,1,toto.GetLength(),file);
				fclose(file);	
				*/
			}
			else
			{
				LOG4CXX_ERROR(LOG.configLog, CStdString("Could not parse config file:") + CONFIG_FILE_NAME);
				failed = true;
			}
		}
		else
		{
			LOG4CXX_WARN(LOG.configLog, CStdString("Could not find config file:") + CONFIG_FILE_NAME);
		}
	}
	catch (const CStdString& e)
	{
		LOG4CXX_ERROR(LOG.configLog, e);
		failed = true;
	}
    catch(const XMLException& e)
    {
		LOG4CXX_ERROR(LOG.configLog, e.getMessage());
		failed = true;
    }
	if (failed)
	{
		exit(0);
	}
}


void ConfigManager::AddConfigureFunction(ConfigureFunction configureFunction)
{
	m_configureFunctions.push_back(configureFunction);
	// Cal the external configure callback straight away
	configureFunction(m_configTopNode);
}