21. GS9 REFERENCE
defines and functions






print,
println,
scanln,
getch,
tabsize,
tabresize,
tabdel,
tabins,
tabadd,
strempty,
strlen,
strpos,
strchr,
strskip,
strlwr,
strupr,
strsub,
strtrim,
strexplode,
strimplode,
gs_time,
gs_systemtime,
gs_systemdate,
gs_rand,
gs_frand,
gs_srand,
gs_gid,
gs_fid,
gs_gidname,
gs_fidname,
gs_compilefile,
gs_compilestring,
gs_dofile,
gs_dostring,
gs_error,
gs_debug


gs_fileopen,
gs_fileseek,
gs_filetell,
gs_filewriteint,
gs_filewritefloat,
gs_filewritetext,
gs_filewritedata,
gs_filereadint,
gs_filereadfloat,
gs_filereadtext,
gs_filereaddata,
gs_fileclose,
gs_fileinfo,
gs_filedelete,
gs_filefind,
gs_directoryget,
gs_directoryset,
gs_directorycreate,
gs_directorydelete,
gs_inigetint,
gs_inigetstr,
gs_inisetint,
gs_inisetstr


gs_sgn,
gs_abs,
gs_floor,
gs_ceil,
gs_round,
gs_trunc,
gs_exp,
gs_log,
gs_pow,
gs_sqrt,
gs_sin,
gs_cos,
gs_tan,
gs_asin,
gs_acos,
gs_atan,
PI,
PI2,
DEG2RAD,
RAD2DEG,


gs_shell,
gs_launch,
gs_commandline,
gs_messagebox,
gs_messagebeep,
MB_OK,
MB_OKCANCEL,
MB_RETRYCANCEL,
MB_YESNO,
MB_YESNOCANCEL,
MB_ABORTRETRYIGNORE,
MB_ICONEXCLAMATION,
MB_ICONQUESTION,
MB_ICONERROR,
MB_SYSTEMMODAL,
IDOK,
IDCANCEL,
IDYES,
IDNO,
IDABORT,
IDRETRY,
IDIGNORE,




print( ... )
IN......any number of parameters of any type is accepted
Can handle multiple parameters of any type, printing all their values in the console, one by one.
There's an decent limit of length when printing, so huge texts will not be fully displayed.

println( ... )
IN......any number of parameters of any type is accepted
Can handle multiple parameters of any type, printing all their values in the console, one by one.
It also adds a new line character (\n), so the next print call will write it's text on the next line.
There's an decent limit of length when printing, so huge texts will not be fully displayed.

str scanln( )
OUTstrinput line
Waits for the user to input some text in the console and to hit enter, then returns it as a string.
This instruction is usefull only in text console applications.
There's an decent limit of input text (256 characters).

int getch( )
OUTintkey code
Waits for the user to hit a key and returns the key's code.
For printable characters the key code is the associated ASCII code.
For special keys like arrows or function keys, the codes are higher values.

int tabsize( t )
INreftabtreference to table
OUTinttable size
Returns the number of elements from the table refered by t, or 0 if bad parameter.
Similar to sizeof.

tabresize( t, size )
INreftabtreference to table
INintsizenew table size
Resizes the table refered by t.
If the new size is smaller than the old size, the last elements will be deleted.
If the new size is bigger, then the new elements, added at the end of the table, will have NUL values.

tabdel( t, pos, count )
INreftabtreference to table
INintposposition to start deleting
INintcountnumber of elements to delete
Deletes count elements from the table refered by t, starting from position pos.
The last elements remaining are shifted and the table is resized.

tabins( t, pos, count )
INreftabtreference to table
INintposposition where to start insertion
INintcountnumber of elements to insert
Inserts count elements into the table refered by t, starting from position pos.
The inserted elements will have NUL values.
The last elements after the insertion, are shifted and the table is resized.

tabadd( t, count )
INreftabtreference to table
INintcountnumber of elements to add at the end of the table
Adds count elements at the end of the table refered by t.

int strempty( s )
INstr|refstrsstring or reference to string
OUTinttrue if string is empty or false if not
Returns true (1) if the string s (or the string refered by s) is empty or VOID and false (0) if not.
Empty strings have the length 0 and their first element is always the eos character (0).
Invalid strings (VOID) have the length 0, by convention.

