2005-07-11 04:23:57 +04:00
/ *
base js library functions
Copyright Andrew Tridgell 2005
released under the GNU GPL v2 or later
* /
if ( global [ "HAVE_BASE_JS" ] != undefined ) {
return ;
}
HAVE _BASE _JS = 1
2005-07-20 11:04:07 +04:00
/* bring the string functions into the global frame */
string _init ( global ) ;
2005-07-12 10:57:25 +04:00
/ *
an essential function !
* /
function printf ( )
{
print ( vsprintf ( arguments ) ) ;
}
2005-07-11 04:23:57 +04:00
/ *
helper function to setup a rpc io object , ready for input
* /
function irpcObj ( )
{
var o = new Object ( ) ;
o . input = new Object ( ) ;
return o ;
}
/ *
check that a status result is OK
* /
function check _status _ok ( status )
{
if ( status . is _ok != true ) {
printVars ( status ) ;
}
assert ( status . is _ok == true ) ;
}
/ *
check that two arrays are equal
* /
function check _array _equal ( a1 , a2 )
{
assert ( a1 . length == a2 . length ) ;
for ( i = 0 ; i < a1 . length ; i ++ ) {
assert ( a1 [ i ] == a2 [ i ] ) ;
}
}
/ *
check that an array is all zeros
* /
function check _array _zero ( a )
{
for ( i = 0 ; i < a . length ; i ++ ) {
assert ( a [ i ] == 0 ) ;
}
}
2005-07-12 06:36:07 +04:00
/ *
substitute strings of the form $ { NAME } in str , replacing
with substitutions from subobj
* /
function substitute _var ( str , subobj )
{
var list = split ( "${" , str ) ;
var i ;
for ( i = 1 ; i < list . length ; i ++ ) {
var list2 = split ( "}" , list [ i ] ) ;
if ( list2 . length < 2 ) {
return undefined ;
}
var key = list2 [ 0 ] ;
var val ;
if ( typeof ( subobj [ key ] ) == "undefined" ) {
val = "${" + key + "}" ;
} else if ( typeof ( subobj [ key ] ) == "string" ) {
val = subobj [ key ] ;
} else {
var fn = subobj [ key ] ;
val = fn ( key ) ;
}
list2 [ 0 ] = "" + val ;
list [ i ] = join ( "" , list2 ) ;
}
return join ( "" , list ) ;
}
2005-07-19 13:30:53 +04:00
/ *
return "s" if a number should be shown as plural
* /
function plural ( n )
{
if ( n == 1 ) {
return "" ;
}
return "s" ;
}