more meaty api...

SVN revision: 9518
This commit is contained in:
Carsten Haitzler 2004-03-29 11:58:57 +00:00
parent 419bd77877
commit 811cd94fba
10 changed files with 650 additions and 9 deletions

View File

@ -12,11 +12,28 @@ public global2 = 77;
main()
{
testfn(12345, "Panties!!!!", 7);
new Float:t;
testfn(12345, "Panties!!!!", 7);
t = seconds();
printf("SECONDS = %f\n", t);
testdate();
return 77;
}
testdate()
{
new year, month, day, yearday, weekday, hour, minute;
new Float:second;
date(year, month, day, yearday, weekday, hour, minute, second);
printf("%i/%i/%i\n", day, month, year);
printf("%i:%i:%f\n", hour, minute, second);
printf("yearday: %i, weekday: %i\n", yearday, weekday);
printf("frand: %f\n", randf());
printf("rand: %X\n", rand());
}
tester(arg1=0, str[]="", arg2=0)
{
if (arg1 == 7) printf("arg1 == 7!!!\n");

View File

@ -4,9 +4,13 @@
* Written by Greg Garner (gmg@artran.com)
* Modified in March 2001 to include user defined
* operators for the floating point functions.
* (c) Copyright 2004, Carsten Haitzler
* Modified March 2004 by Carsten Haitzler <raster@rasterman.com> to conform
* to E coding style
* Became default include for embryo...
* Added string functions
* Added rand functions
* Added time functions
*
* This file is provided as is (no warranties).
*/
@ -34,25 +38,86 @@ native Float:atof(const string[]);
native Float:fract(Float:value);
/* Round a float into a integer value */
native round(Float:value, Float_Round_Method:method=ROUND);
/* Return the square root of the input value, same as float_power(value, 0.5) */
/* Return the square root of value, same as float_power(value, 0.5) */
native Float:sqrt(Float:value);
/* Return the value raised to the power of the exponent */
native Float:pow(Float:value, Float:exponent);
/* Return the logarithm */
native Float:log(Float:value, Float:base=10.0);
/* Return the sine, cosine or tangent. The input angle may be in radian, degrees or grades. */
/* Return the sine, cosine or tangent. The input angle may be in radian*/
/* degrees or grades. */
native Float:sin(Float:value, Float_Angle_Mode:mode=RADIAN);
native Float:cos(Float:value, Float_Angle_Mode:mode=RADIAN);
native Float:tan(Float:value, Float_Angle_Mode:mode=RADIAN);
/* Return the absolute value */
native Float:abs(Float:value);
/* return integer from string */
native atoi(str[]);
/* return 0 if string matches glob, non-zero otherwise */
native fnmatch(glob[], str[]);
/* same as strcmp() */
native strcmp(str1[], str2[]);
/* same as strncmp */
native strncmp(str1[], str2[]);
/* same as strcpy */
native strcpy(dst[], src[]);
/* same as strncpy except it nul terminates */
native strncpy(dst[], src[], n);
/* same as strlen */
native strlen(str[]);
/* same as strcat */
native strcat(dst[], src[]);
/* same as strncat except it nul terminates */
native strncat(dst[], src[], n);
/* prepends src string onto start of dst string */
native strprep(dst[], src[]);
/* prepends at most n chars from src string onto dst string */
native strnprep(dst[], src[], n);
/* cuts chars from char n to (not including) n2, and puts them in str */
native strcut(dst[], str[], n, n2);
/* same as snprintf, except only supports %%, %c, %i, %d, %f, %x, %X, %s, \n and \t */
native snprintf(dst[], dstn, fmt[], ...);
/* same as strstr */
native strstr(str[], ndl[]);
/* same as strchr, except ch must be a 1 charater long string, and returns string index */
native strchr(str[], ch[]);
/* same as strrchr, except ch must be a 1 charater long string and returns string index */
native strrchr(str[], ch[]);
/* return random number 0 - 65535 */
native rand();
/* return random number 0.0 - 1.0 */
native Float:randf();
/* return seconds since midnight as a float */
native Float:seconds();
/* return the current date, year, time etc. in the variables provided */
native date(&year, &month, &day, &yearday, &weekday, &hr, &min, &Float:sec);
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/**************************************************/
/* Hidden calls - all are overloaded on operators */

View File

@ -213,6 +213,7 @@ main(int argc,char *argv[])
char *file = NULL;
char *func = NULL;
embryo_init();
for (i = 1; i < argc; i++)
{
if (argv[i][0] != '-')
@ -304,5 +305,6 @@ main(int argc,char *argv[])
}
printf("Program returned %i\n", (int)embryo_program_return_value_get(ep));
embryo_program_free(ep);
embryo_shutdown();
return 0;
}

