How to translate this from C to FreeBasic?

New to FreeBASIC? Post your questions here.
Post Reply
Iczer
Posts: 99
Joined: Jul 04, 2017 18:09

How to translate this from C to FreeBasic?

Post by Iczer »

I have some troubles understanding how While work in this C code:

Code: Select all

  while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
    if(msg->msg == CURLMSG_DONE) {
      int idx;
...
...
...

    }
  }
How I should translate it to FreeBasic?
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: How to translate this from C to FreeBasic?

Post by coderJeff »

In C, when you see an assignment as part of an expression, the assignment is made and the value returned is the variable assigned.

fbc does not support assignments in expressions so the individual steps need to be separated in to statements

Code: Select all

do
	msg = curl_multi_info_read(multi_handle, @msgs_left)

	'' no more to read? then exit
	if( msg = NULL ) then
		exit do
	end if

	if(msg->msg = CURLMSG_DONE) then
		dim as long idx
		...
		...
		...
	end if
loop
Take care: in C '=' is always assignment and '==' is always comparison. But in fbc, '=' is assignment when used in a statement and comparison when used in an expression.
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: How to translate this from C to FreeBasic?

Post by angros47 »

That code is deceiving: in fact, it would translate into something like:

Code: Select all

  msg = curl_multi_info_read(multi_handle, @msgs_left)
  while(msg) {
    if  msg->msg = CURLMSG_DONE then
      dim idx as long
...
...
...
   end if
   
    msg = curl_multi_info_read(multi_handle, @msgs_left)
wend 
    
    

Specifically, the "while((msg = curl_multi_info_read(multi_handle, &msgs_left)))" does NOT translate to "while(msg = curl_multi_info_read(multi_handle, &msgs_left))".
In Basic, such a line would in fact mean "get the value from the function curl_multi_info_read, compare it to the variable msg, if they are equal perform the operations". But in C, it doesn't work in this way. In C, "=" means always assignment , for comparison you have to use "==". Also, C allows to put a complete command after "while", so the line means "read the value of curl_multi_info_read, assign it to the variable msg, and after that, if that value is not zero, perform the operations"

Coming from Basic, it's easy to miss that, in C.
Iczer
Posts: 99
Joined: Jul 04, 2017 18:09

Re: How to translate this from C to FreeBasic?

Post by Iczer »

Thanks, Both of you!
Post Reply