int strlen( s )
INstr|refstrsstring or reference to string
OUTintstring's length
Returns the number of characters in the string s (or the string refered by s), until the eos character (0) is reached.
For empty or invalid strings it will return 0.

int strpos( s1, s2, pos=0 )
INstr|refstrs1string to search in
INstr|refstrs2string to search for
[IN]intpos=0search starting position
OUTintfound position or -1 if not found
Searches in the string s1 for the presence of string s2, starting from the specified position #pos.
If the start position is not specified, the search will begin with the first character of s1 (at position 0).
Returns the first position where s2 was found, or -1 if not found at all.

int strchr( s1, s2, pos=0 )
INstr | refstrs1string to search in
INstr | refstrs2characters to search for
[IN]intpos=0search starting position
OUTintfound position or -1 if not found
Searches for any characters from string s2 that are present in string s1.
If the start position is not specified, the search will begin with the first character of s1 (at position 0).
Returns the first position in s1, where a character form the s2 was found.
If, from the specified start position, until the end of s1, no characters from the second string were found, -1 is returned.

int strskip( s1, s2, pos=0 )
INstr|refstrs1string to process
INstr|refstrs2characters to skip
[IN]intpos=0position to start skipping
OUTintposition after skipping
Starting with the specified position in string s1, it skips all characters that belong to the second string s2.
If the start position is not specified, the processing will begin with the first character of s1 (at position 0).
Returns the position of the first character in s1, that is not found in s2.
If all characters from s1 are found in the s2 too, then all the length of s1 is returned.
If any of the strings are invalid, -1 will be returned to mark the error.

str strlwr( s )
INstr|refstrsstring or reference to string
OUTstrlower case string
Returns the string s converted to lower case.
All characters from 'A' to 'Z' become 'a' to 'z'.
If the string is invalid, an invalid string will be returned.

str strupr( s )
INstr|refstrsstring or reference to string
OUTstrupper case string
Returns the string s converted to upper case.
All characters from 'a' to 'z' become 'A' to 'Z'.
If the string is invalid, an invalid string will be returned.

str strsub( s, pos, count )
INstr|ref_strsstring to substract from
INintposposition of the substring
[IN]int=allcountnumber of elements of the substring
OUTstrsubstring
Returns a substring (a part) of the string s, starting with position pos and containing count characters (if available).
If the optional count parameter is not specified, the substring will represent the last part s1, from the start position, up to the eos character.
If any of the strings are invalid, an invalid string will be returned.

str strtrim( s1, s2 )
INstr|refstrs1string to be trimmed
INstr|refstrs2characters to trim
OUTstrtrimmed string
Processes the string s1 by removing beginning and ending characters that are also found in string s2 and return the result.
If any of the strings is invalid, an invalid string will be returned.

tab strexplode( s, separator )
INstr|refstrsstring or reference to string
INintseparatorASCII code of the separator character
OUTtabexploded table with substrings
Creates a table containing substrings from s, separated by a specified character.
The separator character is given throught it's ASCII code.
If the input string is invalid, an invalid string will be returned.

str strimplode( t, separator )
INtab|reftabttable of substrings
INintseparatorASCII code of the separator character
OUTstrimploded string with separators
Concatenates all the valid string elements from a table t, into a string, separating them with a specified character.
Non string elements from the table are ignored.

int gs_time( )
OUTinttickcount time (ms)
Returns the computer's time counter, called Tick Count, in milliseconds (1sec = 1000 ms).
This counter is incremented each millisecond, since the computer was started.
This function can be used to track time.

gs_systemtime( hour, minute, second )
INrefinthourcomputer current hour [0..23]
INrefintminutecomputer current minute [0..59]
INrefintsecondcomputer current second [0..59]
Returns in the reference parameters the computer's curent time.

gs_systemdate( year, month, day )
INrefintyearcomputer's current year
INrefintmonthcomputer's current month [0..11]
INrefintdaycomputer's current day [0..31]
Returns in the reference parameters the computer's curent date.

int gs_rand( range=100 )
[IN]intrange=100generation interval from 0 to range
OUTintrandom value in given range
Returns an integer random number in a specified range, form 0 up to #range-1.
If #range parameter is missing a default range of 100 will be used.