View File

@ -19,6 +19,9 @@ libembryo_la_SOURCES = \
embryo_amx.c \
embryo_float.c \
embryo_main.c \
embryo_rand.c \
embryo_str.c \
embryo_time.c \
embryo_private.h
libembryo_la_LIBADD = $(LDFLAGS) -lm

View File

@ -197,6 +197,9 @@ _embryo_program_init(Embryo_Program *ep, void *code)
#endif
/* init native api for handling floating point - default in embryo */
_embryo_fp_init(ep);
_embryo_rand_init(ep);
_embryo_str_init(ep);
_embryo_time_init(ep);
return 1;
}

View File

@ -1,4 +1,5 @@
#include "embryo_private.h"
#include <time.h>
static int _embryo_init_count = 0;
@ -10,6 +11,8 @@ embryo_init(void)
_embryo_init_count++;
if (_embryo_init_count > 1) return _embryo_init_count;
srand(time(NULL));
return _embryo_init_count;
}

View File

@ -276,5 +276,8 @@ struct _Embryo_Header
} __attribute__((packed));
void _embryo_fp_init(Embryo_Program *ep);
void _embryo_rand_init(Embryo_Program *ep);
void _embryo_str_init(Embryo_Program *ep);
void _embryo_time_init(Embryo_Program *ep);
#endif

View File

@ -0,0 +1,29 @@
#include "embryo_private.h"
/* exported random number api */
static Embryo_Cell
_embryo_rand_rand(Embryo_Program *ep, Embryo_Cell *params)
{
return (Embryo_Cell)(rand() & 0xffff);
}
static Embryo_Cell
_embryo_rand_randf(Embryo_Program *ep, Embryo_Cell *params)
{
double r;
float f;
r = (double)(rand() & 0xffff) / 65535.0;
f = (float)r;
return EMBRYO_FLOAT_TO_CELL(f);
}
/* functions used by the rest of embryo */
void
_embryo_rand_init(Embryo_Program *ep)
{
embryo_program_native_call_add(ep, "rand", _embryo_rand_rand);
embryo_program_native_call_add(ep, "randf", _embryo_rand_randf);
}

View File

