FreeCalypso > hg > themwi-system-sw
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 10:960ffce6c542 | 11:1d590563e64b |
|---|---|
| 1 /* | |
| 2 * The library function implemented in this module looks up 4-digit short | |
| 3 * dialing numbers in ThemWi number db to determine their disposition. | |
| 4 */ | |
| 5 | |
| 6 #include <stdio.h> | |
| 7 #include <stdint.h> | |
| 8 #include <stdlib.h> | |
| 9 #include "../include/number_db_file.h" | |
| 10 | |
| 11 extern struct numdb_file_hdr numdb_hdr; | |
| 12 extern struct short_number_map *numdb_short_numbers; | |
| 13 | |
| 14 static int | |
| 15 compare_short_num(p1, p2) | |
| 16 struct short_number_map *p1, *p2; | |
| 17 { | |
| 18 if (p1->short_code < p2->short_code) | |
| 19 return(-1); | |
| 20 else if (p1->short_code > p2->short_code) | |
| 21 return(1); | |
| 22 else | |
| 23 return(0); | |
| 24 } | |
| 25 | |
| 26 lookup_short_dial_number(numstr, nanp_buf) | |
| 27 char *numstr, *nanp_buf; | |
| 28 { | |
| 29 struct short_number_map key, *res; | |
| 30 | |
| 31 if (!numdb_short_numbers || !numdb_hdr.short_number_count) | |
| 32 return(0); | |
| 33 key.short_code = strtoul(numstr, 0, 10); | |
| 34 res = bsearch(&key, numdb_short_numbers, numdb_hdr.short_number_count, | |
| 35 sizeof(struct short_number_map), compare_short_num); | |
| 36 if (!res) | |
| 37 return(0); | |
| 38 if (res->prefix < 200000 || res->prefix > 999999) { | |
| 39 /* either ITN or an invalid record, also treated as an ITN */ | |
| 40 nanp_buf[0] = '\0'; | |
| 41 return(1); | |
| 42 } | |
| 43 sprintf(nanp_buf, "%06u%04u", res->prefix, res->short_code); | |
| 44 return(1); | |
| 45 } |
