view libnumdb/check_nanp.c @ 14:78319ed870dc

convert to new ThemWi configure and build system
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 21 May 2024 00:15:24 +0000
parents 6534965175dd
children
line wrap: on
line source

/*
 * 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 <themwi/nanp/number_db_v2.h>
#include <themwi/nanp/number_lookup.h>
#include <themwi/nanp/number_utils.h>

extern struct numdb_file_hdr numdb_hdr;
extern struct owned_number_rec *numdb_owned_numbers;

static int compare_owned_num(const void *p1v, const void *p2v)
{
	const uint16_t *p1 = p1v, *p2 = p2v;

	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);
}

const struct owned_number_rec *numdb_lookup_nanp(const 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);
}