~ubuntu-branches/ubuntu/oneiric/libgda4/oneiric

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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* GDA common library
 * Copyright (C) 2007 The GNOME Foundation.
 *
 * AUTHORS:
 *      Vivien Malerba <malerba@gnome-db.org>
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gda-binreloc.h"
/* include source file as mentioned in gbr_init_lib()'s doc */
#include "binreloc.c"

#ifdef G_OS_WIN32
#include <windows.h>
/* Remember HMODULE to retrieve path to it lateron */
static HMODULE hdllmodule = NULL;
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
	switch(fdwReason)
	{
	case DLL_PROCESS_ATTACH:
		hdllmodule = (HMODULE)hinstDLL;
		break;
	}

	return TRUE;
}
#elif HAVE_CARBON
  #ifdef ENABLE_BINRELOC
    #include <Carbon/Carbon.h>
  #endif
#endif


/**
 * gda_gbr_init
 */
void
gda_gbr_init (void)
{
#ifdef G_OS_WIN32
	/* nothing */
#elif HAVE_CARBON
	/* nothing */
#else
	_gda_gbr_init_lib (NULL);
#endif
}

/*
 * gda_gbr_get_file_path
 */
gchar *
gda_gbr_get_file_path (GdaPrefixDir where, ...)
{
	gchar *prefix = NULL;
	gchar *tmp, *file_part;
	va_list ap;
	gchar **parts;
	gint size, i;
	const gchar *prefix_dir_name = NULL;
	gint prefix_len = strlen (LIBGDAPREFIX);

	/*
	g_print ("LIBGDAPREFIX = %s\n", LIBGDAPREFIX);
	g_print ("LIBGDABIN = %s\n", LIBGDABIN);
	g_print ("LIBGDASBIN = %s\n", LIBGDASBIN);
	g_print ("LIBGDADATA = %s\n",LIBGDADATA );
	g_print ("LIBGDALIB = %s\n", LIBGDALIB);
	g_print ("LIBGDALIBEXEC = %s\n",LIBGDALIBEXEC );
	g_print ("LIBGDASYSCONF = %s\n", LIBGDASYSCONF);
	*/

#ifdef G_OS_WIN32
	wchar_t path[MAX_PATH]; /* Flawfinder: ignore */
	gchar* p;
#endif

	switch (where) {
	default:
	case GDA_NO_DIR:
		break;
	case GDA_BIN_DIR:
		tmp = LIBGDABIN;
#ifndef G_OS_WIN32
		if (! g_str_has_prefix (tmp, LIBGDAPREFIX) || (tmp [prefix_len] != G_DIR_SEPARATOR)) 
			prefix = g_strdup (tmp);
		else
			prefix_dir_name = tmp + prefix_len + 1;
#else
		prefix_dir_name = "bin";
#endif
		break;
	case GDA_SBIN_DIR:
		tmp = LIBGDASBIN;
#ifndef G_OS_WIN32
		if (! g_str_has_prefix (tmp, LIBGDAPREFIX) || (tmp [prefix_len] != G_DIR_SEPARATOR)) 
			prefix = g_strdup (tmp);
		else
			prefix_dir_name = tmp + prefix_len + 1;
#else
		prefix_dir_name = "sbin";
#endif
		break;
	case GDA_DATA_DIR:
		tmp = LIBGDADATA;
#ifndef G_OS_WIN32
		if (! g_str_has_prefix (tmp, LIBGDAPREFIX) || (tmp [prefix_len] != G_DIR_SEPARATOR)) 
			prefix = g_strdup (tmp);
		else
			prefix_dir_name = tmp + prefix_len + 1;
#else
		prefix_dir_name = "share";
#endif		
		break;
	case GDA_LOCALE_DIR:
		tmp = LIBGDADATA;
#ifndef G_OS_WIN32
		if (! g_str_has_prefix (tmp, LIBGDAPREFIX) || (tmp [prefix_len] != G_DIR_SEPARATOR)) {
			prefix = g_strdup (tmp);
			prefix_dir_name = "locale";
		}
		else
			prefix_dir_name = "share" G_DIR_SEPARATOR_S "locale";
#else
		prefix_dir_name = "share" G_DIR_SEPARATOR_S "locale";
#endif
		break;
	case GDA_LIB_DIR:
		tmp = LIBGDALIB;
#ifndef G_OS_WIN32
		if (! g_str_has_prefix (tmp, LIBGDAPREFIX) || (tmp [prefix_len] != G_DIR_SEPARATOR)) 
			prefix = g_strdup (tmp);
		else 
			prefix_dir_name = tmp + prefix_len + 1;
#else
		prefix_dir_name = "lib";
#endif
		break;
	case GDA_LIBEXEC_DIR:
		tmp = LIBGDALIBEXEC;
#ifndef G_OS_WIN32
		if (! g_str_has_prefix (tmp, LIBGDAPREFIX) || (tmp [prefix_len] != G_DIR_SEPARATOR)) 
			prefix = g_strdup (tmp);
		else
			prefix_dir_name = tmp + prefix_len + 1;
#else
		prefix_dir_name = "libexec";
#endif
		break;
	case GDA_ETC_DIR:
		tmp = LIBGDASYSCONF;
#ifndef G_OS_WIN32
		if (! g_str_has_prefix (tmp, LIBGDAPREFIX) || (tmp [prefix_len] != G_DIR_SEPARATOR)) 
			prefix = g_strdup (tmp);
		else
			prefix_dir_name = tmp + prefix_len + 1;
#else
		prefix_dir_name = "etc";
#endif
		break;
	}

#ifdef GDA_DEBUG_NO
	g_print ("%s ()\n", __FUNCTION__);
#endif

	if (!prefix) {
		/* prefix part for each OS */
#ifdef G_OS_WIN32
		/* Get from location of libgda DLL */
		GetModuleFileNameW (hdllmodule, path, MAX_PATH);
		prefix = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);
		if ((p = strrchr (prefix, G_DIR_SEPARATOR)) != NULL)
			*p = '\0';
		
		p = strrchr (prefix, G_DIR_SEPARATOR);
		if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
			  g_ascii_strcasecmp (p + 1, "lib") == 0))
			*p = '\0';