float gs_frand( range=1.0 )
[IN]floatrange=1.0generation interval from 0 to range
OUTfloatrandom value in given range
Returns a float random number in a specified range, form 0.0 up to almost #range.
If #range parameter is missing a default range of 1.0 will be used.

gs_srand( seed=time )
[IN]intseed=timerandom generator seed
Resets the random numbers generator, with a specified #seed value.
After calling this function with the same seed, the same random pattern will be generated by calls of gs_rand.
By default, if no seed is specified, a value based on the computer's time counter is used, so the random pattern will be different each time.

int gs_gid( varname )
INstrvarnameglobal variable's name
OUTintvariable's id or -1 if not found
Returns the internal id of the varname global variable.
If no global variable is found with the specified name, -1 will be returned.
The id can be used in var commands, to access the variable.
See the Advanced access chapter.

int gs_fid( funcname )
INstrfuncnamefunction's name
OUTintfinction's id or -1 if not found
Returns the internal id of the funcname function.
If no function is found with the specified name, -1 will be returned.
The id can be used in call commands, to call the function.
See the Advanced access chapter.

str gs_gidname( gid )
INintgidglobal variable's id
OUTstrglobal variable's name or VOID if not found
Returns the name of a global variable with the gid internal id.
If no global variable was found with the specified id, VOID will be returned.
Use gs_gid to obtain the id of a global variable.
See the Advanced access chapter.

str gs_fidname( fid )
INintfidfunction's id
OUTstrfunction's name or VOID if not found
Returns the name of a function with the fid internal id.
If no function was found with the specified id, VOID will be returned.
Use gs_fid to obtain the id of a function.
See the Advanced access chapter.

int gs_compilefile( programfile )
INstrprogramfilepath to a valid program file
OUTint1=success, 0=fail
Compiles the program contained in the specified file and remember it's content.
The content of the file must be a valid GS9 program (with functions).
If an error occures or the file is not found, it will return false.
Somehow similar to the #include instruction.

int gs_compilestring( programtext )
INstrprogramtextvalid program text
OUTint1=success, 0=fail
Compiles the program contained in the specified string and remember it's content.
The content of the string must be a valid GS9 program (with functions).

int gs_dofile( codefile )
INstrcodefilepath to a valid instructions file
OUTint1=success, 0=fail
Compiles and execute the instructions contained in the specified file, than forget it's content.
The file must contain valid GS9 instructions, but no functions or other global declarations.
The effect will be like if the code from the file would be in a temporary function and that function is called right there.

int gs_dostring( codetext )
INstrcodetextvalid instrutions text
OUTint1=success, 0=fail
Compiles and execute the instructions contained in the specified string, than forget it's content.
The string must contain valid GS9 instructions, but no functions or other global declarations.
The effect will be like if the code from the string would be in a temporary function and that function is called right there.

gs_error( info )
[IN]strinfoerror info
Generates an user error and stop the execution of the program.
If info is specified, the text will be reported along with the error.
It can be used to handle critical errors, that usually shouldn't happend, and to stop program in a "controlled" way.

gs_debug( debuglevel )
INintdebuglevelbinary debug flags
This function is used for advanced debuging.
It changes the processor level of debug, to receive more or less information when an error occures.
The debug level is an integer value with various flag options stored on bits.
bit 0: halt on errors, with a message box info
bit 1: log error description and short call stack info
bit 2: list current function machine code (used when bit 4 is set)
bit 3: list data on the stack (used when bit 4 is set)
bit 4: list extensive info about the current call stack (use bit 2 and 3)
bit 5: dump global variables with names and values
bit 6: dump all functions assemler code
The default level of debug is 3 (halt and short description) and should be enough to track most common errors.



ptr gs_fileopen( filename, mode )
INstrfilenamepath to the file to be opened
INintmodeopen mode 0=read, 1=write, 2=both
OUTptrfile handler
Opens the file with the path and name specified by the filename parameter.
If mode is 0 then the file will be opened only for read. File must exist.
If mode is 1 then the file will be opened only for write. If not found, the file is created, otherwise it's content will cleared.
If mode is 2 then the file will be opened for both read and write. If not found, the file is created. Useful for appending new data.
If the open is successful, the file handler is returned. If not, it will return an invalid ptr (NULL).
The directories from the specified file path must always exist.
Remember that to specify the backslash in file paths, you must use a double backslash (like "c:\\myfile.txt")
The file cursor is the current position inside the file (in bytes), where the next read or write will take place.
When a file is opened, it's file cursor is set from the beginning of the file (0).
Always remeber to finaly close opened files, with gs_fileclose.

