update dxcc
This commit is contained in:
78
xml.go
Normal file
78
xml.go
Normal file
@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Countries struct {
|
||||
XMLName xml.Name `xml:"Countries"`
|
||||
Countries []Country `xml:"Country"`
|
||||
}
|
||||
|
||||
type Country struct {
|
||||
XMLName xml.Name `xml:"Country"`
|
||||
ArrlPrefix string `xml:"ArrlPrefix"`
|
||||
Comment string `xml:"Comment"`
|
||||
Continent string `xml:"Continent"`
|
||||
CountryName string `xml:"CountryName"`
|
||||
CqZone string `xml:"CqZone"`
|
||||
CqZoneList string `xml:"CqZoneList"`
|
||||
Dxcc string `xml:"Dxcc"`
|
||||
ItuZone string `xml:"ItuZone"`
|
||||
IaruRegion string `xml:"IaruRegion"`
|
||||
ItuZoneList string `xml:"ItuZoneList"`
|
||||
Latitude string `xml:"Latitude"`
|
||||
Longitude string `xml:"Longitude"`
|
||||
Active string `xml:"Active"`
|
||||
CountryTag string `xml:"CountryTag"`
|
||||
CountryPrefixList CountryPrefixList `xml:"CountryPrefixList"`
|
||||
}
|
||||
|
||||
type CountryPrefixList struct {
|
||||
XMLName xml.Name `xml:"CountryPrefixList"`
|
||||
CountryPrefixList []CountryPrefix `xml:"CountryPrefix"`
|
||||
}
|
||||
|
||||
type CountryPrefix struct {
|
||||
XMLName xml.Name `xml:"CountryPrefix"`
|
||||
PrefixList string `xml:"PrefixList"`
|
||||
StartDate string `xml:"StartDate"`
|
||||
EndDate string `xml:"EndDate"`
|
||||
}
|
||||
|
||||
func LoadCountryFile() Countries {
|
||||
// Open our xmlFile
|
||||
xmlFile, err := os.Open("country.xml")
|
||||
// if we os.Open returns an error then handle it
|
||||
if err != nil {
|
||||
Log.Errorln(err)
|
||||
}
|
||||
|
||||
Log.Infoln("Successfully loaded country.xml")
|
||||
// defer the closing of our xmlFile so that we can parse it later on
|
||||
defer xmlFile.Close()
|
||||
|
||||
// read our opened xmlFile as a byte array.
|
||||
byteValue, _ := io.ReadAll(xmlFile)
|
||||
|
||||
var countries Countries
|
||||
|
||||
xml.Unmarshal(byteValue, &countries)
|
||||
return countries
|
||||
}
|
||||
|
||||
func GetDXCC(dxCall string, Countries Countries) string {
|
||||
for i := 0; i < len(Countries.Countries); i++ {
|
||||
for j := 0; j < len(Countries.Countries[i].CountryPrefixList.CountryPrefixList); j++ {
|
||||
regExp := regexp.MustCompile(Countries.Countries[i].CountryPrefixList.CountryPrefixList[j].PrefixList)
|
||||
match := regExp.FindStringSubmatch(dxCall)
|
||||
if len(match) != 0 {
|
||||
return Countries.Countries[i].Dxcc
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
Reference in New Issue
Block a user