QTAwk supports user-defined functions and has enhanced them
over Awk in several important aspects.
Local Variables
In QTAwk it is no longer necessary to declare local variables as excess arguments in the function definition. QTAwk has included the local keyword. This keyword may be used in any compound statement, but was invented specifically for user-defined functions. Consider the simple function to accumulate words from the current input record in the formatting utility:
# accumulate words for line
function addword(w) {
local lw = length(w); # length of added word
# check new line length
if ( cnt + size + lw >= width ) printline(yes);
line[++cnt] = w; # add word to line array
size += lw;
}
The fact that lw is local to the function and will disappear when
the
function is exited is obvious from the definition of lw. It is also now
easy to pick out the function arguments, 'w' in this case. The
initialization
of lw to the length of the argument passed is also easily picked up
from
the definition. The local keyword thus truly separates local
variables
from the function arguments and global variables.
Argument Checking
By using the _arg_chk built-in
variable,
it is also possible to have QTAwk now do some argument checking
for
the user. If _arg_chk is TRUE, then QTAwk will, at
run-time,
check the number of arguments passed against the number of arguments
defined. If the number passed differs from the number defined, then a
run-time error
is issued and QTAwk halts. When _arg_chk is FALSE, QTAwk
will check at run-time only that the number of arguments passed is less
than
or equal to the number defined. This follows the Awk practice
and
allows for the use of arguments defined, but not passed, as local
variables. The default value of _arg_chk is FALSE. It is
recommended that
_arg_chk be set to TRUE and the local
keyword used to define variables meant to be local to a function.
Variable Length Argument Lists
QTAwk allows user-defined functions to be defined with a variable number of arguments. The actual number of arguments will be determined from the call at run-time. QTAwk follows the C syntax for defining a function with a variable number of arguments:
# function to determine maximum value
function max(...) {
local max = vargv[1];
local i;
for ( i = 2 ; i <= vargc ; i++ )
if ( max < vargv[i] ) max = vargv[i];
return max;
}
The ellipses, ..., are used as the last argument in a user-defined argument list to indicate that a variable number of arguments follow. In the max function shown, no fixed arguments are indicated. Within the function, the variable arguments are accessed via the built-in singly-dimensioned array, vargv. The built-in variable vargc is set equal to the number of elements of the array and, hence, the variable number of arguments passed to the function. Since the variable arguments are passed in a singly dimensioned array, the for statement may be used to access each in turn:
# function to determine maximum value
function max(...) {
local max = vargv[1];
local i;
for ( i in vargv )
if ( max < vargv[i] ) max = vargv[i];
return max;
}
A user-defined function may have fixed arguments and a variable number of arguments following:
# function with both fixed and variable number of arguments
function sample(arg1,arg2,...) {
.
.
.
}
If a user-defined function is to have a variable number of
arguments,
then the local keyword must be used
to define
local variables. The ellipses denoting the variable arguments must be
last
in the function definition argument list.
Null Argument List
A user defined function may be defined with no arguments. Consider the function to accumulate words from input records for the text formatter:
# function to add current line to parsed text
function add_line() {
for ( i = 1 ; i <= NF ; i++ ) if ( length($i) ) addword($i);
}
In the case of a user-defined function with no arguments to be passed, the function may be invoked with no parenthesized parameter list. Consider the invocation of the add_line function in the text formatter. The action executed for input records which do not start with a format control word is:
{
if ( format ) add_line;
else if ( table_f ) format_table($0);
else output_line($0,FALSE);
}
In QTAwk, the add_line function may be invoked as "add_line" as above or as "add_line()", with a null length parameter list.
QTAwk has also relaxed the Awk rule that the left parenthesis
of
the parameter list must immediately follow a user-defined function
invocation. QTAwk allows blanks between the name and the left
parenthesis. The
blanks are ignored.
Arrays and Used-Defined Functions
Just as arrays are integrated into QTAwk expressions, arrays are also integrated into the passing of arguments to, and the return value from, user-defined functions. Used-defined functions may return arrays as well as scalars. This will be illustrated in a sample utility later.
QTAwk passes scalar arguments to user-defined functions by value, i.e., if a scalar variable is specified as an argument to a function, a copy of the variable is passed to the function and not the variable itself. This is called pass by value. Thus, if the function alters the value of the argument, the variable is not altered, only the copy. When the function terminates, the copy is discarded, and the variable still retains its original value.
Note that arguments may be passed as constants, e.g., 4. QTAwk does not pass the constant literally, but creates a temporary variable with the desired constant value and passes the temporary variable to the function. The temporary variable is discarded by QTAwk on function termination. Thus, internal to the function the value of the temporary variable may be changed by the function, but any changes are discarded on function termination since the temporary variable is discarded.
In contrast, QTAwk passes array variables by "reference". This means that the local variable represented by the function argument, is the referenced variable and not a copy. Any changes to the local variable are actually made to the referenced variable.
In QTAwk, function arguments may also be constant arrays and not variable arrays, i.e., the argument may be the result of an arithmetic operation on an array. For example, if A is an array, then the result of the expression:
"A + 10"
is an array. QTAwk creates a temprorary array variable and passes a reference to the temporary array as the function argument. The temporary variable is discarded at function termination.
QTAwk passes by reference under two conditions:
This second condition is patterned after the action of the first argument of the split function:
No_fields = split(strg,ary);
Whatever value(s) the variable ary had when split is called, is discarded by the call and ary is an array when the function terminates.
The same action can be acheived with a user-defined function.
The following QTAwk utility with a user-defined function will illustrate the use of arrays and scalars as function arguments and the return of arrays by user-defined functions.
1: BEGIN {
2: # create arrays 'a' and 'b'
3: for ( i = 1 ; i < 6 ; i++ ) a[i] = b[i] = i;
4: # create scalars 'c' and 'f'
5: c = f = 10;
6:
7: # pass scalar variables/values and return scalar value
8: print "scalar : " >< set_var(c,c,c) >< " and c == " >< c;
9:
10: # pass two arrays, 'a' & 'b',
11: # and one scalar constant, 'c+0'
12: # function will return an array "== a + b + (c+0)"
13: d = set_var(a,b,c+0);
14: # print returned array 'd' (== a + b + (c+0))
15: for ( i in d ) print "d[" >< i >< "] = " >< d[i];
16: #print scalar 'c' to show unchanged
17: print c;
18:
19: # pass two arrays, 'a' & 'b',
20: # and one scalar variable, 'c'
21: # function will return an array "== a + b + c"
22: e = set_var(a,b,c);
23: # print returned array
24: for ( i in e ) print "e[" >< i >< "] = " >< e[i];
25: # print former scalar, 'c',
26: # converted to array by operation c = b + 2;
27: for ( i in c ) print "c[" >< i >< "] = " >< c[i];
28:
29: # pass two arrays, 'a' & 'b', and constant array, 'b+0'
30: h = set_var(a,b,b+0);
31: # print returned array
32: ( i in h ) print "h[" >< i >< "] = " >< h[i];
33: # print array 'b' to assure that unchanged
34: for ( i in b ) print "b[" >< i >< "] = " >< b[i];
35: # attempt illegal operation in function: w = f + b
36: # adding array, 'b', to scalar, 'f'.
37: # error message will be issued and execution halted
38: g = set_var(f,b,f);
39: }
40:
41: function set_var(x,y,z) {
42: # create local variable
43: local w = x + y + z;
44:
45: # alter third argument
46: # if second argument an array,
47: # this will convert third to an array
48: # (if not already passed as an array).
49: z = y + 2;
50: return w;
51: }
This QTAwk utility illustrates several ideas in using arrays and user-defined functions in QTAwk. Line 8:
print "scalar : " >< set_var(c,c,c) >< " and c == " >< c;
calls the function 'set_var' with three scalar variables, all 'c'. Three copies of 'c' are actually passed. The local variable, 'w', is computed using scalar quantities and is a scalar quantity. Since argument 'y' is a scalar quantity, the result of the expression:
z = y + 2;
is a scalar and the third argument, 'c', is unchanged. A functional value of 30 (== c + c + c) is returned.
Line 13:
d = set_var(a,b,c+0);
passes arrays as the first and second arguments. The third argument is a constant scalar value, and thus QTAwk creates a temporary variable with a scalar value of c+0 which is passed to the function. Line 49 in the function:
z = y + 2;
changes the third argument internal to the function to an array. However, the third argument for this call is a temporary variable created when the call was made, and it is discarded at function termination.
The return value of the function, line 43:
w = x + y + z; (== a + b + 10;)
is an array. Line 15:
for ( i in d ) print "d[" >< i >< "] = " >< d[i];
prints the values of the array:
d[1] = 12
d[2] = 14
d[3] = 16
d[4] = 18
d[5] = 20
Line 22:
e = set_var(a,b,c);
passes arrays as the first and second arguments. The third argument is a variable, and thus can be changed by the function if the third argument at function termination is an array. The return value of the function:
w = x + y + z; # (== a + b + c;)
is an array as above. Note that at function termination, the third argument, c, is now an array since it was set to the result of an operation on an array, at line 49:
z = y + 2;
which is now equivalent to:
z = b + 2;
Thus, at function termination the scalar variable c has been converted to an array. Line 27:
for ( i in c ) print "c[" >< i >< "] = " >< c[i];
will print the values of the array elements:
c[1] = 3 ( == b[1] + 2)
c[2] = 4 ( == b[2] + 2)
c[3] = 5 ( == b[3] + 2)
c[4] = 6 ( == b[4] + 2)
c[5] = 7 ( == b[5] + 2)
Line 30:
h = set_var(a,b,b+0);
passes arrays as the first and second arguments. The third argument is a constant array. QTAwk creates a temporary variable to hold the constant array which is passed to the function. The return value of the function is an array as above. Note that at function termination, the third argument is again an array as above. However, the third argument has been passed as a temporary variable and it is discarded at function termination. Line 34:
for ( i in b ) print "b[" >< >< i"] = " >< b[i];
prints the array 'b' to assure that it was not changed.
Line 38:
g = set_var(f,b,f);
will result in an illegal operation in the function:
local w = x + y + z; (== f + b + f;)
this operation is now attempting to add an array to a scalar:
f + b
This operation will result in an error message and halt execution.
The above sample QTAwk utility illustrates the power of
user-defined
functions in automatically handling scalars and arrays as both
arguments
and return values and adjusting accordingly. The same function may be
used interchangeably for both arrays and scalars with natural and
predictable
results.
Adding
User-Defined Functions in Modules
There is another way
to add user defined functions to a QTAwk
utility, by dynamically linking
modules, written in 'C', which are compiled and linked. The
modules are then loaded at run-time by using the builtin function 'load_module'. Functions written in
C in the loaded module may then be called just like any user defined
function written in QTAwk.
Such functions have the same properties and
act just like a user defined function defined with the "function"
pre-defined pattern keyword.
Writing
such user defined functions in 'C'
has at least three advantages:
The QTAwk utility, sim_srev.awk, can be run to load and
execute the user defined functions defined in this module.
This module has three user defined functions:
'sim_srev' which duplicates the action of the builtin function 'srev', and
| typedef long long int qta_long; |
This is a double long integer
under "gcc". It is the natural representation of all QTAwk variable integer values. |
| typedef unsigned long long int
qta_ulong; |
This is a double long unsigned integer under "gcc". It is the natural representation of all QTAwk variable unsigned integer values. |
| typedef struct var_inst
*Variable_ptr; |
This a pointer to a QTAwk variable. Any such pointers
returned to the user 'C' module functions should never be changed. |
| typedef struct QTAwk_index
*QTAwk_index_ptr; |
This a pointer to the structure
defined below for passing array indices to and from QTAwk |
| typedef union index_rep { unsigned long numeric_index; /* Numeric Index */ unsigned char *char_index; /* String Index */ } index_rep; |
This is a union for storing
either an integer or string index |
| typedef struct QTAwk_index { unsigned char index_type; index_rep index_rep; } QTAwk_index; |
This is the structure containing
the actual index and specifies the type of index stored. |
| QTAwk_register_function_count |
prototype: void QTAwk_register_function_count( unsigned function_count); Function to prepare to register dynamically loaded function names. IMPORTANT: This function must be called prior to registering any user defined function via the next QTAwk function. Called once before calling next function to register each function. This call passes the number of functions to register. This function is called by the 'initialization' function of a module loaded with the QTAwk built-in function: 'load_module'
|
||||||||||||||||
| QTAwk_register_function |
prototype: unsigned char *QTAwk_register_function( unsigned char *function_name, unsigned char **argument_names, unsigned variable_arguments); function to register function names loaded dynamically. Call for each function loaded. Returns:
'load_module'
When the 'load_module' built-in function is called, QTAwk has the file named in the
first argument loaded into memory and linked with QTAwk. The function named in the
second argument, which is in the module loaded, is called. That
function then initializes the loaded module and must call this
function to register with QTAwk
each function name loaded. If this function is not called to
register the function names, then none of the functions in the loaded
module are known and cannot be called.The arguments are:
The registered function is called with one argument, an unsigned integer specifying the number of arguments which have been loaded into the array of local variables. All registered functions are assumed to return a QTAwk variable, i.e., all registered functions are assumed to have a function prototype of: Variable_ptr
registered_function(unsigned argument_count);
Returning a NULL pointer, is equivalent to returning no value to the calling QTAwk utility. This function returns NULL if no error occurred in registering the function(s) or a pointer to an error string if an error occurred. A module may be "unloaded" by calling the QTAwk built-in function: 'unload_module'
All functions which have been registered for the module are
automatically unregistered and the module unloaded from memory when
'unload_module' is called. |
||||||||||||||||
| QTAwk_set_module_exit_function |
prototype: void QTAwk_set_module_exit_function( unsigned char *function_name); This function registers the module exit function. If the exit function has the standard exit function name 'QTAwk_exit_load_module', then the function was found and linked automatically. If the user desires a different, unique name for the module exit function, then it must be registered using this function call in the initialization function. The module exit function must have a prototype similar to the following: unsigned
QTAwk_exit_load_module(void);
|
||||||||||||||||
| QTAwk_set_module_shutdown_function |
prototype: void QTAwk_set_module_shutdown_function( unsigned char *function_name); This function registers the module shutdown function. If the module is have a shutdown function, then it must be registered using this function call in the initialization function. The module shutdow function is assumed to have a function prototype similar to: void
QTAwk_shutdown_function(void);
That is the shutdown function
takes no arguments and returns no value.
|
||||||||||||||||
| QTAwk_get_argument |
prototype: Variable_ptr QTAwk_get_argument( unsigned arg_cnt, unsigned char *arg_name); QTAwk function to get argument for dynamically loaded function. May pass either the argument count (starting with 1 for first argument), or the argument variable name. The variable name to be used is the name specified in the call to 'QTAwk_register_function' Search by name takes precedence over search by count. |
||||||||||||||||
| QTAwk_get_value |
prototype: void *QTAwk_get_value( Variable_ptr QTAwk_variable, unsigned *return_type); QTAwk function to retrieve value of QTAwk variable. Use variable class defined constants in the header file to specify value desired. The available constants are:
The function returns a pointer to 'void' type which must be cast to the type specified in the call. The following value types all return an 'unsigned char *', i.e., a pointer to string value:
The "LONG_VAL" returns a pointer to a "qta_long" value defined under Linux as "long long int *", "qta_log *" The "DOUBLE_VAL" returns a pointer to a "double" value, "double *" Cast the return pointer to a pointer to the desired type and copy the value to local memory. The memory used for the return value WILL be overwritten by subsequent calls. NOTE: if the return type is specified as "NO_VAL", then the value returned is whatever type the QTAwk variable already is and 'return_type' is set to the appropriate type value. This is one method of ascertaining the type of a QTAwk variable. The 'QTAwk_get_type' function is a better way. If the QTAwk variable is not of the type requested, then it is converted to that type following the QTAwk normal rules for conversion. |
||||||||||||||||
| QTAwk_set_value |
prototype: Variable_ptr QTAwk_set_value( Variable_ptr QTAwk_variable, void const *value, unsigned set_type); QTAwk function to set QTAwk variable value. Any previous variable values are discarded. The first argument is the QTAwk variable to set. The second argument is a pointer to the value (caller casts to a "void *"). The third argument is one of the variable class constants defined above and in the QTAwk header file If one of the following string types is used:
If a type of "NO_VAL" is specified, the variable is initialized to a value of the null string and a zero integral value. Also see the QTAwk function 'QTAwk_assign' for assigning the value of one variable to another. |
||||||||||||||||
| QTAwk_get_type |
prototype: unsigned QTAwk_get_type( Variable_ptr QTAwk_variable); QTAwk function for determining the type of a QTAwk variable. The return value is an unsigned integer value for one of the type constants defined above:
|
||||||||||||||||
| QTAwk_get_name |
prototype: unsigned char *QTAwk_get_name( Variable_ptr QTAwk_variable); QTAwk function to get the name of the specified variable. Return a pointer to unsigned char. The returned value is a copy of the variable name and the memory has been allocated by a call to 'calloc'. The calling function must ensure that the memory is released when no longer needed. |
||||||||||||||||
| QTAwk_get_named_variable |
prototype: Variable_ptr QTAwk_get_named_variable( unsigned char *variable_name); QTAwk function to get the named variable specified. The name specified may be any built-in variable or variable named by the user utility. Note that local variable names are searched first, then global names. Note that "local" names are those 'local' to the braced statement within which the dynamically loaded user function is called. NOTE: If a local or global variable with the specified name does not exist a global variable with the specified name will be created. |
||||||||||||||||
| QTAwk_get_variable_set |
prototype: Variable_ptr QTAwk_get_variable_set( void const *value, unsigned set_type); QTAwk function to get a QTAwk variable initialized to a specified value. The returned variable is assigned the value specified, c.f., 'QTAwk_set_value' function call for the the values that may be used for the first and second arguments. If a type of "NO_VAL" is specifed, this call returns an initialized variable set set to zero This is a "temporary" variable. This variable will disappear "magically" when the dynamically loaded user function returns. An illustration of this function can be found in the moules: |
||||||||||||||||
| QTAwk_get_local_variable_set |
prototype: Variable_ptr QTAwk_get_local_variable_set( unsigned char *vname, void const *value, unsigned set_type); QTAwk function top get a 'local' variable. The returned variable is local to the calling dynamically loaded user function - identical to using the "local" keyword. The Variable will be "lost" when the function returns. Note: the variable MUST be named, i.e., a non-NULL value must be passed for the first argument, 'vname' This variable will disappear "magically" when the dynamically loaded user function returns. |
||||||||||||||||
| QTAwk_assign |
prototype: Variable_ptr QTAwk_assign( Variable_ptr QTAwk_l_variable, Variable_ptr QTAwk_r_variable); QTAwk function to set the value of one QTAwk variable to another QTAwk variable. Assign the value of QTAwk_r_variable to QTAwk_l_variable. Equivalent to: QTAwk_l_variable =
QTAwk_r_variable;
|
||||||||||||||||
| QTAwk_dup |
prototype: Variable_ptr QTAwk_dup( Variable_ptr QTAwk_variable); QTAwk function to duplicate a QTAwk variable. The specified variable value is duplicated and returned. Note that the duplicate variable is NOT named even if the duplicated variable is named. An illustration of this function can be found in the module exe_builtin_mod.c |
||||||||||||||||
| QTAwk_execute_builtin_function |
prototype: Variable_ptr QTAwk_execute_builtin_function( unsigned function_number, unsigned argument_count, ...); QTAwk function to execute a specified built-in function. Returns a pointer to a QTAwk variable. The first argument is one of the defined function constants defined in the QTAwk header file. The second argument is the number of QTAwk variables following the count in the call which are passed to the desired builtin function. The arguments for the builtin function are then the remaining arguments in the call. All of these arguments are QTAwk variables. ALWAYS put a NULL argument at the end of the argument list to signal the end. DO NOT include the terminating NULL pointer in the argument count in the second argument. All built-in functions except 'sub', 'gsub' and 'gensub' may be called. An illustration of this function can be found in the module exe_builtin_mod.c |
||||||||||||||||
| QTAwk_true_false |
prototype: unsigned QTAwk_true_false( Variable_ptr QTAwk_variable); QTAwk function to determine and return the boolean value of the specified QTAwk variable. Returns:
|
||||||||||||||||
| QTAwk_index_var |
prototype: int QTAwk_index_var( Variable_ptr QTAwk_variable, unsigned *index_levels); QTAwk function to determine if variable indexed. Returns:
Assume the following QTAwk variables var1 is unindexed, i.e., a scalar var2 is indexed and has three levels of indices, e.g., var2[3][2][4] Then the following calls return the values indicated: QTAwk_index_var(var1,&index_lvl) == -1 index_lvl == 0 QTAwk_index_var(var2,&index_lvl) == 0 index_lvl == 3 QTAwk_index_var(var2[1],&index_lvl) == 1 index_lvl == 3 QTAwk_index_var(var2[1][2],&index_lvl) == 2 index_lvl == 3 QTAwk_index_var(var2[1][2][2],&index_lvl) == 3 index_lvl == 3 Using the functions 'QTAwk_get_named_variable' and 'QTAwk_get_array_element' and 'var3' is a variable declared in the module function: Variable_ptr var3; qta_long lcl_long = 3; var3 = QTAwk_get_local_variable_set("tvar",(void *)&lcl_long,LONG_VAL); QTAwk_index_var(var3,&index_lvl) == -1 index_lvl = 0 var3 = QTAwk_get_named_variable("var2"); QTAwk_index_var(var3,&index_lvl) == 0 index_lvl = 3 var3 = QTAwk_get_array_element(var3,"n",1); // retrieves var2[1] QTAwk_index_var(var3,&index_lvl) == 1 index_lvl = 3 var3 = QTAwk_get_array_element(var3,"nn",1,2); // retrieves var2[1][2] QTAwk_index_var(var3,&index_lvl) == 2 index_lvl = 3 var3 = QTAwk_get_array_element(var3,"nnn",1,2,2); // retrieves var2[1][2][2] QTAwk_index_var(var3,&index_lvl) == 3 index_lvl = 3 The second argument returns the number of index levels for the variable, e.g., if var2 above is a 3x3 matrix, then 3 is returned in all the above calls. NOTE: since QTAwk uses "sparse" arrays, the returned value in the second argument can vary with calls using diferent array elements within the same array. |
||||||||||||||||
| QTAwk_create_array_element |
prototype: unsigned QTAwk_create_array_element( Variable_ptr QTAwk_variable, void *value, unsigned set_type, unsigned char *index_str, ...); QTAwk function to create an array element. The indices are specified in the calling arguments. The arguments are:
QTAwk_create_array_element(var,val,type,"nsn",2,"State",35);
Returns 1, TRUE, if element successfully created, 0, FALSE, otherwise. An illustration of this function can be found in the module time_mod.c the QTAwk utility to execute is in time_mod.awk and the shell script to compile and link is in time_mod.sh. |
||||||||||||||||
| QTAwk_get_array_element |
prototype: Variable_ptr QTAwk_get_array_element( Variable_ptr QTAwk_variable, unsigned char *index_str, ...); QTAwk function to get an array element. The indices are specified in the calling arguments. The arguments are:
|
||||||||||||||||
| QTAwk_get_indices |
prototype: QTAwk_index_ptr QTAwk_get_indices( Variable_ptr QTAwk_variable, unsigned *index_count); QTAwk function to return array indices for variable specified Integer returned in second argument, index_count, spcifies the number of indices used to specify the array element passed, For example if the array has been created using: tmp_variable = varray[2]["State"][35] then: QTAwk_get_indices(tmp_variable,&index_lvl); then the return value is a pointer to an array of index structures with index_lvl set to 2. The return value is an array of index structures defined above. The memory has been allocated by a call to 'calloc' and the user function must free the memory when the structure array is no longer needed. An illustration of this function can be found in the module ArraySeq_mod.c |
||||||||||||||||
| QTAwk_first_array_element |
prototype: Variable_ptr QTAwk_first_array_element( Variable_ptr QTAwk_variable, unsigned *sequence_state, QTAwk_index_ptr Index); This function and the next two are QTAwk functions to sequence over array elements. They are similar to the statement: for ( i in array )
To execute call function:sequence_state = QTAwk_first_array_element(QTAwk_variable,&seqstate,Index)then call function: array_variable = QTAwk_next_array_element(sequence_state,Index)This is the QTAwk function to initialize the array element sequence function and return the first array element. Returns NULL if variable not indexed. Call with the variable at which to start the index sequencing. The module ArraySeq_mod.c illustrates the use of these functions. The script, ArraySeq_mod.sh, illustrates compiling and linking the module and the QTAwk utility, ArraySeq.awk, illustrates loading and executing the functions defined. The index sequencing will be at the next level of index, e.g., assuming 'ar' is a 3x3 matrix, calling with 'ar' would sequence through the elements ar[1], ar[2], ar[3] and calling with ar[1] would sequence through the elements ar[1][1] ar[1][2] ar[1][3]. If there is no next level of index, this function returns zero, 0. Thus, the above two call is similar to the statements: for ( i in ar ) for ( j in ar[i] ) print(ar[i][j]); This function will a QTAwk variable and an unsigned integer in the second argument. IMPORTANT: DO NOT change the value of this integer, This integer will be passed to the two sequencing functions below. |
||||||||||||||||
| QTAwk_next_array_element |
prototype: Variable_ptr QTAwk_next_array_element( unsigned sequence_state, QTAwk_index_ptr Index); QTAwk function to sequence through variable indices. Called with 2 arguments:
|
||||||||||||||||
| QTAwk_array_finish |
prototype: void QTAwk_array_finish( unsigned sequence_state); ALWAYS call this function when finished sequencing through an array. This is Especially necessary if the last call to 'QTAwk_next_array_element' in the sequencing has not returned a NULL value. NOTE: call after using obtaining the value of the last element returned. If called before obtaining the value by a call to 'QTAwk_get_value', then the value returned will be zero, 0, or the null string. |
||||||||||||||||
| QTAwk_array_delete |
prototype: void QTAwk_array_delete( Variable_ptr QTAwk_variable); QTAwk function to delete array element or all of array. The same as the QTAwk statement: delete QTAwk_variable;
|
||||||||||||||||
| QTAwk_get_field_var |
prototype: Variable_ptr QTAwk_get_field_var( int f_index); QTAwk function to retrieve field variable of current input record. IMPORTANT: DO NOT directly change any values of the variable. ALWAYS use the 'QTAwk_assign' or the 'QTAwk_set_value' functions to change the value. f_index specifies the field variable desired and corresponds to '$f_index' |
||||||||||||||||
| QTAwk_set_file |
prototype: unsigned char *QTAwk_set_file( unsigned char *fname, FILE *ofile, file_modes mode, unsigned error_flag, unsigned char *error_msg); Call this function to set/open a file for use in a QTAwk script. Called with 5 arguments:
This functions returns a string by whiich the file is known in QTAwk, namely the string value of the first argument. |
||||||||||||||||
| QTAwk_close_file |
prototype: int QTAwk_close_file( unsigned char *fname); Call this function to close an open file - normally used to close file opened by function 'QTAwk_set_file', but can close any file currently open for reading/writing by QTAwk. Note this function will not close pipes. Called with one argument, the string by which QTAwk knows the desired file. If called to close a file opened by 'QTAwk_set_file', this string is the return value of that function. |
||||||||||||||||
| QTAwk_get_file |
prototype: FILE *QTAwk_get_file( unsigned char *fname, file_modes *mode); Call this function to get a file known to the QTAwk script. Returns a pointer to the stream for the file or NULL if the ile is not known. 2 arguments:
|
| tmpfile_mod |
Files:
This adds a user defined function to create a temporary file. It uses the operating system call to create the filename. It defines the user defined function: tempfile(filepattern). The call to the system function creates the temporary file following the pattern specified, including any directory path specified in the pattern. NOTE: the temporary filename will be created by appending 6 unique characters to the end of the pattern specified. The file willbe opened and then closed to ensure that the name is available for use and cannot be used by any other process. The module will keep a record of any filenames created and the module shutdown function will automatically delete all temporary files created by calls to the user defined function. User defined functions:
The "exit" function, called when the module is unloaded, frees memory used by the function and removes any temporary files created. The user must ensure that any temporary files are no longer open and will not be use further. The "shutdown" function, called when QTAwk exits, is the same function as the "exit" function. |
||||||||||||||||||||||||||||||||||||||||
| secure_hash_module | Files:
User defined functions:
"exit" and "shutdown" functions are the same function and free memory used by the above functions. |
||||||||||||||||||||||||||||||||||||||||
| cpu.time_module | Files:
Module starts/stops the CPU timer and returns the elapsed time for the QTAwk process and returns the clock ticks for the QTAwk process. User defined functions:
No "exit" function. No "shutdown" function. |
||||||||||||||||||||||||||||||||||||||||
| strtoint_module | Files:
User defined functions:
The "strtoint.awk" QTAwkscript illustrates checking for conversion errors and printing the error string if an error occurs. Function returns the integer conversion value or an error string if Version function: strtoint_version() No "exit" function. No "shutdown" function. |
||||||||||||||||||||||||||||||||||||||||
| system.info_module | Files:
This module supplies system information: Operating system, machine, host name, etc. User defined functions:
No "shutdown" function. |
||||||||||||||||||||||||||||||||||||||||
| format_module |
Files:
User defined fnctions:
No "exit" function. No "shutdown" function. |