int gs_fileseek( f, pos, mode )
INptrffile handler
INintposposition to seek to
INintmodeseek mode 0=beginning, 1=current, 2=end
OUTint1=success, 0=fail
Moves file cursor to the specified position in file.
If mode is 0, pos position is considered from the beginning of the file and it must be positive or zero.
If mode is 1, pos position is considered as an offset from where the cursor is right now.
If mode is 2, pos position is considered from the end of the file and it must be negative or zero.
See gs_fileopen for examples.

int gs_filetell( f )
INptrffile handler
OUTintcurrent position or -1 if error
Returns the current position of the file cursor (in bytes).
Can be used with gs_fileseek(f,0,2) to find the size of the file.
See gs_fileopen for examples.

int gs_filewriteint( n, f )
INintnnumber to write
INptrffile handler
OUTint1=success, 0=fail
Writes the integer value of the parameter n in the file f at the current position of the file cursor.
Since integers in GS9 have 32bits, the file cursor will be incremented with 4 bytes;
The file must be opened for write.

int gs_filewritefloat( n, f )
INfltnnumber to write
INptrffile handler
OUTint1=success, 0=fail
Writes the float value of the parameter n in the file f at the current position of the file cursor.
Floats in GS9 are stored on 32bits, so the file currsor will be incremented with 4 bytes;

int gs_filewritetext( s, f )
INstrsstring to write
INptrffile handler
OUTint1=success, 0=fail
Writes in f all characters from the s string, without eos (0).
Add another two characters 0xd, 0xa (\r\n) representing the end of line marker (eol).
This sequence is specific to text files in Windows.
Even if \n is enought to move on the next line, using only this might puzzle some primitive text editors.

int gs_filewritedata( buffer, size, f )
INrefstrbufferrefered buffer of characters to write
INintsizesize to write (in bytes)
INptrffile handler
OUTint1=success, 0=fail
Writes a specified number of characters from a string refered by buffer.
Warning: buffer string's size must be at least size characters (including eos).

int gs_filereadint( n, f )
INrefintnrefered integer variable to read in
INptrffile handler
OUTint1=success, 0=fail
Reads an integer value from file f into n and increment the file cursor with 4 bytes.

int gs_filereadfloat( n, f )
INreffloatnrefered float variable to read in
INptrffile handler
OUTint1=success, 0=fail
Reads a float value from file f into n and increment the file cursor with 4 bytes.

int gs_filereadtext( s, f )
INrefstrsrefered string variable to read in
INptrffile handler
OUTint1=success, 0=fail
Reads a line of text from a file (all characters until eol(\r\n) is reached).
Store it in a string, without the eol, but adding the eos.
So the string refered by s must be big enough to hold all the caracters from the line plus one for eos.

int gs_filereaddata( buffer, size, f )
INrefstrbufferrefered buffer of characters to read in
INintsizesize to read (in bytes)
INptrffile handler
OUTint1=success, 0=fail
Reads a number of characters (bytes) from file f into a string buffer.
Warning: string buffer's size must be at leaset size (including eos) or the program may crash!

int gs_fileclose( f )
INptrffile handler
OUTint1=success, 0=fail
Closes the file handler f.
After closing the file, the handler f becomes invalid, and the file requires another opening if more read or write is needed.

str gs_fileinfo( filename )
INstrfilenamepath to a file
OUTstrfile info string in a fixed format
Returns file info in the following format: "Wed Jan 02 02:03:55 1980\nFILE_SIZE".
The FILE_SIZE substring have variable length, but the first part is fixed.
If file is not found, a VOID string is returned.

int gs_filedelete( filename )
INstrfilenamepath to file
OUTint1=success, 0=fail
Deletes the specified file from hard disk.

gs_filefind( path, callback, recursive )
INstrpathpath to a directory or "" for current
INstrcallbackfunction name to be called for each found file
INintrecursivego deep into directories
Finds all files from the given directory name. The directory name must include the ending back slash.
For each found file a function with the given callback name is called.
The callback function must have two parameters: filepath (str), and isdir (int) that are received from the find cycle.
If recursive is 1, the files will be searched deep into any found directories.

