+# check that a field only has callsign characters in it
+sub is_callsign
+{
+ return $_[0] =~ m!^
+ (?:\d?[A-Z]{1,2}\d{0,2}/)? # out of area prefix /
+ (?:\d?[A-Z]{1,2}\d{1,5}) # main prefix one (required) - lengthened for special calls
+ [A-Z]{1,8} # callsign letters (required)
+ (?:-(?:\d{1,2}))? # - nn possibly (eg G8BPQ-8)
+ (?:/[0-9A-Z]{1,7})? # / another prefix, callsign or special label (including /MM, /P as well as /EURO or /LGT) possibly
+ (?:/(?:AM?|MM?|P))? # finally /A /AM /M /MM /P
+ $!xo;
+
+ # longest callign allowed is 1X11/1Y11XXXXX-11/XXXXXXX/MM
+}
+
+sub is_prefix
+{
+ return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}}\d+)!x # basic prefix
+}
+
+
+# check that a PC protocol field is valid text
+sub is_pctext
+{
+ return undef unless length $_[0];
+ return undef if $_[0] =~ /[\x00-\x08\x0a-\x1f\x80-\x9f]/;
+ return 1;
+}
+
+# check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
+sub is_pcflag
+{
+ return $_[0] =~ /^[01\*\-]+$/;
+}
+
+# check that a thing is a frequency
+sub is_freq
+{
+ return $_[0] =~ /^\d+(?:\.\d+)?$/;
+}
+
+# check that a thing is just digits
+sub is_digits
+{
+ return $_[0] =~ /^[\d]+$/;
+}
+
+# does it look like a qra locator?
+sub is_qra
+{
+ return unless length $_[0] == 4 || length $_[0] == 6;
+ return $_[0] =~ /^[A-Ra-r][A-Ra-r]\d\d(?:[A-Xa-x][A-Xa-x])?$/;
+}
+
+# does it look like a valid lat/long
+sub is_latlong
+{
+ return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+1?\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
+}
+
+# is it an ip address?
+sub is_ipaddr
+{
+ return $_[0] =~ /^(?:(?:\:\:)?\d+\.\d+\.\d+\.\d+)|(?:[0-9a-f]{1,4}\:)?(?:\:[0-9a-f]{1,4}){1,6}$/i;
+}
+
+# is it a zulu time hhmmZ
+sub is_ztime
+{
+ return $_[0] =~ /^(?:(?:2[0-3])|(?:[01][0-9]))[0-5][0-9]Z$/;
+}
+
+# insert an item into a list if it isn't already there returns 1 if there 0 if not
+sub insertitem
+{
+ my $list = shift;
+ my $item = shift;
+
+ return 1 if grep {$_ eq $item } @$list;
+ push @$list, $item;
+ return 0;
+}
+
+# delete an item from a list if it is there returns no deleted
+sub deleteitem
+{
+ my $list = shift;
+ my $item = shift;
+ my $n = @$list;
+
+ @$list = grep {$_ ne $item } @$list;
+ return $n - @$list;
+}
+
+# find the correct local_data directory
+# basically, if there is a local_data directory with this filename and it is younger than the
+# equivalent one in the (system) data directory then return that name rather than the system one
+sub localdata
+{
+ my $ifn = shift;
+ my $lfn = "$main::local_data/$ifn";
+ my $dfn = "$main::data/$ifn";
+
+ if (-e "$main::local_data") {
+ if ((-e $dfn) && (-e $lfn)) {
+ $lfn = $dfn if -M $dfn < -M $lfn;
+ } else {
+ $lfn = $dfn if -e $dfn;
+ }
+ } else {
+ $lfn = $dfn;
+ }
+
+ return $lfn;
+}
+
+# move a file or a directory from data -> local_data if isn't there already
+sub localdata_mv
+{
+ my $ifn = shift;
+ if (-e "$main::data/$ifn" ) {
+ unless (-e "$main::local_data/$ifn") {
+ move("$main::data/$ifn", "$main::local_data/$ifn") or die "localdata_mv: cannot move $ifn from '$main::data' -> '$main::local_data' $!\n";
+ }
+ }
+}
+
+# measure the time taken for something to happen; use Time::HiRes qw(gettimeofday tv_interval);
+sub _diffms
+{
+ my $ta = shift;
+ my $tb = shift || [gettimeofday];
+ my $a = int($ta->[0] * 1000) + int($ta->[1] / 1000);
+ my $b = int($tb->[0] * 1000) + int($tb->[1] / 1000);
+ return $b - $a;
+}