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 using System.Globalization;
6 
7 namespace System
8 {
9     internal static class AppContextConfigHelper
10     {
GetInt32Config(string configName, int defaultValue, bool allowNegative = true)11         internal static int GetInt32Config(string configName, int defaultValue, bool allowNegative = true)
12         {
13             try
14             {
15                 object config = AppContext.GetData(configName);
16                 int result = defaultValue;
17                 switch (config)
18                 {
19                     case string str:
20                         if (str.StartsWith("0x"))
21                         {
22                             result = Convert.ToInt32(str, 16);
23                         }
24                         else if (str.StartsWith("0"))
25                         {
26                             result = Convert.ToInt32(str, 8);
27                         }
28                         else
29                         {
30                             result = int.Parse(str, NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo);
31                         }
32                         break;
33                     case IConvertible convertible:
34                         result = convertible.ToInt32(NumberFormatInfo.InvariantInfo);
35                         break;
36                 }
37                 return !allowNegative && result < 0 ? defaultValue : result;
38             }
39             catch (FormatException)
40             {
41                 return defaultValue;
42             }
43             catch (OverflowException)
44             {
45                 return defaultValue;
46             }
47         }
48 
49 
GetInt16Config(string configName, short defaultValue, bool allowNegative = true)50         internal static short GetInt16Config(string configName, short defaultValue, bool allowNegative = true)
51         {
52             try
53             {
54                 object config = AppContext.GetData(configName);
55                 short result = defaultValue;
56                 switch (config)
57                 {
58                     case string str:
59                         if (str.StartsWith("0x"))
60                         {
61                             result = Convert.ToInt16(str, 16);
62                         }
63                         else if (str.StartsWith("0"))
64                         {
65                             result = Convert.ToInt16(str, 8);
66                         }
67                         else
68                         {
69                             result = short.Parse(str, NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo);
70                         }
71                         break;
72                     case IConvertible convertible:
73                         result = convertible.ToInt16(NumberFormatInfo.InvariantInfo);
74                         break;
75                 }
76                 return !allowNegative && result < 0 ? defaultValue : result;
77             }
78             catch (FormatException)
79             {
80                 return defaultValue;
81             }
82             catch (OverflowException)
83             {
84                 return defaultValue;
85             }
86         }
87     }
88 }
89