Author Topic: O2 SB Embed  (Read 1634 times)

0 Members and 1 Guest are viewing this topic.

JRS

  • Guest
O2 SB Embed
« on: March 26, 2021, 01:39:58 PM »
Charles,

I'm trying to get scriba_NewSbArgs working in O2. You show an example in SbWin2.o2bas which doesn't complain like it does for me. The scriba.inc definition of this function doesn't reference the arguments after the mask string and I get an O2 error with a parameter mismatch. Did you ever get this function to work?

Here is an example of it working in CBASIC.

https://allbasic.info/forum/index.php?topic=628.msg7563#msg7563
« Last Edit: March 26, 2021, 04:36:15 PM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #1 on: March 26, 2021, 07:28:16 PM »
I noticed that you went SB direct with the following rather than using the scriba.inc file. Does this make it easier to call functions with a variable number of arguments?

Code: OxygenBasic
  1.   sys sb=LoadLibrary libScriba
  2.   '
  3.  'http://www.scriptbasic.org/docs/dg/devguide_3.3.html
  4.  extern
  5.   bind sb
  6.   {
  7.   scriba_new                   ' @newmem, @freemem
  8.  scriba_SetStdin()            ' pProgram,@function
  9.  scriba_SetStdout()           ' pProgram,@function
  10.  scriba_SetEmbedPointer()     ' pProgram,@function
  11.  scriba_LoadConfiguration     ' pProgram,filename
  12.  scriba_destroy               ' pProgram
  13.  scriba_DestroySbData         ' pProgram, pData
  14.  scriba_SetFileName           ' pProgram, strptr filename
  15.  scriba_LoadSourceProgram     ' pProgram
  16.  scriba_LoadProgramString     ' pProgram, SrcString, len
  17.  scriba_Run                   ' pProgram, strptr commandline
  18.  scriba_GetVariable           ' pProgram, serial, psbData
  19.  scriba_SetVariable           ' pProgram, serial, type, longVal, doubleVal, charVal*, size
  20.  scriba_LookupVariableByName  ' pProgram, strptr name
  21.  scriba_LookupFunctionByName  ' pProgram, strptr name
  22.  scriba_Call                  ' pProgram, serial
  23.  scriba_CallArg               ' pProgram, serial,ArgFormString i/r/s/b,args...
  24.  scriba_NewSbArgs             ' pProgram, string i/r/s/b,args
  25.  scriba_CallArgEx             ' pProgram, serial,ReturnData,countArgs,*ArgDate
  26.  scriba_DestroySbArgs         ' pProgram, pArgData, CountArgs
  27.  scriba_DestroySbData         ' pProgram, pSbData
  28.  scriba_NewSbString           ' pProgram, char* s
  29.  }
  30.   end extern
  31.  
« Last Edit: March 26, 2021, 10:08:23 PM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #2 on: March 26, 2021, 10:05:54 PM »
I was able to get it working using the extern approach.

ScriptBasic can be used in 4 ways.
  • Console interpreter with stdio redirection
  • Windows interpreter for GUI apps
  • Muli-threaded webserver as a service
  • Enbeddable API as a DLL

Runs on everything.  ANSI C based.

Code: OxygenBasic
  1. ' O2 SB Embed
  2.  
  3. % filename "o2sb.exe"
  4. includepath "$/inc/"
  5. include "RTL32.inc"
  6. % libScriba = "libScriba.dll"
  7. indexbase 0
  8.  
  9. type SbData
  10.   typ as dword
  11.   siz as dword
  12.   union {
  13.     dbl as double
  14.     lng as sys
  15.     str as char*
  16.     gen as sys
  17.   }
  18. end type
  19.  
  20. #define SBT_UNDEF  0
  21. #define SBT_DOUBLE 1
  22. #define SBT_LONG   2
  23. #define SBT_STRING 3
  24. #define SBT_ZCHAR  4
  25.  
  26. sys pProgram, iError, cArgs
  27. sys f1, f2, v
  28. sys n, m
  29. sys qdat
  30. SbData ReturnData, ArgData[3]
  31. sbData pdat
  32.  
  33. sys sb=LoadLibrary libScriba
  34. extern cdecl
  35.   bind sb
  36.   {
  37.   scriba_new
  38.   scriba_SetStdin()
  39.   scriba_SetStdout()
  40.   scriba_SetEmbedPointer()
  41.   scriba_LoadConfiguration
  42.   scriba_destroy
  43.   scriba_DestroySbData
  44.   scriba_SetFileName
  45.   scriba_LoadSourceProgram
  46.   scriba_LoadProgramString
  47.   scriba_Run
  48.   scriba_GetVariable
  49.   scriba_SetVariable
  50.   scriba_LookupVariableByName
  51.   scriba_LookupFunctionByName
  52.   scriba_Call
  53.   scriba_CallArg
  54.   scriba_NewSbArgs
  55.   scriba_CallArgEx
  56.   scriba_DestroySbArgs
  57.   scriba_DestroySbData
  58.   scriba_NewSbString
  59.   }
  60. end extern
  61.  
  62. function newmem cdecl (sys le) as sys, export
  63.   return getmemory le
  64. end function
  65.  
  66. function freemem cdecl (sys p) export
  67.   freememory p
  68. end function
  69.  
  70. pProgram = scriba_new(@newmem, @freemem)
  71. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  72. scriba_SetFileName(pProgram, "test.sb")
  73. scriba_LoadSourceProgram(pProgram)
  74. scriba_Run(pProgram,"")
  75.  
  76. ' Get Global Var  
  77. sbdata *p  
  78. v = scriba_LookupVariableByName(pProgram, "main::a")
  79. scriba_GetVariable(pProgram, v, @@p)
  80. print "A: " + str(p.lng)
  81.  
  82. ' Create SB Variant Array
  83. sbData *arg
  84. @arg = scriba_NewSbArgs(pProgram,"i r s", 1, .2, "three")
  85. print str(arg[0].lng) + " | " + str(arg[1].dbl) + " | " + arg[2].str
  86.  
  87. scriba_destroy(pProgram)
  88.  

test.sb
Code: Script BASIC
  1. a = 99


03/26/2021  11:01 PM            14,336 o2sb.exe


« Last Edit: March 28, 2021, 02:30:16 AM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #3 on: March 27, 2021, 11:36:21 AM »
This example shows how to use ScriptBasic syntax in your Oxygen Basic program.

I can't thank Charles enough for the enormous effort he invested in untangling ScriptBasic's macro pointer C environment to O2.

Code: OxygenBasic
  1. ' O2 SB Syntax Embed
  2.  
  3. % filename "replace.exe"
  4. includepath "$/inc/"
  5. include "RTL32.inc"
  6. % libScriba = "libScriba.dll"
  7. indexbase 0
  8.  
  9. type SbData
  10.   typ as dword
  11.   siz as dword
  12.   union {
  13.     dbl as double
  14.     lng as sys
  15.     str as char*
  16.     gen as sys
  17.   }
  18. end type
  19.  
  20. sys sb = LoadLibrary libScriba
  21. extern cdecl
  22.   bind sb
  23.   {
  24.   scriba_new
  25.   scriba_LoadConfiguration
  26.   scriba_SetFileName
  27.   scriba_LoadSourceProgram
  28.   scriba_Run
  29.   scriba_LookupFunctionByName
  30.   scriba_NewSbArgs
  31.   scriba_CallArgEx
  32.   scriba_DestroySbArgs
  33.   scriba_destroy
  34.   }
  35. end extern
  36.  
  37. function newmem cdecl (sys le) as sys, export
  38.   return getmemory le
  39. end function
  40.  
  41. function freemem cdecl (sys p) export
  42.   freememory p
  43. end function
  44.  
  45. sys pProgram. fnsn
  46. sbData *arg
  47. sbData *argr
  48.  
  49. pProgram = scriba_new(@newmem, @freemem)
  50. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  51. scriba_SetFileName(pProgram, "sbfunctions")
  52. scriba_LoadSourceProgram(pProgram)
  53. scriba_Run(pProgram, "")
  54.  
  55. @arg = scriba_NewSbArgs(pProgram, "s s s i i", "Hello World", "Hello", "Goodbye", 1, 1)
  56. fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_replace")  
  57. @argr = scriba_NewSbArgs(pProgram,"s", "")
  58. scriba_CallArgEx(pProgram, fnsn, argr, 5, arg)
  59. print argr.str
  60.  
  61. scriba_DestroySbArgs(pProgram, arg, 5)
  62. scriba_DestroySbArgs(pProgram, argr, 1)
  63. scriba_destroy(pProgram)
  64.  

