It is currently Wed Aug 22, 2018 6:41 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: C code questions to understand to convert to PB
PostPosted: Fri Jul 06, 2018 1:42 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 146
Hi,
I have some questions regarding C2PB
1.char * parse_line(char *buf, char *nargv[], int * nargc);
returns String or Pointer to string?
2. char *nargv[] is the pointer to Array?
3. char **carg = nargv+1; char **carg pointer to the 1st element in Array?
4. (*nargc) = 1; if *nargc pointer argument accepts value then why not "int nargc"
5. "char *buf" as argument is used like buf.s or *buf or buf.i in PB
6. while ((c = *buf) >= ' ' || !begun_reading ); can be handled as "while asc(mid(buf,curpos,1))>=charcode ?
7. while (*buf++ != 10);
and
if (c==10 || c==13)
does this mean 10th byte of *buf (and c) or chr(10)
8. (*nargc)++; does this mean next byte of *nargc memory pointer or increment nargc variable by 1?
9. while ((c = *buf) && (c != '"')) buf++; does this mean until next quote char?
10. if (c) *buf++ = '\0'; what does '\0' mean?
11. return (buf); as procedure declared as CHAR in the beginnig does buf contain string, 1 char or pointer?

Code:
char * parse_line(char *buf, char *nargv[], int * nargc)
{
  char **carg = nargv+1;
  char c;
  int begun_reading=FALSE;
  (*nargc) = 1;
 
  while ((c = *buf) >= ' ' || !begun_reading ) {  /* looking for null or non-ASCII*/
    buf++;
    if (c == ' ') continue; /* skip blanks */
   
    if (c == '#'){ /* skip comments */
      while (*buf++ != 10);
      continue;
    }

    if (c==10 || c==13) /* skip empty lines */
      continue;

    begun_reading=TRUE;
    (*nargc)++;
    if (c == '"') { /* quote separator */
      *carg++ = buf;  /* point to first char after quote */
      while ((c = *buf) && (c != '"')) buf++;
      if (c) *buf++ = '\0';
    }
    else { /* blank separator */
      *carg++ = buf-1;
      while ((c = *buf) && c > ' ' ) buf++;
      if (c) *buf++ = '\0';
      if (c < ' ') break;  /* end of line (or file) found */
    }
  }
  *carg = 0;
 
  /* skip end of line stuff */
  while ((c = *buf) && (c < ' ')) buf++;
 
  return (buf); /* this will point to NULL or the next line */


Top
 Profile  
Reply with quote  
 Post subject: Re: C code questions to understand to convert to PB
PostPosted: Fri Jul 06, 2018 2:26 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3234
Location: Netherlands
I'm not a C expert so I might be wrong about some things and I don't have all answers :?

As far as I can tell, the first procedure argument is a pointer to a string (input).
The second argument is the address of an array of pointers that will be filled by the procedure (output).
The third argument is a pointer to an integer value that will return the number of items set in the output array.
The procedure itself returns a pointer to the next line.

C is a rather compact language.
For example while (*buf++ != 10); will increase the pointer and loop as long at the character is not a #LF.
PB equivalent would be something like While *buf\c <> #LF : *buf + SizeOf(Character) : Wend where *buf is defined as *buf.Character

_________________
MacOS 10.13 High Sierra, PB 5.62 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: C code questions to understand to convert to PB
PostPosted: Fri Jul 06, 2018 2:58 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 146
Thank You very much,
up to 3 understood.
And 10, 13 in 7th is #CR and #LF as I understood.

in 4, "(*nargc) = 1", this *nargc is variable with value 1, some unclear to me.
And meaning of '\0' stands for chr(0)?
could You explain please?


Top
 Profile  
Reply with quote  
 Post subject: Re: C code questions to understand to convert to PB
PostPosted: Fri Jul 06, 2018 3:23 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3234
Location: Netherlands
Yes, 10 and 13 is #LF and #CR.
'\0' is indeed chr(0).

LiK137 wrote:
in 4, "(*nargc) = 1", this *nargc is variable with value 1, some unclear to me.

I think it sets the variable to 1 like
Code:
Procedure Example(*nargc.Integer)
  *nargc\i = 1
EndProcedure

Define count.i

Example(@count)

Debug count

_________________
MacOS 10.13 High Sierra, PB 5.62 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: C code questions to understand to convert to PB
PostPosted: Fri Jul 06, 2018 3:48 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Jun 23, 2010 5:13 pm
Posts: 146
Thanx, clear


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye