declare sub trial alias "trial" lib "mdlt"
a=12345+0.67
b="HELLO"
c=8
d=9
print "Sent params: " & b & " " & a & " " & c & " " & d & "\n"
print trial(a,b,c,d)
print "Altered params: " & b & " " & a & " " & c & " " & d & "\n"
line input w
'extension module for ScriptBasic
'23:02 11/05/2011
'17:25 15/05/2012
'
'Charles Pegge
'#file "mdlt.dll"
'
$ dll
$ Filename "mdlt.dll"
include "..\..\inc\RTL32.inc"
function GetStringParam(sys pst,pm) as string
'============================================
sys c,p
c=*(pst+0xf4)
p=*pst
p=call c p, pm, *(p+0x8c)
zstring pz at *p
return pz
end function
function PutStringParam(sys pst,pm,string s)
'===========================================
sys c,p
c=*(pst+0xf4)
p=*pst
p=call c p, pm, *(p+0x8c)
zstring pz at *p
pz=s
end function
function GetLongParam(sys pst,pm) as long
'========================================
sys c,p
c=*(pst+0xf8)
p=*pst
p=call c p, pm, *(p+0x8c)
return *p
end function
function PutLongParam(sys pst,pm, sys v)
'=======================================
sys c,p
c=*(pst+0xf8)
p=*pst
p=call c p, pm, *(p+0x8c)
*p=v
end function
function GetDoubleParam(sys pst,pm) as double
'============================================
sys c,p
c=*(pst+0xfc)
p=*pst
p=call c p, pm, *(p+0x8c)
double pd at p
return pd
end function
function PutDoubleParam(sys pst,pm,double d)
'===========================================
sys c,p
c=*(pst+0xfc)
p=*pst
p=call c p, pm, *(p+0x8c)
double pd at p
pd=d
end function
sub ReturnString(sys pst,pRetVal,string s)
'=========================================
sys c,p,ls
ls=len(s)
c=*(pst+0x0c)
p=*pst
p=call c *(p+0x94),ls , *(p+0x8c)
*pRetVal=p
copy *p,strptr s,ls
end sub
sub ReturnLong(sys pst,pRetVal,Long v)
'=====================================
sys c,p
c=*(pst+0x10)
p=*pst
p=call c *(p+0x94), *(p+0x8c)
*pRetVal=p
long pv at p
pv=v
'
'mov ecx,pst : mov eax,[ecx] : push [eax+0x8c] : push [eax+0x94] : call [ecx+0x10]
'mov ecx,pReturnValue : mov [ecx],eax : mov edx,v : mov [eax],edx
''
end sub
sub ReturnDouble(sys pst,pRetVal,double d)
'=========================================
sys c,p
c=*(pst+0x18)
p=*pst
p=call c *(p+0x94), *(p+0x8c)
*pRetVal=p
double pd at p
pd=d
end sub
function GetParamPtr(sys pParams, pn) as sys
'===========================================
return *(*pParams+pn*4-4) 'pointer to parameter
end function
function versmodu cdecl (sys Version, pszVariation, ppModuleInternal) as sys export
'==================================================================================
'print "Version: " hex version
return Version
end function
function bootmodu cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys export
'===========================================================================================
'print "Boot!"
end function
function finimodu cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys export
'===========================================================================================
'print "Finish!"
end function
function trial cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys export
'========================================================================================
sys w,pm,pn
string cr=chr(13)+chr(10)
'
sys v
string pr
double d
'
if pParameters then
'
'HOW MANY ARGUMENTS GIVEN
'------------------------
'
w=*(pParameters+8) >> 2
'
'print "No of Arguments given: " w
'
'PARAM pn
'--------
'
'pParameters->Value.aValue[(X)-1]
'
for pn=1 to w
'
'index base 1
'
pm=GetParamPtr(pparameters,pn)
pr+= "As String: " GetStringParam(pst,pm) + cr +
"As Long: " GetLongParam(pst,pm) + cr +
"As Double: " GetDoubleParam(pst,pm) + cr +
cr
next
'
pm=GetParamPtr(pParameters,1)
PutDoubleParam(pst,pm,1111.11)
pm=GetParamPtr(pParameters,2)
PutStringParam(pst,pm,"hello")
pm=GetParamPtr(pParameters,3)
PutLongParam(pst,pm,3)
pm=GetParamPtr(pParameters,4)
PutLongParam(pst,pm,4)
'
else
'noparams
end if
'
'ReturnLong(pst,pReturnValue,1234.5)
'ReturnDouble(pst,pReturnValue,1.234.5)
ReturnString(pst,pReturnValue,"From Oxygen:"+cr+cr+pr)
'
end function
'23:02 11/05/2011
'17:25 15/05/2012
declare sub SQLite3_Demo alias "sqlite3_demo" lib "sqlite3_mdl"
declare sub SQLite3_ErrMsg alias "sqlite3_errmsg" lib "sqlite3_mdl"
declare sub SQLite3_Open alias "sqlite3_open" lib "sqlite3_mdl"
declare sub SQLite3_Close alias "sqlite3_close" lib "sqlite3_mdl"
declare sub SQLite3_Exec alias "sqlite3_exec" lib "sqlite3_mdl"
declare sub SQLite3_Prepare_v2 alias "sqlite3_prepare_v2" lib "sqlite3_mdl"
declare sub SQLite3_Step alias "sqlite3_step" lib "sqlite3_mdl"
declare sub SQLite3_Column_Text alias "sqlite3_column_text" lib "sqlite3_mdl"
SQLITE_ROW = 100
'print SQLite3_Demo()
hdb=0
dberr=0
stmt=0
pr="DataBase Listing:\n"
result=0
'
sqlite3_open("testsql",hdb)
'
sqlite3_exec (hdb, "CREATE TABLE demo(someval INTEGER, sometxt TEXT);", 0, 0, dberr)
sqlite3_exec (hdb, "INSERT INTO demo VALUES (123, 'Hello');", 0, 0, dberr)
sqlite3_exec (hdb, "INSERT INTO demo VALUES (234, 'cruel');", 0, 0, dberr)
sqlite3_exec (hdb, "INSERT INTO demo VALUES (345, 'world');", 0, 0, dberr)
'
result = sqlite3_prepare_v2 (hdb, "SELECT * FROM demo;", -1, stmt, 0)
'
if dberr then
print SQLite3_ErrMsg(dberr) & "\n"
endif
'
while (sqlite3_step(stmt) = SQLITE_ROW)
pr=pr & sqlite3_column_text(stmt, 0) & " - " & sqlite3_column_text(stmt, 1) & "\n"
wend
'
sqlite3_close(hdb)
print pr
line input w
for (i = 0; i < n; i++) {
ARRAYVALUE(*Lval,i) = besNEWSTRING(strlen(names[i]));
memcpy(STRINGVALUE(ARRAYVALUE(*Lval,i)), names[i], strlen(names[i]));
}
T=trial(a,b,x,y)
't[1]=123
print T[0] & T[1] & T[2] & T[3] & T[4]
p=CreateArray(pst,pReturnValue,0,16)
for i=0 to 16
ReturnString(pst,@a,"Val:"+str(i*10+1))
q=*p+i*4
*q=a
next
*pReturnValue=p
besFUNCTION(PuiInfo)
VARIABLE Argument;
unsigned long __refcount_;
LEFTVALUE Lval;
char buffer[1024];
int i;
const char *glbvar[] = {
"SYSTEMLANGUAGE",
"DRIVER",
"SYSTEM",
"SYSTEMLOCALE",
"COMPUTERNAME",
"USERNAME",
"MONITORSINFO",
"SCREENSIZE",
"SCREENDEPTH",
"VIRTUALSCREEN",
"DLGFGCOLOR",
"DLGBGCOLOR",
"DEFAULTFONT",
"DEFAULTFONTSIZE",
"TXTFGCOLOR",
"TXTBGCOLOR"
};
besRETURNVALUE = NULL;
Argument = besARGUMENT(1);
besLEFTVALUE(Argument,Lval);
besRELEASE(*Lval);
*Lval = NULL;
*Lval = besNEWARRAY(0,ELEMENTS(glbvar)*2-1);
if( *Lval == NULL )return COMMAND_ERROR_MEMORY_LOW;
for (i =0; i < ELEMENTS(glbvar);i++) {
ARRAYVALUE(*Lval,2*i) = besNEWSTRING(strlen(glbvar[i]));
memcpy(STRINGVALUE(ARRAYVALUE(*Lval,2*i)),glbvar[i],strlen(glbvar[i]));
memset(buffer,0,1024);
strcpy(buffer, IupGetGlobal(glbvar[i]));
ARRAYVALUE(*Lval,2*i+1) = besNEWSTRING(strlen(buffer));
memcpy(STRINGVALUE(ARRAYVALUE(*Lval,2*i+1)),buffer,strlen(buffer));
}
besALLOC_RETURN_LONG;
LONGVALUE(besRETURNVALUE) = -1;
besEND
You can return the whole array to scriptbasic as a return value.
declare sub SQLite3_Demo alias "sqlite3_demo" lib "sqlite3_mdl"
declare sub SQLite3_Table alias "sqlite3_table" lib "sqlite3_mdl"
declare sub SQLite3_ErrMsg alias "sqlite3_errmsg" lib "sqlite3_mdl"
declare sub SQLite3_Open alias "sqlite3_open" lib "sqlite3_mdl"
declare sub SQLite3_Close alias "sqlite3_close" lib "sqlite3_mdl"
declare sub SQLite3_Exec alias "sqlite3_exec" lib "sqlite3_mdl"
declare sub SQLite3_Prepare_v2 alias "sqlite3_prepare_v2" lib "sqlite3_mdl"
declare sub SQLite3_Step alias "sqlite3_step" lib "sqlite3_mdl"
declare sub SQLite3_Column_Text alias "sqlite3_column_text" lib "sqlite3_mdl"
SQLITE_ROW = 100
'
'
T=SQLite3_Table()
e=ubound(t)
print e & "\n\n"
for i=0 to e step 2
print T[i] & " - " & T[i+1] & "\n"
next
'
'
line input w
function sqlite3_table cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys, export
'=================================================================================================
indexbase 1
sys b,e,i
sys hdb
sys dberr
sys stmt
sys result
string s[4000]
'
char * errmsg
string cr=chr(13)+chr(10)
string pr="DataBase Listing:" cr
'
sqlite3_open "testsql",hdb
'
sqlite3_exec hdb, "CREATE TABLE demo(someval INTEGER, sometxt TEXT);", 0, 0, dberr
sqlite3_exec hdb, "INSERT INTO demo VALUES (123, 'Hello');", 0, 0, dberr
sqlite3_exec hdb, "INSERT INTO demo VALUES (234, 'cruel');", 0, 0, dberr
sqlite3_exec hdb, "INSERT INTO demo VALUES (345, 'world');", 0, 0, dberr
'
result = sqlite3_prepare_v2 hdb, "SELECT * FROM demo;" cr, -1, stmt, 0
'
if dberr then @errmsg=dberr : print errmsg
'
'
'COLLECT ALL DATA
'
i=0
while sqlite3_step(stmt) = SQLITE_ROW
i++ : s[i]=sqlite3_column_text(stmt, 0)
i++ : s[i]=sqlite3_column_text(stmt, 1)
if i>=4000 then exit do
wend
'
sqlite3_close hdb
'
'
'RETURNING ARRAY OF VALUES
'=========================
'
b=0 'CHOOSE LBOUND
e=i-1 'CHOOSE UBOUND
'
'pm=GetParamPtr(pParameters,3)
'PutLongParam(pst,pm,b) 'PASS BACK LBOUND
'pm=GetParamPtr(pParameters,4)
'PutLongParam(pst,pm,e) 'PASS BACK UBOUND
'
if e<b then exit function
'
p=CreateArray(pst,b,e)
*pReturnValue=p
'
'
'FILL ARRAY
'
for i= 0 to e
a=ReturnString pst, s[i+1]
q=*p+i*4
*q=a
next
'
end function
/*
Ihandle *IupSetAttributes(Ihandle *ih, const char *str); <iup.h>
Defines a set of attributes for an interface element.
*/
besFUNCTION(PuiSetAttributes)
Ihandle *ih, *sameih;
const char *attstr;
besARGUMENTS("pz")
&ih, &attstr
besARGEND
sameih = IupSetAttributes(ih, attstr);
besRETURN_POINTER(sameih);
besEND
/**REPLACE
=section string
=title REPLACE(base_string,search_string,replace_string [,number_of_replaces] [,position])
=display REPLACE()
This function replaces one or more occurrences of a sub-string in a string.
T<REPLACE(a,b,c)> searches the string T<a> seeking for occurrences of sub-string T<b>
and replaces each of them with the string T<c>.
The fourth and fifth arguments are optional. The fourth argument specifies the number of
replaces to be performed. If this is missing or is T<undef> then all occurrences of string
T<b> will be replaced. The fifth argument may specify the start position of the operation.
For example the function call
=verbatim
REPLACE("alabama mama", "a","x",3,5)
=noverbatim
will replace only three occurrences of string T<"a"> starting at position 5.
The result is T<"alabxmx mxma">.
*/
COMMAND(REPLACE)
#if NOTIMP_REPLACE
NOTIMPLEMENTED;
#else
NODE nItem;
VARIABLE Op1,Op2,Op3,Op4,Op5;
long lRepetitions;
long lCalculatedRepetitions;
int ReplaceAll;
long l_start,lStart,lLength,lSearchLength,lReplaceLength,lResult;
char *r,*s,*q,*w;
int iCase = OPTION("compare")&1;
/* this is an operator and not a command, therefore we do not have our own mortal list */
USE_CALLER_MORTALS;
/* evaluate the parameters */
nItem = PARAMETERLIST;
/* this is the base string that we are searching in */
Op1 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op1) ){
RESULT = NULL;
RETURN;
}
Op1 = CONVERT2STRING(Op1);
nItem = CDR(nItem);
lLength = STRLEN(Op1);
r = STRINGVALUE(Op1);
/* this is the string that we search to replace */
Op2 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op2) ){
RESULT = NULL;
RETURN;
}
Op2 = CONVERT2STRING(Op2);
nItem = CDR(nItem);
lSearchLength = STRLEN(Op2);
s = STRINGVALUE(Op2);
/* this is the string that we put into the place of the searched string */
Op3 = _EVALUATEEXPRESSION(CAR(nItem));
ASSERTOKE;
if( memory_IsUndef(Op3) ){
RESULT = NULL;
RETURN;
}
Op3 = CONVERT2STRING(Op3);
lReplaceLength = STRLEN(Op3);
nItem = CDR(nItem);
w = STRINGVALUE(Op3);
Op4 = NULL;
if( nItem ){
Op4 = EVALUATEEXPRESSION(CAR(nItem));
nItem = CDR(nItem);
ASSERTOKE;
}
if( memory_IsUndef(Op4) ){
lRepetitions = 0;
ReplaceAll = 1;
}else{
lRepetitions = GETLONGVALUE(Op4);
ReplaceAll = 0;
}
if( lRepetitions < 0 )lRepetitions = 0;
Op5 = NULL;
if( nItem ){
Op5 = EVALUATEEXPRESSION(CAR(nItem));
nItem = CDR(nItem);
ASSERTOKE;
}
if( memory_IsUndef(Op5) )
l_start = 1;
else{
l_start = GETLONGVALUE(Op5);
}
if( l_start < 1 )l_start = 1;
lStart = l_start;
/* first calculate the repeat actions */
lCalculatedRepetitions = 0;
while( lStart-1 <= lLength - lSearchLength ){
if( ! SUBSTRCMP(r+lStart-1,s, lSearchLength,iCase ) ){
lCalculatedRepetitions++;
lStart += lSearchLength;
}else lStart ++;
}
if( ! ReplaceAll && lCalculatedRepetitions > lRepetitions )lCalculatedRepetitions = lRepetitions;
/* calculate the length of the new string */
lResult = STRLEN(Op1) + lCalculatedRepetitions * (lReplaceLength-lSearchLength);
/* allocate space for the result */
RESULT = NEWMORTALSTRING(lResult);
ASSERTNULL(RESULT)
/* perform the replacements */
lStart = l_start;
q = STRINGVALUE(RESULT);
if( lStart > 1 ){
memcpy(q,r,lStart-1);
q+=lStart-1;
}
while( lStart <= lLength ){
if( lCalculatedRepetitions && ! SUBSTRCMP(r+lStart-1,s, lSearchLength,iCase ) ){
memcpy(q,w,lReplaceLength);
q += lReplaceLength;
lStart += lSearchLength;
lCalculatedRepetitions--;
}else{
*q++ = r[lStart-1];
lStart ++;
}
}
#endif
END
s ="abcdefghefghefgh"
i_max = 1024 / LEN(s) * 1024 * 4
starttime = NOW
PRINT "exec.tm.sec\tstr.length\n"
gstr=""
i=0
c = 0
WHILE ( i < i_max + 1000 )
i=i+1
gstr&=s
lngth=LEN(s)*i
gstr=REPLACE(gstr,"efgh","____",undef,lngth-11)
IF(lngth % (1024*256) = 0) THEN
PRINT INT(NOW -starttime)," sec\t\t",(lngth/1024)," KB\n"
c+=1
IF c= 2 THEN GOTO finito
END IF
WEND
finito:
PRINT "\nFinished\n"
Superb cheating
I frown at the benchmark, not at SB's smart replace :)
If I am allowed to use a concatenation buffer ...
You can do the same with ScriptBasic
The string processing capabilities of Scriptbasic are impressive, especially its performance. It outperforms almost every other BASIC including BaCon.
DIM(array, #elem, [base_value,] [step,] [inc,] [start_elem])
a[1]="one","two",3,4,5
s = STRING(5,"0")
SPLITA s BY "" TO a
a[0] 0
...
a[4] 0
s = "Hello World,123,.25"
SPLITA s BY "," TO a
FOR x = LBOUND(a) TO UBOUND(a)
PRINT a[x],"\n"
NEXT
s = "Hello World,123,.25"
SPLITA s BY "," TO a[100]
FOR x = LBOUND(a[100]) TO UBOUND(a[100])
PRINT a[100,x],"\n"
NEXT
s = "10,20,30.5"
SPLITA s BY "," TO Customer{"Balance"}
FOR x = LBOUND(Customer{"Balance"}) TO UBOUND(Customer{"Balance"})
PRINT FORMAT("%.2f",Customer{"Balance"}[x]),"\n"
NEXT
DECLARE SUB DIM ALIAS "DIM" LIB "cbx"
DIM(a,1000000)
x = UBOUND(a)
PRINT "UBOUND: ", x, " = ", a[x], "\n"
SPLITA STRING(1000000,"0") BY "" TO a
x = UBOUND(a)
PRINT "UBOUND: ", x, " = ", a[x], "\n"
PRINT
/* Core Extension Library */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../basext.h"
typedef struct _ModuleObject {
void *HandleArray;
}ModuleObject,*pModuleObject;
besVERSION_NEGOTIATE
return (int)INTERFACE_VERSION;
besEND
besSUB_START
pModuleObject p;
besMODULEPOINTER = besALLOC(sizeof(ModuleObject));
if( besMODULEPOINTER == NULL )return 0;
p = (pModuleObject)besMODULEPOINTER;
return 0;
besEND
besSUB_FINISH
pModuleObject p;
p = (pModuleObject)besMODULEPOINTER;
if( p == NULL )return 0;
return 0;
besEND
besFUNCTION(DIM)
VARIABLE Argument;
unsigned long __refcount_;
LEFTVALUE Lval;
int i, e;
besRETURNVALUE = NULL;
Argument = besARGUMENT(2);
besDEREFERENCE(Argument);
e = (int)LONGVALUE(Argument);
Argument = besARGUMENT(1);
besLEFTVALUE(Argument,Lval);
besRELEASE(*Lval);
*Lval = NULL;
*Lval = besNEWARRAY(0, e-1);
if( *Lval == NULL )return COMMAND_ERROR_MEMORY_LOW;
for( i= 0 ; ((unsigned)i) < e ; i++ ){
ARRAYVALUE(*Lval,i) = besNEWSTRING(1);
memcpy(STRINGVALUE(ARRAYVALUE(*Lval,i)),"0",1);
}
besEND
module o2
declare sub ::message alias "message" lib "sbo2"
end module
send="""
Greetings from ScriptBasic!
""" & chr$(0)
print o2::message(send)
line input w
module dyc
declare sub ::dyc alias "dyc" lib "dyc"
end module
send="""
Greetings from ScriptBasic!
""" & chr$(0)
bufferlength=1000
receive=space(bufferlength)
replylength=dyc::dyc("ms,i,sbo2.dll,message,ZZL",send,receive,bufferlength)
'print replylength
print left(receive,replylength)
line input w
module o2
declare sub ::message alias "message" lib "sbo2"
declare sub ::compile alias "compile" lib "sbo2"
declare sub ::start alias "start" lib "sbo2"
declare sub ::stop alias "stop" lib "sbo2"
declare sub ::call alias "call" lib "sbo2"
end module
DECLARE SUB DIM ALIAS "DIM" LIB "cbx"
DIM(a,10,0,2,10)
FOR x = LBOUND(a) TO UBOUND(a)
PRINT x," = ",a[x],"\n"
NEXT
/* Core Extension Library */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../basext.h"
typedef struct _ModuleObject {
void *HandleArray;
}ModuleObject,*pModuleObject;
int my_itoa(int val, char* buf)
{
const unsigned int radix = 10;
char* p;
unsigned int a; //every digit
int len;
char* b; //start of the digit char
char temp;
unsigned int u;
p = buf;
if (val < 0)
{
*p++ = '-';
val = 0 - val;
}
u = (unsigned int)val;
b = p;
do
{
a = u % radix;
u /= radix;
*p++ = a + '0';
} while (u > 0);
len = (int)(p - buf);
*p-- = 0;
//swap
do
{
temp = *p;
*p = *b;
*b = temp;
--p;
++b;
} while (b < p);
return len;
}
besVERSION_NEGOTIATE
return (int)INTERFACE_VERSION;
besEND
besSUB_START
pModuleObject p;
besMODULEPOINTER = besALLOC(sizeof(ModuleObject));
if( besMODULEPOINTER == NULL )return 0;
p = (pModuleObject)besMODULEPOINTER;
return 0;
besEND
besSUB_FINISH
pModuleObject p;
p = (pModuleObject)besMODULEPOINTER;
if( p == NULL )return 0;
return 0;
besEND
/* DIM(array, elements, [base_value,] [step,] [increment,]) */
besFUNCTION(DIM)
VARIABLE Argument;
unsigned long __refcount_;
LEFTVALUE Lval;
int x, e, v, s, i, l;
char str[16];
besRETURNVALUE = NULL;
/* Array varable and number of elements are required */
if( besARGNR < 2 )return EX_ERROR_TOO_FEW_ARGUMENTS;
/* Number of elements */
Argument = besARGUMENT(2);
besDEREFERENCE(Argument);
e = (int)LONGVALUE(Argument);
/* Base value */
Argument = besARGUMENT(3);
besDEREFERENCE(Argument);
if( Argument ){
v = (int)LONGVALUE(Argument);
}else v = 0;
/* Step */
Argument = besARGUMENT(4);
besDEREFERENCE(Argument);
if( Argument ){
s = (int)LONGVALUE(Argument);
}else s = 1;
/* Increment */
Argument = besARGUMENT(5);
besDEREFERENCE(Argument);
if( Argument ){
i = (int)LONGVALUE(Argument);
}else i = 0;
/* Passed variable for array creation */
Argument = besARGUMENT(1);
besLEFTVALUE(Argument,Lval);
besRELEASE(*Lval);
*Lval = NULL;
*Lval = besNEWARRAY(0, e-1);
if( *Lval == NULL )return COMMAND_ERROR_MEMORY_LOW;
for( x = s - 1; ((unsigned)x) < e; x += s){
l = my_itoa(v, str);
ARRAYVALUE(*Lval,x) = besNEWSTRING(l);
memcpy(STRINGVALUE(ARRAYVALUE(*Lval,x)),str,l);
v = v + i;
}
besEND
FOR x = 0 to 999999
a[x] = 0
NEXT
DECLARE SUB DIM ALIAS "DIM" LIB "cbx"
DIM(a,1000000)
FOR x = LBOUND(a) TO UBOUND(a)
a[x] = x
NEXT
DECLARE SUB DIM ALIAS "DIM" LIB "cbx"
DIM(a,1000000)
FOR x = 0 TO 999999
a[x] = x
NEXT
' DIM Test
DECLARE SUB DIM ALIAS "DIM" LIB "cbx"
DIM(a,1000000)
FOR x = 0 TO 999999
a[x] = x
NEXT
PRINT a[UBOUND(a)],"\n\n"
' Str2File Test
DECLARE SUB Str2File ALIAS "str2file" LIB "cbx"
text = "CBX Text String\n\n"
Str2File("cbxtestfile", text)
' File2Str Test
DECLARE SUB File2str ALIAS "file2str" LIB "cbx"
PRINT File2Str("cbxtestfile")
' VARPTR Test
DECLARE SUB VARPTR ALIAS "varptr" LIB "cbx"
z = "zzz..."
PRINT VARPTR(z),"\n"
DECLARE SUB VARPTR ALIAS "varptr" LIB "cbx"
DECLARE SUB PEEK ALIAS "PEEK" LIB "cbx"
a = "JRS"
v = VARPTR(a)
PRINT PEEK(v,2),"\n"
PRINT PEEK(a,2),"\n"
v = VARPTR(MID(a,2))
PRINT PEEK(v,2),"\n"
PRINT PEEK(MID(a,2),2),"\n"
PRINT PEEK(v+1,1),"\n"
BTW, be very careful with POINTERS. A common trap is trying to assign the value of an int to a pointer and vice-versa. It's fine on a 32bit machine, but will throw errors on a 64bit machine because int's are ALWAYS 32bit. Safe bet is to replace int with LONG, which is 32bit on those types of systems, and 64bit on 64bit systems (the same rule applies to POINTER).
DECLARE SUB STRPTR ALIAS "STRPTR" LIB "cbx"
DECLARE SUB VARPTR ALIAS "VARPTR" LIB "cbx"
DECLARE SUB PEEK ALIAS "PEEK" LIB "cbx"
a = "ABC"
v = STRPTR(a)
PRINT PEEK(v,2),"\n"
PRINT PEEK(a,2),"\n"
v = STRPTR(MID(a,2))
PRINT PEEK(v,2),"\n"
PRINT PEEK(MID(a,2),2),"\n"
PRINT PEEK(v+1,1),"\n"
b = 1 + 2
v = VARPTR(b)
PRINT v,"\n"
PRINT PEEK(v,1),"\n"
PRINT ASC(PEEK(v,1)),"\n"
DECLARE SUB STRPTR ALIAS "STRPTR" LIB "cbx"
DECLARE SUB VARPTR ALIAS "VARPTR" LIB "cbx"
DECLARE SUB PEEK ALIAS "PEEK" LIB "cbx"
b[1] = "CAT"
v = STRPTR(b[1])
PRINT v,"\n"
PRINT PEEK(v,3),"\n"
b[1] = 1
v = VARPTR(b[1])
PRINT v,"\n"
PRINT PEEK(v,1),"\n"
PRINT ASC(PEEK(v,1)),"\n"
a{"one"} = "SB"
v = STRPTR(a{"one"})
PRINT v,"\n"
PRINT PEEK(v,2),"\n"
There is no Peek and Poke in Oxygen. Instead, I have standardised on declarations like:
byte b at p
/*
FILE : interface.c
HEADER : interface.h
BAS : sqlite.bas
AUTHOR : Armando I. Rivera
DATE:
CONTENT:
This is the interface.c file for the ScriptBasic module sqlite3
NTLIBS:
UXLIBS: -lc -ldl -lpthread
DWLIBS: -lsqlite3 -lpthread
ADLIBS: sqlite3.a
*/
/*
TO_BAS:
declare sub ::OPEN alias "sql3_open" lib "sqlite"
declare sub ::CLOSE alias "sql3_close" lib "sqlite"
declare sub ::EXECUTE alias "sql3_execute" lib "sqlite"
declare sub ::QUERY alias "sql3_query" lib "sqlite"
declare sub ::ROW alias "sql3_row" lib "sqlite"
declare sub ::ROW_VALUE alias "sql3_row_value" lib "sqlite"
declare sub ::COLUMN_COUNT alias "sql3_column_count" lib "sqlite"
declare sub ::COLUMN_NAME alias "sql3_column_name" lib "sqlite"
declare sub ::FINALIZE alias "sql3_finalize" lib "sqlite"
declare sub ::VERSION alias "sql3_version" lib "sqlite"
*/
#include <stdio.h>
#include <string.h>
#include "../../basext.h"
#include "sqlite3.h"
besVERSION_NEGOTIATE
return (int)INTERFACE_VERSION;
besEND
besSUB_START
long *p;
besMODULEPOINTER = besALLOC(sizeof(long));
if( besMODULEPOINTER == NULL )return 0;
p = (long*)besMODULEPOINTER;
return 0;
besEND
besSUB_FINISH
long *p;
p = (long*)besMODULEPOINTER;
if( p == NULL )return 0;
return 0;
besEND
besFUNCTION(sql3_open)
sqlite3 *db;
const char *fileName;
int i;
besARGUMENTS("s")
&fileName
besARGEND
i = sqlite3_open(fileName, &db);
besRETURN_POINTER(db)
besEND
besFUNCTION(sql3_close)
sqlite3 *db;
int i;
besARGUMENTS("p")
&db
besARGEND
i = sqlite3_close(db);
besRETURN_LONG(i)
besEND
besFUNCTION(sql3_execute)
sqlite3 *db;
char *sqlcmd;
int ret;
besARGUMENTS("ps")
&db,&sqlcmd
besARGEND
ret = sqlite3_exec(db,sqlcmd,NULL,NULL,NULL);
besRETURN_LONG(ret)
besEND
besFUNCTION(sql3_query)
sqlite3 *db;
sqlite3_stmt *stmt;
char *sqlcmd;
int ret;
besARGUMENTS("ps")
&db,&sqlcmd
besARGEND
ret = sqlite3_prepare_v2(db,sqlcmd,strlen(sqlcmd)+1,&stmt,NULL);
besRETURN_POINTER(stmt)
besEND
besFUNCTION(sql3_row)
sqlite3_stmt *stmt;
int i;
besARGUMENTS("p")
&stmt
besARGEND
i = sqlite3_step(stmt);
besRETURN_LONG(i)
besEND
besFUNCTION(sql3_row_value)
sqlite3_stmt *stmt;
const char* cur_column_text;
int i;
besARGUMENTS("pi")
&stmt,&i
besARGEND
cur_column_text = sqlite3_column_text(stmt,i);
besRETURN_STRING(cur_column_text)
besEND
besFUNCTION(sql3_column_count)
sqlite3_stmt *stmt;
int i;
besARGUMENTS("p")
&stmt
besARGEND
i = sqlite3_column_count(stmt);
besRETURN_LONG(i)
besEND
besFUNCTION(sql3_column_name)
sqlite3_stmt *stmt;
const char* cur_column_name;
int i;
besARGUMENTS("pi")
&stmt,&i
besARGEND
cur_column_name = sqlite3_column_name(stmt,i);
besRETURN_STRING(cur_column_name)
besEND
besFUNCTION(sql3_finalize)
sqlite3_stmt *stmt;
int i;
besARGUMENTS("p")
&stmt
besARGEND
i = sqlite3_finalize(stmt);
besRETURN_LONG(i)
besEND
besFUNCTION(sql3_version)
const char *ver = sqlite3_libversion();
besRETURN_STRING(ver)
besEND
START_FUNCTION_TABLE(SQLITE_SLFST)
// Ext. module
EXPORT_MODULE_FUNCTION(versmodu)
EXPORT_MODULE_FUNCTION(bootmodu)
EXPORT_MODULE_FUNCTION(finimodu)
// MOUDLE FUNCTIONS
EXPORT_MODULE_FUNCTION(sql3_open)
EXPORT_MODULE_FUNCTION(sql3_close)
EXPORT_MODULE_FUNCTION(sql3_execute)
EXPORT_MODULE_FUNCTION(sql3_query)
EXPORT_MODULE_FUNCTION(sql3_row)
EXPORT_MODULE_FUNCTION(sql3_row_value)
EXPORT_MODULE_FUNCTION(sql3_column_count)
EXPORT_MODULE_FUNCTION(sql3_column_name)
EXPORT_MODULE_FUNCTION(sql3_finalize)
EXPORT_MODULE_FUNCTION(sql3_version)
END_FUNCTION_TABLE
INCLUDE sqlite.bas
hdb=sqlite::open("testsql")
sqlite::execute(hdb,"create table demo (someval integer, sometxt text);")
sqlite::execute(hdb, "INSERT INTO demo VALUES (123,'hello');")
sqlite::execute(hdb, "INSERT INTO demo VALUES (234, 'cruel');")
sqlite::execute(hdb, "INSERT INTO demo VALUES (345, 'world');")
stmt = sqlite::query(hdb,"SELECT * FROM demo;")
while (sqlite::row(stmt) = 100)
pr = pr & sqlite::row_value(stmt,0) & " - " & sqlite::row_value(stmt,1) & "\n"
wend
sqlite::close(hdb)
print pr
print "SQLite Version: ",sqlite::version(),"\n"
Is there a way of launching SB without the console?
all : /home/jrs/android/scriptbasic/bin/mod/lib/sqlite.a /home/jrs/android/scriptbasic/bin/mod/dll/sqlite.so /home/jrs/android/scriptbasic/bin/texi/mod_sqlite.texi
/home/jrs/android/scriptbasic/bin/mod/lib/sqlite.a : /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/s_interface.o /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/sqlite3.o
arm-linux-androideabi-ar -r /home/jrs/android/scriptbasic/bin/mod/lib/sqlite.a /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/s_interface.o /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/sqlite3.o
/home/jrs/android/scriptbasic/bin/mod/dll/sqlite.so : /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/interface.o /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/sqlite3.o
arm-linux-androideabi-ld -shared -lc -lm -ldl -o /home/jrs/android/scriptbasic/bin/mod/dll/sqlite.so /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/interface.o /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/sqlite3.o
/home/jrs/android/scriptbasic/bin/mod/obj/sqlite/interface.o : interface.c sqlite3.h
arm-linux-androideabi-gcc -w -fsigned-char -fPIC -c -o /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/interface.o interface.c
/home/jrs/android/scriptbasic/bin/mod/obj/sqlite/s_interface.o : interface.c sqlite3.h
arm-linux-androideabi-gcc -w -fsigned-char -fPIC -DSTATIC_LINK=1 -c -o /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/s_interface.o interface.c
/home/jrs/android/scriptbasic/bin/mod/obj/sqlite/sqlite3.o : sqlite3.c sqlite3.h
arm-linux-androideabi-gcc -w -fsigned-char -fPIC -c -o /home/jrs/android/scriptbasic/bin/mod/obj/sqlite/sqlite3.o sqlite3.c
/home/jrs/android/scriptbasic/bin/texi/mod_sqlite.texi : manual.texi.jam
perl /home/jrs/android/scriptbasic/jamal.pl manual.texi.jam /home/jrs/android/scriptbasic/bin/texi/mod_sqlite.texi
perl /home/jrs/android/scriptbasic/t2h.pl /home/jrs/android/scriptbasic/bin/texi/mod_sqlite.texi