sbfunctions
Code: Script BASIC
  1. ' sbfunctions - ScriptBasic syntax function wrappers
  2.  
  3. FUNCTION sb_replace(basestr, searchstr, replacestr, occurances, startpos)
  4.   sb_replace = REPLACE(basestr, searchstr, replacestr, occurances, startpos)
  5. END FUNCTION
  6.  
« Last Edit: March 28, 2021, 02:40:53 AM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #4 on: March 27, 2021, 02:08:21 PM »
This example shows calling a ScriptBasic function that uses the cURL extension module to do a request to the OpenWeather API returning a JSON response.

Code: OxygenBasic
  1. ' O2 SB cURL OpenWeather Example
  2.  
  3. % filename "weather.exe"
  4. includepath "$/inc/"
  5. include "RTL32.inc"
  6. % libScriba = "libScriba.dll"
  7.  
  8. indexbase 0
  9.  
  10. type SbData
  11.   typ as dword
  12.   siz as dword
  13.   union {
  14.     dbl as double
  15.     lng as sys
  16.     str as char*
  17.     gen as sys
  18.   }
  19. end type
  20.  
  21. sys sb = LoadLibrary libScriba
  22. extern cdecl
  23.   bind sb
  24.   {
  25.   scriba_new
  26.   scriba_LoadConfiguration
  27.   scriba_SetFileName
  28.   scriba_LoadSourceProgram
  29.   scriba_Run
  30.   scriba_LookupFunctionByName
  31.   scriba_NewSbArgs
  32.   scriba_CallArgEx
  33.   scriba_DestroySbArgs
  34.   scriba_destroy
  35.   }
  36. end extern
  37.  
  38. function newmem cdecl (sys le) as sys, export
  39.   return getmemory le
  40. end function
  41.  
  42. function freemem cdecl (sys p) export
  43.   freememory p
  44. end function
  45.  
  46. sys pProgram, fnsn
  47. sbData *arg
  48. sbData *argr
  49.  
  50. pProgram = scriba_new(@newmem, @freemem)
  51. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  52. scriba_SetFileName(pProgram, "weather.sb")
  53. scriba_LoadSourceProgram(pProgram)
  54. scriba_Run(pProgram, "")
  55.  
  56. @arg = scriba_NewSbArgs(pProgram, "s", "Anacortes,US")
  57. fnsn = scriba_LookupFunctionByName(pProgram, "main::get_weather")  
  58. @argr = scriba_NewSbArgs(pProgram,"s", "")
  59. scriba_CallArgEx(pProgram, fnsn, argr, 1, arg)
  60. print argr.str
  61.  
  62. scriba_DestroySbArgs(pProgram, arg, 1)
  63. scriba_DestroySbArgs(pProgram, argr, 1)
  64. scriba_destroy(pProgram)
  65.  

weather.sb
Code: Script BASIC
  1. ' OpenWeather - Curl Example
  2.  
  3. IMPORT curl.sbi
  4.  
  5. FUNCTION Get_Weather(place)
  6.   ch = curl::init()
  7.   curl::option(ch, "URL", "http://api.openweathermap.org/data/2.5/weather?q=" & place & "&units=imperial&appid=MY_API_KEY")
  8.   curl::option(ch, "CUSTOMREQUEST", "GET")
  9.   response = curl::perform(ch)
  10.   curl::finish(ch)
  11.   Get_Weather = response
  12. END FUNCTION
  13.  
« Last Edit: March 28, 2021, 02:41:14 AM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #5 on: March 27, 2021, 10:54:54 PM »
It seems that the sbProgram TYPE wasn't needed so I removed it from my examples. The clue was pProgram was being declared as a sys.

I also removed include "minwin.inc" and finit as they weren't required either.

I'm wondering if I can use the scriba.h directly in O2? I'm going to give that a try.
« Last Edit: March 28, 2021, 02:31:57 AM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #6 on: March 27, 2021, 11:50:03 PM »
Charles,

I gave using scriba.h a try but it's complaining about syntax of one of the ScriptBasic .h files. Is this a show stopper using scriba.h with O2?

