1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4//
5// --------------------------------------------------------------------------------------------------
6// configuration.cpp
7//
8//
9// Access and update configuration values, falling back on legacy CLRConfig methods where necessary.
10//
11// --------------------------------------------------------------------------------------------------
12
13#include "stdafx.h"
14
15#include "clrconfig.h"
16#include "configuration.h"
17
18LPCWSTR *knobNames = nullptr;
19LPCWSTR *knobValues = nullptr;
20int numberOfKnobs = 0;
21
22void Configuration::InitializeConfigurationKnobs(int numberOfConfigs, LPCWSTR *names, LPCWSTR *values)
23{
24 numberOfKnobs = numberOfConfigs;
25
26 // Neither should be null, or both should be null
27 _ASSERT(!((names == nullptr) ^ (values == nullptr)));
28
29 knobNames = names;
30 knobValues = values;
31}
32
33static LPCWSTR GetConfigurationValue(LPCWSTR name)
34{
35 _ASSERT(name != nullptr);
36 if (name == nullptr || knobNames == nullptr || knobValues == nullptr)
37 {
38 return nullptr;
39 }
40
41 for (int i = 0; i < numberOfKnobs; ++i)
42 {
43 _ASSERT(knobNames[i] != nullptr);
44 if (wcscmp(name, knobNames[i]) == 0)
45 {
46 return knobValues[i];
47 }
48 }
49
50 return nullptr;
51}
52
53DWORD Configuration::GetKnobDWORDValue(LPCWSTR name, const CLRConfig::ConfigDWORDInfo& dwordInfo)
54{
55 bool returnedDefaultValue;
56 DWORD legacyValue = CLRConfig::GetConfigValue(dwordInfo, true /* acceptExplicitDefaultFromRegutil */, &returnedDefaultValue);
57 if (!returnedDefaultValue)
58 {
59 return legacyValue;
60 }
61
62 LPCWSTR knobValue = GetConfigurationValue(name);
63 if (knobValue != nullptr)
64 {
65 return wcstoul(knobValue, nullptr, 0);
66 }
67
68 return legacyValue;
69}
70
71DWORD Configuration::GetKnobDWORDValue(LPCWSTR name, DWORD defaultValue)
72{
73 LPCWSTR knobValue = GetConfigurationValue(name);
74 if (knobValue != nullptr)
75 {
76 return wcstoul(knobValue, nullptr, 0);
77 }
78
79 return defaultValue;
80}
81
82ULONGLONG Configuration::GetKnobULONGLONGValue(LPCWSTR name)
83{
84 LPCWSTR knobValue = GetConfigurationValue(name);
85 if (knobValue != nullptr)
86 {
87 return _wcstoui64(knobValue, nullptr, 0);
88 }
89
90 return 0;
91}
92
93LPCWSTR Configuration::GetKnobStringValue(LPCWSTR name, const CLRConfig::ConfigStringInfo& stringInfo)
94{
95 LPCWSTR value = CLRConfig::GetConfigValue(stringInfo);
96 if (value == nullptr)
97 {
98 value = GetConfigurationValue(name);
99 }
100
101 return value;
102}
103
104LPCWSTR Configuration::GetKnobStringValue(LPCWSTR name)
105{
106 return GetConfigurationValue(name);
107}
108
109bool Configuration::GetKnobBooleanValue(LPCWSTR name, const CLRConfig::ConfigDWORDInfo& dwordInfo)
110{
111 bool returnedDefaultValue;
112 DWORD legacyValue = CLRConfig::GetConfigValue(dwordInfo, true /* acceptExplicitDefaultFromRegutil */, &returnedDefaultValue);
113 if (!returnedDefaultValue)
114 {
115 return (legacyValue != 0);
116 }
117
118 LPCWSTR knobValue = GetConfigurationValue(name);
119 if (knobValue != nullptr)
120 {
121 return (wcscmp(knobValue, W("true")) == 0);
122 }
123
124 return (legacyValue != 0);
125}
126
127bool Configuration::GetKnobBooleanValue(LPCWSTR name, bool defaultValue)
128{
129 LPCWSTR knobValue = GetConfigurationValue(name);
130 if (knobValue != nullptr)
131 {
132 return (wcscmp(knobValue, W("true")) == 0);
133 }
134
135 return defaultValue;
136}