Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to parse a string like C program parse the command line string?

Reply
Thread Tools

How to parse a string like C program parse the command line string?

 
 
linzhenhua1205@163.com
Guest
Posts: n/a
 
      03-12-2005
I want to parse a string like C program parse the command line into
argc & argv[][].
I hope don't use the array the allocate a fix memory first, and don't
use the memory allocate function like malloc.
who can give me some ideas?
The following is my program, but it has some problem. I hope someone
would correct it.
////////////////////////////
//Test_ConvertArg.c
////////////////////////////
#include <stdio.h>
#include <string.h>

int ConvertArg(char* Str, char* Argv[])
{
int Argc; // Count the argument).
int Count = 0;
int i = 0;
char* StrPtr;
char* TmpStrPtr;
StrPtr = Str;

for(; *StrPtr == ' ' // ignore the whitespace before the
command!
{
++StrPtr;
}

TmpStrPtr = StrPtr;

for (Argc = 0; (*StrPtr) != '\n'; StrPtr++, Count++)
{

if(*StrPtr == ' ')
{
// if exist multi whitespace together,
//argc just count once,and don't continually change argv!
if( *(StrPtr+1) == ' ')
{
Count--;
continue;
}
}

Count--;
// the following setences have problems.
memcpy(Argv[Argc],TmpStrPtr,Count);

#if 0
for(i=0; i <= Count; i++)
{
Argv[Argc][i] = *TmpStrPtr;
TmpStrPtr++;
}
#endif


TmpStrPtr = StrPtr;
i = 0;
Argc++;
}
}

int main(int argc, char* argv[])
{
char** argv1;
char* str = "";

char** str1 = "test";
char* str2 = " ";
memcpy(str2, str1[0], 4);
printf("%s\n", str2);
// test ConvertStr()
str = " a asdf ";
argv1 = "";
printf("argc = %d, argv0 = \n",ConvertArg(str, argv1));
str = " a asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s, argv2 =
%s\n",ConvertArg(str, argv1), argv1[0], argv1[1], argv1[3]);
return 1;
}

 
Reply With Quote
 
 
 
 
Developper
Guest
Posts: n/a
 
      03-12-2005

<(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed) oups.com...
>I want to parse a string like C program parse the command line into
> argc & argv[][].
> I hope don't use the array the allocate a fix memory first, and don't
> use the memory allocate function like malloc.
> who can give me some ideas?
> The following is my program, but it has some problem. I hope someone
> would correct it.
> ////////////////////////////
> //Test_ConvertArg.c
> ////////////////////////////
> #include <stdio.h>
> #include <string.h>
>
> int ConvertArg(char* Str, char* Argv[])
> {
> int Argc; // Count the argument).
> int Count = 0;
> int i = 0;
> char* StrPtr;
> char* TmpStrPtr;
> StrPtr = Str;
>
> for(; *StrPtr == ' ' // ignore the whitespace before the
> command!
> {
> ++StrPtr;
> }
>
> TmpStrPtr = StrPtr;
>
> for (Argc = 0; (*StrPtr) != '\n'; StrPtr++, Count++)
> {
>
> if(*StrPtr == ' ')
> {
> // if exist multi whitespace together,
> //argc just count once,and don't continually change argv!
> if( *(StrPtr+1) == ' ')
> {
> Count--;
> continue;
> }
> }
>
> Count--;
> // the following setences have problems.
> memcpy(Argv[Argc],TmpStrPtr,Count);
>
> #if 0
> for(i=0; i <= Count; i++)
> {
> Argv[Argc][i] = *TmpStrPtr;
> TmpStrPtr++;
> }
> #endif
>
>
> TmpStrPtr = StrPtr;
> i = 0;
> Argc++;
> }
> }
>
> int main(int argc, char* argv[])
> {
> char** argv1;
> char* str = "";
>
> char** str1 = "test";
> char* str2 = " ";
> memcpy(str2, str1[0], 4);
> printf("%s\n", str2);
> // test ConvertStr()
> str = " a asdf ";
> argv1 = "";
> printf("argc = %d, argv0 = \n",ConvertArg(str, argv1));
> str = " a asdf";
> printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
> argv1[0], argv1[1]);
> str = " asdf asdf";
> printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
> argv1[0], argv1[1]);
> str = " asdf asdf";
> printf("argc = %d, argv0 = %s ,argv1 = %s, argv2 =
> %s\n",ConvertArg(str, argv1), argv1[0], argv1[1], argv1[3]);
> return 1;
> }
>


use the getopt function

Johan


 
Reply With Quote
 
 
 
 
linzhenhua1205@163.com
Guest
Posts: n/a
 
      03-12-2005
but my program is not want to run under the systemV Unix or something
like it.
That is not getopt system call

 
Reply With Quote
 
linzhenhua1205@163.com
Guest
Posts: n/a
 
      03-12-2005
I rewrite the Convert function like following:
//////////////////////


///////////////////////////////////////////////////////////////////////////////
///////
//PURPOSE : This function converts the Str into [int argc] & [char*
argv[]],
// which are the same as the argument of the C main function.
//
//ARGUMENT : Str contains the command , switch and archive file.
//RETURN : the number in of the argument.
//INFO : 1.skip leading white space and table space!
// 2.if occur the '\0' break;
// 3.Put the remain string into the Argv[Argc], be remember Argv[]
// is a string Pointer. if you file '\0' in string it means that
// you fill the Argv[][].
// 4.check the ' ' '\t' '\n'
// 5.fill the string by '\n'
// 6.change the string pointer to the next argument.
//TestStatus: UNDO
///////////////////////////////////////////////////////////////////////////////
///////
int ConvertArg(char* Str)
{
int Argc; // Count the argument).
char* Argv[MAXARGC]; // to hold the parse result.
char *StrPtr, *CurrentPtr;
StrPtr = Str;

for(Argc = 0;Argc < MAXARGC;Argc++) // init the Argv.
{
Argv[Argc] = NULLCHAR;
}


for(Argc = 0;Argc < MAXARGC && (*StrPtr) != '\0'
{
// Skip leading white space and table space!
while(*StrPtr == ' ' || *StrPtr == '\t')
{
StrPtr++;
}

// When that is only space in the string this instance will happen!
if((*StrPtr) == '\0') // break if occur the char '\0'
{
break;
}

Argv[Argc++] = StrPtr; // Beginning of token.

// Find space or tab. If not present then we've already found the
last token.
for (CurrentPtr = StrPtr; *CurrentPtr; CurrentPtr++)
{
if (*CurrentPtr == ' ' || *CurrentPtr == '\t')
{
break;
}
}

if (*CurrentPtr != '\0')
{
*CurrentPtr++ = '\0';
}
StrPtr = CurrentPtr;
}

// empty command line
if (Argc < 1)
{
Argc = 1;
Argv[0] = "";
}

return Argc;
}

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-12-2005
On 11 Mar 2005 22:00:35 -0800, in comp.lang.c , http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>but my program is not want to run under the systemV Unix or something
>like it.
>That is not getopt system call


the source for getopt() is available in the public domain. Try the gnu archives.


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-12-2005
Mark McIntyre wrote:
> (E-Mail Removed) wrote:
>
>> but my program is not want to run under the systemV Unix or
>> something like it.That is not getopt system call

>
> the source for getopt() is available in the public domain. Try the
> gnu archives.


You mean "The source for some version of something sometimes known
as getopt is ...,". There is no standard for this. For example,
the following is viable:

char *getopt(void) {return "opt";}

illustrating why the content of this group is constrained.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Daniel Vallstrom
Guest
Posts: n/a
 
      03-12-2005
CBFalconer wrote:
> Mark McIntyre wrote:
> > (E-Mail Removed) wrote:
> >
> >> but my program is not want to run under the systemV Unix or
> >> something like it.That is not getopt system call

> >
> > the source for getopt() is available in the public domain. Try the
> > gnu archives.

>
> You mean "The source for some version of something sometimes known
> as getopt is ...,". There is no standard for this. For example,
> the following is viable:
>
> char *getopt(void) {return "opt";}


Almost certainly not. getopt is covered by POSIX.


Daniel Vallstrom

 
Reply With Quote
 
Mac
Guest
Posts: n/a
 
      03-12-2005
On Sat, 12 Mar 2005 11:16:06 +0000, Mark McIntyre wrote:

> On 11 Mar 2005 22:00:35 -0800, in comp.lang.c , (E-Mail Removed) wrote:
>
>>but my program is not want to run under the systemV Unix or something
>>like it.
>>That is not getopt system call

>
> the source for getopt() is available in the public domain. Try the gnu archives.


I would be very surprised if GNU put any of their stuff into the public
domain. AFAIK, GNU (or the FSF) maintains copyright ownership of all their
software and licenses it according to the GPL or LGPL.

--Mac

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-12-2005
On Sat, 12 Mar 2005 16:10:02 GMT, in comp.lang.c , CBFalconer
<(E-Mail Removed)> wrote:

>Mark McIntyre wrote:
>> (E-Mail Removed) wrote:
>>
>>> but my program is not want to run under the systemV Unix or
>>> something like it.That is not getopt system call

>>
>> the source for getopt() is available in the public domain. Try the
>> gnu archives.

>
>You mean "The source for some version


indeed. I was merely indicating that
a) the wheel doesn't need reinvented
b) getopt is not restricted to SysV.

>illustrating why the content of this group is constrained.


Well, that was rather why I redirected out of it....


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-12-2005
On Sat, 12 Mar 2005 17:13:31 GMT, in comp.lang.c , Mac <(E-Mail Removed)> wrote:

>On Sat, 12 Mar 2005 11:16:06 +0000, Mark McIntyre wrote:
>
>> On 11 Mar 2005 22:00:35 -0800, in comp.lang.c , (E-Mail Removed) wrote:
>>
>>>but my program is not want to run under the systemV Unix or something
>>>like it.
>>>That is not getopt system call

>>
>> the source for getopt() is available in the public domain. Try the gnu archives.

>
>I would be very surprised if GNU put any of their stuff into the public
>domain. AFAIK, GNU (or the FSF) maintains copyright ownership of all their
>software and licenses it according to the GPL or LGPL.


A rose.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Parse tree like data like XML by Perl? Davy Perl Misc 2 10-09-2006 04:30 AM
Side effect of JDK 1.5.0._05. Ant command line parse failure Roedy Green Java 18 09-25-2005 02:31 AM
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM
Re: Parse Command Line corey.frost C++ 1 09-09-2003 12:16 AM



Advertisments