summaryrefslogtreecommitdiff
path: root/app/wlib/mswlib/mswpref.c
blob: e65b0dd4939ce719ffbd35eb1bae5f37c59c3a9f (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <commdlg.h>
#include <math.h>
#include <stdio.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
    strncpy(appLibDirName, module_name, sizeof(appLibDirName));
    size_t len = sizeof(appLibDirName)-strlen(appLibDirName)-1;
    strncat(appLibDirName, "\\..\\share\\xtrkcad", len);
    _fullpath( appLibDirName, appLibDirName, MAX_PATH );
    return appLibDirName;
#endif

    strncpy(appLibDirName, module_name, sizeof(appLibDirName));
    appLibDirName[sizeof(appLibDirName)-1] = '\0';
    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();
    snprintf( mswTmpBuff, sizeof(mswTmpBuff), "%s\\xtrkcad0.ini", appLibDirName );
    rc = GetPrivateProfileString( "workdir", "path", "", appWorkDirName,
                                  sizeof appWorkDirName, mswTmpBuff );
    if ( rc!=0 ) {
        if ( stricmp( appWorkDirName, "installdir" ) == 0 ) {
            strncpy( appWorkDirName, appLibDirName, sizeof(appWorkDirName) );
            appWorkDirName[sizeof(appWorkDirName)-1] = '\0';
        } 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 {
        snprintf( appWorkDirName, sizeof(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 Documents directory.
 *
 * \return    pointer to the user's Documents directory
 */

const char *wGetUserHomeDir( void )
{
    if (SHGetSpecialFolderPath( NULL, mswTmpBuff, CSIDL_PERSONAL, 0 ) == 0 ) {
        wNoticeEx( NT_ERROR, "Cannot get user's documents 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;

static 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);
}

void wPrefsLoad(char * name)
{
    prefs_t *p;
    for (int i= 0; i<prefs_da.cnt; i++) {
        p = &prefs(i);
        if (!name || !name[0]) {
            name = mswProfileFile;
        }
        int rc = GetPrivateProfileString( p->section, p->name, "", mswTmpBuff,
                                          sizeof mswTmpBuff, name );
        if (rc==0) {
            continue;
        }
        p->val = mswStrdup(mswTmpBuff);
    }
}

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];

    snprintf( tmp, sizeof(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];

    snprintf(tmp, sizeof(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( char * name )
{
    prefs_t * p;

    for (p=&prefs(0); p<&prefs(prefs_da.cnt); p++) {
        if (name && name[0]) {
            WritePrivateProfileString( p->section, p->name, p->val, name );
        } else if (p->dirty) {
            WritePrivateProfileString( p->section, p->name, p->val, mswProfileFile );
        }
    }
    if (name && name[0]) {
        WritePrivateProfileString( NULL, NULL, NULL, name );
    } else {
        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;
}

/**
 * Split a line from the config file ie. an ini-file into separate tokens. The
 * line is split into sections, name of value and value following. Pointers
 * to the respective token are returned. These are zero-terminated.
 * If a token is not present, NULL is returned instead.
 * The input line is modified.
 *
 * \param line		input line, modified during excution of function
 * \param section	section if present
 * \param name		name of config value if present
 * \param value		name of value if present
 */

void
wPrefTokenize(char* line, char** section, char** name, char** value)
{
    *section = NULL;
    *name = NULL;
    *value = NULL;

    if (*line == '[') {
        *section = strtok(line, "[]");
    } else {
        *name = strtok(line, "=");
        *value = strtok(NULL, "\n");
    }
}

/**
 * A valid line for a config file is created from the individual elements.
 * Values not need for specific statement are ignored. Eg. when section is
 * present, name and value are not used.
 * The caller has to make sure, that the return buffer is large enough.
 *
 * \param section	section, returned inside squared brackets
 * \param name		name, left side of '='
 * \param value		value, right side of '='
 * \param result	pointer to buffer for formated line.
 */

void
wPrefFormatLine(const char* section, const char* name, 
                const char* value, char* result)
{
    if (!value || *value == '\0') {
        value = "";
    }

    if (section) {
        sprintf(result, "[%s]", section);
    }
    else {
        sprintf(result, "%s=%s", name, value);
    }
}