BASIC Developer & Support Resources > Scripting Languages

CBASIC

<< < (3/3)

John:
This is an example I did in Oxygen Basic which uses the ScriptBasic COM/OLE extension module. This is the CBASIC version of it.


--- Code: C ---// gcc vblogin.c -IC:\sbgcc\source libscriba.dll sbwgui.o -lpthread -o vblogin #include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include "scriba.h"#include "cbasic.h" MAINBEGIN_FUNCTION  DIM pSbProgram AS pProgram;  DIM pSbData AS ArgData;  DIM pSbData AS FunctionResult;  DIM int AS sb_call = 1;  DIM int AS sb_get  = 2;  DIM int AS sb_let  = 4;  DIM int AS sb_set  = 8;  DIM char AS src[] = "IMPORT sbembed.sbi\nIMPORT \"callback.sb\"\n";   pProgram = scriba_new(malloc,free);  scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");  scriba_SetFileName(pProgram, "none");  scriba_LoadProgramString(pProgram, src, strlen(src));  scriba_Run(pProgram, "");   FunctionResult = scriba_NewSbArgs(pProgram,"u");  DIM int AS fn1 = scriba_LookupFunctionByName(pProgram, "main::sb_create");  DIM int AS fn2 = scriba_LookupFunctionByName(pProgram, "main::sb_cbn");  DIM int AS fn3 = scriba_LookupFunctionByName(pProgram, "main::sb_release");  DIM int AS fn4 = scriba_LookupFunctionByName(pProgram, "main::set_callback");   ArgData = scriba_NewSbArgs(pProgram, "s", "O2VB.Login");  scriba_CallArgEx(pProgram, fn1, FunctionResult, 1, ArgData);  DIM unsigned long AS vbobj = FunctionResult[0].v.l;  ArgData = scriba_NewSbArgs(pProgram, "i", vbobj);  scriba_CallArgEx(pProgram, fn4, FunctionResult, 1, ArgData);  ArgData = scriba_NewSbArgs(pProgram, "i s i", vbobj, "ShowOCXForm", sb_call);  scriba_CallArgEx(pProgram, fn2, FunctionResult, 3, ArgData);  DIM unsigned long AS ufunc = scriba_LookupVariableByName(pProgram, "main::userid");  scriba_GetVariable(pProgram, ufunc, AT ArgData);  PRINT("%s\n", ArgData[0].v.s);  ArgData = scriba_NewSbArgs(pProgram, "i", vbobj);  scriba_CallArgEx(pProgram, fn3, FunctionResult, 1, ArgData);   scriba_DestroySbArgs(pProgram, ArgData, 3);  scriba_DestroySbArgs(pProgram, FunctionResult, 1);  scriba_destroy(pProgram);   RETURN_FUNCTION(0);END_FUNCTION   

callback.sb

--- Code: Script BASIC ---' CBASIC SB Callback Functions FUNCTION OK_Button_Click(user)  userid = user  OK_Button_Click = TRUEEND FUNCTION  FUNCTION Set_Callback(vbobj)  oCollection = CBN(vbobj, "CallBackHandlers", sb_get)  CBN(oCollection, "Add", sb_call, ADDRESS(OK_Button_Click()), "Login_Form.OK_Button_Click")END FUNCTION 
VB6 Login Form

--- Code: Visual Basic ---Option Explicit Private Declare Function ext_SBCallBack Lib "COM.dll" Alias "SBCallBack" (ByVal EntryPoint As Long, ByVal arg As Long) As LongPrivate Declare Function ext_SBCallBackEx Lib "COM.dll" Alias "SBCallBackEx" (ByVal EntryPoint As Long, ByRef v As Variant) As Variant Private m_owner As LoginPublic LoginSucceeded As Boolean Function ShowMain_Form(owner As Login) As Long    On Error Resume Next    Set m_owner = owner    Me.Show 1    Set m_owner = Nothing    ShowMain_Form = 0    Unload MeEnd Function Private Function TriggerCallBack(nodeID As Long, argValue As Long) As Long    TriggerCallBack = ext_SBCallBack(nodeID, argValue)End Function Private Function TriggerCallBackEx(nodeID As Long, v() As Variant)    TriggerCallBackEx = ext_SBCallBackEx(nodeID, v)End Function Private Sub Cancel_Button_Click()    LoginSucceeded = False    Me.HideEnd Sub Private Sub OK_Button_Click()    Dim nodeID As Long    Dim arg(0) As Variant    Dim rtnVal As Long    If Password_Text = "OxygenBasic" Then        LoginSucceeded = True        nodeID = m_owner.CallBackHandlers("Login_Form.OK_Button_Click")        arg(0) = Login_Form.User_Text        rtnVal = TriggerCallBackEx(nodeID, arg)        Me.Hide    Else        MsgBox "Invalid Password, try again!", , "Login"        Password_Text.SetFocus        SendKeys "{Home}+{End}"    End IfEnd Sub 
VB6 Login Class