This seems to be a 'rabbit hole' that keeps getting deeper as I try to fill the holes. The bind seems like the way to go.

Code: OxygenBasic
  1. % filename "weather.exe"
  2. includepath "$/inc/"    
  3. include "RTL32.inc"
  4. includepath "C:\sbgcc\source\"
  5.  
  6. extern lib "libscriba.dll" cdecl  
  7. include "scriba.h"
  8. end extern
  9.  
  10. function newmem cdecl (sys le) as sys, export
  11.   return getmemory le
  12. end function
  13.  
  14. function freemem cdecl (sys p) export
  15.   freememory p
  16. end function
  17.  
  18. sys pProgram, fnsn
  19. sbData *arg
  20. sbData *argr
  21.  
  22. pProgram = scriba_new(@newmem, @freemem)
  23. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  24. scriba_SetFileName(pProgram, "weather.sb")
  25. scriba_LoadSourceProgram(pProgram)
  26. scriba_Run(pProgram, "")
  27.  
  28. @arg = scriba_NewSbArgs(pProgram, "s", "Anacortes,US")
  29. fnsn = scriba_LookupFunctionByName(pProgram, "main::get_weather")  
  30. @argr = scriba_NewSbArgs(pProgram,"s", "")
  31. scriba_CallArgEx(pProgram, fnsn, argr, 1, arg)
  32. print argr.v.s
  33.  
  34. scriba_DestroySbArgs(pProgram, arg, 1)
  35. scriba_DestroySbArgs(pProgram, argr, 1)
  36. scriba_destroy(pProgram)
  37.  

This seems to be the problem area in the lexer.h include file.

Code: C
  1. enum LexemeType {
  2.   LEX_T_DOUBLE = 1,
  3.   LEX_T_LONG,
  4.   LEX_T_STRING,
  5.   LEX_T_ASYMBOL,
  6.   LEX_T_NSYMBOL,
  7.   LEX_T_CHARACTER,
  8.  
  9.   LEX_T_SKIP,  
  10.                
  11.   LEX_T_SKIP_SYMBOL,
  12.  
  13.   LEX_T_DUMMY
  14.   };
  15.  
  16.  
  17. typedef struct _Lexeme {
  18.   enum LexemeType type;  
  19.   union {
  20.     double dValue;        
  21.     long   lValue;        
  22.     char  *sValue;        
  23.     } value;
  24.   long sLen;              
  25.   char *szFileName;      
  26.   long lLineNumber;      
  27.   struct _Lexeme *next;  
  28.   }Lexeme, *pLexeme;
  29.  

I tried the basext.h ScriptBasic include which is the include file for the ScriptBasic extension API.

« Last Edit: March 28, 2021, 02:41:27 AM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #7 on: March 28, 2021, 01:06:56 AM »
I'm moving on with trying to access ScriptBasic array structures from O2. Luckily Dave's SB IDE/Debugger shows SB arrary structures so I have some example code to reference.

FYI
It seems that trying to create a COM  instance via 2 cascading DLLs calls doesn't work. My insurance plan went up in smoke. Hope you have better luck than me with your COM/OLE automation library.

I turns out embedding ScriptBasic in O2 is a lightweight solution for off loading code requirements to ScriptBasic you don't want to recreate the wheel in O2. You could use it as a prototyping tool as well.
« Last Edit: March 28, 2021, 02:39:13 AM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #8 on: March 28, 2021, 04:34:00 AM »
There doesn't seem to be any API based functions to pass ScriptBasic arrays back to the the caller. This is my workaround.  ArrayToXML This shouldn't be too hard to parse on the O2 side as the bounds, variable types and element contents are returned.

