Sample

Previous  Next

 

This is a simple console application that defines 2 script functions, INC and TITLE_CASE.  INC will increments the supplied INTEGER by one and TITLE_CASE will convert the supplied string to a Title Case string where each word is capitalized.

 


Sample Application:

 

/////////////////////////////////////////////////////////////////////////////

//

// SampleScriptApp.cpp : Defines the entry point for the console application.

//

/////////////////////////////////////////////////////////////////////////////

//

#include "stdafx.h"

 

/////////////////////////////////////////////////////////////////////////////

// *** NOTE ***

// Make sure to define _MB2_PARSE_MACROS before including MyBASIC2.h if

// we want to use the Parsing Macros

//

#define _MB2_PARSE_MACROS

#include "MyBASIC2.h"

 

/////////////////////////////////////////////////////////////////////////////

// Increment INTEGER parameter by one

int _inc_func( mb_interpreter_t* s, void** l )

{

  // Parse Parameter(s)

  BEGIN_PARSE( s, l )

     POP_INT( nPar )

  END_PARSE

 

  // Return incremented integer parameter

  RETURN_INT( ++nPar );

}

 

/////////////////////////////////////////////////////////////////////////////

// Capitalize each word in a string

int _title_case_func( mb_interpreter_t* s, void** l )

{

  // Parse Parameter

  BEGIN_PARSE( s, l )

     POP_STRING( pStrPar )

  END_PARSE

 

  // Make a copy that we can return

  COPY_STRING( pOutput, pStrPar )

 

  // Make all lower first

  strlwr( pOutput );

 

  // Always make sure first is Upper

  pOutput[0] = toupper( pOutput[0] );

 

  // Convert first character of every word to upper case

  for ( unsigned i = 1; i < strlen( pOutput ); i++ )

  {

     if ( ( pOutput[i-1] == ' ' ) || ( pOutput[i-1] == '\t' ) )

     {

        pOutput[i] = toupper( pOutput[i] );

     }

  }

 

  // Return Converted String

  RETURN_STRING( pOutput );

}

 

/////////////////////////////////////////////////////////////////////////////

// MAIN Function.  If file name argument not supplied then prompt for it

//

int main( int argc, char* argv[] )

{

  char              strName[_MAX_PATH];

  mb_interpreter_t* bas = NULL;

 

  // Check if we have a file as argument

  if ( argc < 2 )

  {

     printf( "_Enter Script File: " );

     scanf( "%s", strName );

  }

  else

  {

     strcpy( strName, argv[1] );

  }

 

  // Make sure we have a file name

  if ( strlen( strName ) > 0 )

  {

     // Initialize and open an interpreter

     mb_init();

     mb_open( &bas );

 

     // Register our functions

     mb_register_func( bas, "INC", _inc_func );

     mb_register_func( bas, "TITLE_CASE", _title_case_func );

 

     // Load and Run our Script file

     if ( mb_load_file( bas, strName ) == MB_FUNC_OK )

     {

        if ( mb_run( bas ) != MB_FUNC_END )

        {

           printf( "ERROR, An Error occurred when running Script File: %s\n", strName );

        }

     }

     else

     {

        printf( "ERROR, Loading Script File: %s\n", strName );

     }

 

     // Remove Functions

     mb_remove_func( bas, "INC" );

     mb_remove_func( bas, "TITLE_CASE" );

 

     // Close down MyBASIC2

     mb_close( &bas );

     mb_exit();

  }

  else

  {

     printf( "ERROR, No Script File Provided\n" );

  }

       

  _getch();

       return 0;

}

 


Sample Script File

 

OPTION BASE 1

 

DIM strings(4) AS STRING

DIM i AS INTEGER

 

strings(1) = "kalle anka was here today."

strings(2) = "Nisse Hult was also here."

strings(3) = "Pelle was somewhere else. Of course !!!"

strings(4) = "kalle and nisse had to do it on their own :-("

 

i = 0

 

DO

  i = INC( i )

  PRINT TITLE_CASE( strings( i ) ),

UNTIL i = 4

 

END

 


Output

 

Kalle Anka Was Here Today.

Nisse Hult Was Also Here.

Pelle Was Somewhere Else. Of Course !!!

Kalle And Nisse Had To Do It On Their Own :-(