diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2024-11-14 19:35:45 +0100 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2024-11-14 19:35:45 +0100 |
commit | df5520aa2dae5b3ce7abf8733dcdd152898af163 (patch) | |
tree | 00d3047bfb14f682bfb5a21010c731ed649bfed7 /app/wlib/mswlib/mswpref.c | |
parent | df247efec654e512242e4f4f1b0212034f9e01fe (diff) | |
parent | ec3c0f6f6e7153fa797dc57a0e95779cbc63a23b (diff) |
Merge branch 'release/debian/1_5.3.0GA-1'debian/1_5.3.0GA-1
Diffstat (limited to 'app/wlib/mswlib/mswpref.c')
-rw-r--r-- | app/wlib/mswlib/mswpref.c | 442 |
1 files changed, 268 insertions, 174 deletions
diff --git a/app/wlib/mswlib/mswpref.c b/app/wlib/mswlib/mswpref.c index 201171a..e65b0dd 100644 --- a/app/wlib/mswlib/mswpref.c +++ b/app/wlib/mswlib/mswpref.c @@ -1,17 +1,15 @@ #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 +#define stricmp _stricmp #endif char * mswStrdup( const char * ); @@ -27,38 +25,41 @@ static char appWorkDirName[MAX_PATH]; const char * wGetAppLibDir( void ) { - char *cp; - char module_name[MAX_PATH]; + char *cp; + char module_name[MAX_PATH]; - if (appLibDirName[0] != '\0') { - return appLibDirName; - } + if (appLibDirName[0] != '\0') { + return appLibDirName; + } - GetModuleFileName( mswHInst, module_name, sizeof module_name ); - cp = strrchr( module_name, '\\' ); - if (cp) - *cp = '\0'; + 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; + 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. + * 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). + * 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. * @@ -67,209 +68,302 @@ const char * wGetAppLibDir( void ) */ 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; + 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 home directory. The environment variable HOME is - * assumed to contain the proper directory. +/** Get the user's Documents directory. * - * \return pointer to the user's home 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 home directory", "Exit", NULL ); - wExit(0); - return( NULL ); - } else { - return( mswTmpBuff ); - } + 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; -dynArr_t prefs_da; + 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 ) +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); + 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; + 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 ); + 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 * 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; + 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 */ + const char * section, /* Section */ + const char * name, /* Name */ + double lval ) /* Value */ /* */ { - char tmp[20]; + char tmp[20]; - sprintf(tmp, "%0.6f", lval ); - wPrefSetString( section, name, tmp ); + 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 * 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; + 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 ) +void wPrefFlush( char * name ) { - 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 ); + 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 ) + 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; + 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); + } +} + |