Code: OxygenBasic
  1. ' O2 SB Syntax Embed
  2.  
  3. % filename "a2s.exe"
  4. includepath "$/inc/"
  5. include "RTL32.inc"
  6. % libScriba="libScriba.dll"
  7. indexbase 0
  8.  
  9. type SbData
  10.   typ as dword
  11.   siz as dword
  12.   union {
  13.     dbl as double
  14.     lng as sys
  15.     str as char*
  16.     gen as sys
  17.   }
  18. end type
  19.  
  20. sys sb = LoadLibrary libScriba
  21. extern cdecl
  22.   bind sb
  23.   {
  24.   scriba_new
  25.   scriba_LoadConfiguration
  26.   scriba_SetFileName
  27.   scriba_LoadSourceProgram
  28.   scriba_Run
  29.   scriba_LookupFunctionByName
  30.   scriba_NewSbArgs
  31.   scriba_CallArgEx
  32.   scriba_DestroySbArgs
  33.   scriba_destroy
  34.   }
  35. end extern
  36.  
  37. function newmem cdecl (sys le) as sys, export
  38.   return getmemory le
  39. end function
  40.  
  41. function freemem cdecl (sys p) export
  42.   freememory p
  43. end function
  44.  
  45. sys pProgram. fnsn
  46. sbData *arg
  47. sbData *argr
  48.  
  49.  
  50. pProgram = scriba_new(@newmem, @freemem)
  51. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  52. scriba_SetFileName(pProgram, "test.sb")
  53. scriba_LoadSourceProgram(pProgram)
  54. scriba_Run(pProgram, "")
  55.  
  56. @arg = scriba_NewSbArgs(pProgram, "u")
  57. fnsn = scriba_LookupFunctionByName(pProgram, "main::a2s")  
  58. @argr = scriba_NewSbArgs(pProgram,"s", "")
  59. scriba_CallArgEx(pProgram, fnsn, argr, 0, arg)
  60. print argr.str
  61.  
  62. scriba_DestroySbArgs(pProgram, argr, 1)
  63. scriba_destroy(pProgram)
  64.  

test.sb
Code: Script BASIC
  1. declare command ArrayToXML alias "xmlserialize" lib "t"
  2.  
  3. FUNCTION a2s
  4.   b[10] = "Ten"
  5.   b[11] = 11
  6.  
  7.   a[0] = "ZERO"
  8.   a[1] = 1
  9.   a[2] = .2
  10.   a[3] = b
  11.  
  12.   a2s = ArrayToXML(a)
  13. END FUNCTION  
  14.  
  15.  

Array2XML also handles associative arrays.

Code: Script BASIC
  1. declare command ArrayToXML alias "xmlserialize" lib "t"
  2.  
  3. FUNCTION a2s
  4.   a{"One"} = 1
  5.   a{"Two"} = .2
  6.   a{"3"} = "Three"
  7.   a2s = ArrayToXML(a)
  8. END FUNCTION  
  9.  
« Last Edit: March 28, 2021, 05:24:40 PM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #9 on: March 28, 2021, 02:58:04 PM »
Code Challenge:

Anyone interested in creating a routine in O2 to parse the XML array definition I return from SB to an O2 array?

I would love to have a SPLITA and LIKE feature in O2.
« Last Edit: March 28, 2021, 03:15:32 PM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #10 on: March 28, 2021, 07:11:01 PM »
I started building the sbembed.inc file and got console support working.

Code: OxygenBasic
  1. ' O2 SB Array Passing
  2.  
  3. % filename "a2s.exe"
  4. includepath "$/inc/"
  5. include "RTL32.inc"
  6. include "sbembed.inc"
  7. #console
  8. uses console
  9.  
  10. sys pProgram. fnsn
  11. sbData *arg
  12. sbData *argr
  13.  
  14. pProgram = scriba_new(@newmem, @freemem)
  15. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  16. scriba_SetFileName(pProgram, "test.sb")
  17. scriba_LoadSourceProgram(pProgram)
  18. scriba_Run(pProgram, "")
  19.  
  20. @arg = scriba_NewSbArgs(pProgram, "u")
  21. fnsn = scriba_LookupFunctionByName(pProgram, "main::a2s")  
  22. @argr = scriba_NewSbArgs(pProgram,"s", "")
  23. scriba_CallArgEx(pProgram, fnsn, argr, 0, arg)
  24. print argr.str
  25.  
  26. scriba_DestroySbArgs(pProgram, arg, 1)
  27. scriba_DestroySbArgs(pProgram, argr, 1)
  28. scriba_destroy(pProgram)
  29.  


test.sb
Code: Script BASIC
  1. DECLARE COMMAND ArrayToXML ALIAS "xmlserialize" LIB "t"
  2.  
  3. FUNCTION a2s
  4.   a{"One"} = 1
  5.   a{"Two"} = .2
  6.   a{"3"} = "Three"
  7.   a2s = ArrayToXML(a)
  8. END FUNCTION  
  9.  


