C code questions to understand to convert to PB

Windows specific forum
LiK137
Enthusiast
Enthusiast
Posts: 279
Joined: Wed Jun 23, 2010 5:13 pm

C code questions to understand to convert to PB

Post by LiK137 »

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: Select all

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 */ 
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: C code questions to understand to convert to PB

Post by wilbert »

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
Windows (x64)
Raspberry Pi OS (Arm64)
LiK137
Enthusiast
Enthusiast
Posts: 279
Joined: Wed Jun 23, 2010 5:13 pm

Re: C code questions to understand to convert to PB

Post by LiK137 »

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?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: C code questions to understand to convert to PB

Post by wilbert »

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: Select all

Procedure Example(*nargc.Integer)
  *nargc\i = 1
EndProcedure

Define count.i

Example(@count)

Debug count
Windows (x64)
Raspberry Pi OS (Arm64)
LiK137
Enthusiast
Enthusiast
Posts: 279
Joined: Wed Jun 23, 2010 5:13 pm

Re: C code questions to understand to convert to PB

Post by LiK137 »

Thanx, clear
Post Reply