str gs_directoryget( )
OUTstrpath to the current directory
Returns a string containing the path of the current directory.

int gs_directoryset( pathdir )
INstrpathdirpath to the directory to set as current
OUTint1=success, 0=fail
Changes current directory to pathdir.

gs_directorycreate( pathdir )
INstrpathdirpath to a directory
Creates a new folder.
The path up to the directory must exists.
No error is returned.

int gs_directorydelete( pathdir )
INstrpathdirpath to a directory
OUTint1=success, 0=fail
Delete anexisting folder.
The path up to the directory must exists.

int gs_inigetint( filename, group, key, val )
INstrfilenamepath to ini file
INstrgroupgroup to search in
INstrkeykey name to search for
INrefintvalreceive key value
OUTint1=success, 0=fail
Reads an integer value from a standard INI file, with the specified group and key.
Warning: this function uses a common internal text buffer that is rewritten after each call.

int gs_inigetstr( filename, group, key, val )
INstrfilenamepath to ini file
INstrgroupgroup to search in
INstrkeykey name to search for
INrefstrvalreceive key value
OUTint1=success, 0=fail
Reads a string value from a standard INI file, with the specified group and key.
Warning: this function uses a common internal text buffer that is rewritten after each call.

gs_inisetint( file_name, group, key, val )
INstrfile_namepath to ini file
INstrgroupgroup to search in
INstrkeykey name to search for
INintvalkey value to set
Writes an integer value into a standard INI file, with the specified group and key.
Warning: this function uses a common internal text buffer that is rewritten after each call.

gs_inisetstr( file_name, group, key, val )
INstrfile_namepath to ini file
INstrgroupgroup to search in
INstrkeykey name to search for
INstrvalkey value to set
Writes a string value into a standard INI file, with the specified group and key.
Warning: this function uses a common internal text buffer that is rewritten after each call.



int|float gs_sgn( n )
INint|floatnnumber
OUTint|floatsign -1=negative,0=zero,1=positive
Returns the sign of the integer or float number n (accepts both types).
Returns -1 if n is negative, 0 if equal to zero and 1 if positive.

int|float gs_abs( n )
INint|floatnnumber
OUTint|floatabsolute value
Returns the same value if n is positive and -n if it's negative.
The result is always greater or equal to zero.

float gs_floor( n )
INfloatnnumber
OUTfloatfloor value
Returns the integer part of n.

float gs_ceil( n )
INfloatnnumber
OUTfloatceil value
Returns the integer part n+1

float gs_round( n )
INfloatnnumber
OUTfloatrounded value
Returns the closest integer value to the specified number.
If the fractional part of the n is less than 0.5, it will return the floor, otherwise it will return the ceil.

float gs_trunc( n, decimals=5 )
INfloatnnumber
[IN]floatdecimals=5count of decimals to keep
OUTfloattruncated number
Will return the same value of n, but only with the specifed number of decimals.
Even if #decimals has a float type, only integer values will make sence for it.

float gs_exp( n )
INfloatnnumber
OUTfloate rised to the power of n
Returns the result of the exponential function, applied on n.

float gs_log( n )
INfloatn
OUTfloatnatural logarithm of n
Returns the result of the natural logarithm function applied on n.

float gs_pow( n, p )
INfloatnnumber
INfloatpexponent
OUTfloatn raised to power of p
Returns the result of n raised to the power of p.

float gs_sqrt( n )
INfloatnnumber
OUTfloatsquare root of n
Returns the square rood of n

float gs_sin( a )
INfloataradians
OUTfloatsine of a
Calculates sine from angle a in radians (~7 exact decimals).

float gs_cos( a )
INfloataradians
OUTfloatcosine of a
Calculates cosine from angle a in radians (~7 exact decimals).

float gs_tan( a )
INfloataradians
OUTfloattangent of a
Calculates tangent from angle a in radians (~7 exact decimals).

float gs_asin( n )
INfloatnnumber
OUTfloatarcsine of n, in radians
Calculate arcsine from n, returning angle in radians.

float gs_acos( n )
INfloatnnumber
OUTfloatarccosine of n, in radians
Calculate arccosine from n, returning angle in radians.

