diff libnumdb/check_short.c @ 11:1d590563e64b

libnumdb: implement short number lookup
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Jun 2022 13:51:12 -0800
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libnumdb/check_short.c	Sun Jun 26 13:51:12 2022 -0800
@@ -0,0 +1,45 @@
+/*
+ * The library function implemented in this module looks up 4-digit short
+ * dialing numbers in ThemWi number db to determine their disposition.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "../include/number_db_file.h"
+
+extern struct numdb_file_hdr numdb_hdr;
+extern struct short_number_map *numdb_short_numbers;
+
+static int
+compare_short_num(p1, p2)
+	struct short_number_map *p1, *p2;
+{
+	if (p1->short_code < p2->short_code)
+		return(-1);
+	else if (p1->short_code > p2->short_code)
+		return(1);
+	else
+		return(0);
+}
+
+lookup_short_dial_number(numstr, nanp_buf)
+	char *numstr, *nanp_buf;
+{
+	struct short_number_map key, *res;
+
+	if (!numdb_short_numbers || !numdb_hdr.short_number_count)
+		return(0);
+	key.short_code = strtoul(numstr, 0, 10);
+	res = bsearch(&key, numdb_short_numbers, numdb_hdr.short_number_count,
+			sizeof(struct short_number_map), compare_short_num);
+	if (!res)
+		return(0);
+	if (res->prefix < 200000 || res->prefix > 999999) {
+		/* either ITN or an invalid record, also treated as an ITN */
+		nanp_buf[0] = '\0';
+		return(1);
+	}
+	sprintf(nanp_buf, "%06u%04u", res->prefix, res->short_code);
+	return(1);
+}