Is there any ANSI method to extract/tokenize argv[ ]

N

Nitin

Hi All,

Is there any ANSI library ( header file ) which contains functions to
extract or tokenize arguments of main( ) ?

We have strtok( ), but thats non re-entrant, as it maintains static
buffers !
We have getopts( ) on UNIX, but not on every flavor !

Do we have any such library which is ANSI, so that it is available
across all platforms ?

Thanks.
Nitin
 
N

Nitin

Emmanuel said:
Nitin wrote on 16/07/05 :

They are already tokenized. The parameters are int argcount char
**argvalues. What do you need more ?


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

.sig under repair

I mean the following:
suppose my program exxpects different switches like below:

myprog -k 20 -m str1 -v 45.56
OR
myprog -k20 -mstr1 -v45.56

So, how do I extract switches and their corresponding values, since
these switches can be in any given order and in diff positions in
argv[].
As in the first case argv[] will be as follows:

argv[1] = "-k"
argv[2] = "20"
argv[3] = "-m"
argv[4] = "str1"
and so on....

while in the second case argv[] will be as follows:
argv[1] = "-k20"
argv[2] = "-mstr1"
argv[3] = "-v45.56"

So, is there any standard way to parse argv[] ?

Actually the right term to use was 'parse' rather than tokenize, sorry
for poorly-phrasing the Q :(

Thanks.
Nitin
 
E

Emmanuel Delahaye

Nitin wrote on 16/07/05 :
suppose my program expects different switches like below:

myprog -k 20 -m str1 -v 45.56
OR
myprog -k20 -mstr1 -v45.56

Ok. Unix/Linux getopt() does that. If you have the source (see GNU
Linux sources) you can port it easily. There is no reason for such a
code not to be portable.

Btw, it is easy to write such a code too, and probably a good
exercise...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
A

Alexei A. Frounze

Nitin said:
I mean the following:
suppose my program exxpects different switches like below:

myprog -k 20 -m str1 -v 45.56
OR
myprog -k20 -mstr1 -v45.56

So, how do I extract switches and their corresponding values, since
these switches can be in any given order and in diff positions in
argv[].
As in the first case argv[] will be as follows:

argv[1] = "-k"
argv[2] = "20"
argv[3] = "-m"
argv[4] = "str1"
and so on....

while in the second case argv[] will be as follows:
argv[1] = "-k20"
argv[2] = "-mstr1"
argv[3] = "-v45.56"

So, is there any standard way to parse argv[] ?

I believe there's no standard way because it is you to define this standard.
I mean, it is you who defines the switches, their parameters (text, bumbers,
with spaces or w/o) and the order.
However, getopt() from <unistd.h> may help.

Alex
 
C

Chris Croughton

Is there any ANSI library ( header file ) which contains functions to
extract or tokenize arguments of main( ) ?

We have strtok( ), but thats non re-entrant, as it maintains static
buffers !
We have getopts( ) on UNIX, but not on every flavor !

Do we have any such library which is ANSI, so that it is available
across all platforms ?

There isn't one specified by the C standard, but I wrote my own
(including getopt_long from the POSIX specifications). It's released
under the zlib licence (basiocally, do anything you like but don't
misrepresent the origin) and was written "clean room" (not looking at
any existing code, only the published interface and "black box" testing
of boundary conditions against some existing implementations).

I wrote it with the intention that it should be fully portable, although
I haven't tested it with many compilers or systems so if you find
problems please let me know. It isn't quite complete as a POSIX
implementation yet, but it is sufficient for all programs where I've
used it (in particular it doesn't handle the POSIX selection of argument
order). And testing them just now I found a couple of interesting
anomalies (I'll have to look at the POSIX spec. again to see what is
supposed to happen, they may be correct but unexpected features of that
spec.).

http://www.keristor.net/stuff/xgetopt.h
http://www.keristor.net/stuff/xgetopt.c

Chris C
 
K

Keith Thompson

Nitin said:
I mean the following:
suppose my program exxpects different switches like below:

myprog -k 20 -m str1 -v 45.56
OR
myprog -k20 -mstr1 -v45.56

So, how do I extract switches and their corresponding values, since
these switches can be in any given order and in diff positions in
argv[].
As in the first case argv[] will be as follows:

argv[1] = "-k"
argv[2] = "20"
argv[3] = "-m"
argv[4] = "str1"
and so on....

while in the second case argv[] will be as follows:
argv[1] = "-k20"
argv[2] = "-mstr1"
argv[3] = "-v45.56"

So, is there any standard way to parse argv[] ?

Actually the right term to use was 'parse' rather than tokenize, sorry
for poorly-phrasing the Q :(

There are no standard C functions that will do this for you, though of
course all the building blocks are there.

There's also no standard syntax for command-line arguments. What
you've shown is Unix-specific. Different systems use different
styles, such as '/' rather than '-', and full words rather than single
letters for option names.

Various versions of "getopt" or "getopts" exist in open source,
implemented in portable C. A bit of Googling should turn up an
implementation that you can incorporate into your program on systems
that don't provide it natively.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top