changeset 235:bd493b21f215

libnumdb2: port check_nanp.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 13:57:10 -0800
parents 44f178901a46
children 38cac1cbe010
files libnumdb2/Makefile libnumdb2/check_nanp.c
diffstat 2 files changed, 47 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libnumdb2/Makefile	Mon Aug 14 13:17:07 2023 -0800
+++ b/libnumdb2/Makefile	Mon Aug 14 13:57:10 2023 -0800
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-OBJS=	readbin.o refresh.o
+OBJS=	check_nanp.o readbin.o refresh.o
 LIB=	libnumdb.a
 
 all:	${LIB}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libnumdb2/check_nanp.c	Mon Aug 14 13:57:10 2023 -0800
@@ -0,0 +1,46 @@
+/*
+ * The library function implemented in this module consults ThemWi number db
+ * to see if a given NANP number is owned by us or not.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "../include/number_db_v2.h"
+
+extern struct numdb_file_hdr numdb_hdr;
+extern struct owned_number_rec *numdb_owned_numbers;
+
+static int
+compare_owned_num(p1, p2)
+	uint16_t *p1, *p2;
+{
+	if (p1[0] < p2[0])
+		return(-1);
+	if (p1[0] > p2[0])
+		return(1);
+	if (p1[1] < p2[1])
+		return(-1);
+	if (p1[1] > p2[1])
+		return(1);
+	if (p1[2] < p2[2])
+		return(-1);
+	if (p1[2] > p2[2])
+		return(1);
+	return(0);
+}
+
+struct owned_number_rec *
+numdb_lookup_nanp(numstr)
+	char *numstr;
+{
+	uint16_t key[3];
+
+	if (!numdb_owned_numbers || !numdb_hdr.owned_number_count)
+		return(0);
+	key[0] = digits3_to_uint16(numstr);
+	key[1] = digits3_to_uint16(numstr + 3);
+	key[2] = digits4_to_uint16(numstr + 6);
+	return bsearch(key, numdb_owned_numbers, numdb_hdr.owned_number_count,
+			sizeof(struct owned_number_rec), compare_owned_num);
+}