#elif HAVE_CARBON
  #ifdef ENABLE_BINRELOC
    #define MAXLEN 500
		ProcessSerialNumber myProcess;
		FSRef bundleLocation;
		unsigned char bundlePath[MAXLEN]; /* Flawfinder: ignore */
		
		if ((GetCurrentProcess (&myProcess) == noErr) &&
		    (GetProcessBundleLocation (&myProcess, &bundleLocation) == noErr) &&
		    (FSRefMakePath (&bundleLocation, bundlePath, MAXLEN) == noErr)) {
			if (g_str_has_suffix ((const gchar*) bundlePath, ".app"))
				prefix = g_strdup_printf ("%s/Contents/Resources", (const gchar*) bundlePath);
			else {
				prefix = g_path_get_dirname ((const char*) bundlePath);
				if (g_str_has_suffix (prefix, "bin"))
					prefix [strlen (prefix) - 3] = 0;
			}
#ifdef GDA_DEBUG_NO
			g_print ("BUNDLE=%s, prefix=%s\n", bundlePath, prefix);
#endif
		}
		else
			g_warning ("Could not get PREFIX (using Mac OS X Carbon)");
  #else
#ifdef GDA_DEBUG_NO
		g_print ("Binreloc disabled, using %s\n", LIBGDAPREFIX);
#endif
  #endif

		if (!prefix)
			prefix = g_strdup (LIBGDAPREFIX);
#else
		if (!prefix)
		 	prefix = _gda_gbr_find_prefix (LIBGDAPREFIX);
#endif
	}

	if (!prefix || !*prefix) {
		g_free (prefix);
		return NULL;
	}
       
	/* filename part */
	size = 10;
	parts = g_new0 (gchar *, size);
	va_start (ap, where);
	for (i = 0, tmp = va_arg (ap, gchar *); tmp; tmp = va_arg (ap, gchar *)) {
		if (i == size - 1) {
			size += 10;
			parts = g_renew (gchar *, parts, size);
		}
		parts[i] = g_strdup (tmp);
#ifdef GDA_DEBUG_NO
		g_print ("\t+ %s\n", tmp);
#endif
		i++;
	}
	parts[i] = NULL;
	va_end (ap);

	file_part = g_build_filenamev (parts);
	g_strfreev (parts);

	/* result */
	if (prefix_dir_name)
		tmp = g_build_filename (prefix, prefix_dir_name, file_part, NULL);
	else
		tmp = g_build_filename (prefix, file_part, NULL);

	if (!g_file_test (tmp, G_FILE_TEST_EXISTS)) {
		if (g_str_has_suffix (prefix, "libgda")) {
			/* test if we are in the sources */
			g_free (tmp);
			if (prefix_dir_name)
				tmp = g_build_filename (LIBGDAPREFIX, prefix_dir_name, file_part, NULL);
			else
				tmp = g_build_filename (LIBGDAPREFIX, file_part, NULL);
		}
		else {
			g_free (prefix);
			prefix = g_strdup (LIBGDAPREFIX);
			g_free (tmp);
			if (prefix_dir_name)
				tmp = g_build_filename (prefix, prefix_dir_name, file_part, NULL);
			else
				tmp = g_build_filename (prefix, file_part, NULL);
		}
	}

	g_free (prefix);
	g_free (file_part);
#ifdef GDA_DEBUG_NO
	g_print ("\t=> %s\n", tmp);
#endif

	return tmp;
}