Mixing C and BCX code

! Inline "C" Code operator

Purpose: BCX allows you pass inline "C" code using the ! operator.


Syntax:

 ! /** C statement is placed here */

Example: The following sample shows how to create a C coded local DWORD variable.


 PRINT GetMachineName$()

 FUNCTION GetMachineName$
  LOCAL A$
  ! DWORD b; /** allocate a local variable in "C" and include C comments! */
  b = 256
  GetComputerName(A$,&b) ' the& operator means "PASS THE ADDRESS" of b
  FUNCTION = A$
 END FUNCTION

$CCODE directive

Purpose: BCX allows you pass inline "C" code using a $CCODE directive placed before and after the "C" code.


Syntax 1:

 $CCODE 
  /** C statements go here */
 $CCODE

Remarks: When $CCODE ... $CCODE is used without an optional parameter the emitted "C" code is located as it was in the BCX code.

Example:


 PRINT "BASIC Code Here."

 $CCODE
  // declare the variables:
  int nNumber;
  int *pPointer;
  // now, give a value to them:
  nNumber = 15;
  pPointer = &nNumber;
  // print out the value of nNumber:
  printf("nNumber is equal to : %d\n", nNumber);
  // now, alter nNumber through pPointer:
  *pPointer = 25;
  // prove that nNumber has changed as a result of the above
  // by printing its value again:
  printf("nNumber is equal to : %d\n", nNumber);
 $CCODE

 PRINT "BASIC Code Here."

Result:


 BASIC Code Here.
 nNumber is equal to : 15
 nNumber is equal to : 25
 BASIC Code Here.


Syntax 2:

 $CCODE SET
  /** C statements go here */
 $CCODE

Parameters:

  • SET [OPTIONAL] When the $CCODE SET directive is used, everything sandwiched between the $CCODE SET and the terminating $CCODE statements is placed at the module level of the emitted "C" source in the 'User GLOBAL SET Statements' section.

Syntax 3:

 $CCODE HEADER 
  /** C statements go here */
 $CCODE

Parameters:

  • HEADER [OPTIONAL] When the $CCODE HEADER directive is used, everything sandwiched between the $CCODE HEADER and the terminating $CCODE statements is placed at the module level of the emitted "C" source immediately after the 'Compiler Defines' section. $CCODE HEADER is useful particularly for inlined C source pragma statements.

Syntax 4:

 $CCODE CONST
  /** C statements go here */
 $CCODE

Parameters:

  • CONST [OPTIONAL] When the $CCODE CONST directive is used, everything sandwiched between the $CCODE CONST and the terminating $CCODE statements is placed at the module level of the emitted "C" source in the 'User Defined Constants' section.

Syntax 5:

 $CCODE UDT
  /** C statements go here */
 $CCODE

Parameters:

  • UDT [OPTIONAL] When the $CCODE UDT directive is used, everything sandwiched between the $CCODE UDT and the terminating $CCODE statements is placed at the module level of the emitted "C" source in the ' User Defined Types And Unions' section.

Syntax 6:

 $CCODE ENUM 
  /** C statements go here */
 $CCODE

Parameters:

  • ENUM [OPTIONAL] When the $CCODE ENUM directive is used, everything sandwiched between the $CCODE ENUM and the terminating $CCODE statements is placed at the module level of the emitted "C" source in the 'User's GLOBAL ENUM blocks' section.

Syntax 7:

 $CCODE FUNCSUB
  /** C statements go here */
 $CCODE

Parameters:

  • FUNCSUB [OPTIONAL] When the $CCODE FUNCSUB directive is used, everything sandwiched between the $CCODE FUNCSUB and the terminating $CCODE statements is placed at the module level of the emitted "C" source in the 'User C code' section.

Remarks: $CCODE FUNCSUB is used to enclose a complete C code procedure. A prototype is not needed in this case because the placement in the emitted "C" source is before the main procedure.

Example:


 DIM a$
 DIM b$
  
 a$ = "     this   is    a     test      "
  
 b$ = Remove_All_White_Space(a$)
  
 PRINT b$
  
 END PROGRAM
  
 $CCODE FUNCSUB
  char* Remove_All_White_Space(char* str1)
  {
   char *obuf,*nbuf;
   if (str1)
    {
     for (obuf=str1,nbuf=str1;*obuf;++obuf)
      {
       if (!isspace(*obuf))
       *nbuf++=*obuf;
      }
    *nbuf=0;
    }
   RETURN str1;
  }
 $CCODE

$HEADER directive

Purpose: The $HEADER directive works like $CCODE except everything sandwiched between two $HEADER statements is placed at the module level of the emitted "C" source. $HEADER is useful particularly for pragma statements and declaring prototypes for inlined C source functions. A $HEADER directive is placed before and after the "C" code.


Syntax:

 $HEADER
  /** C statements go here */
 $HEADER

Example:


 $HEADER 
  #define KitchenSinkIsIncluded 
 $HEADER

 $HEADER
  #ifndef KitchenSinkIsIncluded
   #include <KitchenSink.h>
  #else
   #define CallThePlumber 1
  #endif
 $HEADER 

$CPROTO directive

Purpose: The $CPROTO directive is used for declaring prototypes for inlined C source functions.


Syntax 1:

 $CPROTO ! ProcedureDataType ProcedureName(ParameterDataType);

Remarks: When using $CPROTO with a C language prototype, a space-exclamation mark-space must precede the prototype. Also, in C, a semicolon is required at the end of of the prototype.

Example:


 $CPROTO ! CHAR * Remove_All_White_Space(CHAR *);
  
 DIM a$
 DIM b$
 
 a$ = "     this   is    a     test      "
 
 b$ = Remove_All_White_Space(a$)
 
 PRINT b$
 
 END PROGRAM
 
 ! char* Remove_All_White_Space(char* str1)
 ! {
 !  char *obuf,*nbuf;
 !  if (str1)
 !   {
 !    for (obuf=str1,nbuf=str1;*obuf;++obuf)
 !     {
 !      if (!isspace(*obuf))
 !      *nbuf++=*obuf;
 !     }
 !   *nbuf=0;
 !   }
 !  RETURN str1;
 ! }


Syntax 2:

 $CPROTO [FUNCTION/SUB] Foo$(a%, b$)

Remarks: $CPROTO also will accept a BASIC FUNCTION or SUB declaration and convert it to the approriate C language code.

Example:


 $CPROTO FUNCTION Remove_All_White_Space$(a$)
   
 DIM a$
 DIM b$
  
 a$ = "     this   is    a     test      "
  
 b$ = Remove_All_White_Space(a$)
  
 PRINT b$
  
 END PROGRAM
  
 ! char* Remove_All_White_Space(char* str1)
 ! {
 !  char *obuf,*nbuf;
 !  if (str1)
 !   {
 !    for (obuf=str1,nbuf=str1;*obuf;++obuf)
 !     {
 !      if (!isspace(*obuf))
 !      *nbuf++=*obuf;
 !     }
 !   *nbuf=0;
 !   }
 !  RETURN str1;
 ! }