sbembed.inc
Code: OxygenBasic
  1. ' ScriptBasic Embedding Include
  2.  
  3. % libScriba = "libScriba.dll"
  4.  
  5. type SbData
  6.   typ as dword
  7.   siz as dword
  8.   union {
  9.     dbl as double
  10.     lng as sys
  11.     str as char*
  12.     gen as sys
  13.   }
  14. end type
  15.  
  16. sys sb = LoadLibrary libScriba
  17. extern cdecl
  18.   bind sb
  19.   {
  20.   scriba_new
  21.   scriba_LoadConfiguration
  22.   scriba_SetFileName
  23.   scriba_LoadSourceProgram
  24.   scriba_Run
  25.   scriba_LookupFunctionByName
  26.   scriba_NewSbArgs
  27.   scriba_CallArgEx
  28.   scriba_DestroySbArgs
  29.   scriba_destroy
  30.   }
  31. end extern
  32.  
  33. function newmem cdecl (sys le) as sys, export
  34.   return getmemory le
  35. end function
  36.  
  37. function freemem cdecl (sys p) export
  38.   freememory p
  39. end function
  40.  


C:\OxygenBasic\projectsC\ScriptBasic\EmbedSb>a2s
Code: XML
  1. <?xml version="1.0" encoding="UTF-8"?><V><A l="0" h="5"><S>One</S><I>1</I><S>Two</S><R>0.200000</R><S>3</S><S>Three</S></A></V>
C:\OxygenBasic\projectsC\ScriptBasic\EmbedSb>

a2s.exe Size:  14,848 bytes
Total Seconds: 0.066272

I will be enhancing this include to make the interface to ScriptBasic more seamless.

« Last Edit: March 29, 2021, 02:03:12 AM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #11 on: March 28, 2021, 11:17:25 PM »
This is the REPLACE example but the ScriptBasic function wrapper is embedded in the O2 program. You can also load the binary format of your program (ready to run) which also protects your ScriptBasic code from viewing.

Code: OxygenBasic
  1. ' O2 - SB Source Embedding
  2.  
  3. % filename "repsrc.exe"
  4. includepath "$/inc/"
  5. include "RTL32.inc"
  6. include "sbembed.inc"
  7. #console
  8. uses console
  9.  
  10. sys pProgram, fnsn
  11. sbData *arg
  12. sbData *argr
  13. string cr = chr(13, 10)
  14. string src = "FUNCTION sb_replace(basestr, searchstr, replacestr, occurances, startpos)" + cr + _
  15.              "sb_replace = REPLACE(basestr, searchstr, replacestr, occurances, startpos)" + cr + _
  16.              "END FUNCTION" + cr
  17.  
  18. pProgram = scriba_new(@newmem, @freemem)
  19. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  20. scriba_SetFileName(pProgram, "none")
  21. scriba_LoadProgramString(pProgram, src, len(src))
  22. scriba_Run(pProgram, "")
  23.  
  24. @arg = scriba_NewSbArgs(pProgram, "s s s i i", "Hello World", "Hello", "Goodbye", 1, 1)
  25. fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_replace")  
  26. @argr = scriba_NewSbArgs(pProgram,"s", "")
  27. scriba_CallArgEx(pProgram, fnsn, argr, 5, arg)
  28. print argr.str + cr
  29.  
  30. scriba_DestroySbArgs(pProgram, arg, 5)
  31. scriba_DestroySbArgs(pProgram, argr, 1)
  32. scriba_destroy(pProgram)
  33.  


C:\OxygenBasic\projectsC\ScriptBasic\EmbedSb>repsrc
Goodbye World

C:\OxygenBasic\projectsC\ScriptBasic\EmbedSb>
« Last Edit: March 29, 2021, 02:11:02 AM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #12 on: March 29, 2021, 05:20:38 PM »
I've cleaned up the Array2XML extension module function so it's easier to parse.

Code: XML
  1. <V><A LB="0" UB="5"><S>One</S><I>1</I><S>Two</S><R>0.200000</R><S>3</S><S>Three</S></A></V>

I'm working on adding SPLITA and LIKE callable functions from O2. The LIKE could be used to parse the XML array definition being returned,

