summaryrefslogtreecommitdiff
path: root/app/wlib/mswlib/mswpref.c
blob: 201171a8481127f0e6d547a455b1a32d2a41bd2b (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
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
#include <windows.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <commdlg.h>
#include <math.h>
#include <stdio.h>
#include "misc.h"
#include "mswint.h"
#include <shlobj.h>
#include <Shlwapi.h>

#if _MSC_VER >=1400
	#define stricmp _stricmp
#endif

char * mswStrdup( const char * );
static char appLibDirName[MAX_PATH];
static char appWorkDirName[MAX_PATH];

/**
 * Get the location of the shared files (parameters, help file, etc. ): This location is
 * derived from the modulename, ie. the directory where the exe is installed.
 * For an instalaltion directory of somedir/bin/xtrkcad.exe the library directory is
 * somedir/share/xtrkcad/
 */

const char * wGetAppLibDir( void )
{
	char *cp;
	char module_name[MAX_PATH];

	if (appLibDirName[0] != '\0') {
		return appLibDirName;
	}

	GetModuleFileName( mswHInst, module_name, sizeof module_name );
	cp = strrchr( module_name, '\\' );
	if (cp)
		*cp = '\0';

#ifdef XTRKCAD_CMAKE_BUILD
	strcpy(appLibDirName, module_name);
	strcat(appLibDirName, "\\..\\share\\xtrkcad");
	_fullpath( appLibDirName, appLibDirName, MAX_PATH );
	return appLibDirName;
#endif	

	strcpy(appLibDirName, module_name);
	return appLibDirName;
}


/**
 * Gets the working directory for the application. At least the INI file is stored here. 
 * The working directory can be specified manually by creating a file called xtrkcad0.ini
 * in the application lib dir (the directory where the .EXE is located). 
 *
 * [workdir]
 *		path=somepath
 * 
 * when somepath is set to the keyword "installdir", the install directory for the EXE is
 * used.
 *
 * If no xtrkcad0.ini could be found, the user settings directory (appdata) is used.
 *
 */
const char * wGetAppWorkDir( void )
{
	char *cp;
	int rc;
	if ( appWorkDirName[0] != 0 ) {
		return appWorkDirName;
	}
	wGetAppLibDir();
	sprintf( mswTmpBuff, "%s\\xtrkcad0.ini", appLibDirName );
	rc = GetPrivateProfileString( "workdir", "path", "", appWorkDirName, sizeof appWorkDirName, mswTmpBuff );
	if ( rc!=0 ) {
		if ( stricmp( appWorkDirName, "installdir" ) == 0 ) {
			strcpy( appWorkDirName, appLibDirName );
		} else {
			cp = &appWorkDirName[strlen(appWorkDirName)-1];
			while (cp>appWorkDirName && *cp == '\\') *cp-- = 0;
		}
		return appWorkDirName;
	}

	if (SHGetSpecialFolderPath( NULL, mswTmpBuff, CSIDL_APPDATA, 0 ) == 0 ) {
			wNoticeEx( NT_ERROR, "Cannot get user's profile directory", "Exit", NULL );
			wExit(0);
	} else {
		sprintf( appWorkDirName, "%s\\%s", mswTmpBuff, "XTrackCad" );
		if( !PathIsDirectory( appWorkDirName )) {
			if( !CreateDirectory( appWorkDirName, NULL )) {
				wNoticeEx( NT_ERROR, "Cannot create user's profile directory", "Exit", NULL );
				wExit(0);
			}
		}
	}

	return appWorkDirName;
}

/** Get the user's home directory.  The environment variable HOME is
 * assumed to contain the proper directory.
 *
 * \return    pointer to the user's home directory
 */

const char *wGetUserHomeDir( void )
{
	if (SHGetSpecialFolderPath( NULL, mswTmpBuff, CSIDL_PERSONAL, 0 ) == 0 ) {
		wNoticeEx( NT_ERROR, "Cannot get user's home directory", "Exit", NULL );
		wExit(0);
		return( NULL );
	} else {
		return( mswTmpBuff );
	}
}

typedef struct {
		char * section;
		char * name;
		BOOL_T present;
		BOOL_T dirty;
		char * val;
		} prefs_t;
dynArr_t prefs_da;
#define prefs(N) DYNARR_N(prefs_t,prefs_da,N)

void wPrefSetString( const char * section, const char * name, const char * sval )
{
	prefs_t * p;
	
	for (p=&prefs(0); p<&prefs(prefs_da.cnt); p++) {
		if ( strcmp( p->section, section ) == 0 && strcmp( p->name, name ) == 0 ) {
			if (p->val)
				free(p->val);
			p->dirty = TRUE;
			p->val = mswStrdup( sval );
			return;
		}
	}
	DYNARR_APPEND( prefs_t, prefs_da, 10 );
	p = &prefs(prefs_da.cnt-1);
	p->name = mswStrdup(name);
	p->section = mswStrdup(section);
	p->dirty = TRUE;
	p->val = mswStrdup(sval);
}


char * wPrefGetStringBasic( const char * section, const char * name )
{
	prefs_t * p;
	int rc;
	
	for (p=&prefs(0); p<&prefs(prefs_da.cnt); p++) {
		if ( strcmp( p->section, section ) == 0 && strcmp( p->name, name ) == 0 ) {
			return p->val;
		}
	}
	rc = GetPrivateProfileString( section, name, "", mswTmpBuff, sizeof mswTmpBuff, mswProfileFile );
	if (rc==0)
		return NULL;
	DYNARR_APPEND( prefs_t, prefs_da, 10 );
	p = &prefs(prefs_da.cnt-1);
	p->name = mswStrdup(name);
	p->section = mswStrdup(section);
	p->dirty = FALSE;
	p->val = mswStrdup(mswTmpBuff);
	return p->val;
}


void wPrefSetInteger( const char * section, const char * name, long lval )
{
	char tmp[20];
	
	sprintf( tmp, "%ld", lval );
	wPrefSetString( section, name, tmp );
}


wBool_t wPrefGetIntegerBasic(
		const char * section,
		const char * name,
		long *res,
		long def )
{
	const char * cp;
        char * cp1;

	cp = wPrefGetStringBasic( section, name );
	if (cp == NULL) {
		*res = def;
		return FALSE;
	}
	*res = strtol(cp,&cp1,0);
	if (cp==cp1) {
		*res = def;
		return FALSE;
	}
	return TRUE;
}


void wPrefSetFloat(
		const char * section,			/* Section */
		const char * name,			/* Name */
		double lval )			/* Value */
/*
*/
{
	char tmp[20];

	sprintf(tmp, "%0.6f", lval );
	wPrefSetString( section, name, tmp );
}


wBool_t wPrefGetFloatBasic(
		const char * section,			/* Section */
		const char * name,			/* Name */
		double * res,			/* Address of result */
		double def )			/* Default value */
/*
*/
{
	const char * cp;
        char * cp1;

	cp = wPrefGetStringBasic( section, name );
	if (cp == NULL) {
		*res = def;
		return FALSE;
	}
	*res = strtod(cp, &cp1);
	if (cp == cp1) {
		*res = def;
		return FALSE;
	}
	return TRUE;
}


void wPrefFlush( void )
{
	prefs_t * p;
	
	for (p=&prefs(0); p<&prefs(prefs_da.cnt); p++) {
	  if ( p->dirty )
		   WritePrivateProfileString( p->section, p->name, p->val, mswProfileFile );
	}
	WritePrivateProfileString( NULL, NULL, NULL, mswProfileFile );
}


void wPrefReset(
		void )
/*
*/
{
	prefs_t * p;

	for (p=&prefs(0); p<&prefs(prefs_da.cnt); p++) {
		if (p->section)
			free( p->section );
		if (p->name)
			free( p->name );
		if (p->val)
			free( p->val );
	}
	prefs_da.cnt = 0;
}