better match dxcc
This commit is contained in:
67
xml.go
67
xml.go
@ -5,6 +5,8 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Countries struct {
|
||||
@ -43,15 +45,24 @@ type CountryPrefix struct {
|
||||
EndDate string `xml:"EndDate"`
|
||||
}
|
||||
|
||||
type DXCC struct {
|
||||
Callsign string
|
||||
CountryName string
|
||||
DXCC string
|
||||
RegEx string
|
||||
RegExSplit []string
|
||||
RegExCharacters int
|
||||
Ended bool
|
||||
}
|
||||
|
||||
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)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
Log.Debugln("Successfully loaded country.xml")
|
||||
// defer the closing of our xmlFile so that we can parse it later on
|
||||
defer xmlFile.Close()
|
||||
|
||||
@ -65,14 +76,58 @@ func LoadCountryFile() Countries {
|
||||
}
|
||||
|
||||
func GetDXCC(dxCall string, Countries Countries) string {
|
||||
DXCCList := []DXCC{}
|
||||
d := DXCC{}
|
||||
|
||||
// Get all the matching DXCC for current callsign
|
||||
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[len(Countries.Countries[i].CountryPrefixList.CountryPrefixList)-1].PrefixList)
|
||||
|
||||
match := regExp.FindStringSubmatch(dxCall)
|
||||
if len(match) != 0 {
|
||||
return Countries.Countries[i].Dxcc
|
||||
|
||||
d = DXCC{
|
||||
Callsign: dxCall,
|
||||
CountryName: Countries.Countries[i].CountryName,
|
||||
DXCC: Countries.Countries[i].Dxcc,
|
||||
RegEx: Countries.Countries[i].CountryPrefixList.CountryPrefixList[len(Countries.Countries[i].CountryPrefixList.CountryPrefixList)-1].PrefixList,
|
||||
}
|
||||
|
||||
if Countries.Countries[i].CountryPrefixList.CountryPrefixList[len(Countries.Countries[i].CountryPrefixList.CountryPrefixList)-1].EndDate == "" {
|
||||
d.Ended = false
|
||||
} else {
|
||||
d.Ended = true
|
||||
}
|
||||
|
||||
DXCCList = append(DXCCList, d)
|
||||
}
|
||||
// }
|
||||
}
|
||||
return ""
|
||||
|
||||
for i := 0; i < len(DXCCList); i++ {
|
||||
DXCCList[i].RegExSplit = strings.Split(DXCCList[i].RegEx, "|")
|
||||
|
||||
for j := 0; j < len(DXCCList[i].RegExSplit); j++ {
|
||||
regExp := regexp.MustCompile(DXCCList[i].RegExSplit[j])
|
||||
matched := regExp.FindStringSubmatch(dxCall)
|
||||
if len(matched) > 0 {
|
||||
DXCCList[i].RegExCharacters = utf8.RuneCountInString(DXCCList[i].RegExSplit[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DXCCMatch := DXCCList[0]
|
||||
higherMatch := DXCCList[0].RegExCharacters
|
||||
|
||||
if len(DXCCList) > 1 {
|
||||
for i := 0; i < len(DXCCList); i++ {
|
||||
if DXCCList[i].RegExCharacters > higherMatch && !DXCCList[i].Ended {
|
||||
DXCCMatch = DXCCList[i]
|
||||
higherMatch = DXCCList[i].RegExCharacters
|
||||
}
|
||||
}
|
||||
} else {
|
||||
DXCCMatch = DXCCList[0]
|
||||
}
|
||||
|
||||
return DXCCMatch.DXCC
|
||||
}
|
||||
|
Reference in New Issue
Block a user