// Spludlow Software
// Copyright © Samuel P. Ludlow 2020 All Rights Reserved
// Distributed under the terms of the GNU General Public License version 3
// Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
// https://www.spludlow.co.uk/LICENCE.TXT
// The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission
// v1.14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data;
namespace Spludlow
{
// country countries
// How to prepare the data file if you need to update to latest (Tested using Edge broswer)
//
// Goto https://www.iso.org/iso-3166-country-codes.html
// Goto Online Browsing Platform https://www.iso.org/obp/ui/#search
// change to 300 results per page
// CTRL+A, CTRL-C (copy whole web page)
// Excel CTRL-V (paste to empty excel)
// CTRL+A, CTRL-C (copy all from excel)
// Notepad CTRL-V (paste to empty notepad)
// CTRL+A, CTRL-C (copy all from notepad)
// Excel CTRL-V (paste to empty excel) Now in easy to read format
// Select relevent data Top Left:"English short name" -> Bottom Right:"716" (Zimbabwe)
// CTRL-C (copy selection from excel)
// Notepad CTRL-V (paste to empty notepad)
// Change 1 line:
// English short name French short name Alpha-2 code Alpha-3 code Numeric
// To 2 Lines (tab delimiters):
// EnglishName FrenchName Alpha2 Alpha3 Numeric
// String String String String Int32
// Save as (Encoding UTF-8) C:\ProgramData\SpludlowV1\Data\ISO3166.txt
// Bingo
///
/// Simple lookup for ISO3166 country codes to english names
///
public class ISO3166
{
private static DataTable Table;
private static Dictionary Alpha2EnglishNameLookup = new Dictionary(StringComparer.InvariantCultureIgnoreCase);
private static Dictionary Alpha3EnglishNameLookup = new Dictionary(StringComparer.InvariantCultureIgnoreCase);
private static Dictionary NumericEnglishNameLookup = new Dictionary();
static ISO3166()
{
string filename = Spludlow.Config.ProgramData + @"\Data\ISO3166.txt";
if (File.Exists(filename) == false)
throw new ApplicationException("ISO3166; Data file not found: " + filename);
Table = Spludlow.Data.TextTable.ReadFile(filename, Encoding.UTF8);
foreach (DataRow row in Table.Rows)
{
string englishName = (string)row["EnglishName"];
Alpha2EnglishNameLookup.Add((string)row["Alpha2"], englishName);
Alpha3EnglishNameLookup.Add((string)row["Alpha3"], englishName);
NumericEnglishNameLookup.Add((int)row["Numeric"], englishName);
}
}
public static string Alpha2EnglishName(string alpha2Code)
{
if (Alpha2EnglishNameLookup.ContainsKey(alpha2Code) == false)
return null;
return Alpha2EnglishNameLookup[alpha2Code];
}
public static string Alpha3EnglishName(string alpha3Code)
{
if (Alpha3EnglishNameLookup.ContainsKey(alpha3Code) == false)
return null;
return Alpha3EnglishNameLookup[alpha3Code];
}
public static string NumericEnglishName(int numericCode)
{
if (NumericEnglishNameLookup.ContainsKey(numericCode) == false)
return null;
return NumericEnglishNameLookup[numericCode];
}
}
}