SlideShare a Scribd company logo
1 of 10
Download to read offline
Ring Documentation, Release 1.3
ring_vm_funcregister("tan",ring_vm_math_tan);
ring_vm_funcregister("asin",ring_vm_math_asin);
ring_vm_funcregister("acos",ring_vm_math_acos);
ring_vm_funcregister("atan",ring_vm_math_atan);
ring_vm_funcregister("atan2",ring_vm_math_atan2);
ring_vm_funcregister("sinh",ring_vm_math_sinh);
ring_vm_funcregister("cosh",ring_vm_math_cosh);
ring_vm_funcregister("tanh",ring_vm_math_tanh);
ring_vm_funcregister("exp",ring_vm_math_exp);
ring_vm_funcregister("log",ring_vm_math_log);
ring_vm_funcregister("log10",ring_vm_math_log10);
ring_vm_funcregister("ceil",ring_vm_math_ceil);
ring_vm_funcregister("floor",ring_vm_math_floor);
ring_vm_funcregister("fabs",ring_vm_math_fabs);
ring_vm_funcregister("pow",ring_vm_math_pow);
ring_vm_funcregister("sqrt",ring_vm_math_sqrt);
ring_vm_funcregister("unsigned",ring_vm_math_unsigned);
ring_vm_funcregister("decimals",ring_vm_math_decimals);
ring_vm_funcregister("murmur3hash",ring_vm_math_murmur3hash);
}
Tip: Remember that the function ring_vm_math_loadfunctions() will be called by the ring_vm_extension() function
(in the ring_ext.c file).
60.4 Function Structure
Each module function may contains the next steps
1 - Check Parameters Count
2 - Check Parameters Type
3 - Get Parameters Values
4 - Execute Code/Call Functions
5 - Return Value
The structure is very similar to any function (Input - Process - Output) But here we will use the Ring API for the steps
1,2,3 and 5.
60.5 Check Parameters Count
We can check the parameters count using the RING_API_PARACOUNT macro.
We can compare RING_API_PARACOUNT with any numeric value using == or != operators.
Example:
if ( RING_API_PARACOUNT != 1 ) {
/* code */
}
Example:
60.4. Function Structure 578
Ring Documentation, Release 1.3
if ( RING_API_PARACOUNT == 1 ) {
/* code */
}
60.6 Display Error Message
We can display error messages using the RING_API_ERROR() function.
The function will display the error and end the execution of the program.
Note: the behaviour of this function can be changed by the Ring code using Try/Catch/Done statements, so in your C
code, use Return after this function.
Syntax:
RING_API_ERROR(const char *cErrorMsg);
The Ring API comes with some of predefined error messages that we can use
#define RING_API_MISS1PARA "Bad parameters count, the function expect one parameter"
#define RING_API_MISS2PARA "Bad parameters count, the function expect two parameters"
#define RING_API_MISS3PARA "Bad parameters count, the function expect three parameters"
#define RING_API_MISS4PARA "Bad parameters count, the function expect four parameters"
#define RING_API_BADPARATYPE "Bad parameter type!"
#define RING_API_BADPARACOUNT "Bad parameters count!"
#define RING_API_BADPARARANGE "Bad parameters value, error in range!"
#define RING_API_NOTPOINTER "Error in parameter, not pointer!"
#define RING_API_NULLPOINTER "Error in parameter, NULL pointer!"
#define RING_API_EMPTYLIST "Bad parameter, empty list!"
60.7 Check Parameters Type
We can check the parameter type using the next functions
int RING_API_ISNUMBER(int nParameterNumber);
int RING_API_ISSTRING(int nParameterNumber);
int RING_API_ISLIST(int nParameterNumber);
int RING_API_ISPOINTER(int nParameterNumber);
The output of these functions will be 1 (True) or 0 (False).
60.8 Get Parameters Values
We can get paramters values using the next functions
double RING_API_GETNUMBER(int nParameterNumber);
const char *RING_API_GETSTRING(int nParameterNumber);
int RING_API_GETSTRINGSIZE(int nParameterNumber);
List *RING_API_GETLIST(int nParameterNumber);
void *RING_API_GETCPOINTER(int nParameterNumber, const char *cPoinerType);
int RING_API_GETPOINTERTYPE(int nParameterNumber);
60.6. Display Error Message 579
Ring Documentation, Release 1.3
60.9 Return Value
We can return values from our function using the next functions.
RING_API_RETNUMBER(double nValue);
RING_API_RETSTRING(const char *cString);
RING_API_RETSTRING2(const char *cString,int nStringSize);
RING_API_RETLIST(List *pList);
RING_API_RETCPOINTER(void *pValue,const char *cPointerType);
60.10 Function Prototype
When we define new function to be used for RingVM extension, we use the next prototype
void my_function_name( void *pPointer );
or we can use the RING_FUNC() Macro
RING_FUNC(my_function_name);
60.11 Sin() Function Implementation
The next code represents the sin() function implementation using the Ring API and the sin() C function.
void ring_vm_math_sin ( void *pPointer )
{
if ( RING_API_PARACOUNT != 1 ) {
RING_API_ERROR(RING_API_MISS1PARA);
return ;
}
if ( RING_API_ISNUMBER(1) ) {
RING_API_RETNUMBER(sin(RING_API_GETNUMBER(1)));
} else {
RING_API_ERROR(RING_API_BADPARATYPE);
}
}
60.12 Fopen() and Fclose() Functions Implementation
The next code represents the fopen() function implementation using the Ring API and the fopen() C Function.
The function takes two parameters, the first parameter is the file name as string. The second parameter is the mode as
string.
In the file ring_vmfile.h we have some constants to use as the pointer type like
#define RING_VM_POINTER_FILE "file"
#define RING_VM_POINTER_FILEPOS "filepos"
The function implementation in ring_vmfile.c
60.9. Return Value 580
Ring Documentation, Release 1.3
void ring_vm_file_fopen ( void *pPointer )
{
FILE *fp ;
if ( RING_API_PARACOUNT != 2 ) {
RING_API_ERROR(RING_API_MISS2PARA);
return ;
}
if ( RING_API_ISSTRING(1) && RING_API_ISSTRING(2) ) {
fp = fopen(RING_API_GETSTRING(1),RING_API_GETSTRING(2));
RING_API_RETCPOINTER(fp,RING_VM_POINTER_FILE);
} else {
RING_API_ERROR(RING_API_BADPARATYPE);
}
}
The next code represents the fclose() function implementation
void ring_vm_file_fclose ( void *pPointer )
{
FILE *fp ;
if ( RING_API_PARACOUNT != 1 ) {
RING_API_ERROR(RING_API_MISS1PARA);
return ;
}
if ( RING_API_ISPOINTER(1) ) {
fp = (FILE *) RING_API_GETCPOINTER(1,RING_VM_POINTER_FILE) ;
if ( fp != NULL ) {
RING_API_RETNUMBER(fclose(fp));
RING_API_SETNULLPOINTER(1);
}
} else {
RING_API_ERROR(RING_API_BADPARATYPE);
}
}
From fopen() and fclose() implementation we learned
1 - how to return C pointer using RING_API_RETCPOINTER() function
2 - how to check if the parameter is a pointer using the RING_API_ISPOINTER() function
3 - how to get C pointer value using the RING_API_GETCPOINTER() function
4 - how to set the C pointer variable (in RingVM) to NULL using the RING_API_SETNULLPOINTER() function
60.13 Ring API - List Functions
In this section we will learn about the list functions provided by the Ring API to create new lists and manipulate the
list items.
List * ring_list_new ( int nSize ) ;
void ring_list_newitem ( List *pList ) ;
Item * ring_list_getitem ( List *pList,int index ) ;
List * ring_list_delete ( List *pList ) ;
void ring_list_deleteitem ( List *pList,int index ) ;
void ring_list_print ( List *pList ) ;
int ring_list_gettype ( List *pList, int index ) ;
void ring_list_setint ( List *pList, int index ,int number ) ;
60.13. Ring API - List Functions 581
Ring Documentation, Release 1.3
void ring_list_addint ( List *pList,int x ) ;
void ring_list_setpointer ( List *pList, int index ,void *pValue ) ;
void ring_list_addpointer ( List *pList,void *pValue ) ;
void ring_list_setfuncpointer ( List *pList, int index ,void (*pFunc)(void *) ) ;
void ring_list_addfuncpointer ( List *pList,void (*pFunc)(void *) ) ;
int ring_list_isfuncpointer ( List *pList, int index ) ;
void ring_list_setdouble ( List *pList, int index ,double number ) ;
void ring_list_adddouble ( List *pList,double x ) ;
void ring_list_setstring ( List *pList, int index ,const char *str ) ;
void ring_list_setstring2 ( List *pList, int index ,const char *str,int nStrSize ) ;
void ring_list_addstring ( List *pList,const char *str ) ;
void ring_list_addstring2 ( List *pList,const char *str,int nStrSize ) ;
List * ring_list_newlist ( List *pList ) ;
List * ring_list_getlist ( List *pList, int index ) ;
void ring_list_setlist ( List *pList, int index ) ;
void ring_list_setactiveitem ( List *pList, Items *pItems, int index ) ;
void ring_list_copy ( List *pNewList, List *pList ) ;
int ring_list_isnumber ( List *pList, int index ) ;
int ring_list_isstring ( List *pList, int index ) ;
int ring_list_islist ( List *pList, int index ) ;
int ring_list_ispointer ( List *pList, int index ) ;
void ring_list_deleteallitems ( List *pList ) ;
void ring_list_insertitem ( List *pList,int x ) ;
void ring_list_insertint ( List *pList,int nPos,int x ) ;
void ring_list_insertdouble ( List *pList,int nPos,double x ) ;
void ring_list_insertpointer ( List *pList,int nPos,void *pValue ) ;
void ring_list_insertstring ( List *pList,int nPos,const char *str ) ;
void ring_list_insertstring2 ( List *pList,int nPos,const char *str,int nStrSize ) ;
void ring_list_insertfuncpointer ( List *pList,int nPos,void (*pFunc)(void *) ) ;
List * ring_list_insertlist ( List *pList,int nPos ) ;
int ring_list_isiteminsidelist ( List *pList,Item *pItem ) ;
int ring_list_findstring ( List *pList,const char *str,int nColumn ) ;
int ring_list_finddouble ( List *pList,double nNum1,int nColumn ) ;
void ring_list_sortnum ( List *pList,int left,int right,int nColumn ) ;
void ring_list_sortstr ( List *pList,int left,int right,int nColumn ) ;
int ring_list_binarysearchnum ( List *pList,double nNum1,int nColumn ) ;
int ring_list_binarysearchstr ( List *pList,const char *cFind,int nColumn ) ;
void ring_list_swap ( List *pList,int x,int y ) ;
double ring_list_getdoublecolumn ( List *pList,int nIndex,int nColumn ) ;
char * ring_list_getstringcolumn ( List *pList,int nIndex,int nColumn ) ;
void ring_list_genarray ( List *pList ) ;
void ring_list_deletearray ( List *pList ) ;
void ring_list_genhashtable ( List *pList ) ;
void ring_list_genhashtable2 ( List *pList ) ;
void ring_list_refcopy ( List *pNewList, List *pList ) ;
void ring_list_clear ( List *pList ) ;
/* Macro */
ring_list_isdouble(pList,index)
ring_list_isint(pList,index)
ring_list_deletelastitem(x)
ring_list_gethashtable(x)
ring_list_getint(pList,index)
ring_list_getpointer(pList,index)
ring_list_getfuncpointer(pList,index)
ring_list_callfuncpointer(pList,index,x)
ring_list_getdouble(pList,index)
ring_list_getstring(pList,index)
ring_list_getstringobject(pList,index)
60.13. Ring API - List Functions 582
Ring Documentation, Release 1.3
ring_list_getstringsize(pList,index)
ring_list_getsize(x) (x->nSize)
60.14 Ring API - String Functions
In this section we will learn about the string functions provided by the Ring API to create new string and manipulate
the string content.
String * ring_string_new ( const char *str ) ;
String * ring_string_new2 ( const char *str,int nStrSize ) ;
String * ring_string_delete ( String *pString ) ;
int ring_string_size ( String *pString ) ;
void ring_string_set ( String *pString,const char *str ) ;
void ring_string_set2 ( String *pString,const char *str,int nStrSize ) ;
void ring_string_add ( String *pString,const char *str ) ;
void ring_string_add2 ( String *pString,const char *str,int nStrSize ) ;
void ring_string_print ( String *pString ) ;
void ring_string_setfromint ( String *pString,int x ) ;
char * ring_string_lower ( char *cStr ) ;
char * ring_string_upper ( char *cStr ) ;
char * ring_string_lower2 ( char *cStr,int nStrSize ) ;
char * ring_string_upper2 ( char *cStr,int nStrSize ) ;
char * ring_string_find ( char *cStr1,char *cStr2 ) ;
char * ring_string_find2 ( char *cStr1,int nStrSize1,char *cStr2,int nStrSize2 ) ;
/* Macro */
ring_string_tolower(x)
ring_string_toupper(x)
ring_string_get(x)
60.15 MySQL_Columns() Function Implementation
The next code presents the MySQL_Columns() function implementation.
This function returns table columns information.
void ring_vm_mysql_columns ( void *pPointer )
{
MYSQL *con ;
MYSQL_RES *result ;
int nColumns,x ;
MYSQL_ROW row ;
MYSQL_FIELD *field ;
List *pList, *pList2 ;
if ( RING_API_PARACOUNT != 1 ) {
RING_API_ERROR(RING_API_MISS1PARA);
return ;
}
if ( RING_API_ISPOINTER(1) ) {
con = (MYSQL *) RING_API_GETCPOINTER(1,RING_VM_POINTER_MYSQL) ;
if ( con == NULL ) {
return ;
}
result = mysql_store_result(con);
if ( result == NULL ) {
60.14. Ring API - String Functions 583
Ring Documentation, Release 1.3
RING_API_RETNUMBER(0);
return ;
}
pList = RING_API_NEWLIST ;
nColumns = mysql_num_fields(result);
if ( row = mysql_fetch_row(result) ) {
while ( field = mysql_fetch_field(result) ) {
pList2 = ring_list_newlist(pList);
ring_list_addstring(pList2,field->name);
ring_list_adddouble(pList2,field->length);
ring_list_adddouble(pList2,field->type);
ring_list_adddouble(pList2,field->flags);
}
}
mysql_free_result(result);
RING_API_RETLIST(pList);
} else {
RING_API_ERROR(RING_API_BADPARATYPE);
}
}
Lists are of type List, in the previoud function we declared two pointers of type List using List *pList, *pList2;
Note: The function uses RING_API_NEWLIST to create new list instead of ring_list_new() to create the list in
Temp. Memory related to the function scope. This way we can return the list from the function. Also we don’t delete
the list, if it’s stored in a variable by Ring Code it will be saved, if not it will be automatically deleted by RingVM.
The list can contains sub lists, we used the function ring_list_newlist() to create a sublist.
The function ring_list_addstring() is used to add string items to the list/sublist.
The function ring_list_adddouble() is used to add numeric items to the list/sublist.
Note: All numeric items in lists returned from RingVM extension functions must be of type double and added to the
list using ring_list_adddouble() function.
We return the list from the extension function using the RING_API_RETLIST() function.
60.16 Dynamic/Shared Libraries (DLL/So) and LoadLib() function
Instead of rebuilding the RingVM after writing new functions using C/C++ and the Ring API, we can create a DLL/So
file and dynamically use the functions provided by this file in the runtime using the LoadLib() function.
Dynamic library example in C
#include "ring.h"
RING_DLL __declspec(dllexport)
RING_FUNC(ring_ringlib_dlfunc)
{
printf("Message from dlfunc");
}
RING_DLL void ringlib_init(RingState *pRingState)
{
60.16. Dynamic/Shared Libraries (DLL/So) and LoadLib() function 584
Ring Documentation, Release 1.3
ring_vm_funcregister("dlfunc",ring_ringlib_dlfunc);
}
the idea is to create the ringlib_init() function, this function will be called by the RingVM when we use the generated
DLL file though the LoadLib() function.
Inside the ringlib_init() function we can register the module function or call a function that do the registration process
for all of the module functions.
The next Ring code demonstrates how to use the DLL library during the runtime.
See "Dynamic DLL" + NL
LoadLib("ringlib.dll")
dlfunc()
Output:
Dynamic DLL
Message from dlfunc
60.16. Dynamic/Shared Libraries (DLL/So) and LoadLib() function 585
CHAPTER
SIXTYONE
EMBEDDING RING LANGUAGE IN C/C++ PROGRAMS
We can use the Ring language from C/C++ programs using the next functions
RingState *ring_state_init();
ring_state_runcode(RingState *pState,const char *cCode);
ring_state_delete(RingState *pState);
61.1 Ring State
The idea is to use the ring_state_init() to create new state for the Ring Language then call the ring_state_runcode()
function to execut Ring code using the same state. When we are done, we call the ring_state_delete() to free the
memory.
Example:
#include "ring.h"
#include "stdlib.h"
int main(int argc, char *argv[])
{
RingState *pState = ring_state_init();
printf("welcomen");
ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl");
ring_state_delete(pState);
}
Output:
welcome
hello world from the ring programming language
61.2 Ring State Functions
The Ring API comes with the next functions to create and delete the state. Also we have functions to create new
variables and get variables values.
RingState * ring_state_init ( void ) ;
RingState * ring_state_delete ( RingState *pRingState ) ;
void ring_state_runcode ( RingState *pRingState,const char *cStr ) ;
List * ring_state_findvar ( RingState *pRingState,const char *cStr ) ;
List * ring_state_newvar ( RingState *pRingState,const char *cStr ) ;
void ring_state_main ( int argc, char *argv[] ) ;
586
Ring Documentation, Release 1.3
void ring_state_runfile ( RingState *pRingState,const char *cFileName ) ;
void ring_state_runobjectfile ( RingState *pRingState,const char *cFileName ) ;
61.3 Ring State Variables
We can create more than one ring state in the same program and we can create and modify variable values.
To get the variable list we can use the ring_state_findvar() function.
To create new variable we can use the ring_state_newvar() function.
Example:
#include "ring.h"
#include "stdlib.h"
int main(int argc, char *argv[])
{
List *pList;
RingState *pState = ring_state_init();
RingState *pState2 = ring_state_init();
printf("welcomen");
ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl");
printf("Again from C we will call ring coden");
ring_state_runcode(pState,"for x = 1 to 10 see x + nl next");
ring_state_runcode(pState2,"for x = 1 to 5 see x + nl next");
printf("Now we will display the x variable value from ring coden");
ring_state_runcode(pState,"see 'x value : ' + x + nl ");
ring_state_runcode(pState2,"see 'x value : ' + x + nl ");
pList = ring_state_findvar(pState,"x");
printf("Printing Ring variable value from C , %.0fn",
ring_list_getdouble(pList,RING_VAR_VALUE));
printf("now we will set the ring variable value from Cn");
ring_list_setdouble(pList,RING_VAR_VALUE,20);
ring_state_runcode(pState,"see 'x value after update : ' + x + nl ");
pList = ring_state_newvar(pState,"v1");
ring_list_setdouble(pList,RING_VAR_VALUE,10);
pList = ring_state_newvar(pState,"v2");
ring_list_setdouble(pList,RING_VAR_VALUE,20);
ring_state_runcode(pState,"see 'v1 + v2 = ' see v1+v2 see nl");
ring_state_runcode(pState,"see 'end of test' + nl");
ring_state_delete(pState);
61.3. Ring State Variables 587

More Related Content

What's hot

35787646 system-software-lab-manual
35787646 system-software-lab-manual35787646 system-software-lab-manual
35787646 system-software-lab-manual
Naveen Kumar
 
Unit 5
Unit 5Unit 5
Unit 5
siddr
 
Unit 4
Unit 4Unit 4
Unit 4
siddr
 
Unit 6
Unit 6Unit 6
Unit 6
siddr
 
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...
Software Guru
 

What's hot (20)

35787646 system-software-lab-manual
35787646 system-software-lab-manual35787646 system-software-lab-manual
35787646 system-software-lab-manual
 
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6
 
Unit 5
Unit 5Unit 5
Unit 5
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
Unit 4
Unit 4Unit 4
Unit 4
 
Congfigure python as_ide
Congfigure python as_ideCongfigure python as_ide
Congfigure python as_ide
 
Linked lists
Linked listsLinked lists
Linked lists
 
Javascript fundamentals for php developers
Javascript fundamentals for php developersJavascript fundamentals for php developers
Javascript fundamentals for php developers
 
Climbing the Abstract Syntax Tree (PHP Russia 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)Climbing the Abstract Syntax Tree (PHP Russia 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)
 
Climbing the Abstract Syntax Tree (Forum PHP 2017)
Climbing the Abstract Syntax Tree (Forum PHP 2017)Climbing the Abstract Syntax Tree (Forum PHP 2017)
Climbing the Abstract Syntax Tree (Forum PHP 2017)
 
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему кодуITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
 
Exploit Research and Development Megaprimer: Win32 Egghunter
Exploit Research and Development Megaprimer: Win32 EgghunterExploit Research and Development Megaprimer: Win32 Egghunter
Exploit Research and Development Megaprimer: Win32 Egghunter
 
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
 
Коварный code type ITGM #9
Коварный code type ITGM #9Коварный code type ITGM #9
Коварный code type ITGM #9
 
Unit 6
Unit 6Unit 6
Unit 6
 
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...
 
Sysprog 13
Sysprog 13Sysprog 13
Sysprog 13
 
CGI With Object Oriented Perl
CGI With Object Oriented PerlCGI With Object Oriented Perl
CGI With Object Oriented Perl
 
Perl IO
Perl IOPerl IO
Perl IO
 
Thoughts On Learning A New Programming Language
Thoughts On Learning A New Programming LanguageThoughts On Learning A New Programming Language
Thoughts On Learning A New Programming Language
 

Similar to The Ring programming language version 1.3 book - Part 61 of 88

Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
ujihisa
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
ujihisa
 
Stabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationStabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance Evaluation
Emery Berger
 

Similar to The Ring programming language version 1.3 book - Part 61 of 88 (20)

The Ring programming language version 1.4.1 book - Part 22 of 31
The Ring programming language version 1.4.1 book - Part 22 of 31The Ring programming language version 1.4.1 book - Part 22 of 31
The Ring programming language version 1.4.1 book - Part 22 of 31
 
The Ring programming language version 1.5.1 book - Part 77 of 180
The Ring programming language version 1.5.1 book - Part 77 of 180The Ring programming language version 1.5.1 book - Part 77 of 180
The Ring programming language version 1.5.1 book - Part 77 of 180
 
The Ring programming language version 1.6 book - Part 84 of 189
The Ring programming language version 1.6 book - Part 84 of 189The Ring programming language version 1.6 book - Part 84 of 189
The Ring programming language version 1.6 book - Part 84 of 189
 
The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210
 
The Ring programming language version 1.7 book - Part 86 of 196
The Ring programming language version 1.7 book - Part 86 of 196The Ring programming language version 1.7 book - Part 86 of 196
The Ring programming language version 1.7 book - Part 86 of 196
 
The Ring programming language version 1.5.2 book - Part 78 of 181
The Ring programming language version 1.5.2 book - Part 78 of 181The Ring programming language version 1.5.2 book - Part 78 of 181
The Ring programming language version 1.5.2 book - Part 78 of 181
 
The Ring programming language version 1.5.4 book - Part 81 of 185
The Ring programming language version 1.5.4 book - Part 81 of 185The Ring programming language version 1.5.4 book - Part 81 of 185
The Ring programming language version 1.5.4 book - Part 81 of 185
 
The Ring programming language version 1.5.1 book - Part 76 of 180
The Ring programming language version 1.5.1 book - Part 76 of 180The Ring programming language version 1.5.1 book - Part 76 of 180
The Ring programming language version 1.5.1 book - Part 76 of 180
 
The Ring programming language version 1.8 book - Part 89 of 202
The Ring programming language version 1.8 book - Part 89 of 202The Ring programming language version 1.8 book - Part 89 of 202
The Ring programming language version 1.8 book - Part 89 of 202
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
 
Ponters
PontersPonters
Ponters
 
Ponters
PontersPonters
Ponters
 
The Ring programming language version 1.8 book - Part 90 of 202
The Ring programming language version 1.8 book - Part 90 of 202The Ring programming language version 1.8 book - Part 90 of 202
The Ring programming language version 1.8 book - Part 90 of 202
 
The Ring programming language version 1.5.4 book - Part 80 of 185
The Ring programming language version 1.5.4 book - Part 80 of 185The Ring programming language version 1.5.4 book - Part 80 of 185
The Ring programming language version 1.5.4 book - Part 80 of 185
 
The Ring programming language version 1.5.2 book - Part 77 of 181
The Ring programming language version 1.5.2 book - Part 77 of 181The Ring programming language version 1.5.2 book - Part 77 of 181
The Ring programming language version 1.5.2 book - Part 77 of 181
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Stabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationStabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance Evaluation
 
The Ring programming language version 1.2 book - Part 61 of 84
The Ring programming language version 1.2 book - Part 61 of 84The Ring programming language version 1.2 book - Part 61 of 84
The Ring programming language version 1.2 book - Part 61 of 84
 

More from Mahmoud Samir Fayed

More from Mahmoud Samir Fayed (20)

The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 212 of 212The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 212 of 212
 
The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 211 of 212The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 211 of 212
 
The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 210 of 212The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 210 of 212
 
The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 208 of 212The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 208 of 212
 
The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 207 of 212The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 207 of 212
 
The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 205 of 212The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 205 of 212
 
The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 206 of 212The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 206 of 212
 
The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 204 of 212The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 204 of 212
 
The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 203 of 212The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 203 of 212
 
The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 202 of 212The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 202 of 212
 
The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 201 of 212The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 201 of 212
 
The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 200 of 212The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 200 of 212
 
The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 199 of 212The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 199 of 212
 
The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 198 of 212The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 198 of 212
 
The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 197 of 212The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 197 of 212
 
The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 196 of 212The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 196 of 212
 
The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 195 of 212The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 195 of 212
 
The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 194 of 212The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 194 of 212
 
The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 193 of 212The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 193 of 212
 
The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 192 of 212The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 192 of 212
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 

The Ring programming language version 1.3 book - Part 61 of 88

  • 1. Ring Documentation, Release 1.3 ring_vm_funcregister("tan",ring_vm_math_tan); ring_vm_funcregister("asin",ring_vm_math_asin); ring_vm_funcregister("acos",ring_vm_math_acos); ring_vm_funcregister("atan",ring_vm_math_atan); ring_vm_funcregister("atan2",ring_vm_math_atan2); ring_vm_funcregister("sinh",ring_vm_math_sinh); ring_vm_funcregister("cosh",ring_vm_math_cosh); ring_vm_funcregister("tanh",ring_vm_math_tanh); ring_vm_funcregister("exp",ring_vm_math_exp); ring_vm_funcregister("log",ring_vm_math_log); ring_vm_funcregister("log10",ring_vm_math_log10); ring_vm_funcregister("ceil",ring_vm_math_ceil); ring_vm_funcregister("floor",ring_vm_math_floor); ring_vm_funcregister("fabs",ring_vm_math_fabs); ring_vm_funcregister("pow",ring_vm_math_pow); ring_vm_funcregister("sqrt",ring_vm_math_sqrt); ring_vm_funcregister("unsigned",ring_vm_math_unsigned); ring_vm_funcregister("decimals",ring_vm_math_decimals); ring_vm_funcregister("murmur3hash",ring_vm_math_murmur3hash); } Tip: Remember that the function ring_vm_math_loadfunctions() will be called by the ring_vm_extension() function (in the ring_ext.c file). 60.4 Function Structure Each module function may contains the next steps 1 - Check Parameters Count 2 - Check Parameters Type 3 - Get Parameters Values 4 - Execute Code/Call Functions 5 - Return Value The structure is very similar to any function (Input - Process - Output) But here we will use the Ring API for the steps 1,2,3 and 5. 60.5 Check Parameters Count We can check the parameters count using the RING_API_PARACOUNT macro. We can compare RING_API_PARACOUNT with any numeric value using == or != operators. Example: if ( RING_API_PARACOUNT != 1 ) { /* code */ } Example: 60.4. Function Structure 578
  • 2. Ring Documentation, Release 1.3 if ( RING_API_PARACOUNT == 1 ) { /* code */ } 60.6 Display Error Message We can display error messages using the RING_API_ERROR() function. The function will display the error and end the execution of the program. Note: the behaviour of this function can be changed by the Ring code using Try/Catch/Done statements, so in your C code, use Return after this function. Syntax: RING_API_ERROR(const char *cErrorMsg); The Ring API comes with some of predefined error messages that we can use #define RING_API_MISS1PARA "Bad parameters count, the function expect one parameter" #define RING_API_MISS2PARA "Bad parameters count, the function expect two parameters" #define RING_API_MISS3PARA "Bad parameters count, the function expect three parameters" #define RING_API_MISS4PARA "Bad parameters count, the function expect four parameters" #define RING_API_BADPARATYPE "Bad parameter type!" #define RING_API_BADPARACOUNT "Bad parameters count!" #define RING_API_BADPARARANGE "Bad parameters value, error in range!" #define RING_API_NOTPOINTER "Error in parameter, not pointer!" #define RING_API_NULLPOINTER "Error in parameter, NULL pointer!" #define RING_API_EMPTYLIST "Bad parameter, empty list!" 60.7 Check Parameters Type We can check the parameter type using the next functions int RING_API_ISNUMBER(int nParameterNumber); int RING_API_ISSTRING(int nParameterNumber); int RING_API_ISLIST(int nParameterNumber); int RING_API_ISPOINTER(int nParameterNumber); The output of these functions will be 1 (True) or 0 (False). 60.8 Get Parameters Values We can get paramters values using the next functions double RING_API_GETNUMBER(int nParameterNumber); const char *RING_API_GETSTRING(int nParameterNumber); int RING_API_GETSTRINGSIZE(int nParameterNumber); List *RING_API_GETLIST(int nParameterNumber); void *RING_API_GETCPOINTER(int nParameterNumber, const char *cPoinerType); int RING_API_GETPOINTERTYPE(int nParameterNumber); 60.6. Display Error Message 579
  • 3. Ring Documentation, Release 1.3 60.9 Return Value We can return values from our function using the next functions. RING_API_RETNUMBER(double nValue); RING_API_RETSTRING(const char *cString); RING_API_RETSTRING2(const char *cString,int nStringSize); RING_API_RETLIST(List *pList); RING_API_RETCPOINTER(void *pValue,const char *cPointerType); 60.10 Function Prototype When we define new function to be used for RingVM extension, we use the next prototype void my_function_name( void *pPointer ); or we can use the RING_FUNC() Macro RING_FUNC(my_function_name); 60.11 Sin() Function Implementation The next code represents the sin() function implementation using the Ring API and the sin() C function. void ring_vm_math_sin ( void *pPointer ) { if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_MISS1PARA); return ; } if ( RING_API_ISNUMBER(1) ) { RING_API_RETNUMBER(sin(RING_API_GETNUMBER(1))); } else { RING_API_ERROR(RING_API_BADPARATYPE); } } 60.12 Fopen() and Fclose() Functions Implementation The next code represents the fopen() function implementation using the Ring API and the fopen() C Function. The function takes two parameters, the first parameter is the file name as string. The second parameter is the mode as string. In the file ring_vmfile.h we have some constants to use as the pointer type like #define RING_VM_POINTER_FILE "file" #define RING_VM_POINTER_FILEPOS "filepos" The function implementation in ring_vmfile.c 60.9. Return Value 580
  • 4. Ring Documentation, Release 1.3 void ring_vm_file_fopen ( void *pPointer ) { FILE *fp ; if ( RING_API_PARACOUNT != 2 ) { RING_API_ERROR(RING_API_MISS2PARA); return ; } if ( RING_API_ISSTRING(1) && RING_API_ISSTRING(2) ) { fp = fopen(RING_API_GETSTRING(1),RING_API_GETSTRING(2)); RING_API_RETCPOINTER(fp,RING_VM_POINTER_FILE); } else { RING_API_ERROR(RING_API_BADPARATYPE); } } The next code represents the fclose() function implementation void ring_vm_file_fclose ( void *pPointer ) { FILE *fp ; if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_MISS1PARA); return ; } if ( RING_API_ISPOINTER(1) ) { fp = (FILE *) RING_API_GETCPOINTER(1,RING_VM_POINTER_FILE) ; if ( fp != NULL ) { RING_API_RETNUMBER(fclose(fp)); RING_API_SETNULLPOINTER(1); } } else { RING_API_ERROR(RING_API_BADPARATYPE); } } From fopen() and fclose() implementation we learned 1 - how to return C pointer using RING_API_RETCPOINTER() function 2 - how to check if the parameter is a pointer using the RING_API_ISPOINTER() function 3 - how to get C pointer value using the RING_API_GETCPOINTER() function 4 - how to set the C pointer variable (in RingVM) to NULL using the RING_API_SETNULLPOINTER() function 60.13 Ring API - List Functions In this section we will learn about the list functions provided by the Ring API to create new lists and manipulate the list items. List * ring_list_new ( int nSize ) ; void ring_list_newitem ( List *pList ) ; Item * ring_list_getitem ( List *pList,int index ) ; List * ring_list_delete ( List *pList ) ; void ring_list_deleteitem ( List *pList,int index ) ; void ring_list_print ( List *pList ) ; int ring_list_gettype ( List *pList, int index ) ; void ring_list_setint ( List *pList, int index ,int number ) ; 60.13. Ring API - List Functions 581
  • 5. Ring Documentation, Release 1.3 void ring_list_addint ( List *pList,int x ) ; void ring_list_setpointer ( List *pList, int index ,void *pValue ) ; void ring_list_addpointer ( List *pList,void *pValue ) ; void ring_list_setfuncpointer ( List *pList, int index ,void (*pFunc)(void *) ) ; void ring_list_addfuncpointer ( List *pList,void (*pFunc)(void *) ) ; int ring_list_isfuncpointer ( List *pList, int index ) ; void ring_list_setdouble ( List *pList, int index ,double number ) ; void ring_list_adddouble ( List *pList,double x ) ; void ring_list_setstring ( List *pList, int index ,const char *str ) ; void ring_list_setstring2 ( List *pList, int index ,const char *str,int nStrSize ) ; void ring_list_addstring ( List *pList,const char *str ) ; void ring_list_addstring2 ( List *pList,const char *str,int nStrSize ) ; List * ring_list_newlist ( List *pList ) ; List * ring_list_getlist ( List *pList, int index ) ; void ring_list_setlist ( List *pList, int index ) ; void ring_list_setactiveitem ( List *pList, Items *pItems, int index ) ; void ring_list_copy ( List *pNewList, List *pList ) ; int ring_list_isnumber ( List *pList, int index ) ; int ring_list_isstring ( List *pList, int index ) ; int ring_list_islist ( List *pList, int index ) ; int ring_list_ispointer ( List *pList, int index ) ; void ring_list_deleteallitems ( List *pList ) ; void ring_list_insertitem ( List *pList,int x ) ; void ring_list_insertint ( List *pList,int nPos,int x ) ; void ring_list_insertdouble ( List *pList,int nPos,double x ) ; void ring_list_insertpointer ( List *pList,int nPos,void *pValue ) ; void ring_list_insertstring ( List *pList,int nPos,const char *str ) ; void ring_list_insertstring2 ( List *pList,int nPos,const char *str,int nStrSize ) ; void ring_list_insertfuncpointer ( List *pList,int nPos,void (*pFunc)(void *) ) ; List * ring_list_insertlist ( List *pList,int nPos ) ; int ring_list_isiteminsidelist ( List *pList,Item *pItem ) ; int ring_list_findstring ( List *pList,const char *str,int nColumn ) ; int ring_list_finddouble ( List *pList,double nNum1,int nColumn ) ; void ring_list_sortnum ( List *pList,int left,int right,int nColumn ) ; void ring_list_sortstr ( List *pList,int left,int right,int nColumn ) ; int ring_list_binarysearchnum ( List *pList,double nNum1,int nColumn ) ; int ring_list_binarysearchstr ( List *pList,const char *cFind,int nColumn ) ; void ring_list_swap ( List *pList,int x,int y ) ; double ring_list_getdoublecolumn ( List *pList,int nIndex,int nColumn ) ; char * ring_list_getstringcolumn ( List *pList,int nIndex,int nColumn ) ; void ring_list_genarray ( List *pList ) ; void ring_list_deletearray ( List *pList ) ; void ring_list_genhashtable ( List *pList ) ; void ring_list_genhashtable2 ( List *pList ) ; void ring_list_refcopy ( List *pNewList, List *pList ) ; void ring_list_clear ( List *pList ) ; /* Macro */ ring_list_isdouble(pList,index) ring_list_isint(pList,index) ring_list_deletelastitem(x) ring_list_gethashtable(x) ring_list_getint(pList,index) ring_list_getpointer(pList,index) ring_list_getfuncpointer(pList,index) ring_list_callfuncpointer(pList,index,x) ring_list_getdouble(pList,index) ring_list_getstring(pList,index) ring_list_getstringobject(pList,index) 60.13. Ring API - List Functions 582
  • 6. Ring Documentation, Release 1.3 ring_list_getstringsize(pList,index) ring_list_getsize(x) (x->nSize) 60.14 Ring API - String Functions In this section we will learn about the string functions provided by the Ring API to create new string and manipulate the string content. String * ring_string_new ( const char *str ) ; String * ring_string_new2 ( const char *str,int nStrSize ) ; String * ring_string_delete ( String *pString ) ; int ring_string_size ( String *pString ) ; void ring_string_set ( String *pString,const char *str ) ; void ring_string_set2 ( String *pString,const char *str,int nStrSize ) ; void ring_string_add ( String *pString,const char *str ) ; void ring_string_add2 ( String *pString,const char *str,int nStrSize ) ; void ring_string_print ( String *pString ) ; void ring_string_setfromint ( String *pString,int x ) ; char * ring_string_lower ( char *cStr ) ; char * ring_string_upper ( char *cStr ) ; char * ring_string_lower2 ( char *cStr,int nStrSize ) ; char * ring_string_upper2 ( char *cStr,int nStrSize ) ; char * ring_string_find ( char *cStr1,char *cStr2 ) ; char * ring_string_find2 ( char *cStr1,int nStrSize1,char *cStr2,int nStrSize2 ) ; /* Macro */ ring_string_tolower(x) ring_string_toupper(x) ring_string_get(x) 60.15 MySQL_Columns() Function Implementation The next code presents the MySQL_Columns() function implementation. This function returns table columns information. void ring_vm_mysql_columns ( void *pPointer ) { MYSQL *con ; MYSQL_RES *result ; int nColumns,x ; MYSQL_ROW row ; MYSQL_FIELD *field ; List *pList, *pList2 ; if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_MISS1PARA); return ; } if ( RING_API_ISPOINTER(1) ) { con = (MYSQL *) RING_API_GETCPOINTER(1,RING_VM_POINTER_MYSQL) ; if ( con == NULL ) { return ; } result = mysql_store_result(con); if ( result == NULL ) { 60.14. Ring API - String Functions 583
  • 7. Ring Documentation, Release 1.3 RING_API_RETNUMBER(0); return ; } pList = RING_API_NEWLIST ; nColumns = mysql_num_fields(result); if ( row = mysql_fetch_row(result) ) { while ( field = mysql_fetch_field(result) ) { pList2 = ring_list_newlist(pList); ring_list_addstring(pList2,field->name); ring_list_adddouble(pList2,field->length); ring_list_adddouble(pList2,field->type); ring_list_adddouble(pList2,field->flags); } } mysql_free_result(result); RING_API_RETLIST(pList); } else { RING_API_ERROR(RING_API_BADPARATYPE); } } Lists are of type List, in the previoud function we declared two pointers of type List using List *pList, *pList2; Note: The function uses RING_API_NEWLIST to create new list instead of ring_list_new() to create the list in Temp. Memory related to the function scope. This way we can return the list from the function. Also we don’t delete the list, if it’s stored in a variable by Ring Code it will be saved, if not it will be automatically deleted by RingVM. The list can contains sub lists, we used the function ring_list_newlist() to create a sublist. The function ring_list_addstring() is used to add string items to the list/sublist. The function ring_list_adddouble() is used to add numeric items to the list/sublist. Note: All numeric items in lists returned from RingVM extension functions must be of type double and added to the list using ring_list_adddouble() function. We return the list from the extension function using the RING_API_RETLIST() function. 60.16 Dynamic/Shared Libraries (DLL/So) and LoadLib() function Instead of rebuilding the RingVM after writing new functions using C/C++ and the Ring API, we can create a DLL/So file and dynamically use the functions provided by this file in the runtime using the LoadLib() function. Dynamic library example in C #include "ring.h" RING_DLL __declspec(dllexport) RING_FUNC(ring_ringlib_dlfunc) { printf("Message from dlfunc"); } RING_DLL void ringlib_init(RingState *pRingState) { 60.16. Dynamic/Shared Libraries (DLL/So) and LoadLib() function 584
  • 8. Ring Documentation, Release 1.3 ring_vm_funcregister("dlfunc",ring_ringlib_dlfunc); } the idea is to create the ringlib_init() function, this function will be called by the RingVM when we use the generated DLL file though the LoadLib() function. Inside the ringlib_init() function we can register the module function or call a function that do the registration process for all of the module functions. The next Ring code demonstrates how to use the DLL library during the runtime. See "Dynamic DLL" + NL LoadLib("ringlib.dll") dlfunc() Output: Dynamic DLL Message from dlfunc 60.16. Dynamic/Shared Libraries (DLL/So) and LoadLib() function 585
  • 9. CHAPTER SIXTYONE EMBEDDING RING LANGUAGE IN C/C++ PROGRAMS We can use the Ring language from C/C++ programs using the next functions RingState *ring_state_init(); ring_state_runcode(RingState *pState,const char *cCode); ring_state_delete(RingState *pState); 61.1 Ring State The idea is to use the ring_state_init() to create new state for the Ring Language then call the ring_state_runcode() function to execut Ring code using the same state. When we are done, we call the ring_state_delete() to free the memory. Example: #include "ring.h" #include "stdlib.h" int main(int argc, char *argv[]) { RingState *pState = ring_state_init(); printf("welcomen"); ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl"); ring_state_delete(pState); } Output: welcome hello world from the ring programming language 61.2 Ring State Functions The Ring API comes with the next functions to create and delete the state. Also we have functions to create new variables and get variables values. RingState * ring_state_init ( void ) ; RingState * ring_state_delete ( RingState *pRingState ) ; void ring_state_runcode ( RingState *pRingState,const char *cStr ) ; List * ring_state_findvar ( RingState *pRingState,const char *cStr ) ; List * ring_state_newvar ( RingState *pRingState,const char *cStr ) ; void ring_state_main ( int argc, char *argv[] ) ; 586
  • 10. Ring Documentation, Release 1.3 void ring_state_runfile ( RingState *pRingState,const char *cFileName ) ; void ring_state_runobjectfile ( RingState *pRingState,const char *cFileName ) ; 61.3 Ring State Variables We can create more than one ring state in the same program and we can create and modify variable values. To get the variable list we can use the ring_state_findvar() function. To create new variable we can use the ring_state_newvar() function. Example: #include "ring.h" #include "stdlib.h" int main(int argc, char *argv[]) { List *pList; RingState *pState = ring_state_init(); RingState *pState2 = ring_state_init(); printf("welcomen"); ring_state_runcode(pState,"see 'hello world from the ring programming language'+nl"); printf("Again from C we will call ring coden"); ring_state_runcode(pState,"for x = 1 to 10 see x + nl next"); ring_state_runcode(pState2,"for x = 1 to 5 see x + nl next"); printf("Now we will display the x variable value from ring coden"); ring_state_runcode(pState,"see 'x value : ' + x + nl "); ring_state_runcode(pState2,"see 'x value : ' + x + nl "); pList = ring_state_findvar(pState,"x"); printf("Printing Ring variable value from C , %.0fn", ring_list_getdouble(pList,RING_VAR_VALUE)); printf("now we will set the ring variable value from Cn"); ring_list_setdouble(pList,RING_VAR_VALUE,20); ring_state_runcode(pState,"see 'x value after update : ' + x + nl "); pList = ring_state_newvar(pState,"v1"); ring_list_setdouble(pList,RING_VAR_VALUE,10); pList = ring_state_newvar(pState,"v2"); ring_list_setdouble(pList,RING_VAR_VALUE,20); ring_state_runcode(pState,"see 'v1 + v2 = ' see v1+v2 see nl"); ring_state_runcode(pState,"see 'end of test' + nl"); ring_state_delete(pState); 61.3. Ring State Variables 587