up
This commit is contained in:
@@ -36,16 +36,24 @@ func (e Exception) covers(t time.Time) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DB holds the parsed exception list, keyed by upper-cased callsign.
|
||||
// DB holds the parsed exception + prefix tables. Exceptions are keyed by the
|
||||
// full callsign; prefixes are keyed by the prefix string (both may hold several
|
||||
// date-ranged entries). cty.xml carries entity + CQ zone + continent per
|
||||
// record, but NOT ITU zone.
|
||||
type DB struct {
|
||||
exceptions map[string][]Exception
|
||||
date string // cty.xml generation date (for the UI)
|
||||
count int
|
||||
exceptions map[string][]Exception
|
||||
prefixes map[string][]Exception
|
||||
date string // cty.xml generation date (for the UI)
|
||||
count int
|
||||
prefixCount int
|
||||
}
|
||||
|
||||
// Count returns how many exceptions were loaded.
|
||||
func (db *DB) Count() int { return db.count }
|
||||
|
||||
// PrefixCount returns how many prefix records were loaded.
|
||||
func (db *DB) PrefixCount() int { return db.prefixCount }
|
||||
|
||||
// Date returns the cty.xml generation timestamp.
|
||||
func (db *DB) Date() string { return db.date }
|
||||
|
||||
@@ -75,7 +83,7 @@ func LoadGzip(r io.Reader) (*DB, error) {
|
||||
// Load parses a (plain) ClubLog cty.xml stream, extracting only the
|
||||
// <exceptions> section via a streaming decoder (the file is ~10 MB).
|
||||
func Load(r io.Reader) (*DB, error) {
|
||||
db := &DB{exceptions: map[string][]Exception{}}
|
||||
db := &DB{exceptions: map[string][]Exception{}, prefixes: map[string][]Exception{}}
|
||||
dec := xml.NewDecoder(r)
|
||||
for {
|
||||
tok, err := dec.Token()
|
||||
@@ -113,11 +121,69 @@ func Load(r io.Reader) (*DB, error) {
|
||||
}
|
||||
db.exceptions[call] = append(db.exceptions[call], e)
|
||||
db.count++
|
||||
case "prefix":
|
||||
var x xlException // same shape; <call> holds the prefix string
|
||||
if err := dec.DecodeElement(&x, &se); err != nil {
|
||||
continue
|
||||
}
|
||||
pfx := strings.ToUpper(strings.TrimSpace(x.Call))
|
||||
if pfx == "" || x.ADIF == 0 {
|
||||
continue
|
||||
}
|
||||
e := Exception{
|
||||
Call: pfx, Entity: x.Entity, ADIF: x.ADIF, CQZ: x.CQZ,
|
||||
Cont: strings.ToUpper(strings.TrimSpace(x.Cont)),
|
||||
Lat: parseFloat(x.Lat), Lon: parseFloat(x.Long),
|
||||
Start: parseTime(x.Start), End: parseTime(x.End),
|
||||
}
|
||||
db.prefixes[pfx] = append(db.prefixes[pfx], e)
|
||||
db.prefixCount++
|
||||
}
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// ResolvePrefix returns the longest prefix entry matching a callsign and valid
|
||||
// at the given date (cascade step 2). The callsign should already be normalized
|
||||
// (operating affixes stripped); we still strip a trailing "/x" defensively.
|
||||
func (db *DB) ResolvePrefix(call string, date time.Time) (Exception, bool) {
|
||||
if db == nil {
|
||||
return Exception{}, false
|
||||
}
|
||||
c := strings.ToUpper(strings.TrimSpace(call))
|
||||
if i := strings.LastIndex(c, "/"); i > 0 {
|
||||
// Prefer the operating prefix when present (MM/DL1ABC → MM).
|
||||
if pre, post := c[:i], c[i+1:]; len(pre) <= len(post) {
|
||||
c = pre
|
||||
}
|
||||
}
|
||||
for n := len(c); n >= 1; n-- {
|
||||
for _, e := range db.prefixes[c[:n]] {
|
||||
if e.covers(date) {
|
||||
return e, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return Exception{}, false
|
||||
}
|
||||
|
||||
// ResolveFull runs the ClubLog cascade: a full-callsign Exception first, then
|
||||
// the longest valid Prefix. Returns the matched record and its source
|
||||
// ("exception" | "prefix"), or ok=false when ClubLog has nothing (caller falls
|
||||
// back to cty.dat).
|
||||
func (db *DB) ResolveFull(call string, date time.Time) (e Exception, source string, ok bool) {
|
||||
if db == nil {
|
||||
return Exception{}, "", false
|
||||
}
|
||||
if e, ok := db.Resolve(call, date); ok {
|
||||
return e, "exception", true
|
||||
}
|
||||
if e, ok := db.ResolvePrefix(call, date); ok {
|
||||
return e, "prefix", true
|
||||
}
|
||||
return Exception{}, "", false
|
||||
}
|
||||
|
||||
// Resolve returns the exception for a callsign valid at the given date, if any.
|
||||
// It tries the call as-is, then with a trailing "/x" affix stripped (so
|
||||
// VK2/SP9FIH/P still matches the VK2/SP9FIH exception).
|
||||
|
||||
Reference in New Issue
Block a user