File: ConfigModule.cpp

package info (click to toggle)
sbnc 1.2-26
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,216 kB
  • sloc: cpp: 17,556; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 284
file content (194 lines) | stat: -rw-r--r-- 4,915 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
 * shroudBNC - an object-oriented framework for IRC                            *
 * Copyright (C) 2005-2007 Gunnar Beutner                                      *
 *                                                                             *
 * This program is free software; you can redistribute it and/or               *
 * modify it under the terms of the GNU General Public License                 *
 * as published by the Free Software Foundation; either version 2              *
 * of the License, or (at your option) any later version.                      *
 *                                                                             *
 * This program is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
 * GNU General Public License for more details.                                *
 *                                                                             *
 * You should have received a copy of the GNU General Public License           *
 * along with this program; if not, write to the Free Software                 *
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
 *******************************************************************************/

#include "StdAfx.h"

CConfigModule::CConfigModule(const char *Filename) {
	if (Filename == NULL) {
		m_Far = new CDefaultConfigModule();
		m_File = NULL;
		m_Error = NULL;
	} else {
		char *CorePath;
		bool Result = false;

		m_Far = NULL;
		m_Image = NULL;
		m_File = strdup(Filename);

		CorePath = strdup(sbncGetModulePath());

		if (CorePath != NULL && *CorePath != '\0') {
			for (size_t i = strlen(CorePath) - 1; i >= 0; i--) {
				if (CorePath[i] == '/' || CorePath[i] == '\\') {
					CorePath[i] = '\0';

					break;
				}
			}

	#if !defined(_WIN32) || defined(__MINGW32__)
			lt_dlsetsearchpath(CorePath);
	#endif
		
			Result = InternalLoad(g_Bouncer->BuildPath(Filename, CorePath));

			free(CorePath);
		}

		if (!Result) {
			InternalLoad(Filename);
		}
	}
}

CConfigModule::~CConfigModule(void) {
	if (m_Far) {
		m_Far->Destroy();
	}

	if (m_Image != NULL) {
		FreeLibrary(m_Image);
	}

	free(m_File);

	free(m_Error);
}

bool CConfigModule::InternalLoad(const char *Filename) {
	m_Image = LoadLibrary(Filename);

	if (!m_Image) {
#ifdef _WIN32
		char *ErrorMsg, *p;

		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, (char *)&ErrorMsg, 0, NULL);

		p = strchr(ErrorMsg, '\r');

		if (p != NULL) {
			p[0] = '\0';
		}

		p = strchr(ErrorMsg, '\n');

		if (p != NULL) {
			p[0] = '\0';
		}
#else
		const char *ErrorMsg;

		ErrorMsg = lt_dlerror();
#endif

		if (ErrorMsg == NULL) {
			m_Error = strdup("Unknown error.");
		} else {
			m_Error = strdup(ErrorMsg);
		}

#ifdef _WIN32
		if (ErrorMsg != NULL) {
			LocalFree(ErrorMsg);
		}
#endif

		return false;
	} else {
		FNGETINTERFACEVERSION pfGetInterfaceVersion =
			(FNGETINTERFACEVERSION)GetProcAddress(m_Image,
			"bncGetInterfaceVersion");

		if (pfGetInterfaceVersion != NULL && pfGetInterfaceVersion() < INTERFACEVERSION) {
			m_Error = strdup("This module was compiled for an earlier version"
				" of shroudBNC. Please recompile the module and try again.");

			FreeLibrary(m_Image);
			m_Image = NULL;

			return false;
		}

		if (GetModule() == NULL) {
			m_Error = strdup("GetModule() failed.");

			FreeLibrary(m_Image);
			m_Image = NULL;

			return false;
		}

		m_Error = NULL;
	}

	return true;
}

void CConfigModule::Destroy(void) {
	m_Far->Destroy();
}

void CConfigModule::Init(CCore *Root) {
	m_Far->Init(Root);
}

CConfig *CConfigModule::CreateConfigObject(const char *Name, CUser *Owner) {
	return m_Far->CreateConfigObject(Name, Owner);
}

CConfigModuleFar *CConfigModule::GetModule(void) {
	if (!m_Image) {
		return NULL;
	}

	if (m_Far != NULL) {
		return m_Far;
	} else {
		FNGETCONFIGOBJECT pfGetConfigObject = (FNGETCONFIGOBJECT)GetProcAddress(m_Image, "bncGetConfigObject");

		if (pfGetConfigObject) {
			m_Far = pfGetConfigObject();

			return m_Far;
		} else {
			return NULL;
		}
	}
}

const char *CConfigModule::GetFilename(void) {
	return m_File;
}

HMODULE CConfigModule::GetHandle(void) {
	return m_Image;
}

RESULT<bool> CConfigModule::GetError(void) {
	if (m_Error != NULL) {
		THROW(bool, Generic_Unknown, m_Error);
	} else  {
		RETURN(bool, true);
	}
}

CConfig *CDefaultConfigModule::CreateConfigObject(const char *Name, CUser *Owner) {
	return new CConfigFile(g_Bouncer->BuildPath(Name), Owner);
}