FreeCalypso > hg > themwi-nanp
comparison libnumdb/check_nanp.c @ 1:6534965175dd
libnumdb ported over
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Wed, 13 Dec 2023 00:53:04 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:159dd90eeafe | 1:6534965175dd |
|---|---|
| 1 /* | |
| 2 * The library function implemented in this module consults ThemWi number db | |
| 3 * to see if a given NANP number is owned by us or not. | |
| 4 */ | |
| 5 | |
| 6 #include <stdio.h> | |
| 7 #include <stdint.h> | |
| 8 #include <stdlib.h> | |
| 9 | |
| 10 #include <themwi/nanp/number_db_v2.h> | |
| 11 #include <themwi/nanp/number_lookup.h> | |
| 12 #include <themwi/nanp/number_utils.h> | |
| 13 | |
| 14 extern struct numdb_file_hdr numdb_hdr; | |
| 15 extern struct owned_number_rec *numdb_owned_numbers; | |
| 16 | |
| 17 static int compare_owned_num(const void *p1v, const void *p2v) | |
| 18 { | |
| 19 const uint16_t *p1 = p1v, *p2 = p2v; | |
| 20 | |
| 21 if (p1[0] < p2[0]) | |
| 22 return(-1); | |
| 23 if (p1[0] > p2[0]) | |
| 24 return(1); | |
| 25 if (p1[1] < p2[1]) | |
| 26 return(-1); | |
| 27 if (p1[1] > p2[1]) | |
| 28 return(1); | |
| 29 if (p1[2] < p2[2]) | |
| 30 return(-1); | |
| 31 if (p1[2] > p2[2]) | |
| 32 return(1); | |
| 33 return(0); | |
| 34 } | |
| 35 | |
| 36 const struct owned_number_rec *numdb_lookup_nanp(const char *numstr) | |
| 37 { | |
| 38 uint16_t key[3]; | |
| 39 | |
| 40 if (!numdb_owned_numbers || !numdb_hdr.owned_number_count) | |
| 41 return(0); | |
| 42 key[0] = digits3_to_uint16(numstr); | |
| 43 key[1] = digits3_to_uint16(numstr + 3); | |
| 44 key[2] = digits4_to_uint16(numstr + 6); | |
| 45 return bsearch(key, numdb_owned_numbers, numdb_hdr.owned_number_count, | |
| 46 sizeof(struct owned_number_rec), compare_owned_num); | |
| 47 } |
