diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2024-07-03 10:19:30 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2024-07-03 10:19:30 +0200 |
commit | a14a7a0ccc9de76aeab0b2e4bbf58f1a79deedc2 (patch) | |
tree | e469179df67a0e0db49161a43cbf8076a189f6f4 /app/bin/appdefaults.c | |
parent | 5d2c2b27a6323e2666378b986129b2a7c2c39e5c (diff) |
New upstream version 5.3.0GAupstream/5.3.0GAupstream
Diffstat (limited to 'app/bin/appdefaults.c')
-rw-r--r-- | app/bin/appdefaults.c | 349 |
1 files changed, 179 insertions, 170 deletions
diff --git a/app/bin/appdefaults.c b/app/bin/appdefaults.c index e5cff8b..0e1898d 100644 --- a/app/bin/appdefaults.c +++ b/app/bin/appdefaults.c @@ -17,7 +17,7 @@ * * 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. +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include <wchar.h> @@ -28,28 +28,28 @@ #include "paths.h" enum defaultTypes { - INTEGERCONSTANT, - FLOATCONSTANT, - STRINGCONSTANT, - INTEGERFUNCTION, - FLOATFUNCTION, - STRINGFUNCTION + INTEGERCONSTANT, + FLOATCONSTANT, + STRINGCONSTANT, + INTEGERFUNCTION, + FLOATFUNCTION, + STRINGFUNCTION }; struct appDefault { - char *defaultKey; /**< the key used to access the value */ - bool wasUsed; /**< value has already been used on this run */ - enum defaultTypes - valueType; /**< type of default, constant or pointer to a function */ - union { - int intValue; - double floatValue; - char *stringValue; - int (*intFunction)(struct appDefault *, void *); - double (*floatFunction)(struct appDefault *, void *); - char *(*stringFunction)(struct appDefault *, void *); - } defaultValue; - void *additionalData; + char *defaultKey; /**< the key used to access the value */ + bool wasUsed; /**< value has already been used on this run */ + enum defaultTypes + valueType; /**< type of default, constant or pointer to a function */ + union { + int intValue; + double floatValue; + char *stringValue; + int (*intFunction)(struct appDefault *, void *); + double (*floatFunction)(struct appDefault *, void *); + char *(*stringFunction)(struct appDefault *, void *); + } defaultValue; + void *additionalData; }; static int GetLocalMeasureSystem(struct appDefault *ptrDefault, @@ -63,7 +63,7 @@ static double GetLocalRoomSize(struct appDefault *ptrDefault, static char *GetParamFullPath(struct appDefault *ptrDefault, void *additionalData); static char *GetParamPrototype(struct appDefault *ptrDefault, - void *additionalData); + void *additionalData); /** * List of application default settings. As this is searched by binary search, the list has to be kept sorted @@ -73,46 +73,51 @@ static char *GetParamPrototype(struct appDefault *ptrDefault, struct appDefault xtcDefaults[] = { { "DialogItem.cmdopt-preselect", 0, INTEGERCONSTANT,{ .intValue = 1 } }, /**< default command is select */ - { "DialogItem.cmdopt-rightclickmode", 0, INTEGERCONSTANT,{ .intValue = 1 } }, /**< swap default to context */ + { "DialogItem.cmdopt-rightclickmode", 0, INTEGERCONSTANT,{ .intValue = 1 } }, /**< swap default to context */ { "DialogItem.cmdopt-selectmode", 0, INTEGERCONSTANT,{ .intValue = 0 } }, /**< 'Only' mode */ { "DialogItem.cmdopt-selectzero", 0, INTEGERCONSTANT,{ .intValue = 1 } }, /**< 'On' mode */ { "DialogItem.grid-horzenable", 0, INTEGERCONSTANT, { .intValue = 0 }}, { "DialogItem.grid-vertenable", 0, INTEGERCONSTANT,{ .intValue = 0 } }, { "DialogItem.pref-dstfmt", 0, INTEGERFUNCTION,{ .intFunction = GetLocalDistanceFormat } }, /**< number format for distances */ - { "DialogItem.pref-units", 0, INTEGERFUNCTION,{ .intFunction = GetLocalMeasureSystem } }, /**< default unit depends on region */ + { "DialogItem.pref-units", 0, INTEGERFUNCTION,{ .intFunction = GetLocalMeasureSystem } }, /**< default unit depends on region */ { "DialogItem.rgbcolor-exception", 0, INTEGERCONSTANT, { .intValue = 15923462 }}, /**< rich yellow as exception color */ { "Parameter File Map.British stock", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "br.xtp" }, { "Parameter File Map.European stock", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "eu.xtp" }, - { "Parameter File Map.NMRA RP12-25 Feb 2015 O scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-o.xtp" }, - { "Parameter File Map.NMRA RP12-27 Feb 2015 S Scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-s.xtp" }, - { "Parameter File Map.NMRA RP12-31 Feb 2015 HO Scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-ho.xtp" }, - { "Parameter File Map.NMRA RP12-33 Feb 2015 TT Scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-tt.xtp" }, - { "Parameter File Map.NMRA RP12-35 Feb 2015 N Scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-n.xtp" }, - { "Parameter File Map.NMRA RP12-37 Feb 2015 Z scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-z.xtp" }, + { "Parameter File Map.NMRA RP-12.25 Feb 2015 O scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-o.xtp" }, + { "Parameter File Map.NMRA RP-12.27 Feb 2015 S Scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-s.xtp" }, + { "Parameter File Map.NMRA RP-12.31 Feb 2015 HO Scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-ho.xtp" }, + { "Parameter File Map.NMRA RP-12.33 Feb 2015 TT Scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-tt.xtp" }, + { "Parameter File Map.NMRA RP-12.35 Feb 2015 N Scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-n.xtp" }, + { "Parameter File Map.NMRA RP-12.37 Feb 2015 Z scale Turnouts", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "nmra-z.xtp" }, { "Parameter File Map.North American Prototypes", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "protoam.xtp" }, - { "Parameter File Map.Trees", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath } , "trees.xtp" }, + { "Parameter File Map.Trees", 0, STRINGFUNCTION,{ .stringFunction = GetParamFullPath }, "trees.xtp" }, { "Parameter File Names.File1", 0, STRINGFUNCTION,{ .stringFunction = GetParamPrototype }}, { "Parameter File Names.File2", 0, STRINGCONSTANT,{ .stringValue = "Trees" } }, - { "Parameter File Names.File3", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP12-37 Feb 2015 Z scale Turnouts" } }, - { "Parameter File Names.File4", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP12-35 Feb 2015 N Scale Turnouts" } }, - { "Parameter File Names.File5", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP12-33 Feb 2015 TT Scale Turnouts" } }, - { "Parameter File Names.File6", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP12-31 Feb 2015 HO Scale Turnouts" } }, - { "Parameter File Names.File7", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP12-27 Feb 2015 S Scale Turnouts" } }, - { "Parameter File Names.File8", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP12-25 Feb 2015 O scale Turnouts" } }, - { "draw.roomsizeX", 0, FLOATFUNCTION, {.floatFunction = GetLocalRoomSize }}, /**< layout width */ - { "draw.roomsizeY", 0, FLOATFUNCTION,{ .floatFunction = GetLocalRoomSize } }, /**< layout depth */ - { "misc.scale", 0, STRINGFUNCTION, { .stringFunction = GetLocalPopularScale}}, /**< the (probably) most popular scale for a region */ + { "Parameter File Names.File3", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP-12.37 Feb 2015 Z scale Turnouts" } }, + { "Parameter File Names.File4", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP-12.35 Feb 2015 N Scale Turnouts" } }, + { "Parameter File Names.File5", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP-12.33 Feb 2015 TT Scale Turnouts" } }, + { "Parameter File Names.File6", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP-12.31 Feb 2015 HO Scale Turnouts" } }, + { "Parameter File Names.File7", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP-12.27 Feb 2015 S Scale Turnouts" } }, + { "Parameter File Names.File8", 0, STRINGCONSTANT,{ .stringValue = "NMRA RP-12.25 Feb 2015 O scale Turnouts" } }, + { "draw.roomsizeX", 0, FLOATFUNCTION, {.floatFunction = GetLocalRoomSize }}, /**< layout width */ + { "draw.roomsizeY", 0, FLOATFUNCTION,{ .floatFunction = GetLocalRoomSize } }, /**< layout depth */ + { "misc.scale", 0, STRINGFUNCTION, { .stringFunction = GetLocalPopularScale}}, /**< the (probably) most popular scale for a region */ }; #define DEFAULTCOUNT COUNT(xtcDefaults) static long bFirstRun; /**< TRUE if appl is run the first time */ -static char regionCode[3]; /**< will be initialized to the locale's region code */ +static char +regionCode[3]; /**< will be initialized to the locale's region code */ -static wBool_t(*GetIntegerPref)(const char *, const char *, long *, long) = wPrefGetIntegerExt; /**< pointer to active integer pref getter */ -static wBool_t(*GetFloatPref)(const char *, const char *, double *, double) = wPrefGetFloatExt; /**< pointer to active float pref getter */ -static char *(*GetStringPref)(const char *, const char *) = wPrefGetStringExt; /**< pointer to active string pref getter */ +static wBool_t(*GetIntegerPref)(const char *, const char *, long *, + long) = wPrefGetIntegerExt; /**< pointer to active integer pref getter */ +static wBool_t(*GetFloatPref)(const char *, const char *, double *, + double) = wPrefGetFloatExt; /**< pointer to active float pref getter */ +static char *(*GetStringPref)(const char *, + const char *) = + wPrefGetStringExt; /**< pointer to active string pref getter */ /** * A recursive binary search function. It returns location of x in @@ -128,32 +133,32 @@ static char *(*GetStringPref)(const char *, const char *) = wPrefGetStringExt; static int binarySearch(struct appDefault arr[], int l, int r, char *key) { - if (r >= l) { - int mid = l + (r - l) / 2; - int res = strcmp(key, arr[mid].defaultKey); - - // If the element is present at the middle itself - if (!res) { - return mid; - } - - // If the array size is 1 - if (r == 0) { - return -1; - } - - // If element is smaller than mid, then it can only be present - // in left subarray - if (res < 0) { - return binarySearch(arr, l, mid - 1, key); - } - - // Else the element can only be present in right subarray - return binarySearch(arr, mid + 1, r, key); - } - - // We reach here when element is not present in array - return -1; + if (r >= l) { + int mid = l + (r - l) / 2; + int res = strcmp(key, arr[mid].defaultKey); + + // If the element is present at the middle itself + if (!res) { + return mid; + } + + // If the array size is 1 + if (r == 0) { + return -1; + } + + // If element is smaller than mid, then it can only be present + // in left subarray + if (res < 0) { + return binarySearch(arr, l, mid - 1, key); + } + + // Else the element can only be present in right subarray + return binarySearch(arr, mid + 1, r, key); + } + + // We reach here when element is not present in array + return -1; } /** @@ -168,20 +173,20 @@ struct appDefault * FindDefault(struct appDefault *defaultValues, const char *section, const char *name) { - char *searchString = malloc(strlen(section) + strlen(name) + - 2); //includes separator and terminating \0 - int res; - sprintf(searchString, "%s.%s", section, name); - - res = binarySearch(defaultValues, 0, DEFAULTCOUNT-1, searchString); - free(searchString); - - if (res != -1 && defaultValues[res].wasUsed == FALSE) { - defaultValues[res].wasUsed = TRUE; - return (defaultValues + res); - } else { - return (NULL); - } + char *searchString = malloc(strlen(section) + strlen(name) + + 2); //includes separator and terminating \0 + int res; + sprintf(searchString, "%s.%s", section, name); + + res = binarySearch(defaultValues, 0, DEFAULTCOUNT-1, searchString); + free(searchString); + + if (res != -1 && defaultValues[res].wasUsed == FALSE) { + defaultValues[res].wasUsed = TRUE; + return (defaultValues + res); + } else { + return (NULL); + } } /** * Get the application's default region code. On Windows, the system's API is used. @@ -191,32 +196,32 @@ FindDefault(struct appDefault *defaultValues, const char *section, static void InitializeRegionCode(void) { - strcpy(regionCode, "US"); + strcpy(regionCode, "US"); // TODO Move this to wlib #ifdef WINDOWS - { - LCID lcid; - char iso3166[10]; - - lcid = GetThreadLocale(); - GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof(iso3166)); - strncpy(regionCode, iso3166, 2); - } + { + LCID lcid; + char iso3166[10]; + + lcid = GetThreadLocale(); + GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof(iso3166)); + strncpy(regionCode, iso3166, 2); + } #else - { - char *pLang; - pLang = getenv("LANG"); - - if (pLang) { - char *ptr; - ptr = strpbrk(pLang, "_-"); - - if (ptr) { - strncpy(regionCode, ptr + 1, 2); - } - } - } + { + char *pLang; + pLang = getenv("LANG"); + + if (pLang) { + char *ptr; + ptr = strpbrk(pLang, "_-"); + + if (ptr) { + strncpy(regionCode, ptr + 1, 2); + } + } + } #endif } @@ -226,7 +231,7 @@ InitializeRegionCode(void) static bool UseMetric() { return ( strcmp( regionCode, "US" ) != 0 && - strcmp( regionCode, "CA" ) != 0 ); + strcmp( regionCode, "CA" ) != 0 ); } /** * For the US the classical 4x8 sheet is used as default size. in the metric world 1,25x2,0m is used. @@ -235,15 +240,15 @@ static bool UseMetric() static double GetLocalRoomSize(struct appDefault *ptrDefault, void *data) { - if (!strcmp(ptrDefault->defaultKey, "draw.roomsizeY")) { - return (UseMetric() ? 125.0/2.54 : 48); - } + if (!strcmp(ptrDefault->defaultKey, "draw.roomsizeY")) { + return (UseMetric() ? 125.0/2.54 : 48); + } - if (!strcmp(ptrDefault->defaultKey, "draw.roomsizeX")) { - return (UseMetric() ? 200.0 / 2.54 : 96); - } + if (!strcmp(ptrDefault->defaultKey, "draw.roomsizeX")) { + return (UseMetric() ? 200.0 / 2.54 : 96); + } - return (0.0); // should never get here + return (0.0); // should never get here } /** @@ -253,7 +258,7 @@ GetLocalRoomSize(struct appDefault *ptrDefault, void *data) static char * GetLocalPopularScale(struct appDefault *ptrDefault, void *data) { - return (strcmp(regionCode, "GB") ? "HO" : "OO"); + return (strcmp(regionCode, "GB") ? "HO" : "OO"); } /** @@ -262,7 +267,7 @@ GetLocalPopularScale(struct appDefault *ptrDefault, void *data) static int GetLocalMeasureSystem(struct appDefault *ptrDefault, void *data) { - return (UseMetric() ? 1 : 0); + return (UseMetric() ? 1 : 0); } /** @@ -271,18 +276,19 @@ GetLocalMeasureSystem(struct appDefault *ptrDefault, void *data) static int GetLocalDistanceFormat(struct appDefault *ptrDefault, void *data) { - return (UseMetric() ? 8 : 4); + return (UseMetric() ? 8 : 4); } /** -* Prototype definitions currently only exist for US and British. So US -* is assumed to be the default. +* Prototype definitions currently only exist for US and British. So US +* is assumed to be the default. */ static char* GetParamPrototype(struct appDefault *ptrDefault, void *additionalData) { - return (strcmp(regionCode, "GB") ? "North American Prototypes" : "British stock"); + return (strcmp(regionCode, + "GB") ? "North American Prototypes" : "British stock"); } /** @@ -291,9 +297,9 @@ GetParamPrototype(struct appDefault *ptrDefault, void *additionalData) static char * GetParamFullPath(struct appDefault *ptrDefault, void *additionalData) { - char *str; - MakeFullpath(&str, libDir, PARAM_SUBDIR, (char*)additionalData, I2VP(0)); - return str; + char *str; + MakeFullpath(&str, libDir, PARAM_SUBDIR, (char*)additionalData, I2VP(0)); + return str; } @@ -302,19 +308,21 @@ GetParamFullPath(struct appDefault *ptrDefault, void *additionalData) * allows to switch from the extended default version to the basic implementation. */ -wBool_t -wPrefGetInteger(const char *section, const char *name, long *result, long defaultValue) +wBool_t +wPrefGetInteger(const char *section, const char *name, long *result, + long defaultValue) { return GetIntegerPref(section, name, result, defaultValue); } wBool_t -wPrefGetFloat(const char *section, const char *name, double *result, double defaultValue) +wPrefGetFloat(const char *section, const char *name, double *result, + double defaultValue) { return GetFloatPref(section, name, result, defaultValue); } -char * +char * wPrefGetString(const char *section, const char *name) { return GetStringPref(section, name); @@ -334,20 +342,20 @@ wBool_t wPrefGetIntegerExt(const char *section, const char *name, long *result, long defaultValue) { - struct appDefault *thisDefault; + struct appDefault *thisDefault; - thisDefault = FindDefault(xtcDefaults, section, name); + thisDefault = FindDefault(xtcDefaults, section, name); - if (thisDefault) { - if (thisDefault->valueType == INTEGERCONSTANT) { - defaultValue = thisDefault->defaultValue.intValue; - } else { - defaultValue = (thisDefault->defaultValue.intFunction)(thisDefault, - thisDefault->additionalData); - } - } + if (thisDefault) { + if (thisDefault->valueType == INTEGERCONSTANT) { + defaultValue = thisDefault->defaultValue.intValue; + } else { + defaultValue = (thisDefault->defaultValue.intFunction)(thisDefault, + thisDefault->additionalData); + } + } - return (wPrefGetIntegerBasic(section, name, result, defaultValue)); + return (wPrefGetIntegerBasic(section, name, result, defaultValue)); } /** @@ -365,20 +373,20 @@ wBool_t wPrefGetFloatExt(const char *section, const char *name, double *result, double defaultValue) { - struct appDefault *thisDefault; + struct appDefault *thisDefault; - thisDefault = FindDefault(xtcDefaults, section, name); + thisDefault = FindDefault(xtcDefaults, section, name); - if (thisDefault) { - if (thisDefault->valueType == FLOATCONSTANT) { - defaultValue = thisDefault->defaultValue.floatValue; - } else { - defaultValue = (thisDefault->defaultValue.floatFunction)(thisDefault, - thisDefault->additionalData); - } - } + if (thisDefault) { + if (thisDefault->valueType == FLOATCONSTANT) { + defaultValue = thisDefault->defaultValue.floatValue; + } else { + defaultValue = (thisDefault->defaultValue.floatFunction)(thisDefault, + thisDefault->additionalData); + } + } - return (wPrefGetFloatBasic(section, name, result, defaultValue)); + return (wPrefGetFloatBasic(section, name, result, defaultValue)); } /** @@ -392,37 +400,38 @@ wPrefGetFloatExt(const char *section, const char *name, double *result, char * wPrefGetStringExt(const char *section, const char *name) { - struct appDefault *thisDefault; - - thisDefault = FindDefault(xtcDefaults, section, name); - - if (thisDefault) { - char *prefString; - char *defaultValue; - - if (thisDefault->valueType == STRINGCONSTANT) { - defaultValue = thisDefault->defaultValue.stringValue; - } else { - defaultValue = (thisDefault->defaultValue.stringFunction)(thisDefault, - thisDefault->additionalData); - } - - prefString = (char *)wPrefGetStringBasic(section, name); - return (prefString ? prefString : defaultValue); - } else { - return ((char *)wPrefGetStringBasic(section, name)); - } + struct appDefault *thisDefault; + + thisDefault = FindDefault(xtcDefaults, section, name); + + if ( thisDefault == NULL ) { + // Either we don't have a default value or we've already fetched it + return ((char *)wPrefGetStringBasic(section, name)); + } + + char *prefString; + char *defaultValue; + + if (thisDefault->valueType == STRINGCONSTANT) { + defaultValue = thisDefault->defaultValue.stringValue; + } else { + defaultValue = (thisDefault->defaultValue.stringFunction)(thisDefault, + thisDefault->additionalData); + } + // Next call will get value from Prefs + wPrefSetString( section, name, defaultValue ); + return (defaultValue); } /** * Initialize the application default system. The flag firstrun is used to find * out whether the application was run before. This is accomplished by trying - * to read it from the configuration file. As it is only written after this + * to read it from the configuration file. As it is only written after this * test, it can never be found on the first run of the application ie. when the * configuration file does not exist yet. */ -void +void InitAppDefaults(void) { wPrefGetIntegerBasic( "misc", "firstrun", &bFirstRun, TRUE); |