File: util.c

package info (click to toggle)
gnubg 0.90-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 30,800 kB
  • ctags: 8,224
  • sloc: ansic: 92,884; xml: 13,338; sh: 9,675; makefile: 425; python: 392; yacc: 295; sql: 237; lex: 220; php: 102; cs: 81; awk: 25; sed: 16
file content (106 lines) | stat: -rw-r--r-- 2,526 bytes parent folder | download
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
/*
 * util.c
 *
 * by Christian Anthon 2007
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 or later of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * 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
 *
 * $Id: util.c,v 1.14 2008/02/06 22:47:58 Superfly_Jon Exp $
 */

#include "config.h"
#include "util.h"
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#if WIN32
#include <windows.h>

char *aszRNG[1]; 
char *aszSkillType[ 1 ]; 
int exsExport;
int ap;

extern char * getInstallDir( void )
{
  static char *ret = NULL;
  if (!ret)
  {
	char buf[FILENAME_MAX];
	if (GetModuleFileName(NULL, buf, sizeof(buf)) != 0)
	{
		char *p1 = strrchr(buf, '/'), *p2 = strrchr(buf, '\\');
		int pos1 = (p1 != NULL) ? (int)(p1 - buf) : -1;
		int pos2 = (p2 != NULL) ? (int)(p2 - buf) : -1;
		int pos = MAX(pos1, pos2);
		if (pos > 0)
			buf[pos] = '\0';
		ret = g_strdup(buf);
	}
  }
  return ret;
}

extern void PrintSystemError(const char* message)
{
	LPVOID lpMsgBuf;
	if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS ,
		NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR) &lpMsgBuf, 0, NULL) != 0)
	{
		g_print("** Windows error while %s **\n", message);
		g_print(": %s", (LPCTSTR)lpMsgBuf);

		LocalFree(lpMsgBuf);
	}
}
#else
#include <errno.h>

extern void PrintSystemError(const char* message)
{
	printf("Unknown system error while %s!\n", message);
}
#endif

void PrintError(const char* str)
{
	g_printerr("%s: %s", str, strerror(errno));
}

/* Non-Ansi compliant function */
#ifdef __STRICT_ANSI__
FILE *fdopen(int, const char *);
#endif

extern FILE *GetTemporaryFile(const char *nameTemplate, char **retName)
{
    FILE *pf;
    int tmpd = g_file_open_tmp(nameTemplate, retName, NULL); 
    if (tmpd < 0)
	{
      PrintError("creating temporary file");
      return NULL;
    }
	pf = fdopen(tmpd, "w+" );
	if (pf == NULL)
	{
		g_free(retName);
		PrintError("opening temporary file");
		return NULL;
	}
	else
		return pf;
}