c3da7f | 2000-07-04 | Martin Stjernholm | | /* -*- c -*- */
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | #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"
|
bb8a78 | 2000-12-01 | Fredrik Hübinette (Hubbe) | | #include "pike_error.h"
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | #include "constants.h"
#include "mapping.h"
#include "stralloc.h"
#include "multiset.h"
#include "pike_types.h"
#include "pike_memory.h"
#include "threads.h"
#include <math.h>
#include <ctype.h>
#include "module_support.h"
#include "cyclic.h"
#include "bignum.h"
|
098c80 | 2000-05-24 | Fredrik Hübinette (Hubbe) | | #include "main.h"
|
b8c5b2 | 2000-05-25 | Fredrik Hübinette (Hubbe) | | #include "operators.h"
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | |
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl array column(array data, mixed index)
*!
*! Extract a column from a two-dimensional array.
*!
*! This function is exactly equivalent to:
*! @code{map(@[data], lambda(mixed x,mixed y) { return x[y]; }, @[index])@}
*!
*! 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()]
*/
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | PIKEFUN array column(array data, mixed index)
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | efun;
optflags OPT_TRY_OPTIMIZE;
{
INT32 e;
struct array *a;
DECLARE_CYCLIC();
/* Optimization */
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | if(data->refs == 1)
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | {
/* An array with one ref cannot possibly be cyclic */
struct svalue sval;
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | data->type_field = BIT_MIXED | BIT_UNFINISHED;
for(e=0;e<data->size;e++)
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | {
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | index_no_free(&sval, ITEM(data)+e, index);
free_svalue(ITEM(data)+e);
ITEM(data)[e]=sval;
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | }
pop_stack();
return;
}
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | if((a=(struct array *)BEGIN_CYCLIC(data,0)))
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | {
add_ref(a);
}else{
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | push_array(a=allocate_array(data->size));
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | SET_CYCLIC_RET(a);
for(e=0;e<a->size;e++)
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | index_no_free(ITEM(a)+e, ITEM(data)+e, index);
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | |
sp--;
}
END_CYCLIC();
RETURN a;
}
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl multiset mkmultiset(array a)
*!
*! This function creates a multiset from an array.
*!
*! @seealso
*! @[aggregate_multiset()]
*!
*/
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | PIKEFUN multiset(1) mkmultiset(array(1=mixed) a)
efun;
|
8f998d | 2000-08-31 | Henrik Grubbström (Grubba) | | optflags OPT_TRY_OPTIMIZE|OPT_EXTERNAL_DEPEND;
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | {
RETURN mkmultiset(a);
}
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl int trace(int t)
*!
*! This function changes the debug trace level.
*!
*! The old level is returned.
*!
*! Trace level 1 or higher means that calls to Pike functions are
*! printed to stderr, level 2 or higher means calls to builtin functions
*! are printed, 3 means every opcode interpreted is printed, 4 means
*! arguments to these opcodes are printed as well.
*!
*! See the @tt{-t@} command-line option for more information.
*/
|
098c80 | 2000-05-24 | Fredrik Hübinette (Hubbe) | | PIKEFUN int trace(int t)
efun;
optflags OPT_SIDE_EFFECT;
{
pop_n_elems(args);
push_int(t_flag);
t_flag=t;
}
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*!
*! Convert the output from a previous call to @[time()] into a readable
*! string containing the current year, month, day and time.
*!
*! @seealso
*! @[time()], @[localtime()], @[mktime()], @[gmtime()]
*/
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | PIKEFUN string ctime(int timestamp)
|
098c80 | 2000-05-24 | Fredrik Hübinette (Hubbe) | | efun;
optflags OPT_TRY_OPTIMIZE;
{
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | time_t i=(time_t)timestamp;
|
098c80 | 2000-05-24 | Fredrik Hübinette (Hubbe) | | RETURN make_shared_string(ctime(&i));
}
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl mapping mkmapping(array ind, array val)
*!
*! Make a mapping from two arrays.
*!
*! 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()]
*/
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | PIKEFUN mapping(1:2) mkmapping(array(1=mixed) ind, array(2=mixed) val)
|
098c80 | 2000-05-24 | Fredrik Hübinette (Hubbe) | | efun;
|
8f998d | 2000-08-31 | Henrik Grubbström (Grubba) | | optflags OPT_TRY_OPTIMIZE|OPT_EXTERNAL_DEPEND;
|
098c80 | 2000-05-24 | Fredrik Hübinette (Hubbe) | | {
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | if(ind->size != val->size)
|
098c80 | 2000-05-24 | Fredrik Hübinette (Hubbe) | | bad_arg_error("mkmapping", sp-args, args, 2, "array", sp+1-args,
"mkmapping called on arrays of different sizes (%d != %d)\n",
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | ind->size, val->size);
|
098c80 | 2000-05-24 | Fredrik Hübinette (Hubbe) | |
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | RETURN mkmapping(ind, val);
|
098c80 | 2000-05-24 | Fredrik Hübinette (Hubbe) | | }
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | |
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl int string_count(string haystack, string needle)
*!
*! Count the number of non-overlapping times the string @[needle] occurrs
*! in the string @[haystack].
*!
*! @seealso
*! @[search()], @[`/()]
*/
|
661305 | 2000-08-10 | Henrik Grubbström (Grubba) | | PIKEFUN int string_count(string haystack, string needle)
|
991fdf | 2000-05-25 | Fredrik Hübinette (Hubbe) | | errname String.count;
optflags OPT_TRY_OPTIMIZE;
{
|
89fc4c | 2000-08-10 | Henrik Grubbström (Grubba) | | ptrdiff_t c = 0;
ptrdiff_t i, j;
|
991fdf | 2000-05-25 | Fredrik Hübinette (Hubbe) | |
switch (needle->len)
{
case 0:
switch (haystack->len)
{
case 0: c=1; break; /* "" appears one time in "" */
case 1: c=0; break; /* "" doesn't appear in "x" */
default: c=haystack->len-1; /* one time between each character */
}
break;
case 1:
/* maybe optimize? */
default:
for (i=0; i<haystack->len; i++)
{
j=string_search(haystack,needle,i);
if (j==-1) break;
i=j+needle->len-1;
c++;
}
break;
}
|
661305 | 2000-08-10 | Henrik Grubbström (Grubba) | | RETURN DO_NOT_WARN((INT_TYPE)c);
|
991fdf | 2000-05-25 | Fredrik Hübinette (Hubbe) | | }
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl int program_implements(program prog, program api)
*!
*! Returns 1 if @[prog] implements @[api].
*/
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | PIKEFUN int program_implements(program prog, program api)
|
991fdf | 2000-05-25 | Fredrik Hübinette (Hubbe) | | errname Program.implements;
optflags OPT_TRY_OPTIMIZE;
{
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | RETURN implements(prog, api);
|
991fdf | 2000-05-25 | Fredrik Hübinette (Hubbe) | | }
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl int program_inherits(program child, program parent)
*!
*! Returns 1 if @[child] has inherited @[parent].
*/
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | PIKEFUN int program_inherits(program child, program parent)
|
991fdf | 2000-05-25 | Fredrik Hübinette (Hubbe) | | errname Program.inherits;
optflags OPT_TRY_OPTIMIZE;
{
|
404e63 | 2001-01-08 | Henrik Grubbström (Grubba) | | RETURN !!low_get_storage(parent, child);
|
991fdf | 2000-05-25 | Fredrik Hübinette (Hubbe) | | }
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl string program defined(program p)
*!
*! Returns a string with filename and linenumber describing where
*! the program @[p] was defined.
*!
*! The returned string is of the format @tt{"@i{filename@}:@i{linenumber@}"@}.
*!
*! If it cannot be determined where the program was defined, @tt{0@} (zero)
*! will be returned.
*/
|
b8c5b2 | 2000-05-25 | Fredrik Hübinette (Hubbe) | | PIKEFUN string program_defined(program p)
errname Program.defined;
optflags OPT_TRY_OPTIMIZE;
{
if(p && p->num_linenumbers)
{
char *tmp;
INT32 line;
if((tmp=get_line(p->program, p, &line)))
{
struct pike_string *tmp2;
tmp2=make_shared_string(tmp);
pop_n_elems(args);
push_string(tmp2);
if(line > 1)
{
push_constant_text(":");
push_int(line);
f_add(3);
}
return;
}
}
pop_n_elems(args);
push_int(0);
}
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl int(8..8)|int(16..16)|int(32..32) string_width(string s)
*!
*! Returns the width of a string.
*!
*! 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
*/
PIKEFUN int(8..8)|int(16..16)|int(32..32) string_width(string s)
|
991fdf | 2000-05-25 | Fredrik Hübinette (Hubbe) | | errname String.width;
optflags OPT_TRY_OPTIMIZE;
{
RETURN 8 * (1 << s->size_shift);
}
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl mixed m_delete(object|mapping map, mixed index)
*!
*! If @[map] is an object that implements @[lfun::_m_delete()],
*! that function will be called with @[index] as the signle argument.
*!
*! Other wise if @[map] is a mapping the entry with index @[index]
*! will be removed from @[map] destructively.
*!
*! If the mapping does not have an entry with index @[index], nothing is done.
*!
*! @returns
*! The value that was removed will be returned.
*!
*! @note
*! Note that @[m_delete()] changes @[map] destructively.
*!
*! @seealso
*! @[mappingp()]
*/
|
a3453e | 2001-02-05 | Per Hedbor | | PIKEFUN mixed m_delete(object|mapping map, mixed index)
|
7f80d4 | 2000-06-19 | Fredrik Hübinette (Hubbe) | | efun;
optflags OPT_SIDE_EFFECT;
{
|
a3453e | 2001-02-05 | Per Hedbor | | /*FIXME: Should be
* type function(mapping(1=mixed:2=mixed),1:2)|
* function(object,mixed:mixed);
*
* or similar
*/
if( map->type == T_MAPPING )
{
struct svalue s;
map_delete_no_free(map->u.mapping, index, &s);
pop_n_elems(args);
*sp=s;
sp++;
}
|
79f698 | 2001-02-05 | Henrik Grubbström (Grubba) | | else if (map->type == T_OBJECT)
|
a3453e | 2001-02-05 | Per Hedbor | | {
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | int id = FIND_LFUN(map->u.object->prog, LFUN__M_DELETE);
|
ea5601 | 2001-02-09 | Per Hedbor | |
if( id == -1 )
SIMPLE_BAD_ARG_ERROR("m_delete", 1, "object with _m_delete");
apply_low( map->u.object, id, 1 );
|
a3453e | 2001-02-05 | Per Hedbor | | stack_swap();
pop_stack();
|
79f698 | 2001-02-05 | Henrik Grubbström (Grubba) | | } else {
SIMPLE_BAD_ARG_ERROR("m_delete", 1, "object|mapping");
|
a3453e | 2001-02-05 | Per Hedbor | | }
|
7f80d4 | 2000-06-19 | Fredrik Hübinette (Hubbe) | | }
|
049833 | 2001-02-10 | Henrik Grubbström (Grubba) | | /*! @decl int(0..1) get_weak_flag(array|mapping|multiset m)
*!
*! Returns 1 if the weak flag has been set for @[m].
*/
|
b0f835 | 2001-01-07 | Henrik Grubbström (Grubba) | | PIKEFUN int(0 .. 1) get_weak_flag(array|mapping|multiset m)
|
ee9fa9 | 2000-07-06 | Martin Stjernholm | | efun;
|
8f998d | 2000-08-31 | Henrik Grubbström (Grubba) | | optflags OPT_EXTERNAL_DEPEND;
|
ee9fa9 | 2000-07-06 | Martin Stjernholm | | {
|
37b878 | 2000-11-02 | Henrik Grubbström (Grubba) | | int flag = 0;
|
ee9fa9 | 2000-07-06 | Martin Stjernholm | | switch (m->type) {
case T_ARRAY:
flag = !!(m->u.array->flags & ARRAY_WEAK_FLAG);
break;
case T_MAPPING:
|
880be6 | 2000-09-04 | Martin Stjernholm | | flag = !!(mapping_get_flags(m->u.mapping) & MAPPING_FLAG_WEAK);
|
ee9fa9 | 2000-07-06 | Martin Stjernholm | | break;
case T_MULTISET:
flag = !!(m->u.multiset->ind->flags & (ARRAY_WEAK_FLAG|ARRAY_WEAK_SHRINK));
break;
default:
SIMPLE_BAD_ARG_ERROR("get_weak_flag",1,"array|mapping|multiset");
}
pop_n_elems(args);
push_int(flag);
}
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | void init_builtin(void)
{
|
ab8282 | 2000-05-25 | Fredrik Hübinette (Hubbe) | | INIT
|
3a5b1d | 2000-05-24 | Fredrik Hübinette (Hubbe) | | }
|