--- Code: Visual Basic ---Public CallBackHandlers As New Collection Public Function ShowOCXForm() As Long    ShowOCXForm = Login_Form.ShowMain_Form(Me)End Function 

C:\ScriptBASIC\cbasic>vblogin
John

C:\ScriptBASIC\cbasic>

John:
This example uses the ScriptBasic cURL extension module to fetch the current weather data. It also uses the ScriptBasic LIKE / MATCH functions to parse the returned JSON response.

https://openweathermap.org/

CBASIC offers the a cross platform solution using ScriptBasic's high level C functions to supplement what C doesn't provide native. One can call standard C libraries, ScriptBasic C extension modules and ScriptBasic native C based language functions. It also supports ScriptBasic's variant based thread safe variable structure.


--- Code: C ---// gcc weather.c -IC:\sbgcc\source libscriba.dll -lpthread -o weather #include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include "scriba.h"#include "cbasic.h" MAINBEGIN_FUNCTION  DIM pSbProgram AS pProgram;  DIM pSbData AS arg;  DIM pSbData AS argr;  DIM int AS fnsn;   pProgram = scriba_new(malloc,free);  scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");  scriba_SetFileName(pProgram, "weather.sb");  scriba_LoadSourceProgram(pProgram);  scriba_Run(pProgram, "");   argr = scriba_NewSbArgs(pProgram,"u");  arg = scriba_NewSbArgs(pProgram, "s", "Anacortes,US");  fnsn = scriba_LookupFunctionByName(pProgram, "main::get_weather");  scriba_CallArgEx(pProgram, fnsn, argr, 1, arg);  PRINT("%s\n\n", argr[0].v.s);  DIM char AS mask[] = "*temp\":*,*";  arg = scriba_NewSbArgs(pProgram, "s s", argr[0].v.s, mask);  fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_like");  scriba_CallArgEx(pProgram, fnsn, argr, 2, arg);  arg = scriba_NewSbArgs(pProgram, "i", 2);  fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_match");  scriba_CallArgEx(pProgram, fnsn, argr, 1, arg);  PRINT("Anacortes Temperature is %s F\n", argr[0].v.s);   scriba_DestroySbArgs(pProgram, arg, 2);  scriba_DestroySbArgs(pProgram, argr, 1);  scriba_destroy(pProgram);    RETURN_FUNCTION(0);END_FUNCTION   
weather.sb

--- Code: Script BASIC ---' OpenWeather - Curl Example IMPORT sbembed.sbiIMPORT curl.sbi FUNCTION Get_Weather(place)  ch = curl::init()  curl::option(ch, "URL", "http://api.openweathermap.org/data/2.5/weather?q=" & place & "&units=imperial&appid=MY_API_KEY")  curl::option(ch, "CUSTOMREQUEST", "GET")  response = curl::perform(ch)  curl::finish(ch)  Get_Weather = responseEND FUNCTION 
LIKE / MATCH FUNCTIONS

--- Code: Script BASIC ---' LIKEFUNCTION sb_like(basestr, mask)  ok = basestr LIKE mask  sb_like = okEND FUNCTION ' MATCHFUNCTION sb_match(segment)  sb_match = JOKER(segment)END FUNCTION 

