|
#########################################################
#
# This compiled module contains miscellanous auxiliary functions
#
# LIST of FUNCTIONS:
#
# General:
#
# min, max
#
# String functions:
#
# abs
# rindex
# ltrim, rtrim
# repl
# count_str
# isalpha, isdigit
# gen_str
#
# File Operations:
#
# gen_file
# dirname
# f_tmp_name
#
#########################################################
# -------------------------------------------------------------------------------
# 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);
src = s1;
lpos = 0;
while ((ptr = index(src, s2)) != 0) {
lpos += ptr + (s2_len - 1);
src = substr(src, ptr + s2_len);
}
if (lpos > 0)
lpos -= s2_len - 1;
return(lpos);
}
# -------------------------------------------------------------------------------
# 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;
while ((index(src, " ") == 1) || (index(src, "\t") == 1))
src = substr(src, 2);
return(src);
}
# -------------------------------------------------------------------------------
# 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;
while ((rindex(src, " ") == length(src)) || (rindex(src, "\t") == length(src))) {
src = substr(src, 1, length(src) - 1);
if (length(src) == 0)
break;
}
return(src);
}
# -------------------------------------------------------------------------------
# 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);
while (ptr = index(src, sub1))
src = substr(src, 1, ptr - 1) & sub2 & substr(src, ptr + sub1_len);
return(src);
}
# -------------------------------------------------------------------------------
# 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;
for (sub_cnt = 0; ptr = index(src, s2); sub_cnt++)
src = substr(src, ptr + s2_len);
return(sub_cnt);
}
# -------------------------------------------------------------------------------
# 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));
if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
return(1);
else
return(0);
}
# -------------------------------------------------------------------------------
# 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));
if (ch >= 48 && ch <= 57)
return(1);
else
return(0);
}
# -------------------------------------------------------------------------------
# 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;
}
str = str & word;
if (length(str) < slen)
str = str & " ";
}
return(str);
}
######## File Operations #####################
#
#
# -------------------------------------------------------------------------------
# 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( nargs() < 1)
call_chain_get_attr("testname", 1, file_name);
else
file_name = fp;
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;
c_time = get_time();
return getenv("TMP") & "\\tst" & substr(c_time, length(c_time) - 4);
} |
|