2004-01-07 07:36:17 +00:00
|
|
|
/*
|
|
|
|
bi_string.c
|
|
|
|
|
|
|
|
QuakeC string api
|
|
|
|
|
|
|
|
Copyright (C) 2004 Bill Currie
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2004/1/6
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
2020-04-08 12:53:53 +00:00
|
|
|
#include <ctype.h>
|
2023-05-27 03:30:06 +00:00
|
|
|
#include <stdlib.h>
|
2004-01-07 07:36:17 +00:00
|
|
|
|
2013-01-22 05:09:41 +00:00
|
|
|
#include "qfalloca.h"
|
|
|
|
|
2011-08-25 13:35:20 +00:00
|
|
|
#if defined(_WIN32) && defined(HAVE_MALLOC_H)
|
2007-03-22 23:20:57 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2004-01-07 07:36:17 +00:00
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/progs.h"
|
2004-01-16 07:03:58 +00:00
|
|
|
|
|
|
|
#include "rua_internal.h"
|
2004-01-07 07:36:17 +00:00
|
|
|
|
2020-03-24 15:04:20 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_strlen (progs_t *pr, void *data)
|
2020-03-24 15:04:20 +00:00
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
s = P_GSTRING (pr, 0);
|
|
|
|
R_INT (pr) = strlen(s);
|
|
|
|
}
|
|
|
|
|
2022-02-01 00:24:02 +00:00
|
|
|
void
|
2022-02-05 11:24:17 +00:00
|
|
|
RUA_Sprintf (progs_t *pr, dstring_t *dstr, const char *func, int fmt_arg)
|
2020-03-24 15:04:20 +00:00
|
|
|
{
|
2022-02-05 11:24:17 +00:00
|
|
|
const char *fmt = P_GSTRING (pr, fmt_arg);
|
|
|
|
int count = pr->pr_argc - (fmt_arg + 1);
|
|
|
|
pr_type_t **args = pr->pr_params + (fmt_arg + 1);
|
2022-02-01 00:24:02 +00:00
|
|
|
|
|
|
|
if (pr->progs->version == PROG_VERSION) {
|
2022-02-05 11:24:17 +00:00
|
|
|
__auto_type va_list = &P_PACKED (pr, pr_va_list_t, (fmt_arg + 1));
|
2022-02-01 00:24:02 +00:00
|
|
|
count = va_list->count;
|
|
|
|
if (count) {
|
|
|
|
args = alloca (count * sizeof (pr_type_t *));
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
args[i] = &pr->pr_globals[va_list->list + i * 4];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
args = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:24:17 +00:00
|
|
|
PR_Sprintf (pr, dstr, func, fmt, count, args);
|
2022-02-01 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_sprintf (progs_t *pr, void *data)
|
2022-02-01 00:24:02 +00:00
|
|
|
{
|
2020-03-24 15:04:20 +00:00
|
|
|
dstring_t *dstr;
|
|
|
|
|
|
|
|
dstr = dstring_newstr ();
|
2022-02-05 11:24:17 +00:00
|
|
|
RUA_Sprintf (pr, dstr, "sprintf", 0);
|
2020-03-24 15:04:20 +00:00
|
|
|
RETURN_STRING (pr, dstr->str);
|
|
|
|
dstring_delete (dstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_vsprintf (progs_t *pr, void *data)
|
2020-03-24 15:04:20 +00:00
|
|
|
{
|
|
|
|
const char *fmt = P_GSTRING (pr, 0);
|
|
|
|
__auto_type args = &P_PACKED (pr, pr_va_list_t, 1);
|
|
|
|
pr_type_t *list_start = PR_GetPointer (pr, args->list);
|
|
|
|
pr_type_t **list = alloca (args->count * sizeof (*list));
|
|
|
|
dstring_t *dstr;
|
|
|
|
|
|
|
|
for (int i = 0; i < args->count; i++) {
|
|
|
|
list[i] = list_start + i * pr->pr_param_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
dstr = dstring_newstr ();
|
|
|
|
PR_Sprintf (pr, dstr, "bi_vsprintf", fmt, args->count, list);
|
|
|
|
RETURN_STRING (pr, dstr->str);
|
|
|
|
dstring_delete (dstr);
|
|
|
|
}
|
|
|
|
|
2004-01-07 07:36:17 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_new (progs_t *pr, void *data)
|
2004-01-07 07:36:17 +00:00
|
|
|
{
|
2004-11-11 09:40:00 +00:00
|
|
|
R_STRING (pr) = PR_NewMutableString (pr);
|
2004-01-07 07:36:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 03:30:06 +00:00
|
|
|
static void
|
|
|
|
bi_str_unmutable (progs_t *pr, void *data)
|
|
|
|
{
|
|
|
|
RETURN_STRING (pr, P_GSTRING (pr, 0));
|
|
|
|
}
|
|
|
|
|
2004-01-07 07:36:17 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_free (progs_t *pr, void *data)
|
2004-01-07 07:36:17 +00:00
|
|
|
{
|
|
|
|
PR_FreeString (pr, P_STRING (pr, 0));
|
|
|
|
}
|
|
|
|
|
2020-03-26 07:39:21 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_hold (progs_t *pr, void *data)
|
2020-03-26 07:39:21 +00:00
|
|
|
{
|
2022-01-18 03:11:14 +00:00
|
|
|
pr_string_t str = P_STRING (pr, 0);
|
2020-03-26 07:39:21 +00:00
|
|
|
PR_HoldString (pr, str);
|
|
|
|
R_STRING (pr) = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_valid (progs_t *pr, void *data)
|
2020-03-26 07:39:21 +00:00
|
|
|
{
|
|
|
|
R_INT (pr) = PR_StringValid (pr, P_STRING (pr, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_mutable (progs_t *pr, void *data)
|
2020-03-26 07:39:21 +00:00
|
|
|
{
|
|
|
|
R_INT (pr) = PR_StringMutable (pr, P_STRING (pr, 0));
|
|
|
|
}
|
|
|
|
|
2004-01-07 07:36:17 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_copy (progs_t *pr, void *data)
|
2004-01-07 07:36:17 +00:00
|
|
|
{
|
|
|
|
dstring_t *dst = P_DSTRING (pr, 0);
|
|
|
|
const char *src = P_GSTRING (pr, 1);
|
|
|
|
|
|
|
|
dstring_copystr (dst, src);
|
|
|
|
R_STRING (pr) = P_STRING (pr, 0);
|
|
|
|
}
|
|
|
|
|
2005-06-15 01:41:10 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_cat (progs_t *pr, void *data)
|
2005-06-15 01:41:10 +00:00
|
|
|
{
|
|
|
|
dstring_t *dst = P_DSTRING (pr, 0);
|
|
|
|
const char *src = P_GSTRING (pr, 1);
|
|
|
|
|
|
|
|
dstring_appendstr (dst, src);
|
|
|
|
R_STRING (pr) = P_STRING (pr, 0);
|
|
|
|
}
|
|
|
|
|
2004-01-07 07:36:17 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_clear (progs_t *pr, void *data)
|
2004-01-07 07:36:17 +00:00
|
|
|
{
|
|
|
|
dstring_t *str = P_DSTRING (pr, 0);
|
|
|
|
|
|
|
|
dstring_clearstr (str);
|
|
|
|
R_STRING (pr) = P_STRING (pr, 0);
|
|
|
|
}
|
|
|
|
|
2004-11-11 09:40:00 +00:00
|
|
|
static void
|
2022-02-04 02:38:36 +00:00
|
|
|
str_mid (progs_t *pr, const char *str, int pos, int end, int size)
|
2004-11-11 09:40:00 +00:00
|
|
|
{
|
|
|
|
char *temp;
|
|
|
|
|
|
|
|
R_STRING (pr) = 0;
|
|
|
|
if (pos < 0)
|
|
|
|
pos += size;
|
|
|
|
if (end < 0)
|
|
|
|
end += size;
|
|
|
|
if (end > size)
|
|
|
|
end = size;
|
|
|
|
if (pos < 0 || pos >= size || end <= pos)
|
|
|
|
return;
|
2020-02-19 05:40:01 +00:00
|
|
|
if (end == size) {
|
|
|
|
R_STRING (pr) = str + pos - pr->pr_strings;
|
|
|
|
return;
|
|
|
|
}
|
2004-11-11 09:40:00 +00:00
|
|
|
temp = alloca (end - pos + 1);
|
|
|
|
strncpy (temp, str + pos, end - pos);
|
|
|
|
temp[end - pos] = 0;
|
|
|
|
RETURN_STRING (pr, temp);
|
|
|
|
}
|
|
|
|
|
2022-02-04 02:38:36 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_mid_2 (progs_t *pr, void *data)
|
2022-02-04 02:38:36 +00:00
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
int pos = P_INT (pr, 1);
|
|
|
|
int size = strlen (str);
|
|
|
|
|
|
|
|
str_mid (pr, str, pos, size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_mid_3 (progs_t *pr, void *data)
|
2022-02-04 02:38:36 +00:00
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
int pos = P_INT (pr, 1);
|
|
|
|
int end = P_INT (pr, 2);
|
|
|
|
int size = strlen (str);
|
|
|
|
|
|
|
|
str_mid (pr, str, pos, end, size);
|
|
|
|
}
|
|
|
|
|
2004-11-11 09:40:00 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_str (progs_t *pr, void *data)
|
2004-11-11 09:40:00 +00:00
|
|
|
{
|
|
|
|
const char *haystack = P_GSTRING (pr, 0);
|
|
|
|
const char *needle = P_GSTRING (pr, 1);
|
|
|
|
char *res = strstr (haystack, needle);
|
|
|
|
|
2020-04-08 12:54:41 +00:00
|
|
|
R_INT (pr) = -1;
|
|
|
|
if (res) {
|
|
|
|
R_INT (pr) = res - haystack;
|
|
|
|
}
|
2004-11-11 09:40:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 09:16:55 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_char (progs_t *pr, void *data)
|
2020-03-10 09:16:55 +00:00
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
int ind = P_INT (pr, 1);
|
|
|
|
|
|
|
|
if (ind < 0) {
|
|
|
|
PR_RunError (pr, "negative index to str_char");
|
|
|
|
}
|
|
|
|
R_INT (pr) = str[ind];
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:28:25 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_quote (progs_t *pr, void *data)
|
2020-04-01 12:28:25 +00:00
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
// can have up to 4 chars per char (a -> \x61)
|
|
|
|
char *quote = alloca (strlen (str) * 4 + 1);
|
|
|
|
char *q = quote;
|
|
|
|
char c;
|
|
|
|
int h;
|
|
|
|
|
|
|
|
while ((c = *str++)) {
|
|
|
|
if (c >= ' ' && c < 127 && c != '\"') {
|
|
|
|
*q++ = c;
|
|
|
|
} else {
|
|
|
|
*q++ = '\\';
|
|
|
|
switch (c) {
|
|
|
|
case '\a': c = 'a'; break;
|
|
|
|
case '\b': c = 'b'; break;
|
|
|
|
case '\f': c = 'f'; break;
|
|
|
|
case '\n': c = 'n'; break;
|
|
|
|
case '\r': c = 'r'; break;
|
|
|
|
case '\t': c = 't'; break;
|
|
|
|
case '\"': c = '\"'; break;
|
|
|
|
default:
|
|
|
|
*q++ = 'x';
|
|
|
|
h = (c & 0xf0) >> 4;
|
|
|
|
*q++ = h > 9 ? h + 'a' - 10 : h + '0';
|
|
|
|
h = (c & 0x0f);
|
|
|
|
c = h > 9 ? h + 'a' - 10 : h + '0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*q++ = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q++ = 0;
|
|
|
|
|
|
|
|
RETURN_STRING (pr, quote);
|
|
|
|
}
|
|
|
|
|
2020-04-08 12:53:53 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_lower (progs_t *pr, void *data)
|
2020-04-08 12:53:53 +00:00
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
char *lower = alloca (strlen (str) + 1);
|
|
|
|
char *l = lower;
|
|
|
|
byte c;
|
|
|
|
|
|
|
|
while ((c = *str++)) {
|
|
|
|
*l++ = tolower (c);
|
|
|
|
}
|
|
|
|
*l++ = 0;
|
|
|
|
|
|
|
|
RETURN_STRING (pr, lower);
|
|
|
|
}
|
|
|
|
|
2021-01-05 09:24:18 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_str_upper (progs_t *pr, void *data)
|
2021-01-05 09:24:18 +00:00
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
char *upper = alloca (strlen (str) + 1);
|
|
|
|
char *l = upper;
|
|
|
|
byte c;
|
|
|
|
|
|
|
|
while ((c = *str++)) {
|
|
|
|
*l++ = toupper (c);
|
|
|
|
}
|
|
|
|
*l++ = 0;
|
|
|
|
|
|
|
|
RETURN_STRING (pr, upper);
|
|
|
|
}
|
|
|
|
|
2023-05-27 03:30:06 +00:00
|
|
|
static void
|
|
|
|
bi_strtod (progs_t *pr, void *data)
|
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
pr_type_t *end = P_GPOINTER (pr, 1);
|
|
|
|
char *end_ptr;
|
|
|
|
R_DOUBLE (pr) = strtod (str, &end_ptr);
|
|
|
|
if (end) {
|
|
|
|
end->value = end_ptr - str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_strtof (progs_t *pr, void *data)
|
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
pr_type_t *end = P_GPOINTER (pr, 1);
|
|
|
|
char *end_ptr;
|
|
|
|
R_FLOAT (pr) = strtof (str, &end_ptr);
|
|
|
|
if (end) {
|
|
|
|
end->value = end_ptr - str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_strtol (progs_t *pr, void *data)
|
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
pr_type_t *end = P_GPOINTER (pr, 1);
|
|
|
|
char *end_ptr;
|
|
|
|
R_LONG (pr) = strtol (str, &end_ptr, P_INT (pr, 2));
|
|
|
|
if (end) {
|
|
|
|
end->value = end_ptr - str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_strtoul (progs_t *pr, void *data)
|
|
|
|
{
|
|
|
|
const char *str = P_GSTRING (pr, 0);
|
|
|
|
pr_type_t *end = P_GPOINTER (pr, 1);
|
|
|
|
char *end_ptr;
|
|
|
|
R_ULONG (pr) = strtoul (str, &end_ptr, P_INT (pr, 2));
|
|
|
|
if (end) {
|
|
|
|
end->value = end_ptr - str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 12:54:03 +00:00
|
|
|
#define bi(x,np,params...) {#x, bi_##x, -1, np, {params}}
|
|
|
|
#define p(type) PR_PARAM(type)
|
|
|
|
#define P(a, s) { .size = (s), .alignment = BITOP_LOG2 (a), }
|
2004-01-07 07:36:17 +00:00
|
|
|
static builtin_t builtins[] = {
|
2022-01-23 12:54:03 +00:00
|
|
|
bi(strlen, 1, p(string)),
|
|
|
|
bi(sprintf, -2, p(string)),
|
|
|
|
bi(vsprintf, 2, p(string), P(1, 2)),
|
|
|
|
bi(str_new, 0),
|
2023-05-27 03:30:06 +00:00
|
|
|
bi(str_unmutable, 1, p(string)),
|
2022-01-23 12:54:03 +00:00
|
|
|
bi(str_free, 1, p(string)),
|
|
|
|
bi(str_hold, 1, p(string)),
|
|
|
|
bi(str_valid, 1, p(string)),
|
|
|
|
bi(str_mutable, 1, p(string)),
|
|
|
|
bi(str_copy, 2, p(string), p(string)),
|
|
|
|
bi(str_cat, 2, p(string), p(string)),
|
|
|
|
bi(str_clear, 1, p(string)),
|
2022-02-04 02:38:36 +00:00
|
|
|
{"str_mid|*i", bi_str_mid_2, -1, 2, {p(string), p(int)}},
|
|
|
|
{"str_mid|*ii", bi_str_mid_3, -1, 3, {p(string), p(int), p(int)}},
|
2022-01-23 12:54:03 +00:00
|
|
|
bi(str_str, 2, p(string), p(string)),
|
|
|
|
bi(str_char, 2, p(string), p(int)),
|
|
|
|
bi(str_quote, 1, p(string)),
|
|
|
|
bi(str_lower, 1, p(string)),
|
|
|
|
bi(str_upper, 1, p(string)),
|
2023-05-27 03:30:06 +00:00
|
|
|
bi(strtod, 1, p(string)),
|
|
|
|
bi(strtof, 1, p(string)),
|
|
|
|
bi(strtol, 1, p(string)),
|
|
|
|
bi(strtoul, 1, p(string)),
|
2004-01-07 07:36:17 +00:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2004-01-16 07:03:58 +00:00
|
|
|
RUA_String_Init (progs_t *pr, int secure)
|
2004-01-07 07:36:17 +00:00
|
|
|
{
|
2022-01-23 15:20:05 +00:00
|
|
|
PR_RegisterBuiltins (pr, builtins, 0);
|
2004-01-07 07:36:17 +00:00
|
|
|
}
|