Code: OxygenBasic
  1. ' O2 SB Array Passing
  2.  
  3. % filename "a2s.exe"
  4. includepath "$/inc/"
  5. include "RTL32.inc"
  6. include "sbembed.inc"
  7. #console
  8. uses console
  9.  
  10. sys pProgram. fnsn
  11. sbData *arg
  12. sbData *argr
  13.  
  14. pProgram = scriba_new(@newmem, @freemem)
  15. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  16. scriba_SetFileName(pProgram, "test.sb")
  17. scriba_LoadSourceProgram(pProgram)
  18. scriba_Run(pProgram, "")
  19.  
  20. @arg = scriba_NewSbArgs(pProgram, "u")
  21. fnsn = scriba_LookupFunctionByName(pProgram, "main::a2s")  
  22. @argr = scriba_NewSbArgs(pProgram,"s", "")
  23. scriba_CallArgEx(pProgram, fnsn, argr, 0, arg)
  24. print argr.str
  25.  
  26. scriba_DestroySbArgs(pProgram, argr, 1)
  27. scriba_destroy(pProgram)
  28.  

Code: Script BASIC
  1. DECLARE COMMAND ArrayToXML ALIAS "xmlserialize" LIB "t"
  2.  
  3. FUNCTION a2s
  4.   a{"One"} = 1
  5.   a{"Two"} = .2
  6.   a{"3"} = "Three"
  7.   a2s = ArrayToXML(a)
  8. END FUNCTION  
  9.  
« Last Edit: March 29, 2021, 06:00:05 PM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #13 on: March 29, 2021, 09:20:00 PM »
I got LIKE and MATCH working. This example shows using LIKE to parse the JSON response and using MATCH to extract the temperature. This example uses the new sb_embed.sbi include which is a library of ScriptBasic function wrappers.

weather.o2bas
Code: OxygenBasic
  1. ' O2 SB cURL OpenWeather Example
  2.  
  3. % filename "weather.exe"
  4. includepath "$/inc/"
  5. include "RTL32.inc"
  6. include "sbembed.inc"
  7. #console
  8. uses console
  9.  
  10. sys pProgram, fnsn
  11. sbData *arg
  12. sbData *argr
  13. string cr = chr(13,10)
  14.  
  15. pProgram = scriba_new(@newmem, @freemem)
  16. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  17. scriba_SetFileName(pProgram, "weather.sb")
  18. scriba_LoadSourceProgram(pProgram)
  19. scriba_Run(pProgram, "")
  20.  
  21. @arg = scriba_NewSbArgs(pProgram, "s", "Anacortes,US")
  22. fnsn = scriba_LookupFunctionByName(pProgram, "main::get_weather")  
  23. @argr = scriba_NewSbArgs(pProgram,"s", "")
  24. scriba_CallArgEx(pProgram, fnsn, argr, 1, arg)
  25. string json = argr.str
  26. string mask = `*temp":*,*`
  27. @arg = scriba_NewSbArgs(pProgram, "s s", json, mask)
  28. fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_like")  
  29. @argr = scriba_NewSbArgs(pProgram,"i", 0)
  30. scriba_CallArgEx(pProgram, fnsn, argr, 2, arg)
  31. @arg = scriba_NewSbArgs(pProgram, "i", 2)
  32. fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_match")
  33. @argr = scriba_NewSbArgs(pProgram,"s", "")
  34. scriba_CallArgEx(pProgram, fnsn, argr, 1, arg)
  35. print "Anacortes Temperature is " + argr.str + " F" + cr
  36.  
  37. scriba_DestroySbArgs(pProgram, arg, 2)
  38. scriba_DestroySbArgs(pProgram, argr, 1)
  39. scriba_destroy(pProgram)
  40.  

weather.sb
Code: Script BASIC
  1. ' OpenWeather - Curl Example
  2.  
  3. IMPORT sbembed.sbi
  4. IMPORT curl.sbi
  5.  
  6. FUNCTION Get_Weather(place)
  7.   ch = curl::init()
  8.   curl::option(ch, "URL", "http://api.openweathermap.org/data/2.5/weather?q=" & place & "&units=imperial&appid=MY_API_KEY")
  9.   curl::option(ch, "CUSTOMREQUEST", "GET")
  10.   response = curl::perform(ch)
  11.   curl::finish(ch)
  12.   Get_Weather = response
  13. END FUNCTION
  14.  