float gs_atan( n )
INfloatnnumber
OUTfloatarctangent of n, in radians
Calculate arctangent from n, returning angle radians.

PI 3.1415926535897932385
Define with the value of PI.

PI2 6.283185307179586477
Define with the value of 2*PI.

DEG2RAD PI / 180.0
Multiply this with degrees to convert them into radians.

RAD2DEG 180.0 / PI
Multiply this with radians to convert them into degrees.



int gs_shell( filename, workdir, cmdline )
INstrfilenamepath to a file
[IN]strworkdirstart directory
[IN]strcmdlinecommand line parameters
OUTint1=success, 0=fail
Execute the file filename or opens it in the default viewer associated with the file's extension.
The default web browser is used for .html, text viewer for .txt, etc.
Starting folder workdir and command line cmdline parameters are optional.

int gs_launch( exefile, workdir, cmdline )
INstrexefilepath to an executable file
[IN]strworkdirstart directory
[IN]strcmdlinecommand line parameters
OUTint1=success, 0=fail
Launch an executable (.exe) file.
Starting folder workdir and command line cmdline parameters are optional.

str gs_commandline( )
OUTstrcommand line
Returns the command line the console or the embedding application was launched with.
The command line will begin with the executable file's name that may use quotes.
The GS9 console command line will continue with the name of the program file, since that is the first parameter.

int gs_messagebox( text, title, type=MB_OK )
INstrtextsome text message
[IN]strtitlebox title
[IN]inttype=MB_OKbuttons and icon flags
OUTintselected button id
Pops an Windows message box with the specified parameters.
The type can be a binary combination of button and icon options.
MB_OK, MB_OKCANCEL, MB_YESNO, MB_YESNOCANCEL, MB_RETRYCANCEL, MB_ABORTRETRYIGNORE for specifying the box's buttons.
MB_ICONEXCLAMATION, MB_ICONQUESTION, MB_ICONERROR for specifying the box's icon
MB_SYSTEMMODAL for blocking other applications until this box is closed
It returns a response generating by chooseing one of the box's buttons.
IDOK, IDCANCEL, IDYES, IDNO, IDABORT, IDRETRY, IDIGNORE

int gs_messagebeep( type=-1 )
[IN]inttype=-1sound
OUTint1=success, 0=fail
Generate an Windows message beep with the specidied parameters.
The type can be one of the following:
MB_ICONEXCLAMATION, MB_ICONQUESTION, MB_ICONERROR for specifying the box's icon.

MB_OK MB_OK
Used by gs_messagebox to specify dialog buttons.

MB_OKCANCEL MB_OKCANCEL
Used by gs_messagebox to specify dialog buttons.

MB_RETRYCANCEL MB_RETRYCANCEL
Used by gs_messagebox to specify dialog buttons.

MB_YESNO MB_YESNO
Used by gs_messagebox to specify dialog buttons.

MB_YESNOCANCEL MB_YESNOCANCEL
Used by gs_messagebox to specify dialog buttons.

MB_ABORTRETRYIGNORE MB_ABORTRETRYIGNORE
Used by gs_messagebox to specify dialog buttons.

MB_ICONEXCLAMATION MB_ICONEXCLAMATION
Used by gs_messagebox or gs_messagebeep to specify icon or sound.

MB_ICONQUESTION MB_ICONQUESTION
Used by gs_messagebox or gs_messagebeep to specify icon or sound.

MB_ICONERROR MB_ICONERROR
Used by gs_messagebox or gs_messagebeep to specify icon or sound.

MB_SYSTEMMODAL MB_SYSTEMMODAL
Used by gs_messagebox for blocking message boxes.

IDOK IDOK
Returned by gs_messagebox depending on the dialog response.

IDCANCEL IDCANCEL
Returned by gs_messagebox depending on the dialog response.

IDYES IDYES
Returned by gs_messagebox depending on the dialog response.

IDNO IDNO
Returned by gs_messagebox depending on the dialog response.

IDABORT IDABORT
Returned by gs_messagebox depending on the dialog response.

IDRETRY IDRETRY
Returned by gs_messagebox depending on the dialog response.

IDIGNORE IDIGNORE
Returned by gs_messagebox depending on the dialog response.