[Asterisk-Dev] Algorithm to parse country code

Rob Gagnon rob at networkip.net
Mon May 10 10:39:58 MST 2004


Check the attached (very old) c code snippet I wrote years ago....

You may want to modify it to protect for dropping data into the "buff"
field, as code that used this assumed a char[5] was there.

This works because country codes in the world follow a pattern.... the
first, and/or second digit of the country code will tell you how many digits
total are in the country code.

The input "pattern" to the routine MUST NOT contain the leading
"International Access digits" for the country you are in.  (EXAMPLE:  Do not
have the leading "011" if in the US, or "00/01" in other countries)

watch how the routine, when given something like 52583839337737 will find
and spit back 52 as the country code

ROb

----- Original Message ----- 
From: "Rob Gagnon" <rob at networkip.net>
To: <asterisk-dev at lists.digium.com>
Sent: Monday, May 10, 2004 12:22 PM
Subject: Re: [Asterisk-Dev] Algorithm to parse country code


> There is a subroutine for it as rules exist for how country codes are laid
> out...
>
> Let me find the one I wrote
>
> and you dont need a database or config to do it
>
>
>
> ----- Original Message ----- 
> From: "Robert Bedell" <robert.bedell at zivVa.com>
> To: <asterisk-dev at lists.digium.com>
> Sent: Monday, May 10, 2004 11:15 AM
> Subject: RE: [Asterisk-Dev] Algorithm to parse country code
>
>
> > Hey all,
> >
> > TP> I am trying to parse a dialed extension in order to deduce the
country
> > TP> code - but can't seem to find any references on efficient ways to do
> > TP> this... though surely there is a well known good algorithm for this
> > TP> already?
> >
> > TP> I plan to store my country details in a MySQL database.
Conceptually,
> > TP> i've already thought that I could successfully match a country from
> the
> > TP> dialled extension by reading the countries into an array (or b-tree
or
> > TP> something), and then going through the list several times starting
> with
> > TP> the four digit codes, then three digit ones, then two, then one
etc...
> > TP> until a match is made. I assume that country codes do not overlap in
a
> > TP> way which would make this not possible.
> >
> > TP> Is there a better way to do this? Even if I read all entries and
store
> > TP> them in a tree, I still have to potentially go through the tree four
> > TP> times to reach the single digit country codes (most notably
> USA/Canada)
> > TP> - and every call of the PHP script would search the SQL
> > TP> database/re-populate the tree... surely this would be very expensive
> > TP> computationally?
> >
> > TP> I don't recall a feature in MySQL that allows you to query x number
of
> > TP> characters of a given field only.
> >
> > TP> Anyone have any better ideas?
> >
> > TP> Thanks
> >
> > TP> - Terence
> >
> > MT> DOnt' clearly understood what you want, but maybe you need this?
> >
> > MT> If you have table like:
> >
> > Call this "routes"
> > MT> route   country
> > MT> 1       USA
> > MT> 1212    USA-NY
> > MT> 49      Germany
> >
> > MT> and you have called number like:
> > MT> 12121234567
> >
> > We do something similar here.  Why not use substring on the entire
dialed
> > extension?  If the 'route' is unique in the table above, the first row
of
> > the following query will give you the longest possible match on a given
> > extension:
> >
> >   select *
> >     from routes
> >    where route=substring('12121234567',0,char_length(route))
> >    order by length(route) desc
> >
> > Cheers!
> >
> > Robert
> >
> > _______________________________________________
> > Asterisk-Dev mailing list
> > Asterisk-Dev at lists.digium.com
> > http://lists.digium.com/mailman/listinfo/asterisk-dev
> > To UNSUBSCRIBE or update options visit:
> >    http://lists.digium.com/mailman/listinfo/asterisk-dev
> >
>
> _______________________________________________
> Asterisk-Dev mailing list
> Asterisk-Dev at lists.digium.com
> http://lists.digium.com/mailman/listinfo/asterisk-dev
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-dev
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cc_code.c
Type: application/octet-stream
Size: 1778 bytes
Desc: not available
Url : http://lists.digium.com/pipermail/asterisk-dev/attachments/20040510/a6c6e70f/cc_code.obj


More information about the asterisk-dev mailing list