Fixed a few GIB bugs, added the chomp builtin.

This commit is contained in:
Brian Koropoff 2003-05-14 21:13:41 +00:00
parent fe5a62b75a
commit bf675e793e
5 changed files with 35 additions and 10 deletions

View file

@ -51,7 +51,7 @@ typedef struct gib_domain_s {
gib_var_t *GIB_Var_Get (hashtab_t *first, hashtab_t *second, const char *key);
gib_var_t *GIB_Var_Get_Complex (hashtab_t **first, hashtab_t **second, char *key, unsigned int *ind, qboolean create);
gib_var_t *GIB_Var_Get_Very_Complex (hashtab_t ** first, hashtab_t ** second, dstring_t *key, unsigned int start, unsigned int *ind, qboolean create);
void GIB_Var_Assign (gib_var_t *var, unsigned int index, dstring_t **values, unsigned int numv);
void GIB_Var_Assign (gib_var_t *var, unsigned int index, dstring_t **values, unsigned int numv, qboolean shrink);
hashtab_t *GIB_Domain_Get (const char *name);
void GIB_Var_Init (void);

View file

@ -114,8 +114,8 @@ GIB_Builtin_Remove (const char *name)
{
gib_builtin_t *del;
if ((del = Hash_Find (gib_builtins, name)))
Hash_Free (gib_builtins, Hash_DelElement (gib_builtins, del));
if ((del = Hash_Del (gib_builtins, name)))
Hash_Free (gib_builtins, del);
}
qboolean
@ -225,7 +225,7 @@ GIB_Local_f (void)
GIB_Argv (1), &index, true);
if (GIB_Argc () >= 3)
GIB_Var_Assign (var, index, cbuf_active->args->argv + 3,
GIB_Argc () - 3);
GIB_Argc () - 3, GIB_Argv (1)[strlen (GIB_Argv(1)) - 1] != ']');
if (GIB_CanReturn ())
for (i = 3; i < GIB_Argc(); i++)
GIB_Return (GIB_Argv(i));
@ -250,7 +250,7 @@ GIB_Global_f (void)
GIB_Argv (1), &index, true);
if (GIB_Argc () >= 3)
GIB_Var_Assign (var, index, cbuf_active->args->argv + 3,
GIB_Argc () - 3);
GIB_Argc () - 3, GIB_Argv (1)[strlen (GIB_Argv(1)) - 1] != ']');
if (GIB_CanReturn ())
for (i = 3; i < GIB_Argc(); i++)
GIB_Return (GIB_Argv(i));
@ -512,6 +512,30 @@ GIB_Split_f (void)
}
}
static void
GIB_Chomp_f (void)
{
char *str;
const char *junk;
unsigned int i;
if (GIB_Argc () < 2 || GIB_Argc () > 3) {
GIB_USAGE ("string [junk]");
return;
}
str = GIB_Argv (1);
if (GIB_Argc () == 2)
junk = " \t\n\r";
else
junk = GIB_Argv (2);
for (; *str && strchr (junk, *str); str++);
for (i = strlen(str) - 1; i && strchr (junk, str[i]); i--);
str[i+1] = 0;
GIB_Return (str);
}
static void
GIB_Regex_Match_f (void)
{
@ -948,6 +972,7 @@ GIB_Builtin_Init (qboolean sandbox)
GIB_Builtin_Add ("slice", GIB_Slice_f);
GIB_Builtin_Add ("slice::find", GIB_Slice_Find_f);
GIB_Builtin_Add ("split", GIB_Split_f);
GIB_Builtin_Add ("chomp", GIB_Chomp_f);
GIB_Builtin_Add ("regex::match", GIB_Regex_Match_f);
GIB_Builtin_Add ("regex::replace", GIB_Regex_Replace_f);
GIB_Builtin_Add ("regex::extract", GIB_Regex_Extract_f);

View file

@ -240,7 +240,7 @@ GIB_Execute (cbuf_t * cbuf)
&g->locals,
&g->globals,
cbuf->args->argv[0]->str, &index, true);
GIB_Var_Assign (var, index, cbuf->args->argv + 2, cbuf->args->argc - 2);
GIB_Var_Assign (var, index, cbuf->args->argv + 2, cbuf->args->argc - 2, cbuf->args->argv[0]->str[strlen (cbuf->args->argv[0]->str) - 1] != ']');
if (g->ip->flags & TREE_L_EMBED) {
GIB_Buffer_Push_Sstack (cbuf);
g->waitret = true;

View file

@ -81,7 +81,7 @@ GIB_Handle_Free (unsigned long int num, unsigned short int class)
void *
GIB_Handle_Get (unsigned long int num, unsigned short int class)
{
if (num >= gib_next_handle || gib_handles[num]->class != class)
if (num >= gib_next_handle || !gib_handles[num] || gib_handles[num]->class != class)
return 0;
return gib_handles[num]->data;
}

View file

@ -242,7 +242,7 @@ GIB_Var_Get_Very_Complex (hashtab_t ** first, hashtab_t ** second, dstring_t *ke
void
GIB_Var_Assign (gib_var_t * var, unsigned int index, dstring_t ** values,
unsigned int numv)
unsigned int numv, qboolean shrink)
{
unsigned int i, len;
@ -253,7 +253,7 @@ GIB_Var_Assign (gib_var_t * var, unsigned int index, dstring_t ** values,
memset (var->array + var->size, 0,
(len - var->size) * sizeof (struct gib_varray_s));
var->size = len;
} else if (len < var->size) {
} else if (len < var->size && shrink) {
for (i = len; i < var->size; i++) {
if (var->array[i].value)
dstring_delete (var->array[i].value);
@ -261,8 +261,8 @@ GIB_Var_Assign (gib_var_t * var, unsigned int index, dstring_t ** values,
Hash_DelTable (var->array[i].leaves);
}
var->array = realloc (var->array, len * sizeof (struct gib_varray_s));
var->size = len;
}
var->size = len;
for (i = 0; i < numv; i++) {
if (var->array[i + index].value)
dstring_clearstr (var->array[i + index].value);