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;
2using System.Collections.Generic;
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 XmlDocument Doc = new XmlDocument();
24 Doc.Load(Path.Combine(Gateway.AppDataFolder, "PhoneCountryCodes.xml"));
25
26 foreach (XmlNode N in Doc.DocumentElement.ChildNodes)
27 {
28 if (N is XmlElement E && E.LocalName == "Entry")
29 {
30 string Code = XML.Attribute(E, "code");
31 string Country = XML.Attribute(E, "country");
32
33 if (!string.IsNullOrEmpty(Code) && !string.IsNullOrEmpty(Country))
34 {
35 Add(countriesByCode, Code, Country);
36 Add(codesByCountry, Country, Code);
37 }
38 }
39 }
40 }
41 catch (Exception ex)
42 {
43 Log.Exception(ex);
44 }
45 }
46
47 private static void Add(SortedDictionary<string, string[]> List, string Key, string Value)
48 {
49 if (List.TryGetValue(Key, out string[] Values))
50 {
51 int c = Values.Length;
52 Array.Resize(ref Values, c + 1);
53 Values[c] = Value;
54 List[Key] = Values;
55 }
56 else
57 List[Key] = new string[] { Value };
58 }
59
63 public static string[] Codes
64 {
65 get
66 {
67 string[] Result = new string[countriesByCode.Count];
68 countriesByCode.Keys.CopyTo(Result, 0);
69 return Result;
70 }
71 }
72
76 public static string[] Countries
77 {
78 get
79 {
80 string[] Result = new string[codesByCountry.Count];
81 codesByCountry.Keys.CopyTo(Result, 0);
82 return Result;
83 }
84 }
85
93 public static bool TryGetCountries(string Code, out string[] Countries)
94 {
95 return countriesByCode.TryGetValue(Code, out Countries);
96 }
97
105 public static bool TryGetCodes(string Country, out string[] Codes)
106 {
107 return codesByCountry.TryGetValue(Country, out Codes);
108 }
109
116 public static bool TryGetShortestCode(string Country, out string Code)
117 {
118 Code = string.Empty;
119
120 if (!TryGetCodes(Country, out string[] Codes))
121 return false;
122
123 int Best = int.MaxValue;
124 int c;
125
126 foreach (string Code2 in Codes)
127 {
128 c = Code2.Length;
129 if (c < Best)
130 {
131 Best = c;
132 Code = Code2;
133 }
134 }
135
136 return !string.IsNullOrEmpty(Code);
137 }
138 }
139}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static string AppDataFolder
Application data folder.
Definition: Gateway.cs:2369