Code and comment cleanups to the Karll array functions

There should be no behavioural change.
This commit is contained in:
Kevin Easton
2016-10-01 01:37:27 +10:00
parent e2b351b9ce
commit 729b029385

View File

@@ -417,26 +417,26 @@ m_s3cat(&result, space, array_info.item[array_info.index[index]]);
*/ */
BUILT_IN_FUNCTION(function_matchitem) BUILT_IN_FUNCTION(function_matchitem)
{ {
char *name; char *name = next_arg(input, &input);
long index; long index;
an_array *array; an_array *array;
long current_match; long current_match;
long best_match = 0; long best_match = 0;
long match = -1; long match = -1;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)))
{ {
match = -2; match = -2;
if (*input) if (*input)
{ {
for (index = 0; index < array->size; index++) for (index = 0; index < array->size; index++)
{ {
if ((current_match = wild_match(input, array->item[index])) > best_match) if ((current_match = wild_match(input, array->item[index])) > best_match)
{ {
match = index; match = index;
best_match = current_match; best_match = current_match;
} }
} }
} }
} }
@@ -445,21 +445,17 @@ BUILT_IN_FUNCTION(function_matchitem)
BUILT_IN_FUNCTION(function_igetmatches) BUILT_IN_FUNCTION(function_igetmatches)
{ {
char *result = NULL; char *result = NULL;
char *name = NULL; char *name = next_arg(input, &input);
long item; long item;
an_array *array; an_array *array;
if ((name = next_arg(input, &input)) && if (name && (array = get_array(name)) && *input)
(array = get_array(name)) && *input)
{ {
if (*input) for (item = 0; item < array->size; item++)
{ {
for (item = 0; item < array->size; item++) if (wild_match(input, array->item[item]) > 0)
{ m_s3cat(&result, space, ltoa(find_index(array, item)));
if (wild_match(input, array->item[item]) > 0)
m_s3cat(&result, space, ltoa(find_index(array, item)));
}
} }
} }
@@ -475,12 +471,12 @@ BUILT_IN_FUNCTION(function_igetmatches)
*/ */
BUILT_IN_FUNCTION(function_listarray) BUILT_IN_FUNCTION(function_listarray)
{ {
char *name;
an_array *array;
long index;
char *result = NULL; char *result = NULL;
char *name = next_arg(input, &input);
long index;
an_array *array;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)))
{ {
for (index = 0; index < array->size; index++) for (index = 0; index < array->size; index++)
m_s3cat(&result, space, array->item[index]); m_s3cat(&result, space, array->item[index]);
@@ -496,26 +492,24 @@ BUILT_IN_FUNCTION(function_listarray)
*/ */
BUILT_IN_FUNCTION(function_getmatches) BUILT_IN_FUNCTION(function_getmatches)
{ {
char *result = NULL; char *result = NULL;
char *name = NULL; char *name = next_arg(input, &input);
long index; long index;
an_array *array; an_array *array;
if (name && (array = get_array(name)) && *input)
{
for (index = 0; index < array->size; index++)
{
if (wild_match(input, array->item[index]) > 0)
m_s3cat(&result, space, ltoa(index));
}
}
if ((name = next_arg(input, &input)) &&
(array = get_array(name)) && *input)
{
if (*input)
{
for (index = 0; index < array->size; index++)
{
if (wild_match(input, array->item[index]) > 0)
m_s3cat(&result, space, ltoa(index));
}
}
}
if (!result) if (!result)
RETURN_EMPTY; RETURN_EMPTY;
return result;
return result;
} }
/* /*
@@ -525,28 +519,28 @@ BUILT_IN_FUNCTION(function_getmatches)
*/ */
BUILT_IN_FUNCTION(function_rmatchitem) BUILT_IN_FUNCTION(function_rmatchitem)
{ {
char *name = NULL; char *name = next_arg(input, &input);
long index; long index;
an_array *array; an_array *array;
long current_match; long current_match;
long best_match = 0; long best_match = 0;
long match = -1; long match = -1;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)))
{ {
match = -2; match = -2;
if (*input) if (*input)
{ {
for (index = 0; index < array->size; index++) for (index = 0; index < array->size; index++)
{ {
if ((current_match = wild_match(array->item[index], input)) > best_match) if ((current_match = wild_match(array->item[index], input)) > best_match)
{ {
match = index; match = index;
best_match = current_match; best_match = current_match;
} }
} }
} }
} }
RETURN_INT(match) RETURN_INT(match)
} }
@@ -558,40 +552,37 @@ BUILT_IN_FUNCTION(function_rmatchitem)
*/ */
BUILT_IN_FUNCTION(function_getrmatches) BUILT_IN_FUNCTION(function_getrmatches)
{ {
char *result = NULL; char *result = NULL;
char *name = NULL; char *name = next_arg(input, &input);
long index; long index;
an_array *array; an_array *array;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)) && *input)
{ {
if (*input) for (index = 0; index < array->size; index++)
{ {
for (index = 0; index < array->size; index++) if (wild_match(array->item[index], input) > 0)
{ m_s3cat(&result, space, ltoa(index));
if (wild_match(array->item[index], input) > 0) }
m_s3cat(&result, space, ltoa(index)); }
}
}
}
if (!result) if (!result)
RETURN_EMPTY; RETURN_EMPTY;
return result; return result;
} }
/* /*
* function_numitems() returns the number of items in an array, or -1 if unable * function_numitems() returns the number of items in an array, or 0 if unable
* to find the array * to find the array
*/ */
BUILT_IN_FUNCTION(function_numitems) BUILT_IN_FUNCTION(function_numitems)
{ {
char *name = NULL; char *name = next_arg(input, &input);
an_array *array; an_array *array;
long items = 0; long items = 0;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)))
items = array->size; items = array->size;
RETURN_INT(items); RETURN_INT(items);
} }
@@ -602,20 +593,17 @@ BUILT_IN_FUNCTION(function_numitems)
*/ */
BUILT_IN_FUNCTION(function_getitem) BUILT_IN_FUNCTION(function_getitem)
{ {
char *name = NULL; char *name = next_arg(input, &input);
char *itemstr = NULL; char *itemstr = next_arg(input, &input);
long item; long item;
an_array *array; an_array *array;
char *found = NULL; char *found = NULL;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)) && itemstr)
{ {
if ((itemstr = next_arg(input, &input))) item = my_atol(itemstr);
{ if (item >= 0 && item < array->size)
item = my_atol(itemstr); found = array->item[item];
if (item >= 0 && item < array->size)
found = array->item[item];
}
} }
RETURN_STR(found); RETURN_STR(found);
} }
@@ -634,71 +622,68 @@ BUILT_IN_FUNCTION(function_getitem)
*/ */
BUILT_IN_FUNCTION(function_setitem) BUILT_IN_FUNCTION(function_setitem)
{ {
char *name = NULL; char *name = next_arg(input, &input);
char *itemstr = NULL; char *itemstr = next_arg(input, &input);
long item; long item;
long index = 0; long index = 0;
long oldindex; long oldindex;
an_array *array; an_array *array;
int result = -1; int result = -1;
if ((name = next_arg(input, &input))) if (name && itemstr)
{ {
if (strlen(name) && (itemstr = next_arg(input, &input))) item = my_atol(itemstr);
if (item >= 0)
{ {
item = my_atol(itemstr); upper(name);
if (item >= 0) if (array_info.size && ((index = find_item(array_info, name)) >= 0))
{ {
upper(name); array = &array_array[array_info.index[index]];
if (array_info.size && ((index = find_item(array_info, name)) >= 0)) result = -2;
{ if (item < array->size)
array = &array_array[array_info.index[index]];
result = -2;
if (item < array->size)
{
oldindex = find_index(array, item);
index = find_item(*array, input);
index = (index >= 0) ? index : (-index) - 1;
move_index(array, oldindex, index);
new_free(&array->item[item]);
malloc_strcpy(&array->item[item], input);
result = 0;
}
else if (item == array->size)
{
RESIZE(array->item, char *, (array->size + 1));
array->item[item] = NULL;
malloc_strcpy(&array->item[item], input);
index = find_item(*array, input);
index = (index >= 0) ? index : (-index) - 1;
insert_index(&array->index, &array->size, index);
result = 2;
}
}
else
{ {
if (item == 0) oldindex = find_index(array, item);
{ index = find_item(*array, input);
if (array_info.size) index = (index >= 0) ? index : (-index) - 1;
RESIZE(array_array, an_array, (array_info.size + 1)); move_index(array, oldindex, index);
else new_free(&array->item[item]);
array_array = (an_array*)new_malloc(sizeof(an_array)); malloc_strcpy(&array->item[item], input);
array = &array_array[array_info.size]; result = 0;
array->size = 1; }
array->item = (char **)new_malloc(sizeof(char *)); else if (item == array->size)
array->index = (long *)new_malloc(sizeof(long)); {
array->item[0] = (char*) 0; RESIZE(array->item, char *, (array->size + 1));
array->index[0] = 0; array->item[item] = NULL;
malloc_strcpy(&array->item[0], input); malloc_strcpy(&array->item[item], input);
if (array_info.size) index = find_item(*array, input);
RESIZE(array_info.item, char *, (array_info.size + 1)); index = (index >= 0) ? index : (-index) - 1;
else insert_index(&array->index, &array->size, index);
array_info.item = (char **)new_malloc(sizeof(char *)); result = 2;
array_info.item[array_info.size] = NULL; }
malloc_strcpy(&array_info.item[array_info.size], name); }
insert_index(&array_info.index, &array_info.size, (-index) - 1); else
result = 1; {
} if (item == 0)
{
if (array_info.size)
RESIZE(array_array, an_array, (array_info.size + 1));
else
array_array = (an_array*)new_malloc(sizeof(an_array));
array = &array_array[array_info.size];
array->size = 1;
array->item = (char **)new_malloc(sizeof(char *));
array->index = (long *)new_malloc(sizeof(long));
array->item[0] = (char*) 0;
array->index[0] = 0;
malloc_strcpy(&array->item[0], input);
if (array_info.size)
RESIZE(array_info.item, char *, (array_info.size + 1));
else
array_info.item = (char **)new_malloc(sizeof(char *));
array_info.item[array_info.size] = NULL;
malloc_strcpy(&array_info.item[array_info.size], name);
insert_index(&array_info.index, &array_info.size, (-index) - 1);
result = 1;
} }
} }
} }
@@ -716,8 +701,8 @@ BUILT_IN_FUNCTION(function_getarrays)
char *result = NULL; char *result = NULL;
for (index = 0; index < array_info.size; index++) for (index = 0; index < array_info.size; index++)
if (!input || !*input || wild_match(input, array_info.item[array_info.index[index]])) if (!input || !*input || wild_match(input, array_info.item[array_info.index[index]]))
m_s3cat(&result, space, array_info.item[array_info.index[index]]); m_s3cat(&result, space, array_info.item[array_info.index[index]]);
if (!result) if (!result)
RETURN_EMPTY; RETURN_EMPTY;
@@ -740,18 +725,15 @@ BUILT_IN_FUNCTION(function_numarrays)
*/ */
BUILT_IN_FUNCTION(function_finditem) BUILT_IN_FUNCTION(function_finditem)
{ {
char *name = NULL; char *name = next_arg(input, &input);
an_array *array; an_array *array;
long item = -1; long item = -1;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)) && *input)
{ {
if (*input) item = find_item(*array, input);
{ item = (item >= 0) ? array->index[item] : -2;
item = find_item(*array, input); }
item = (item >= 0) ? array->index[item] : -2;
}
}
RETURN_INT(item) RETURN_INT(item)
} }
@@ -762,18 +744,15 @@ BUILT_IN_FUNCTION(function_finditem)
*/ */
BUILT_IN_FUNCTION(function_ifinditem) BUILT_IN_FUNCTION(function_ifinditem)
{ {
char *name = NULL; char *name = next_arg(input, &input);
an_array *array; an_array *array;
long item = -1; long item = -1;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)) && *input)
{ {
if (*input) if ((item = find_item(*array, input)) < 0)
{ item = -2;
if ((item = find_item(*array, input)) < 0) }
item = -2;
}
}
RETURN_INT(item) RETURN_INT(item)
} }
@@ -784,21 +763,18 @@ BUILT_IN_FUNCTION(function_ifinditem)
*/ */
BUILT_IN_FUNCTION(function_igetitem) BUILT_IN_FUNCTION(function_igetitem)
{ {
char *name = NULL; char *name = next_arg(input, &input);
char *itemstr = NULL; char *itemstr = next_arg(input, &input);
long item; long item;
an_array *array; an_array *array;
char *found = NULL; char *found = NULL;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)) && itemstr)
{ {
if ((itemstr = next_arg(input, &input))) item = my_atol(itemstr);
{ if (item >= 0 && item < array->size)
item = my_atol(itemstr); found = array->item[array->index[item]];
if (item >= 0 && item < array->size) }
found = array->item[array->index[item]];
}
}
RETURN_STR(found) RETURN_STR(found)
} }
@@ -809,22 +785,22 @@ BUILT_IN_FUNCTION(function_igetitem)
*/ */
BUILT_IN_FUNCTION(function_indextoitem) BUILT_IN_FUNCTION(function_indextoitem)
{ {
char *name = NULL; char *name = next_arg(input, &input);
char *itemstr = NULL; char *itemstr = next_arg(input, &input);
long item; long item;
an_array *array; an_array *array;
long found = -1; long found = -1;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)))
{ {
found = -2; found = -2;
if ((itemstr = next_arg(input, &input))) if (itemstr)
{ {
item = my_atol(itemstr); item = my_atol(itemstr);
if (item >= 0 && item < array->size) if (item >= 0 && item < array->size)
found = array->index[item]; found = array->index[item];
} }
} }
RETURN_INT(found) RETURN_INT(found)
} }
@@ -835,22 +811,22 @@ BUILT_IN_FUNCTION(function_indextoitem)
*/ */
BUILT_IN_FUNCTION(function_itemtoindex) BUILT_IN_FUNCTION(function_itemtoindex)
{ {
char *name; char *name = next_arg(input, &input);
char *itemstr; char *itemstr = next_arg(input, &input);
long item; long item;
an_array *array; an_array *array;
long found = -1; long found = -1;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)))
{ {
found = -2; found = -2;
if ((itemstr = next_arg(input, &input))) if (itemstr)
{ {
item = my_atol(itemstr); item = my_atol(itemstr);
if (item >= 0 && item < array->size) if (item >= 0 && item < array->size)
found = find_index(array, item); found = find_index(array, item);
} }
} }
RETURN_INT(found) RETURN_INT(found)
} }
@@ -862,24 +838,25 @@ BUILT_IN_FUNCTION(function_itemtoindex)
*/ */
BUILT_IN_FUNCTION(function_delitem) BUILT_IN_FUNCTION(function_delitem)
{ {
char *name; char *name = next_arg(input, &input);
char *itemstr; char *itemstr = next_arg(input, &input);
char **strptr; char **strptr;
long item; long item;
long cnt; long cnt;
long oldindex; long oldindex;
an_array *array; an_array *array;
long found = -1; long found = -1;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)))
{ {
found = -2; found = -2;
if ((itemstr = next_arg(input, &input))) if (itemstr)
{ {
item = my_atol(itemstr); item = my_atol(itemstr);
if (item >= 0 && item < array->size) if (item >= 0 && item < array->size)
{ {
if (array->size == 1) found = 0;
if (array->size == 1)
delete_array(name); delete_array(name);
else else
{ {
@@ -888,17 +865,16 @@ BUILT_IN_FUNCTION(function_delitem)
if (array->index[cnt] > item) if (array->index[cnt] > item)
(array->index[cnt])--; (array->index[cnt])--;
move_index(array, oldindex, array->size); move_index(array, oldindex, array->size);
new_free(&array->item[item]); new_free(&array->item[item]);
array->size--; array->size--;
for(strptr=&(array->item[item]), cnt=item; cnt < array->size; cnt++, strptr++) for(strptr=&(array->item[item]), cnt=item; cnt < array->size; cnt++, strptr++)
*strptr = *(strptr + 1); *strptr = *(strptr + 1);
RESIZE(array->item, char *, array->size); RESIZE(array->item, char *, array->size);
RESIZE(array->index, long, array->size); RESIZE(array->index, long, array->size);
} }
found = 0; }
} }
} }
}
RETURN_INT(found) RETURN_INT(found)
} }
@@ -908,41 +884,38 @@ BUILT_IN_FUNCTION(function_delitem)
*/ */
BUILT_IN_FUNCTION(function_delarray) BUILT_IN_FUNCTION(function_delarray)
{ {
char *name; char *name = next_arg(input, &input);
long found = -1; long found = -1;
if ((name = next_arg(input, &input)) && (get_array(name))) if (name && get_array(name))
{ {
delete_array(name); delete_array(name);
found = 0; found = 0;
} }
RETURN_INT(found) RETURN_INT(found)
} }
/* /*
* function_ifindfirst() returns the first index of an exact match with the * function_ifindfirst() returns the first index of an exact match with the
* search string, or returns -2 if unable to find the array, or -1 if unable * search string, or returns -1 if unable to find the array, or -2 if unable
* to find any matches. * to find any matches.
*/ */
BUILT_IN_FUNCTION(function_ifindfirst) BUILT_IN_FUNCTION(function_ifindfirst)
{ {
char *name; char *name = next_arg(input, &input);
an_array *array; an_array *array;
long item = -1; long item = -1;
if ((name = next_arg(input, &input)) && (array = get_array(name))) if (name && (array = get_array(name)) && *input)
{ {
if (*input) if ((item = find_item(*array, input)) < 0)
item = -2;
else
{ {
if ((item = find_item(*array, input)) < 0) while (item >= 0 && !strcmp(array->item[array->index[item]], input))
item = -2; item--;
else item++;
{
while (item >= 0 && !strcmp(array->item[array->index[item]], input))
item--;
item++;
}
} }
} }
RETURN_INT(item) RETURN_INT(item)
} }
@@ -951,57 +924,50 @@ BUILT_IN_FUNCTION(function_ifindfirst)
* the elements within the array. This allows parsing using % and * * the elements within the array. This allows parsing using % and *
* for wildcards. We return only the best match from the array, unlike * for wildcards. We return only the best match from the array, unlike
* getmatch() which returns ALL the matching items. * getmatch() which returns ALL the matching items.
*
* <shade> gettmatch(users % user@host *) would match the userhost mask in the
* second word of the array
*/ */
BUILT_IN_FUNCTION(function_gettmatch) BUILT_IN_FUNCTION(function_gettmatch)
{ {
char *name; char *name = next_arg(input, &input);
an_array *array; an_array *array;
char *ret = NULL; char *ret = NULL;
#if 0
<shade> gettmatch(users % user@host *) would match the userhost mask in the
second word of the array
#endif
if ((name = next_arg(input, &input)) && (array = get_array(name)))
{
if (*input)
{
int index, current_match;
int best_match = 0;
int match = -1;
for (index = 0; index < array->size; index++)
{
if ((current_match = wild_match(input, array->item[index])) > best_match)
{
match = index;
best_match = current_match;
}
}
if (match != -1)
ret = array->item[match];
if (name && (array = get_array(name)) && *input)
{
int index, current_match;
int best_match = 0;
int match = -1;
for (index = 0; index < array->size; index++)
{
if ((current_match = wild_match(input, array->item[index])) > best_match)
{
match = index;
best_match = current_match;
}
} }
if (match != -1)
ret = array->item[match];
} }
RETURN_STR(ret); RETURN_STR(ret);
} }
BUILT_IN_FUNCTION(function_igetrmatches) BUILT_IN_FUNCTION(function_igetrmatches)
{ {
char *result = (char *) 0; char *result = NULL;
char *name = (char *) 0; char *name = next_arg(input, &input);
long item; long item;
an_array *array; an_array *array;
if ((name = next_arg(input, &input)) && if (name && (array = get_array(name)) && *input)
(array = get_array(name)) && *input)
{ {
if (*input) for (item = 0; item < array->size; item++)
{ {
for (item = 0; item < array->size; item++) if (wild_match(array->item[item], input) > 0)
{ m_s3cat(&result, space, ltoa(find_index(array, item)));
if (wild_match(array->item[item], input) > 0)
m_s3cat(&result, space, ltoa(find_index(array, item)));
}
} }
} }