Adding callsign validation

pull/2/head
Jean-Marc MEESSEN 4 years ago
parent 913f77cb46
commit c8fe301c85

@ -0,0 +1,62 @@
package cmd
/*
Copyright © 2020 Jean-Marc Meessen, ON4KJM <on4kjm@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"regexp"
"strings"
)
var validCallRegexp = regexp.MustCompile(`[\d]{0,1}[A-Z]{1,2}\d([A-Z]{1,4}|\d{3,3}|\d{1,3}[A-Z])[A-Z]{0,5}`)
var validPrefixRegexp = regexp.MustCompile(`\A\d?[a-zA-Z]{1,2}$`)
// ValidateCall veriffies whether the supplied string is a valid callsign.
// prefix and suffix are not checked for validity
// If it is not valid, the supicious string is prefixed with a * and an erroMsg is genrated.
func ValidateCall(sign string) (call, errorMsg string) {
sign = strings.ToUpper(strings.TrimSpace(sign))
sp := strings.Split(sign, "/")
wrongSign := "*" + sign
switch len(sp) {
case 1:
if validCallRegexp.MatchString(sign) {
return sign, ""
} else {
return wrongSign, "Invalid call"
}
case 2:
// some ambiguity here we need to resolve, could be a prefix or a suffix
if validCallRegexp.MatchString(sp[0]) {
//Callisign with suffix (unchecked)
return sign, ""
} else {
//Callsign with prefix
//validate the part that should contain the call (sp[1])
if !validCallRegexp.MatchString(sp[1]) {
return wrongSign, "Invalid call"
}
//validate the prefix
if !validPrefixRegexp.MatchString(sp[0]) {
return wrongSign, "Invalid prefix"
}
return sign, ""
}
case 3:
return sign, "??????"
}
return sign, "?????"
}

@ -22,7 +22,7 @@ import (
"log"
"os"
"regexp"
"strings"
//"strings"
)
// loadCmd represents the load command
@ -79,13 +79,16 @@ func loadFile() {
regexpStartMultiLineComment, _ := regexp.Compile("^{")
regexpEndMultiLineComment, _ := regexp.Compile("}$")
regexpHeaderMyCall, _ := regexp.Compile("(?i)^mycall ")
// regexpHeaderOperator, _ := regexp.Compile("(?i)^operator ")
regexpHeaderOperator, _ := regexp.Compile("(?i)^operator ")
// regexpHeaderMyWwff, _ := regexp.Compile("(?i)^mywwff ")
// regexpHeaderMySota, _ := regexp.Compile("(?i)^mysota ")
// regexpHeaderQslMsg, _ := regexp.Compile("(?i)^qslmsg ")
// regexpHeaderNickname, _ := regexp.Compile("(?i)^nickname ")
// regexpHeaderDate, _ := regexp.Compile("(?i)^date ")
headerMyCall := ""
headerOperator := ""
var isInMultiLine = false
@ -125,19 +128,28 @@ func loadFile() {
// ****
if(regexpHeaderMyCall.MatchString(eachline)) {
myCallList := regexpHeaderMyCall.Split(eachline,-1)
myCall := ""
errorMsg := ""
myCallList := regexpHeaderMyCall.Split(eachline,-1)
if(len(myCallList[1]) > 0) {
myCall = strings.ToUpper(myCallList[1])
headerMyCall, errorMsg = ValidateCall(myCallList[1])
}
fmt.Println("my call: ", myCall)
fmt.Println("my call: ", headerMyCall, "-", errorMsg)
continue
}
if(regexpHeaderOperator.MatchString(eachline)) {
errorMsg := ""
myOperatorList := regexpHeaderOperator.Split(eachline,-1)
if(len(myOperatorList[1]) > 0) {
headerOperator, errorMsg = ValidateCall(myOperatorList[1])
}
fmt.Println("Operator: ", headerOperator, "-", errorMsg)
continue
}
// ****
// ** Process the data block
// ****
fmt.Println(eachline)
}
}
}

Loading…
Cancel
Save