PS C:\ScriptBASIC\cbasic> Measure-Command { .\weather.exe | Out-Host }
{"coord":{"lon":-122.6127,"lat":48.5126},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":52.61,"feels_like":50.9,"temp_min":51.8,"temp_max":54,"pressure":1024,"humidity":71},"visibility":10000,"wind":{"speed":1.9,"deg":286,"gust":3.2},"clouds":{"all":1},"dt":1620617857,"sys":{"type":1,"id":3524,"country":"US","sunrise":1620563861,"sunset":1620617767},"timezone":-25200,"id":5785657,"name":"Anacortes","cod":200}

Anacortes Temperature is 52.61 F

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 296
Ticks             : 2962516
TotalDays         : 3.42883796296296E-06
TotalHours        : 8.22921111111111E-05
TotalMinutes      : 0.00493752666666667
TotalSeconds      : 0.2962516
TotalMilliseconds : 296.2516


46,312  bytes - weather.exe

John:
This is the Excel example in CBASIC. This example uses the ScriptBasic COM/OLE automation extension module.


--- Code: C ---// gcc cbasic_excel.c -IC:\sbgcc\source libscriba.dll -lpthread -o cbasic_excel #include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include "scriba.h"#include "cbasic.h" MAINBEGIN_FUNCTION  DIM pSbProgram AS pProgram;  DIM pSbData AS arg;  DIM pSbData AS argr;  DIM int AS sb_call = 1;  DIM int AS sb_get  = 2;  DIM int AS sb_let  = 4;  DIM int AS sb_set  = 8;  DIM int AS i, j;  DIM char AS src[] = "IMPORT sbembed.sbi\n";   pProgram = scriba_new(malloc,free);  scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");  scriba_SetFileName(pProgram, "none");  scriba_LoadProgramString(pProgram, src, strlen(src));  scriba_Run(pProgram, "");   argr = scriba_NewSbArgs(pProgram,"u");  DIM int AS fn1 = scriba_LookupFunctionByName(pProgram, "main::sb_create");  DIM int AS fn2 = scriba_LookupFunctionByName(pProgram, "main::sb_cbn");  DIM int AS fn3 = scriba_LookupFunctionByName(pProgram, "main::sb_release");  arg = scriba_NewSbArgs(pProgram, "s", "Excel.Application");  scriba_CallArgEx(pProgram, fn1, argr, 1, arg);  DIM unsigned long AS oexcel = argr[0].v.l;  arg = scriba_NewSbArgs(pProgram, "i s i", oexcel, "Workbooks", sb_get);  scriba_CallArgEx(pProgram, fn2, argr, 3, arg);  DIM unsigned long AS oWorkBook = argr[0].v.l;  arg = scriba_NewSbArgs(pProgram, "i s i", oWorkBook, "Add", sb_call);  scriba_CallArgEx(pProgram, fn2, argr, 3, arg);  DIM unsigned long AS oExcelWorkbook = argr[0].v.l;  arg = scriba_NewSbArgs(pProgram, "i s i i", oExcelWorkbook, "Worksheets", sb_get, 1);  scriba_CallArgEx(pProgram, fn2, argr, 4, arg);  DIM unsigned long AS oExcelSheet = argr[0].v.l;  DIM unsigned long AS oCell;  DIM char AS cell_text[16];  DEF_FOR (i = 1 TO i <= 10 STEP INCR i)  BEGIN_FOR    DEF_FOR (j = 1 TO j <= 10 STEP INCR j)    BEGIN_FOR      arg = scriba_NewSbArgs(pProgram, "i s i i i", oExcelSheet, "Cells", sb_get, i, j);      scriba_CallArgEx(pProgram, fn2, argr, 5, arg);      oCell = argr[0].v.l;      sprintf(cell_text, "test-%i-%i", i, j);      arg = scriba_NewSbArgs(pProgram, "i s i s",  oCell, "Value", sb_let, cell_text);      scriba_CallArgEx(pProgram, fn2, argr, 4, arg);      >> Release oCell before next assignment       arg = scriba_NewSbArgs(pProgram, "i", oCell);      scriba_CallArgEx(pProgram, fn3, argr, 1, arg);    NEXT  NEXT  arg = scriba_NewSbArgs(pProgram, "i s i s", oExcelWorkbook, "SaveAs", sb_call, "C:\\ScriptBASIC\\cbasic\\CBASIC_Excel.xls");  scriba_CallArgEx(pProgram, fn2, argr, 4, arg);  arg = scriba_NewSbArgs(pProgram, "i s i", oExcelWorkbook, "Close", sb_call);  scriba_CallArgEx(pProgram, fn2, argr, 3, arg);  arg = scriba_NewSbArgs(pProgram, "i s i", oexcel, "Quit", sb_call);  scriba_CallArgEx(pProgram, fn2, argr, 3, arg);   PRINT("Spreadsheet Created\n");    arg = scriba_NewSbArgs(pProgram, "i", oExcelSheet);  scriba_CallArgEx(pProgram, fn3, argr, 1, arg);  arg = scriba_NewSbArgs(pProgram, "i", oExcelWorkbook);  scriba_CallArgEx(pProgram, fn3, argr, 1, arg);  arg = scriba_NewSbArgs(pProgram, "i", oWorkBook);  scriba_CallArgEx(pProgram, fn3, argr, 1, arg);  arg = scriba_NewSbArgs(pProgram, "i", oexcel);  scriba_CallArgEx(pProgram, fn3, argr, 1, arg);    scriba_DestroySbArgs(pProgram, arg, 5);  scriba_DestroySbArgs(pProgram, argr, 1);  scriba_destroy(pProgram);    RETURN_FUNCTION(0);END_FUNCTION   

C:\ScriptBASIC\cbasic>cbasic_excel
Spreadsheet Created

C:\ScriptBASIC\cbasic>


This is the C source after the pre-processor expansion of Defines and Macros for the CBASIC program above.


--- Code: C ---#include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include "scriba.h"#include "cbasic.h" int main (int argc, char** argv){  pSbProgram pProgram;  pSbData arg;  pSbData argr;  int sb_call = 1;  int sb_get = 2;  int sb_let = 4;  int sb_set = 8;  int i, j;  char src[] = "IMPORT sbembed.sbi\n";   pProgram = scriba_new(malloc,free);  scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");  scriba_SetFileName(pProgram, "none");  scriba_LoadProgramString(pProgram, src, strlen(src));  scriba_Run(pProgram, "");   argr = scriba_NewSbArgs(pProgram,"u");  int fn1 = scriba_LookupFunctionByName(pProgram, "main::sb_create");  int fn2 = scriba_LookupFunctionByName(pProgram, "main::sb_cbn");  int fn3 = scriba_LookupFunctionByName(pProgram, "main::sb_release");  arg = scriba_NewSbArgs(pProgram, "s", "Excel.Application");  scriba_CallArgEx(pProgram, fn1, argr, 1, arg);  unsigned long oexcel = argr[0].v.l;  arg = scriba_NewSbArgs(pProgram, "i s i", oexcel, "Workbooks", sb_get);  scriba_CallArgEx(pProgram, fn2, argr, 3, arg);  unsigned long oWorkBook = argr[0].v.l;  arg = scriba_NewSbArgs(pProgram, "i s i", oWorkBook, "Add", sb_call);  scriba_CallArgEx(pProgram, fn2, argr, 3, arg);  unsigned long oExcelWorkbook = argr[0].v.l;  arg = scriba_NewSbArgs(pProgram, "i s i i", oExcelWorkbook, "Worksheets", sb_get, 1);  scriba_CallArgEx(pProgram, fn2, argr, 4, arg);  unsigned long oExcelSheet = argr[0].v.l;  unsigned long oCell;  char cell_text[16];  for (i = 1 ; i <= 10 ; ++ i)  {    for (j = 1 ; j <= 10 ; ++ j)    {      arg = scriba_NewSbArgs(pProgram, "i s i i i", oExcelSheet, "Cells", sb_get, i, j);      scriba_CallArgEx(pProgram, fn2, argr, 5, arg);      oCell = argr[0].v.l;      sprintf(cell_text, "test-%i-%i", i, j);      arg = scriba_NewSbArgs(pProgram, "i s i s", oCell, "Value", sb_let, cell_text);      scriba_CallArgEx(pProgram, fn2, argr, 4, arg);      arg = scriba_NewSbArgs(pProgram, "i", oCell);      scriba_CallArgEx(pProgram, fn3, argr, 1, arg);    }  }  arg = scriba_NewSbArgs(pProgram, "i s i s", oExcelWorkbook, "SaveAs", sb_call, "C:\\ScriptBASIC\\cbasic\\CBASIC_Excel.xls");  scriba_CallArgEx(pProgram, fn2, argr, 4, arg);  arg = scriba_NewSbArgs(pProgram, "i s i", oExcelWorkbook, "Close", sb_call);  scriba_CallArgEx(pProgram, fn2, argr, 3, arg);  arg = scriba_NewSbArgs(pProgram, "i s i", oexcel, "Quit", sb_call);  scriba_CallArgEx(pProgram, fn2, argr, 3, arg);   printf("Spreadsheet Created\n");   arg = scriba_NewSbArgs(pProgram, "i", oExcelSheet);  scriba_CallArgEx(pProgram, fn3, argr, 1, arg);  arg = scriba_NewSbArgs(pProgram, "i", oExcelWorkbook);  scriba_CallArgEx(pProgram, fn3, argr, 1, arg);  arg = scriba_NewSbArgs(pProgram, "i", oWorkBook);  scriba_CallArgEx(pProgram, fn3, argr, 1, arg);  arg = scriba_NewSbArgs(pProgram, "i", oexcel);  scriba_CallArgEx(pProgram, fn3, argr, 1, arg);   scriba_DestroySbArgs(pProgram, arg, 5);  scriba_DestroySbArgs(pProgram, argr, 1);  scriba_destroy(pProgram);   return(0);} 

John:
A good example of CBASIC is the SDL_gfx extension module I wrote. CBASIC is a weaning language to help BASIC programmers learn C and still feel like home.

You can use as much of CBASIC syntax you wish with your C programs. CBASIC just substitutes C syntax with BASIC.

https://basic-sandbox.us/scriptbasic/extensions/-/blob/master/extensions/gfx/interface.c

John:
I'm working on extending the CBASIC language syntax by including the ScriptBasic embedded interface and its language function wrappers.

I'll get a CBASIC project going in the sandbox to document and provide examples of its use.

Navigation

[0] Message Index

[*] Previous page

Go to full version