# -------------------------------------------------------------------------------
# DESCRIPTION: The function returns the lesser of two numbers passed to
# the function as parameters.
#
# PARAMETERS: in a and in b - two numbers to get the lesser of them
#
# RETURN CODE: Lesser of the two parameters passed to the function .
#
public function min(in a, in b)
{
if (a > b)
return(b);
else
return(a);
}
# -------------------------------------------------------------------------------
# DESCRIPTION: The function returns the greater of two numbers passed to
# the function as parameters.
#
# PARAMETERS: in a and in b - two numbers to get the greater of them
#
# RETURN CODE: Greater of the two parameters passed to the function .
#
public function max(in a, in b)
{
if (a > b)
return(a);
else
return(b);
}
# -------------------------------------------------------------------------------
# DESCRIPTION: This function performs an absolute value on a number.
#
# PARAMETERS: in number - a number.
#
# RETURN CODE: Returns the abs number.
#
public function abs(in number)
{
return number <0 ? -number : number;
}
# -------------------------------------------------------------------------------
# DESCRIPTION: The function returns a position of the last occurrence of the
# string s2 in the string s1.
#
# PARAMETERS: in s1 and in s2 - character strings.
#
# RETURN CODE: Position of the last occurrence of the string s2 in the string s1.
#
public function rindex(in s1, in s2)
{
auto ptr, lpos, src;
auto s2_len = length(s2);
# -------------------------------------------------------------------------------
# DESCRIPTION: The function removes leading spaces and tabs in the string s.
#
# PARAMETERS: in s - character string.
#
# RETURN CODE: A string that is the copy of s where leading spaces and tabs are
# removed .
#
public function ltrim(in s)
{
auto i;
auto src = s;
# -------------------------------------------------------------------------------
# DESCRIPTION: The function removes trailing spaces and tabs in the string s.
#
# PARAMETERS: in s - character string.
#
# RETURN CODE: A string that is the copy of s where trailing spaces and tabs are
# removed.
#
public function rtrim(in s)
{
auto i;
auto src = s;
# -------------------------------------------------------------------------------
# DESCRIPTION: The function replaces all occurrences of the substring sub1 in the
# string s by the substring sub2.
#
# PARAMETERS: in s, in sub1, in sub2 - character strings.
#
# RETURN CODE: A string that is the copy of s where all occurrences of the substring
# sub1 in the string s are replaced by the substring sub2.
#
public function repl_str(in s, in sub1, in sub2)
{
auto ptr;
auto src = s;
auto sub1_len = length(sub1);
# -------------------------------------------------------------------------------
# DESCRIPTION: The function returns the number of occurrences of the substring s2
# in the string s1.
#
# PARAMETERS: in s1, in s2 - character strings.
#
# RETURN CODE: A number of occurrences of the substring s2 in the string s1.
#
public function count_str(in s1, in s2)
{
auto src = s1;
auto s2_len = length(s2);
auto sub_cnt, ptr;
# -------------------------------------------------------------------------------
# DESCRIPTION: The function checks if c is a letter.
#
# PARAMETERS: in c - a character.
#
# RETURN CODE: 1 - if c is a letter, otherwise - 0.
#
public function isalpha(c)
{
auto ch = ascii(substr(c, 1, 1));
# -------------------------------------------------------------------------------
# DESCRIPTION: The function checks if c is a digit.
#
# PARAMETERS: in c - a character.
#
# RETURN CODE: 1 - if c is a digit, otherwise - 0.
#
public function isdigit(c)
{
auto ch = ascii(substr(c, 1, 1));
# -------------------------------------------------------------------------------
# DESCRIPTION: The function generates a random string (containing characters
# which ASCII is in the range 32 - 126).
#
# PARAMETERS: in slen - length of the string to generate,
# in max_wlen - maximal length of a word in the string (length of
# a word is random restricted from the top by
# max_wlen).
#
# RETURN CODE: The generated random string.
#
public function gen_str(in slen, in max_wlen)
{
auto rand_ch, wlen, word, i;
auto str = "";
# If max_wlen is not defined use the default.
#
if (nargs() < 2)
max_wlen = min(slen, 7);
srand();
# Generate random string.
#
while (length(str) < slen) {
# Generate random word.
#
word = "";
wlen = min(int(rand() * max_wlen) + 1, slen - length(str));
for (i = 0; i < wlen; i++) {
# Generate random character.
rand_ch = sprintf("%c", int(rand() * 95) + 32);
word = word & rand_ch;
}
# -------------------------------------------------------------------------------
# DESCRIPTION: The function generates a file consisting of a random string
# (containing characters which ASCII is in the range 32 - 126).
#
# PARAMETERS: in fname - the name of the file to generate,
# in slen - length of the string to generate,
# in scnt - number of lines in the file.
#
# RETURN CODE: None.
#
public function gen_file(in fname, in slen, in scnt)
{
auto i;
file_open(fname, FO_MODE_WRITE);
for (i = 1; i <= scnt; i++)
file_printf(fname,"%s\r\n", gen_str(slen));
file_close(fname);
}
# -------------------------------------------------------------------------------
# DESCRIPTION: The function returns all but the last level of the pathname in fp.
#
# PARAMETERS: in fp - the pathname to proceed.
#
# RETURN CODE: Pathname without the last level.
#
public function dirname(in fp)
{
auto file_name, i, d, dirs[], st;
if (!index(file_name, FILE_SEP)) # If file_name is only name of a file.
if( file_name = "." )
return "..\\.";
else
return "";
do
file_name = substr(file_name,1,length(file_name)-1);
while (substr(file_name,length(file_name)) != FILE_SEP);
if (file_name == "." )
file_name = "..\\.";
return file_name;
}
# -------------------------------------------------------------------------------
# DESCRIPTION: The function generates a random temporary file name.
#
# PARAMETERS:
#
# RETURN CODE: Returns random file name.
#
public function f_tmp_name() {
auto c_time;