Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PhoneCountryCodes.cs
1using System;
3using System.IO;
4using System.Xml;
6using Waher.Events;
8
10{
14 public static class PhoneCountryCodes
15 {
16 private static readonly SortedDictionary<string, string[]> countriesByCode = new SortedDictionary<string, string[]>(StringComparer.InvariantCultureIgnoreCase);
17 private static readonly SortedDictionary<string, string[]> codesByCountry = new SortedDictionary<string, string[]>(StringComparer.InvariantCultureIgnoreCase);
18
19 internal static void Load()
20 {
21 try
22 {
23 if (!string.IsNullOrEmpty(Gateway.AppDataFolder))
24 {
25 XmlDocument Doc = XML.LoadFromFile(Path.Combine(Gateway.AppDataFolder, "PhoneCountryCodes.xml"));
26
27 foreach (XmlNode N in Doc.DocumentElement.ChildNodes)
28 {
29 if (N is XmlElement E && E.LocalName == "Entry")
30 {
31 string Code = XML.Attribute(E, "code");
32 string Country = XML.Attribute(E, "country");
33
34 if (!string.IsNullOrEmpty(Code) && !string.IsNullOrEmpty(Country))
35 {
36 Add(countriesByCode, Code, Country);
37 Add(codesByCountry, Country, Code);
38 }
39 }
40 }
41 }
42 }
43 catch (Exception ex)
44 {
45 Log.Exception(ex);
46 }
47 }
48
49 private static void Add(SortedDictionary<string, string[]> List, string Key, string Value)
50 {
51 if (List.TryGetValue(Key, out string[] Values))
52 {
53 int c = Values.Length;
54 Array.Resize(ref Values, c + 1);
55 Values[c] = Value;
56 List[Key] = Values;
57 }
58 else
59 List[Key] = new string[] { Value };
60 }
61
65 public static string[] Codes
66 {
67 get
68 {
69 string[] Result = new string[countriesByCode.Count];
70 countriesByCode.Keys.CopyTo(Result, 0);
71 return Result;
72 }
73 }
74
78 public static string[] Countries
79 {
80 get
81 {
82 string[] Result = new string[codesByCountry.Count];
83 codesByCountry.Keys.CopyTo(Result, 0);
84 return Result;
85 }
86 }
87
95 public static bool TryGetCountries(string Code, out string[] Countries)
96 {
97 return countriesByCode.TryGetValue(Code, out Countries);
98 }
99
107 public static bool TryGetCodes(string Country, out string[] Codes)
108 {
109 return codesByCountry.TryGetValue(Country, out Codes);
110 }
111
118 public static bool TryGetShortestCode(string Country, out string Code)
119 {
120 Code = string.Empty;
121
122 if (!TryGetCodes(Country, out string[] Codes))
123 return false;
124
125 int Best = int.MaxValue;
126 int c;
127
128 foreach (string Code2 in Codes)
129 {
130 c = Code2.Length;
131 if (c < Best)
132 {
133 Best = c;
134 Code = Code2;
135 }
136 }
137
138 return !string.IsNullOrEmpty(Code);
139 }
140 }
141}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
static XmlDocument LoadFromFile(string FileName)
Loads an XML document from a file.
Definition: XML.cs:1808
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static string AppDataFolder
Application data folder.
Definition: Gateway.cs:3132