|
Input and Output |
|
|
To enable input and output using the BASIC INPUT and PRINT statements you need to define your input and output handlers. Some of the handlers have 2 versions available. We recommend you use the ones that end with _ex (2nd generation) since this will give you the most flexibility with little added cost. The differences between the two version is that the 2nd generation function passes the interpreter structure as the first parameter allowing you to pass user data by using mb_set_userdata and mb_get_userdata from handlers. This allows you to pass information to the handlers such as pointers to classes or functions.
Enabling Input and Output Handlers
static int _printstring( mb_interpreter_t* s, const char* pStr, ... ); static int _getstring( mb_interpreter_t* s, char* pStr, int nLen, const char* pVarName );
mb_set_printer_ex( bas, _printstring ); mb_set_inputer_ex( bas, _getstring );
///////////////////////////////////////////////////////////////////////////// // static int _printstring( mb_interpreter_t* s, const char* pStr, ... ) { char pBuffer[4096];
va_list params;
va_start( params, pStr ); sprintf( pBuffer, pStr, va_arg( params, void* ) ); va_end( params );
if ( mb_get_environment( s ) == ENV_WINDOWS ) { AfxMessageBox( pBuffer ); } else { printf( pBuffer ); }
return strlen( pStr ); }
///////////////////////////////////////////////////////////////////////////// // static int _getstring( mb_interpreter_t* s, char* pStr, int nLen, const char* pVarName ) { if ( mb_get_environment( s ) == ENV_WINDOWS ) { return MB2Console_Input( pStr, nLen, pVarName ); } else { // Must have room for at least terminator
if ( nLen == 0 ) return 0;
// Make room for '\0'
nLen--;
int c; int n = 0;
do { c = getchar();
if ( c == '\n' ) break; if ( n == nLen ) break;
pStr[n++] = c; }
pStr[n] = 0;
return strlen( pStr ); }
return 0; }
|