aboutsummaryrefslogtreecommitdiff
path: root/hge/ini.cpp
blob: 6fed586284fdcac3f6fc34d0d247e9e733f51fda (plain) (blame)
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
/*
** Haaf's Game Engine 1.8
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** Core functions implementation: unix ini file
*/

#include "hge_impl.h"

const char *HGE_Impl::_BuildProfilePath(const char *section, const char *name, const char *szIniFile)
{
	// !!! FIXME: not efficient.
	static char path[_MAX_PATH];
	const char *home = getenv("HOME");
	strcpy(path, home);
	mkdir(path, S_IRWXU);

	#if PLATFORM_MACOSX
	strcat(path, "/Library");
	mkdir(path, S_IRWXU);
	strcat(path, "/Application Support");
	mkdir(path, S_IRWXU);
	strcat(path, "/");
	strcat(path, szWinTitle);
	mkdir(path, S_IRWXU);
	#else
	strcat(path, "/.");
	strcat(path, szWinTitle);
	mkdir(path, S_IRWXU);
	#endif

	strcat(path, "/inis");
	mkdir(path, S_IRWXU);

	strcat(path, "/");
	strcat(path, szIniFile);
	mkdir(path, S_IRWXU);
	strcat(path, "/");
	strcat(path, section);
	mkdir(path, S_IRWXU);
	strcat(path, "/");
	strcat(path, name);

	return path;
}

bool HGE_Impl::_WritePrivateProfileString(const char *section, const char *name, const char *buf, const char *szIniFile)
{
	const char *path = _BuildProfilePath(section, name, szIniFile);
	FILE *io = fopen(path, "wb");
	if (io == NULL)
		return false;
	const size_t rc = fwrite(buf, strlen(buf), 1, io);
	if (fclose(io) == EOF)
		return false;
	return (rc == 1);
}

bool HGE_Impl::_GetPrivateProfileString(const char *section, const char *name, const char *deflt, char *buf, size_t bufsize, const char *szIniFile)
{
	bool retval = false;
	const char *path = _BuildProfilePath(section, name, szIniFile);
	FILE *io = fopen(path, "rb");
	if (io != NULL)
	{
		const size_t rc = fread(buf, 1, bufsize-1, io);
		retval = (ferror(io) == 0);
		fclose(io);
		if (retval)
			buf[rc] = '\0';
	}

	if (!retval)
		snprintf(buf, bufsize, "%s", deflt);

	return retval;
}

// We parse the usual .ini files, and build them into our directory tree when items don't exist.
// !!! FIXME: this code sort of stinks. In fact, the whole directory tree thing could be a mistake...
void HGE_Impl::_LoadIniFile(const char *fname)
{
	char section[128] = { 0 };
	struct stat statbuf;
	if (stat(fname, &statbuf) == -1)
		return;
	FILE *io = fopen(fname, "rb");
	char *buf = new char[statbuf.st_size + 1];
	size_t rc = fread(buf, statbuf.st_size, 1, io);
	buf[statbuf.st_size] = '\0';
	fclose(io);
	if (rc == 1)
	{
		char *start = buf;
		char *ptr = start;
		while (start <= (buf + statbuf.st_size))
		{
			while ((*ptr != '\r') && (*ptr != '\n') && (*ptr != '\0'))
				ptr++;
			*ptr = '\0';

			while ((*start == ' ') || (*start == '\t') || (*start == '\r') || (*start == '\n'))
				start++;

			if ((*start == ';') || (*start == '\0'))  // comment or empty line.
				;
			else if (*start == '[')  // section
			{
				start++;
				char *end = strchr(start, ']');
				if (end != NULL)
				{
					*end = '\0';
					snprintf(section, sizeof (section), "%s", start);
				}
			}
			else if (section[0] != '\0')
			{
				char *eq = strchr(start, '=');
				if (eq != NULL)
				{
					*eq = '\0';
					eq++;
					char tmpbuf[128];
					if (!_GetPrivateProfileString(section, start, "", tmpbuf, sizeof (tmpbuf), fname))
						_WritePrivateProfileString(section, start, eq, fname);
				}
			}
			ptr++;
			start = ptr;
		}
	}
	delete[] buf;
}

void CALL HGE_Impl::Ini_SetInt(const char *section, const char *name, int value)
{
	char buf[256];

	if(szIniFile[0]) {
		sprintf(buf,"%d",value);
		_WritePrivateProfileString(section, name, buf, szIniFile);
	}
}


int CALL HGE_Impl::Ini_GetInt(const char *section, const char *name, int def_val)
{
	char buf[256];

	if(szIniFile[0]) {
		if(_GetPrivateProfileString(section, name, "", buf, sizeof(buf), szIniFile))
		{ return atoi(buf); }
		else { return def_val; }
	}
	return def_val;
}


void CALL HGE_Impl::Ini_SetFloat(const char *section, const char *name, float value)
{
	char buf[256];

	if(szIniFile[0]) {
		sprintf(buf,"%f",value);
		_WritePrivateProfileString(section, name, buf, szIniFile);
	}
}


float CALL HGE_Impl::Ini_GetFloat(const char *section, const char *name, float def_val)
{
	char buf[256];

	if(szIniFile[0]) {
		if(_GetPrivateProfileString(section, name, "", buf, sizeof(buf), szIniFile))
		{ return (float)atof(buf); }
		else { return def_val; }
	}
	return def_val;
}


void CALL HGE_Impl::Ini_SetString(const char *section, const char *name, const char *value)
{
	if(szIniFile[0]) _WritePrivateProfileString(section, name, value, szIniFile);
}


char* CALL HGE_Impl::Ini_GetString(const char *section, const char *name, const char *def_val)
{
	if(szIniFile[0]) _GetPrivateProfileString(section, name, def_val, szIniString, sizeof(szIniString), szIniFile);
	else strcpy(szIniString, def_val);
	return szIniString;
}