@ -0,0 +1,448 @@
#include "embryo_private.h"
#include <fnmatch.h>
#define STRGET(ep, str, par) { \
Embryo_Cell *___cptr; \
if ((___cptr = embryo_data_address_get(ep, par))) { \
int ___l; \
___l = embryo_data_string_length_get(ep, ___cptr); \
(str) = alloca(___l + 1); \
if (str) embryo_data_string_get(ep, ___cptr, str); \
} }
#define STRSET(ep, par, str) { \
Embryo_Cell *___cptr; \
if ((___cptr = embryo_data_address_get(ep, par))) { \
embryo_data_string_set(ep, str, ___cptr); \
} }
/* exported string api */
static Embryo_Cell
_embryo_str_atoi(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1;
/* params[1] = str */
if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
STRGET(ep, s1, params[1]);
if (!s1) return 0;
return (Embryo_Cell)atoi(s1);
}
static Embryo_Cell
_embryo_str_fnmatch(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2;
/* params[1] = glob */
/* params[2] = str */
if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
if ((!s1) || (!s2)) return -1;
return (Embryo_Cell)fnmatch(s1, s2, 0);
}
static Embryo_Cell
_embryo_str_strcmp(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2;
/* params[1] = str1 */
/* params[2] = str2 */
if (params[0] != (2 * sizeof(Embryo_Cell))) return -1;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
if ((!s1) || (!s2)) return -1;
return (Embryo_Cell)strcmp(s1, s2);
}
static Embryo_Cell
_embryo_str_strncmp(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2;
/* params[1] = str1 */
/* params[2] = str2 */
/* params[3] = n */
if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
if (params[3] < 0) params[3] = 0;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
if ((!s1) || (!s2)) return -1;
return (Embryo_Cell)strncmp(s1, s2, (size_t)params[3]);
}
static Embryo_Cell
_embryo_str_strcpy(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1;
/* params[1] = dst */
/* params[2] = str */
if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
STRGET(ep, s1, params[2]);
if (!s1) return 0;
STRSET(ep, params[1], s1);
return 0;
}
static Embryo_Cell
_embryo_str_strncpy(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1;
int l;
/* params[1] = dst */
/* params[2] = str */
/* params[3] = n */
if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
if (params[3] < 0) params[3] = 0;
STRGET(ep, s1, params[2]);
if (!s1) return 0;
l = strlen(s1);
if (l > params[3]) s1[params[3]] = 0;
STRSET(ep, params[1], s1);
return 0;
}
static Embryo_Cell
_embryo_str_strlen(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1;
/* params[1] = str */
if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
STRGET(ep, s1, params[1]);
if (!s1) return 0;
return (Embryo_Cell)strlen(s1);
}
static Embryo_Cell
_embryo_str_strcat(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2, *s3;
/* params[1] = dsr */
/* params[2] = str */
if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
if ((!s1) || (!s2)) return 0;
s3 = alloca(strlen(s1) + strlen(s2) + 1);
if (!s3) return 0;
strcpy(s3, s1);
strcat(s3, s2);
STRSET(ep, params[1], s3);
return 0;
}
static Embryo_Cell
_embryo_str_strncat(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2, *s3;
int l1, l2;
/* params[1] = dst */
/* params[2] = str */
/* params[3] = n */
if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
if (params[3] < 0) params[3] = 0;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
if ((!s1) || (!s2)) return 0;
l1 = strlen(s1);
l2 = strlen(s2);
s3 = alloca(l1 + l2 + 1);
if (!s3) return 0;
strcpy(s3, s1);
strncat(s3, s2, params[3]);
if (l2 >= params[3]) s3[l1 + params[3]] = 0;
STRSET(ep, params[1], s3);
return 0;
}
static Embryo_Cell
_embryo_str_strprep(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2, *s3;
/* params[1] = dst */
/* params[2] = str */
if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
if ((!s1) || (!s2)) return 0;
s3 = alloca(strlen(s1) + strlen(s2) + 1);
if (!s3) return 0;
strcpy(s3, s2);
strcat(s3, s1);
STRSET(ep, params[1], s3);
return 0;
}
static Embryo_Cell
_embryo_str_strnprep(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2, *s3;
int l1, l2;
/* params[1] = dst */
/* params[2] = str */
/* params[3] = n */
if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
if (params[3] < 0) params[3] = 0;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
if ((!s1) || (!s2)) return 0;
l1 = strlen(s1);
l2 = strlen(s2);
s3 = alloca(l1 + l2 + 1);
if (!s3) return 0;
strncpy(s3, s2, params[3]);
if (params[3] <= l2) s3[params[3]] = 0;
strcat(s3, s1);
STRSET(ep, params[1], s3);
return 0;
}
static Embryo_Cell
_embryo_str_strcut(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2;
int l1;
/* params[1] = dst */
/* params[2] = str */
/* params[3] = n */
/* params[4] = n2 */
if (params[0] != (4 * sizeof(Embryo_Cell))) return 0;
if (params[3] < 0) params[3] = 0;
if (params[4] < params[3]) params[4] = params[3];
STRGET(ep, s1, params[2]);
if (!s1) return 0;
l1 = strlen(s1);
if (params[3] >= l1) params[3] = l1;
if (params[4] >= l1) params[4] = l1;
if (params[4] == params[3])
{
STRSET(ep, params[1], "");
return 0;
}
s2 = alloca(params[4] - params[3] + 1);
strncpy(s2, s1 + params[3], params[4] - params[3]);
s2[params[4] - params[3]] = 0;
STRSET(ep, params[1], s2);
return 0;
}
static Embryo_Cell
_embryo_str_snprintf(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2;
int i, o;
int inesc = 0;
int insub = 0;
int p, pnum;
/* params[1] = buf */
/* params[2] = bufsize */
/* params[3] = format_string */
/* params[4] = first arg ... */
if (params[0] < (3 * sizeof(Embryo_Cell))) return 0;
if (params[2] <= 0) return 0;
STRGET(ep, s1, params[3]);
if (!s1) return -1;
s2 = alloca(params[2] + 1);
if (!s2) return -1;
s2[0] = 0;
pnum = (params[0] / sizeof(Embryo_Cell)) - 3;
for (p = 0, o = 0; (s1[i]) && (o < (params[2] - 1)) && (p < pnum); i++)
{
if ((!inesc) && (!insub))
{
if (s1[i] == '\\') inesc = 1;
else if (s1[i] == '%') insub = 1;
if ((!inesc) && (!insub))
{
s2[o] = s1[i];
o++;
}
}
else
{
if (inesc)
{
switch (s1[i])
{
case 't':
s2[o] = '\t';
o++;
break;
case 'n':
s2[o] = '\n';
o++;
break;
default:
s2[o] = s1[i];
o++;
break;
}
inesc = 0;
}
if (insub)
{
switch (s1[i])
{
case '%':
s2[o] = '%';
o++;
break;
case 'c':
s2[o] = params[4 + p];
p++;
o++;
break;
case 'i':
case 'd':
case 'x':
case 'X':
{
char fmt[10] = "";
char tmp[256] = "";
int l;
if (s1[i] == 'i') strcpy(fmt, "%i");
else if (s1[i] == 'd') strcpy(fmt, "%d");
else if (s1[i] == 'x') strcpy(fmt, "%x");
else if (s1[i] == 'X') strcpy(fmt, "%08x");
snprintf(tmp, sizeof(tmp), fmt, params[4 + p]);
l = strlen(tmp);
if ((o + l) > (params[2] - 1))
{
l = params[2] - 1 - o;
if (l < 0) l = 0;
tmp[l] = 0;
}
strcat(s2, tmp);
o += l;
p++;
}
break;
case 'f':
{
char tmp[256] = "";
int l;
snprintf(tmp, sizeof(tmp), "%f", EMBRYO_CELL_TO_FLOAT(params[4 + p]));
l = strlen(tmp);
if ((o + l) > (params[2] - 1))
{
l = params[2] - 1 - o;
if (l < 0) l = 0;
tmp[l] = 0;
}
strcat(s2, tmp);
o += l;
p++;
}
break;
case 's':
{
char *tmp;
int l;
STRGET(ep, tmp, params[4 + p]);
l = strlen(tmp);
if ((o + l) > (params[2] - 1))
{
l = params[2] - 1 - o;
if (l < 0) l = 0;
tmp[l] = 0;
}
strcat(s2, tmp);
o += l;
p++;
}
break;
default:
break;
}
insub = 0;
}
}
}
s2[o] = 0;
STRSET(ep, params[1], s2);
return 0;
}
static Embryo_Cell
_embryo_str_strstr(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2, *p;
/* params[1] = str */
/* params[2] = ndl */
if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
if ((!s1) || (!s2)) return -1;
p = strstr(s1, s2);
if (p == NULL) return -1;
return (Embryo_Cell)(p - s1);
}
static Embryo_Cell
_embryo_str_strchr(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2, *p;
/* params[1] = str */
/* params[2] = ch */
if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
p = strchr(s1, s2[0]);
if (p == NULL) return -1;
return (Embryo_Cell)(p - s1);
}
static Embryo_Cell
_embryo_str_strrchr(Embryo_Program *ep, Embryo_Cell *params)
{
char *s1, *s2, *p;
/* params[1] = str */
/* params[2] = ch */
if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
STRGET(ep, s1, params[1]);
STRGET(ep, s2, params[2]);
p = strrchr(s1, s2[0]);
if (p == NULL) return -1;
return (Embryo_Cell)(p - s1);
}
/* functions used by the rest of embryo */
void
_embryo_str_init(Embryo_Program *ep)
{
embryo_program_native_call_add(ep, "atoi", _embryo_str_atoi);
embryo_program_native_call_add(ep, "fnmtach", _embryo_str_fnmatch);
embryo_program_native_call_add(ep, "strcmp", _embryo_str_strcmp);
embryo_program_native_call_add(ep, "strncmp", _embryo_str_strncmp);
embryo_program_native_call_add(ep, "strcpy", _embryo_str_strcpy);
embryo_program_native_call_add(ep, "strncpy", _embryo_str_strncpy);
embryo_program_native_call_add(ep, "strlen", _embryo_str_strlen);
embryo_program_native_call_add(ep, "strcat", _embryo_str_strcat);
embryo_program_native_call_add(ep, "strncat", _embryo_str_strncat);
embryo_program_native_call_add(ep, "strprep", _embryo_str_strprep);
embryo_program_native_call_add(ep, "strnprep", _embryo_str_strnprep);
embryo_program_native_call_add(ep, "strcut", _embryo_str_strcut);
embryo_program_native_call_add(ep, "snprintf", _embryo_str_snprintf);
embryo_program_native_call_add(ep, "strstr", _embryo_str_strstr);
embryo_program_native_call_add(ep, "strchr", _embryo_str_strchr);
embryo_program_native_call_add(ep, "strrchr", _embryo_str_strrchr);
}

