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.131 2003/04/07 17:28:54 nilsson Exp $ + || $Id: builtin.cmod,v 1.132 2003/04/10 17:08:16 nilsson Exp $   */      #include "global.h"   #include "interpret.h"   #include "svalue.h"   #include "opcodes.h"   #include "pike_macros.h"   #include "object.h"   #include "program.h"   #include "array.h"
pike.git/src/builtin.cmod:821:   PIKEFUN int get_weak_flag(multiset m)   {   #ifdef PIKE_NEW_MULTISETS    RETURN multiset_get_flags(m) & MULTISET_WEAK;   #else    RETURN (m->ind->flags & (ARRAY_WEAK_FLAG|ARRAY_WEAK_SHRINK)) ?    PIKE_WEAK_INDICES : 0;   #endif   }    + /*! @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;   {    if (line && line->type != T_INT)    SIMPLE_BAD_ARG_ERROR("__empty_program", 1, "int|void");    else if (file && file->type != T_STRING)    SIMPLE_BAD_ARG_ERROR("__empty_program", 2, "string|void");    else {    struct program *prog = low_allocate_program();
pike.git/src/builtin.cmod:1012:    break;       default:    SIMPLE_BAD_ARG_ERROR("function_program", 1, "function");    }    pop_n_elems(args);    push_int(0);   }       + /*! @decl mixed random(object o) +  *! If random is called with an object, @[lfun::random] will be +  *! called in the object. +  *! @seealso +  *! @[lfun::_random] +  */    - /*! @decl int random(int max) -  *! -  *! This function returns a random number in the range 0 - @[max]-1. -  *! + /*! @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 -  *! @[random_seed()] +  *! @[predef::random()]    */    -  +    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()] +  */ +    PIKEFUN int random(int i)   {    if(i <= 0) RETURN 0;    RETURN my_rand() % i;   }      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 xa) +  *! Returns a random element from @[x]. +  */ +    PIKEFUN mixed random(array a)   {    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();   }      PIKEFUN mixed random(multiset m)
pike.git/src/builtin.cmod:1078:    push_multiset_index (m, multiset_get_nth (m, my_rand() % multiset_sizeof (m)));    sub_msnode_ref (m);    }   #else    push_svalue(m->ind->item + (my_rand() % m->ind->size));   #endif    stack_swap();    pop_stack();   }    + /*! @decl array random(mapping m) +  *! Returns a random index-value pair from the mapping. +  */ +    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 */
pike.git/src/builtin.cmod:1167:    free_string(THIS->filename);    THIS->filename = NULL;    }    THIS->pc = NULL;    THIS->lineno = -1;    free_svalue(&THIS->fun);    THIS->fun.type = T_INT;    THIS->fun.u.integer = 0;    }    +  /*! @decl int(0..1) _is_type(string t) +  *! This object claims to be an array for backward compatibility. +  */    PIKEFUN int(0..1) _is_type(string t)    {    INT_TYPE res = (t == findstring("array"));    pop_n_elems(args);    push_int(res);    }       static void fill_in_file_and_line()    {    struct pike_string *file;
pike.git/src/builtin.cmod:1198:       if (!THIS->filename) THIS->filename = file;    else free_string (file);       if (THIS->prog) {    free_program(THIS->prog);    THIS->prog = NULL;    }    }    +  /*! @decl string _sprintf(int c, mapping|void opts) +  */    PIKEFUN string _sprintf(int c, mapping|void opts)    {    pop_n_elems(args);       if (c != 'O') {    push_undefined ();    return;    }       push_text("backtrace_frame(");
pike.git/src/builtin.cmod:1248:    push_text("Args: ");    push_int(THIS->args->size);    f_add(2);    } else {    push_text("No args");    }    push_text(")");    f_add(5);    }    -  PIKEFUN int _sizeof() +  /*! @decl int(3..) _sizeof() +  */ +  PIKEFUN int(3..) _sizeof()    {    if (THIS->args) {    push_int(THIS->args->size + 3);    } else {    push_int(3);    }    }    -  +  /*! @decl mixed `[](int index, int|void end_or_none) +  *! The BacktraceFrame object can be indexed as an array. +  */    PIKEFUN mixed `[](int index, int|void end_or_none)    {    INT_TYPE end = index;    INT32 numargs = 0;    INT32 i;       if (THIS->args) {    numargs = THIS->args->size;    }   
pike.git/src/builtin.cmod:1335:    }    /* NOT_REACHED */    break;    }    }    if (end_or_none) {    f_aggregate(1 + end - index);    }    }    +  /*! @decl mixed `[]=(int index, mixed value) +  */    PIKEFUN mixed `[]=(int index, mixed value)    {    INT32 numargs = 0;       if (THIS->args) {    numargs = THIS->args->size;    }       numargs += 3;   
pike.git/src/builtin.cmod:1600:    struct Buffer_struct *str = THIS;    if( args )    str->initial = MAXIMUM( size->u.integer, 512 );    else    {    str->initial = 256;    push_int(0);    }    }    +  /*! @decl string _sprintf( int flag, mapping flags ) +  *! It is possible to @[sprintf] a String.Buffer object +  *! as @tt{%s@} just as if it was a string. +  */    PIKEFUN string _sprintf( int flag, mapping flags )    {    switch( flag )    {    case 'O':    {    struct pike_string *res;    struct Buffer_struct *str = THIS;    push_text( "Buffer(%d /* %d */)" );    if( str->str.s )
pike.git/src/builtin.cmod:1644:    return;       case 't':    RETURN make_shared_binary_string("Buffer",6);    }    pop_n_elems( args );    push_int( 0 );    Pike_sp[-1].subtype = 1;    }    +  /*! @decl mixed cast( string type ) +  *! It is possible to cast a String.Buffer object to +  *! a @expr{string@} and an @expr{int@}. +  */    PIKEFUN mixed cast( string type )    {    struct pike_string *string_t;    struct pike_string *int_t;    MAKE_CONST_STRING( string_t, "string" );    MAKE_CONST_STRING( int_t, "int" );       if( type == string_t )    {    pop_n_elems( args );
pike.git/src/builtin.cmod:1675:    if( Pike_fp->current_object->refs != 1 )    f_Buffer_get_copy( 0 );    else    f_Buffer_get( 0 );    o_cast_to_int( );    return;    }    Pike_error("Cannot cast to %s\n", type->str );    }    +  /*! @decl String.Buffer `+( string what ) +  */    PIKEFUN object `+( string what )    {    struct Buffer_struct *str = THIS, *str2;    struct object *res = fast_clone_object( Buffer_program );    str2 = OBJ2_BUFFER( res );    str2->initial = str->initial;    if( str->str.s )    init_string_builder_copy (&str2->str, &str->str);    apply( res, "add", 1 );    RETURN res;    }    -  +  /*! @decl String.Buffer `+=( string what ) +  */    PIKEFUN object `+=( string what )    {    f_Buffer_add( 1 );    REF_RETURN Pike_fp->current_object;    }       /*! @decl int add(string ... data)    *!    *! Adds @[data] to the buffer. Returns the size of the buffer.    *!
pike.git/src/builtin.cmod:1834:    CVAR size_t v_sz;    CVAR size_t sz;    CVAR INT32 set_start[256];    CVAR INT32 set_end[256];       static int replace_sortfun(struct tupel *a,struct tupel *b)    {    return DO_NOT_WARN((int)my_quick_strcmp(a->ind, b->ind));    }    +  /*! @decl void create(array(string)|void from, array(string)|void to) +  */    PIKEFUN void create(array(string)|void from_, array(string)|void to_)    {    int i;    struct array *from;    struct array *to;    if (!args) {    push_int(0);    return;    }    if (!from_ || !to_) {
pike.git/src/builtin.cmod:1967:    }    else    {    a=c+1; /* There might still be a better match... */    match=c;    }    }    return match;    }    +  /*! @decl string `()(string str) +  */    PIKEFUN string `()(string str)    {    struct string_builder ret;    ptrdiff_t length = str->len;    ptrdiff_t s;    int *set_start = THIS->set_start;    int *set_end = THIS->set_end;    struct tupel *v = THIS->v;    int num = THIS->sz;   
pike.git/src/builtin.cmod:2026:    }    string_builder_putchar(&ret,    DO_NOT_WARN((INT32)ch));    s++;    length--;    }       RETURN finish_string_builder(&ret);    }    +  /*! @decl array(string) _encode() +  */    PIKEFUN array(string) _encode()    {    size_t i;    for (i=0; i < THIS->sz; i++) {    ref_push_string(THIS->v[i].ind);    }    f_aggregate(DO_NOT_WARN((INT32)THIS->sz));    for (i=0; i < THIS->sz; i++) {    ref_push_string(THIS->v[i].val);    }    f_aggregate(DO_NOT_WARN((INT32)THIS->sz));    f_aggregate(2);    }    -  +  /*! @decl void _decode(array(array(string)) encoded) +  */    PIKEFUN void _decode(array(array(string)) encoded)    {    INT32 i;       for (i=0; i < encoded->size; i++) {    push_svalue(encoded->item + i);    stack_swap();    }    pop_stack();   
pike.git/src/builtin.cmod:2115:    if (THIS->del) {    free_string(THIS->del);    THIS->del = NULL;    }    if (THIS->to) {    free_string(THIS->to);    THIS->to = NULL;    }    }    +  /*! @decl void create(string|void del, string|void to) +  */    PIKEFUN void create(string|void del_, string|void to_)    {    struct pike_string *del;    struct pike_string *to;       /* Clean up... */    exit_single_string_replace_struct();       if (!del_) return;   
pike.git/src/builtin.cmod:2144:    copy_shared_string(THIS->del, del = del_->u.string);    copy_shared_string(THIS->to, to = to_->u.string);       if (del->len) {    THIS->mojt = simple_compile_memsearcher(del);    }    }       /*** replace function ***/    typedef char *(* replace_searchfunc)(void *,void *,size_t); +  +  /*! @decl string `()(string str) +  */    PIKEFUN string `()(string str)    {    int shift;    struct pike_string *del = THIS->del;    struct pike_string *to = THIS->to;    struct pike_string *ret = NULL;       if (!str->len || !del || !to) {    /* The result is already on the stack in the correct place... */    return;
pike.git/src/builtin.cmod:2230:    INC_PCHARP(r,(tmp-s)>>str->size_shift);    pike_string_cpy(r,to);    INC_PCHARP(r,to->len);    s=tmp+(del->len << str->size_shift);    }    generic_memcpy(r,MKPCHARP(s,str->size_shift),(end-s)>>str->size_shift);    }    RETURN end_shared_string(ret);    }    +  /*! @decl array(string) _encode() +  */    PIKEFUN array(string) _encode()    {    if (THIS->del) {    ref_push_string(THIS->del);    ref_push_string(THIS->to);    f_aggregate(2);    } else {    push_int(0);    }    }    -  +  /*! @decl void _decode(array(string)|int(0..0) encoded) +  */    PIKEFUN void _decode(array(string)|int(0..0) encoded_)    {    INT32 i = 0;    if (encoded_->type == PIKE_T_ARRAY) {    struct array *encoded = encoded_->u.array;       for (i=0; i < encoded->size; i++) {    push_svalue(encoded->item + i);    stack_swap();    }