sbembed.sbi
Code: Script BASIC
  1. ' sbembed.sbi - ScriptBasic Embedded Function Wrappers
  2.  
  3. ' REPLACE
  4. FUNCTION sb_replace(basestr, searchstr, replacestr, occurances, startpos)
  5.   sb_replace = REPLACE(basestr, searchstr, replacestr, occurances, startpos)
  6. END FUNCTION
  7.  
  8. ' LIKE
  9. FUNCTION sb_like(basestr, mask)
  10.   ok = basestr LIKE mask
  11.   sb_like = ok
  12. END FUNCTION
  13.  
  14. ' MATCH
  15. FUNCTION sb_match(segment)
  16.   sb_match = JOKER(segment)
  17. END FUNCTION  
  18.  


C:\OxygenBasic\projectsC\ScriptBasic\EmbedSb>weather
Anacortes Temperature is 39.27 F

C:\OxygenBasic\projectsC\ScriptBasic\EmbedSb>



« Last Edit: March 29, 2021, 10:06:08 PM by John »

JRS

  • Guest
Re: O2 SB Embed
« Reply #14 on: March 29, 2021, 11:05:59 PM »
I added SPLITA to the sbembed.sbi include. This also shows parsing a XML array returned string with LIKE / MATCH.

Notable Attributes
  • The splitby argument can be more than one character
  • The ScriptBasic program loaded was a string that consisted of a IMPORT of the sbembed.sbi include file. IMPORT only loads the code once even with multiple IMPORTs of the same module.

Code: Script BASIC
  1. ' SPLITA
  2. FUNCTION sb_splita(basestr, splitby)
  3.   SPLITA basestr BY splitby TO splitarray
  4.   sb_splita = ArrayToXML(splitarray)
  5. END FUNCTION  
  6.  

splita.o2bas
Code: OxygenBasic
  1. ' O2 - SB SPLITA Example
  2.  
  3. % filename "splita.exe"
  4. includepath "$/inc/"
  5. include "RTL32.inc"
  6. include "sbembed.inc"
  7. #console
  8. uses console
  9.  
  10. sys pProgram, fnsn
  11. sbData *arg
  12. sbData *argr
  13. string cr = chr(13, 10)
  14. string src = "IMPORT sbembed.sbi" + cr
  15. int i
  16.  
  17. pProgram = scriba_new(@newmem, @freemem)
  18. scriba_LoadConfiguration(pProgram, "C:\Windows\SCRIBA.INI")
  19. scriba_SetFileName(pProgram, "none")
  20. scriba_LoadProgramString(pProgram, src, len(src))
  21. scriba_Run(pProgram, "")
  22.  
  23. @argr = scriba_NewSbArgs(pProgram,"s", "")
  24. string datalist = "1,.2,Three"
  25. fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_splita")
  26. @arg = scriba_NewSbArgs(pProgram, "s s", datalist, ",")
  27. scriba_CallArgEx(pProgram, fnsn, argr, 2, arg)
  28. print argr.str + cr + cr
  29. string xml = argr.str
  30. string mask = "*<S>*</S><S>*</S><S>*</S>*"
  31. @arg = scriba_NewSbArgs(pProgram, "s s", xml, mask)
  32. fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_like")  
  33. @argr = scriba_NewSbArgs(pProgram,"i", 0)
  34. scriba_CallArgEx(pProgram, fnsn, argr, 2, arg)
  35. fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_match")
  36. @argr = scriba_NewSbArgs(pProgram,"s", "")
  37. for i = 2 to 4
  38.   @arg = scriba_NewSbArgs(pProgram, "i", i)
  39.   scriba_CallArgEx(pProgram, fnsn, argr, 1, arg)
  40.   print argr.str + cr
  41. next i
  42.  
  43. scriba_DestroySbArgs(pProgram, arg, 2)
  44. scriba_DestroySbArgs(pProgram, argr, 1)
  45. scriba_destroy(pProgram)
  46.  

Code: XML
  1. <V><A LB="0" UB="2"><S>1</S><S>.2</S><S>Three</S></A></V>

1
.2
Three

« Last Edit: March 30, 2021, 01:19:01 AM by John »