[SOLVED] sscanf does not work in INLINE code

Previous topic - Next topic

Eric.Erpelding

I am trying to use sscanf inside a block of INLINE code.
But the GLBasic compiler gives this error:
Quoteerror: `sscanf' was not declared in this scope
The same "not declared in this scope" error occurs if I try to call the printf fuction as well.
Can one use these functions in INLINE code?

dreamerman

Any code to begin with? C++ inline gives freedom to use any c lib/function but requires more knowledge about C. If you don't need it, use internal GLB commands.
First include 'stdio' lib as in any C app:
Code (glbasic) Select
INLINE
        }
                extern "C" {
                        #include <cstdio>
                }
        namespace __GLBASIC__ {
ENDINLINE

Then you may use functions from that lib in that source file:
Code (glbasic) Select
LOCAL sentence$ = "Rudolph is 12 years old"
INLINE
char str [20];
  int i;

sscanf (sentence_Str,"%s %*s %d",str,&i);
printf ("%s -> %d\n",str,i);
ENDINLINE

This is example from cplusplus.com
Note that it's using printf (stdout) so result wont be visible in debug window, either use other editor or command line:
Code (glbasic) Select
c:\project\test\your_app.exe >> out.txt
Check my source code editor for GLBasic - link Update: 20.04.2020

Eric.Erpelding

#2
Thank you dreamerman.
I am still having problems making a GLBasic console program that will convert a Hex string into a Decimal value.
Here is my code:
Code (glbasic) Select
// --------------------------------- //
// Project: INLINE Test
// Start: Friday, August 12, 2016
// IDE Version: 14.001
//
// Note:
// Check "Project->Options->Console" to make console exe

GLOBAL value%
GLOBAL A$
STDOUT "***INLINE Test***\n"
value=0

STDOUT "Enter Hex Number > "
A$=STDIN$()

INLINE
}  // Close __GLBASIC__ namespace

extern "C" {
#include <cstdio>
sscanf(A_Str,"%x",&value);                     
}
             
namespace __GLBASIC__ {
ENDINLINE

STDOUT "Decimal Value = "+value

// wait for user input to exit
A$=STDIN$()


I am getting these kinds of errors when compiling:

Quoteerror: expected constructor, destructor, or type conversion before '(' token
error: expected constructor, destructor, or type conversion before '=' token
error: expected unqualified-id before "return"

dreamerman

Hm... probably you need to put Inline part out of main source code, don't remember why atm, but in separate file it works ok:
main file:
Code (glbasic) Select
// Note:
// Check "Project->Options->Console" to make console exe



GLOBAL value%
GLOBAL A$
STDOUT "***INLINE Test***\n"
value=0

STDOUT "Enter Hex Number > "
A$=STDIN$()

scan_key()

STDOUT "Decimal Value = " + (value)

// wait for user input to exit
A$=STDIN$()

second file:
Code (glbasic) Select
INLINE
        }
                extern "C" {
                        #include <cstdio>
                }
        namespace __GLBASIC__ {
ENDINLINE

FUNCTION scan_key%:
INLINE
sscanf(A_Str,"%x",&value);
ENDINLINE
ENDFUNCTION

Another thing that to use GLB variables you need to be in GLB scope/namespace when in Inline.
Check my source code editor for GLBasic - link Update: 20.04.2020

Eric.Erpelding

#4
There seems to be a problem when closing and then opening the __GLBASIC__ namespace in the main program.
This code will not compile.
Code (glbasic) Select
// --------------------------------- //
// Project: INLINE
// Start: Friday, August 12, 2016
// IDE Version: 14.001

STDOUT "***INLINE Test***\n"

INLINE
}
namespace __GLBASIC__ {
ENDINLINE

END


The errors reported are:
Quoteerror: expected constructor, destructor, OR TYPE conversion before ';' token
error: expected unqualified-id before "return"

Code is being compiled on a Windows 7 SP3 64-bit computer.

Eric.Erpelding

#5
I was looking at the generated code for my INLINE test program that does not compile.
It seems that the first '}' in the INLINE code will match up with the starting '{' of the main program code block,
Quoteint __MainGameSub_(void)
{
After the first '}' in the INLINE code there is no more main program code to continue with!
So it looks like one cannot close the __GLBASIC__ namespace in the main program.
What can one do?

dreamerman

Just put that inline to second file and add it to your project, like in my previous post. ;]
Don't remember if you can somehow bypass that and have include in main source...
Check my source code editor for GLBasic - link Update: 20.04.2020

Eric.Erpelding

QuoteJust put that inline to second file and add it to your project, like in my previous post. ;]
Yes, that is the solution. Thank you again dreamerman.
I named the second file "INLINE2.gbas" and added it to the project.
It works, and then I tried removing the closing and opening of the __GLBASIC__ namespace as is shown in this code:

Code (glbasic) Select
// --------------------------------- //
// Project: Inline
// File:    Inline2.gbas
// Start: Friday, August 12, 2016
// IDE Version: 14.001


// SETCURRENTDIR("Media") // go to media files
?IF 1
INLINE
//        }
                extern "C" {
                        #include <cstdio>
                }
//        namespace __GLBASIC__ {
ENDINLINE
?ENDIF

FUNCTION scan_key%:
        INLINE
                sscanf(A_Str,"%x",&value);
        ENDINLINE
ENDFUNCTION


And the code still worked!

What are the rules for when one should close and then re-open a namespace in INLINE code?