pike.git / src / builtin.cmod

version» Context lines:

pike.git/src/builtin.cmod:1:   /* -*- c -*-   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: builtin.cmod,v 1.189 2006/04/22 15:27:39 grubba Exp $ + || $Id: builtin.cmod,v 1.190 2006/07/05 19:21:29 mast Exp $   */      #include "global.h"   #include "interpret.h"   #include "svalue.h"   #include "pike_macros.h"   #include "object.h"   #include "program.h"   #include "array.h"   #include "pike_error.h"
pike.git/src/builtin.cmod:61:    *! Depth in the inherit tree for the inherit containing    *! the symbol.    *! @endarray    *!    *! @note    *! The API for this function is not fixed, and has changed    *! since Pike 7.6. In particular it would make sense to return    *! an array of objects instead, and more information about the    *! symbols might be added.    */ + PMOD_EXPORT   PIKEFUN array(array(int|string)) _describe_program(mixed x)    efun;   {    struct program *p;    struct array *res;    int i;       if (!(p = program_from_svalue(Pike_sp - args)))    SIMPLE_BAD_ARG_ERROR("_describe_program", 1, "program");   
pike.git/src/builtin.cmod:103:    push_array(res);   }      /*! @decl string basetype(mixed x)    *!    *! Same as sprintf("%t",x);    *!    *! @seealso    *! @[sprintf()]    */ + PMOD_EXPORT   PIKEFUN string basetype(mixed x)    efun;    optflags OPT_TRY_OPTIMIZE;   {    int t=x->type;    struct program *p;    if(x->type == T_OBJECT && (p = x->u.object->prog))    {    ptrdiff_t fun = FIND_LFUN(p->inherits[x->subtype].prog, LFUN__SPRINTF);    if(fun != -1)
pike.git/src/builtin.cmod:166:         /*! @decl string int2char(int x)    *! @appears String.int2char    *!    *! Same as sprintf("%c",x);    *!    *! @seealso    *! @[sprintf()]    */ + PMOD_EXPORT   PIKEFUN string int2char(int|object x)    efun;    optflags OPT_TRY_OPTIMIZE;   {    int c;    struct program *p;    if(x->type == T_OBJECT && (p = x->u.object->prog))    {    ptrdiff_t fun = FIND_LFUN(p->inherits[x->subtype].prog, LFUN__SPRINTF);    if(fun != -1)
pike.git/src/builtin.cmod:218:      /*! @decl string int2hex(int x)    *! @appears String.int2hex    *!    *! Same as @expr{sprintf("%x",x);@}, i.e. returns the integer @[x] in    *! hexadecimal base using lower cased symbols.    *!    *! @seealso    *! @[sprintf()]    */ + PMOD_EXPORT   PIKEFUN string int2hex(int|object x)    efun;    optflags OPT_TRY_OPTIMIZE;   {    INT_TYPE c;    unsigned INT_TYPE n;    int len;    struct pike_string *s;    struct program *p;   
pike.git/src/builtin.cmod:296:   }      /*! @decl string string2hex(string data)    *! @appears String.string2hex    *!    *! Convert a string of binary data to a hexadecimal string.    *!    *! @seealso    *! @[hex2string()]    */ + PMOD_EXPORT   PIKEFUN string string2hex(string s)    errname String.string2hex;    optflags OPT_TRY_OPTIMIZE;   {    struct pike_string *hex;    unsigned char *st = s->str;    int i;       if (s->size_shift)    Pike_error("Bad argument 1 to string2hex(), expected 8-bit string.\n");
pike.git/src/builtin.cmod:325:   }      /*! @decl string hex2string(string hex)    *! @appears String.hex2string    *!    *! Convert a string of hexadecimal digits to binary data.    *!    *! @seealso    *! @[string2hex()]    */ + PMOD_EXPORT   PIKEFUN string hex2string(string hex)    errname String.hex2string;    optflags OPT_TRY_OPTIMIZE;   {    struct pike_string *s;    int i, o=0;    unsigned char *q = hex->str;    int l = hex->len>>1;    if(hex->size_shift) Pike_error("Only hex digits allowed.\n");    if(hex->len&1) Pike_error("Can't have odd number of digits.\n");
pike.git/src/builtin.cmod:361:    *! map(@[data], lambda(mixed x,mixed y) { return x[y]; }, @[index])    *! @endcode    *!    *! Except of course it is a lot shorter and faster.    *! That is, it indices every index in the array data on the value of    *! the argument index and returns an array with the results.    *!    *! @seealso    *! @[rows()]    */ + PMOD_EXPORT   PIKEFUN array column(array data, mixed index)    efun;    optflags OPT_TRY_OPTIMIZE;   {    RETURN array_column (data, index, 1);   }      /*! @decl multiset mkmultiset(array a)    *!    *! This function creates a multiset from an array.    *!    *! @seealso    *! @[aggregate_multiset()]    *!    */ -  + PMOD_EXPORT   PIKEFUN multiset(1) mkmultiset(array(1=mixed) a)    efun;    optflags OPT_TRY_OPTIMIZE|OPT_EXTERNAL_DEPEND;   {    RETURN mkmultiset(a);   }      /*! @decl int trace(int level, void|string facility, void|int all_threads)    *!    *! This function changes the trace level for the subsystem identified
pike.git/src/builtin.cmod:428:    *! @endstring    *!    *! @param all_threads    *! Trace levels are normally thread local, so changes affect only    *! the current thread. To change the level in all threads, pass a    *! nonzero value in this argument.    *!    *! @returns    *! The old trace level in the current thread is returned.    */ + PMOD_EXPORT   PIKEFUN int trace(int level, void|string facility, void|int all_threads)    efun;    optflags OPT_SIDE_EFFECT;   {    INT32 old_level;    if (facility) {    struct pike_string *gc_str;    MAKE_CONST_STRING(gc_str, "gc");    if (facility == gc_str) {    old_level = gc_trace;
pike.git/src/builtin.cmod:502:    *! @member float "average_slowness"    *! When predicting the next gc interval, use a decaying average    *! with this slowness factor. It should be a value between 0.0 and    *! 1.0 that specifies the weight to give to the old average value.    *! The remaining weight up to 1.0 is given to the last reading.    *! @endmapping    *!    *! @seealso    *! @[gc], @[Debug.gc_status]    */ + PMOD_EXPORT   PIKEFUN mapping(string:mixed) gc_parameters (void|mapping(string:mixed) params)    errname Pike.gc_parameters;    optflags OPT_SIDE_EFFECT;   {    struct pike_string *str;    struct svalue *set;    struct svalue get;       if (!params) {    push_mapping (allocate_mapping (5));
pike.git/src/builtin.cmod:579:    *! string containing the current year, month, day and time.    *!    *! Like @[localtime], this function might throw an error if the    *! ctime(2) call failed on the system. It's platform dependent what    *! time ranges that function can handle, e.g. Windows doesn't handle    *! a negative @[timestamp].    *!    *! @seealso    *! @[time()], @[localtime()], @[mktime()], @[gmtime()]    */ + PMOD_EXPORT   PIKEFUN string ctime(int timestamp)    efun;    optflags OPT_TRY_OPTIMIZE;   {    time_t i=(time_t)timestamp;    char *s = ctime (&i);    if (!s) Pike_error ("ctime() on this system cannot handle "    "the timestamp %ld.\n", (long) i);    RETURN make_shared_string(s);   }
pike.git/src/builtin.cmod:603:    *!    *! Makes a mapping @[ind[x]]:@[val[x]], @tt{0 <= x < sizeof(ind)@}.    *!    *! @[ind] and @[val] must have the same size.    *!    *! This is the inverse operation of @[indices()] and @[values()].    *!    *! @seealso    *! @[indices()], @[values()]    */ + PMOD_EXPORT   PIKEFUN mapping(1:2) mkmapping(array(1=mixed) ind, array(2=mixed) val)    efun;    optflags OPT_TRY_OPTIMIZE|OPT_EXTERNAL_DEPEND;   {    if(ind->size != val->size)    bad_arg_error("mkmapping", Pike_sp-args, args, 2, "array", Pike_sp+1-args,    "mkmapping called on arrays of different sizes (%d != %d)\n",    ind->size, val->size);       RETURN mkmapping(ind, val);
pike.git/src/builtin.cmod:627:    *!    *! Count the number of non-overlapping times the string @[needle]    *! occurs in the string @[haystack]. The special cases for the needle    *! @expr{""@} is that it occurs one time in the empty string, zero    *! times in a one character string and between every character    *! (length-1) in any other string.    *!    *! @seealso    *! @[search()], @[`/()]    */ + PMOD_EXPORT   PIKEFUN int string_count(string haystack, string needle)    errname String.count;    optflags OPT_TRY_OPTIMIZE;   {    ptrdiff_t c = 0;    ptrdiff_t i, j;       switch (needle->len)    {    case 0:
pike.git/src/builtin.cmod:665:    break;    }    RETURN DO_NOT_WARN((INT_TYPE)c);   }      /*! @decl string trim_whites (string s)    *! @belongs String    *!    *! Trim leading and trailing spaces and tabs from the string @[s].    */ + PMOD_EXPORT   PIKEFUN string string_trim_whites (string s)    errname String.trim_whites;    optflags OPT_TRY_OPTIMIZE;   {    ptrdiff_t start = 0, end = s->len;    int chr;    switch (s->size_shift) {   #define DO_IT(TYPE) \    { \    for (; start < s->len; start++) { \
pike.git/src/builtin.cmod:697:    }    RETURN string_slice (s, start, end + 1 - start);   }      /*! @decl string trim_all_whites (string s)    *! @belongs String    *!    *! Trim leading and trailing white spaces characters (space, tab,    *! newline and carriage return) from the string @[s].    */ + PMOD_EXPORT   PIKEFUN string string_trim_all_whites (string s)    errname String.trim_all_whites;    optflags OPT_TRY_OPTIMIZE;   {    ptrdiff_t start = 0, end = s->len;    int chr;    switch (s->size_shift) {   #define DO_IT(TYPE) \    { \    for (; start < s->len; start++) { \
pike.git/src/builtin.cmod:730:   #undef DO_IT    }    RETURN string_slice (s, start, end + 1 - start);   }      /*! @decl int implements(program prog, program api)    *! @belongs Program    *!    *! Returns 1 if @[prog] implements @[api].    */ + PMOD_EXPORT   PIKEFUN int program_implements(program prog, program api)    errname Program.implements;    optflags OPT_TRY_OPTIMIZE;   {    RETURN implements(prog, api);   }      /*! @decl int inherits(program child, program parent)    *! @belongs Program    *!    *! Returns 1 if @[child] has inherited @[parent].    */ -  + PMOD_EXPORT   PIKEFUN int program_inherits(program parent, program child)    errname Program.inherits;    optflags OPT_TRY_OPTIMIZE;   {    RETURN low_get_storage(parent, child) != -1;   }      /*! @decl string defined(program p)    *! @belongs Program    *!    *! Returns a string with filename and linenumber describing where    *! the program @[p] was defined.    *!    *! The returned string is of the format @expr{"filename:linenumber"@}.    *!    *! If it cannot be determined where the program was defined, @expr{0@}    *! (zero) will be returned.    */ -  + PMOD_EXPORT   PIKEFUN string program_defined(program p)    errname Program.defined;    optflags OPT_TRY_OPTIMIZE;   {    INT32 line;    struct pike_string *tmp = low_get_program_line(p, &line);       pop_n_elems(args);       if (tmp) {
pike.git/src/builtin.cmod:797:    *! Three return values are possible:    *! @int    *! @value 8    *! The string @[s] only contains characters <= 255.    *! @value 16    *! The string @[s] only contains characters <= 65535.    *! @value 32    *! The string @[s] contains characters >= 65536.    *! @endint    */ + PMOD_EXPORT   PIKEFUN int(8 .. 8)|int(16 .. 16)|int(32 .. 32) string_width(string s)    errname String.width;    optflags OPT_TRY_OPTIMIZE;   {    RETURN 8 * (1 << s->size_shift);   }      /*! @decl mixed m_delete(object|mapping map, mixed index)    *!    *! If @[map] is an object that implements @[lfun::_m_delete()],
pike.git/src/builtin.cmod:823:    *!    *! @returns    *! The value that was removed will be returned.    *!    *! @note    *! Note that @[m_delete()] changes @[map] destructively.    *!    *! @seealso    *! @[mappingp()]    */ + PMOD_EXPORT   PIKEFUN mixed m_delete(object|mapping map, mixed index)    efun;    optflags OPT_SIDE_EFFECT;    rawtype tOr(tFunc(tMap(tSetvar(0,tMix),tSetvar(1,tMix)) tVar(0),tVar(1)),tFunc(tObj tMix,tMix))   {    struct program *p;    if( map->type == T_MAPPING )    {    struct svalue s;    map_delete_no_free(map->u.mapping, index, &s);
pike.git/src/builtin.cmod:859:    } else {    SIMPLE_BAD_ARG_ERROR("m_delete", 1, "object|mapping");    }   }      /*! @decl int get_weak_flag(array|mapping|multiset m)    *!    *! Returns the weak flag settings for @[m]. It's a combination of    *! @[Pike.WEAK_INDICES] and @[Pike.WEAK_VALUES].    */ + PMOD_EXPORT   PIKEFUN int get_weak_flag(array m)    efun;    optflags OPT_EXTERNAL_DEPEND;   {    RETURN (m->flags & ARRAY_WEAK_FLAG) ? PIKE_WEAK_VALUES : 0;   }    -  + PMOD_EXPORT   PIKEFUN int get_weak_flag(mapping m)   {    RETURN mapping_get_flags(m) & MAPPING_WEAK;   }    -  + PMOD_EXPORT   PIKEFUN int get_weak_flag(multiset m)   {    RETURN multiset_get_flags(m) & MULTISET_WEAK;   }      /*! @decl program __empty_program(int|void line, string|void file)    */   PIKEFUN program __empty_program(int|void line, string|void file)    efun;    optflags OPT_EXTERNAL_DEPEND;
pike.git/src/builtin.cmod:901:   /*! @decl string function_name(function f)    *!    *! Return the name of the function @[f].    *!    *! If @[f] is a global function defined in the runtime @expr{0@}    *! (zero) will be returned.    *!    *! @seealso    *! @[function_object()]    */ + PMOD_EXPORT   PIKEFUN string function_name(program|function func)    efun;    optflags OPT_TRY_OPTIMIZE;   {    switch(func->type)    {    default:    SIMPLE_BAD_ARG_ERROR("function_name", 1, "function|program");    return; /* NOTREACHED */   
pike.git/src/builtin.cmod:984:    *! If @[f] is a global function defined in the runtime @expr{0@}    *! (zero) will be returned.    *!    *! Zero will also be returned if @[f] is a constant in the    *! parent class. In that case @[function_program()] can be    *! used to get the parent program.    *!    *! @seealso    *! @[function_name()], @[function_program()]    */ + PMOD_EXPORT   PIKEFUN object function_object(function|program func)    efun;    optflags OPT_TRY_OPTIMIZE;    type function(function:object);   {    switch(func->type)    {    case PIKE_T_PROGRAM:    break;   
pike.git/src/builtin.cmod:1027:   /*! @decl program function_program(function|program f)    *!    *! Return the program the function @[f] is in.    *!    *! If @[f] is a global function defined in the runtime @expr{0@}    *! (zero) will be returned.    *!    *! @seealso    *! @[function_name()], @[function_object()]    */ + PMOD_EXPORT   PIKEFUN program function_program(program|function func)    efun;    optflags OPT_TRY_OPTIMIZE;   {    switch(func->type)    {    case PIKE_T_PROGRAM:    {    struct program *p;    if(!(p=func->u.program->parent)) break;
pike.git/src/builtin.cmod:1086:    */      /*! @decl mixed lfun::_random()    *! Called by @[random]. Typical uses is when the object implements    *! a ADT, then a call to this lfun should return a random member of    *! the ADT or range implied by the ADT.    *! @seealso    *! @[predef::random()]    */    + PMOD_EXPORT   PIKEFUN mixed random(object o)    efun;    optflags OPT_TRY_OPTIMIZE|OPT_EXTERNAL_DEPEND;   {    apply(o,"_random",0);    stack_swap();    pop_stack();   }      /*! @decl int random(int max)    *! @decl float random(float max)    *!    *! This function returns a random number in the range 0 - @[max]-1.    *!    *! @seealso    *! @[random_seed()]    */    -  + PMOD_EXPORT   PIKEFUN int random(int i)   {    if(i <= 0) RETURN 0;    RETURN my_rand() % i;   }    -  + PMOD_EXPORT   PIKEFUN float random(float f)   {    if(f<=0.0) RETURN 0.0;   #define N 1048576    RETURN f * (my_rand()%N/((float)N)) +    f * (my_rand()%N/( ((float)N) * ((float)N) ));      }      /*! @decl mixed random(array|multiset x)    *! Returns a random element from @[x].    */    -  + PMOD_EXPORT   PIKEFUN mixed random(array a)    rawtype tFunc(tArr(tSetvar(0,tMix)),tVar(0));   {    if(!a->size)    SIMPLE_BAD_ARG_ERROR("random", 1, "array with elements in it");    push_svalue(a->item + (my_rand() % a->size));    stack_swap();    pop_stack();   }    -  + PMOD_EXPORT   PIKEFUN mixed random(multiset m)    rawtype tFunc(tSet(tSetvar(1,tMix)),tVar(1));   {    if(multiset_is_empty (m))    SIMPLE_BAD_ARG_ERROR("random", 1, "multiset with elements in it");    if (multiset_indval (m)) {    ptrdiff_t nodepos = multiset_get_nth (m, my_rand() % multiset_sizeof (m));    push_multiset_index (m, nodepos);    push_multiset_value (m, nodepos);    sub_msnode_ref (m);
pike.git/src/builtin.cmod:1158:    sub_msnode_ref (m);    }    stack_swap();    pop_stack();   }      /*! @decl array random(mapping m)    *! Returns a random index-value pair from the mapping.    */    + PMOD_EXPORT   PIKEFUN array random(mapping m)   {    struct mapping_data *md=m->data;    size_t bucket, count;    struct keypair *k;       if(!m_sizeof(m))    SIMPLE_BAD_ARG_ERROR("random", 1, "mapping with elements in it");       /* Find a random, nonempty bucket */