/* phone.c: Format and store telephone numbers */ /* Copyright (C) J.G.Harston, 2002-2005 */ /* version 1.00 */ /* char *phone_format(char *string, int zone) on entry: string = raw telephone number string zone = pass as zero in this version on exit: returns formatted telephone number string The passed string is stripped of all any any invalid characters and a telephone number string correctly formatted for the UK telephone numbering system is returned. The formatted telephone number string is returned stored in the memory the passed string is stored in. That memory must have enough space to store 16 characters including terminator. The zone parameter is reserved for future versions to specify the telephone numbering zone to use. */ char *phone_format(char *s, int z) { char *s1, *s2; int n1, n2; s1=s; while (*s1) { if (*s1>='0' && *s1<='9') *s2++=*s1; // Strip out non-digits s1++; } *s2=0; if (z!=0 && z!=44) return s; // Not default or UK if (s[0]!='0') return s; // Not 0xxx if (s[1]=='0') return s; // 00xxx if (strlen(s)!=11) return s; // Not 0xxxxxxxxxx n1=3; n2=5; // 0xxx xxx-xxxx default if (s[1]=='2') { n1=4; n2=5; } // 02x xxxx-xxxx if (s[1]=='1' && s[2]!='1' && s[3]!='1') { n1=0; n2=7; } // 01xxx xxxxxx s1=s+strlen(s)+1; // Point to end of source string s2=s+n1+n2+1; // Point to dest to copy string to while(n2--) *s2--=*s1--; // Copy second component and '\0' terminator if (n1) *s2--='-'; // Hyphenate long numbers while(n1--) *s2--=*s1--; // Copy first component *s2=' '; // Space after dialling code return s; }