View File

@ -0,0 +1,68 @@
#include "embryo_private.h"
#include <sys/time.h>
#include <time.h>
/* exported time api */
static Embryo_Cell
_embryo_time_seconds(Embryo_Program *ep, Embryo_Cell *params)
{
struct timeval timev;
double t;
float f;
gettimeofday(&timev, NULL);
t = (double)(timev.tv_sec - ((timev.tv_sec / (60 * 60 * 24)) * (60 * 60 * 24)))
+ (((double)timev.tv_usec) / 1000000);
f = (float)t;
return EMBRYO_FLOAT_TO_CELL(f);
}
static Embryo_Cell
_embryo_time_date(Embryo_Program *ep, Embryo_Cell *params)
{
struct timeval timev;
struct tm *tm;
time_t tt;
if (params[0] != (8 * sizeof(Embryo_Cell))) return 0;
gettimeofday(&timev, NULL);
tt = (time_t)(timev.tv_sec);
tm = localtime(&tt);
if (tm)
{
Embryo_Cell *cptr;
double t;
float f;
cptr = embryo_data_address_get(ep, params[1]);
if (cptr) *cptr = tm->tm_year + 1900;
cptr = embryo_data_address_get(ep, params[2]);
if (cptr) *cptr = tm->tm_mon + 1;
cptr = embryo_data_address_get(ep, params[3]);
if (cptr) *cptr = tm->tm_mday;
cptr = embryo_data_address_get(ep, params[4]);
if (cptr) *cptr = tm->tm_yday;
cptr = embryo_data_address_get(ep, params[5]);
if (cptr) *cptr = (tm->tm_wday + 6) % 7;
cptr = embryo_data_address_get(ep, params[6]);
if (cptr) *cptr = tm->tm_hour;
cptr = embryo_data_address_get(ep, params[7]);
if (cptr) *cptr = tm->tm_min;
cptr = embryo_data_address_get(ep, params[8]);
t = (double)tm->tm_sec + (((double)timev.tv_usec) / 1000000);
f = (float)t;
if (cptr) *cptr = EMBRYO_FLOAT_TO_CELL(f);
}
return 0;
}
/* functions used by the rest of embryo */
void
_embryo_time_init(Embryo_Program *ep)
{
embryo_program_native_call_add(ep, "seconds", _embryo_time_seconds);
embryo_program_native_call_add(ep, "date", _embryo_time_date);
}