2001-03-06 04:22:33 +00:00
|
|
|
/*
|
|
|
|
pr_strings.c
|
|
|
|
|
|
|
|
progs string managment
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2001-03-06 04:22:33 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
#include <ctype.h>
|
2001-03-06 04:22:33 +00:00
|
|
|
#include <stdarg.h>
|
2001-09-10 12:56:23 +00:00
|
|
|
#include <stdlib.h>
|
2001-03-06 04:22:33 +00:00
|
|
|
|
2004-01-03 08:43:57 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/hash.h"
|
|
|
|
#include "QF/progs.h"
|
2001-03-06 04:22:33 +00:00
|
|
|
|
2020-02-26 09:19:21 +00:00
|
|
|
// format adjustments
|
|
|
|
#define FMT_ALTFORM (1<<0)
|
|
|
|
#define FMT_LJUSTIFY (1<<1)
|
|
|
|
#define FMT_ZEROPAD (1<<2)
|
|
|
|
#define FMT_ADDSIGN (1<<3)
|
|
|
|
#define FMT_ADDBLANK (1<<4)
|
|
|
|
#define FMT_HEX (1<<5)
|
|
|
|
#define FMT_LONG (1<<6)
|
|
|
|
|
|
|
|
typedef struct fmt_item_s {
|
|
|
|
byte type;
|
|
|
|
unsigned flags;
|
|
|
|
int minFieldWidth;
|
|
|
|
int precision;
|
|
|
|
union {
|
|
|
|
const char *string_var;
|
|
|
|
int integer_var;
|
|
|
|
unsigned uinteger_var;
|
|
|
|
float float_var;
|
|
|
|
double double_var;
|
|
|
|
} data;
|
|
|
|
struct fmt_item_s *next;
|
|
|
|
} fmt_item_t;
|
|
|
|
|
2020-02-26 04:43:03 +00:00
|
|
|
typedef struct strref_slot_s {
|
|
|
|
struct strref_slot_s *next;
|
|
|
|
struct strref_slot_s **prev;
|
|
|
|
strref_t *strref;
|
|
|
|
} strref_slot_t;
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
typedef struct prstr_resources_s {
|
|
|
|
progs_t *pr;
|
|
|
|
dstring_mem_t ds_mem;
|
|
|
|
strref_t *free_string_refs;
|
|
|
|
strref_t *static_strings;
|
|
|
|
strref_t **string_map;
|
2020-02-26 04:43:03 +00:00
|
|
|
strref_slot_t return_strings[PR_RS_SLOTS];
|
|
|
|
strref_slot_t *rs_slot;
|
2020-02-24 15:21:56 +00:00
|
|
|
unsigned dyn_str_size;
|
|
|
|
struct hashtab_s *strref_hash;
|
|
|
|
int num_strings;
|
2020-02-26 09:19:21 +00:00
|
|
|
fmt_item_t *free_fmt_items;
|
2020-02-26 09:35:19 +00:00
|
|
|
dstring_t *print_str;
|
2020-02-24 15:21:56 +00:00
|
|
|
} prstr_resources_t;
|
|
|
|
|
2004-11-11 09:40:00 +00:00
|
|
|
typedef enum {
|
2005-04-30 08:37:55 +00:00
|
|
|
str_free,
|
2004-11-11 09:40:00 +00:00
|
|
|
str_static,
|
|
|
|
str_dynamic,
|
|
|
|
str_mutable,
|
|
|
|
str_temp,
|
|
|
|
str_return,
|
|
|
|
} str_e;
|
|
|
|
|
2004-01-05 07:10:32 +00:00
|
|
|
struct strref_s {
|
|
|
|
strref_t *next;
|
2020-02-26 04:43:03 +00:00
|
|
|
strref_slot_t *rs_slot;
|
2004-11-11 09:40:00 +00:00
|
|
|
str_e type;
|
|
|
|
union {
|
|
|
|
char *string;
|
|
|
|
dstring_t *dstring;
|
|
|
|
} s;
|
2004-01-05 07:10:32 +00:00
|
|
|
};
|
|
|
|
|
2004-01-03 08:43:57 +00:00
|
|
|
static void *
|
|
|
|
pr_strings_alloc (void *_pr, size_t size)
|
|
|
|
{
|
|
|
|
progs_t *pr = (progs_t *) _pr;
|
|
|
|
return PR_Zone_Malloc (pr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pr_strings_free (void *_pr, void *ptr)
|
|
|
|
{
|
|
|
|
progs_t *pr = (progs_t *) _pr;
|
|
|
|
PR_Zone_Free (pr, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
pr_strings_realloc (void *_pr, void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
progs_t *pr = (progs_t *) _pr;
|
|
|
|
return PR_Zone_Realloc (pr, ptr, size);
|
|
|
|
}
|
2001-09-10 12:56:23 +00:00
|
|
|
|
2001-08-13 20:01:37 +00:00
|
|
|
static strref_t *
|
2020-02-24 15:21:56 +00:00
|
|
|
new_string_ref (prstr_resources_t *res)
|
2001-08-13 20:01:37 +00:00
|
|
|
{
|
|
|
|
strref_t *sr;
|
2020-02-24 15:21:56 +00:00
|
|
|
if (!res->free_string_refs) {
|
2007-04-08 01:22:00 +00:00
|
|
|
int i;
|
|
|
|
size_t size;
|
2001-09-10 12:56:23 +00:00
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
res->dyn_str_size++;
|
|
|
|
size = res->dyn_str_size * sizeof (strref_t *);
|
|
|
|
res->string_map = realloc (res->string_map, size);
|
|
|
|
if (!res->string_map)
|
|
|
|
PR_Error (res->pr, "out of memory");
|
|
|
|
if (!(res->free_string_refs = calloc (1024, sizeof (strref_t))))
|
|
|
|
PR_Error (res->pr, "out of memory");
|
|
|
|
res->string_map[res->dyn_str_size - 1] = res->free_string_refs;
|
|
|
|
for (i = 0, sr = res->free_string_refs; i < 1023; i++, sr++)
|
2001-08-13 20:01:37 +00:00
|
|
|
sr->next = sr + 1;
|
|
|
|
sr->next = 0;
|
|
|
|
}
|
2020-02-24 15:21:56 +00:00
|
|
|
sr = res->free_string_refs;
|
|
|
|
res->free_string_refs = sr->next;
|
2001-08-13 20:01:37 +00:00
|
|
|
sr->next = 0;
|
2020-02-26 04:43:03 +00:00
|
|
|
sr->rs_slot = 0;
|
2001-08-13 20:01:37 +00:00
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-24 15:21:56 +00:00
|
|
|
free_string_ref (prstr_resources_t *res, strref_t *sr)
|
2001-08-13 20:01:37 +00:00
|
|
|
{
|
2005-05-01 00:01:28 +00:00
|
|
|
sr->type = str_free;
|
2020-02-24 15:21:56 +00:00
|
|
|
sr->next = res->free_string_refs;
|
|
|
|
res->free_string_refs = sr;
|
2001-08-13 20:01:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 03:35:01 +00:00
|
|
|
static __attribute__((pure)) string_t
|
2020-02-24 15:21:56 +00:00
|
|
|
string_index (prstr_resources_t *res, strref_t *sr)
|
2001-08-13 20:01:37 +00:00
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
long o = (long) (sr - res->static_strings);
|
2007-04-08 01:22:00 +00:00
|
|
|
unsigned i;
|
2001-08-13 20:01:37 +00:00
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
if (o >= 0 && o < res->num_strings)
|
|
|
|
return sr->s.string - res->pr->pr_strings;
|
|
|
|
for (i = 0; i < res->dyn_str_size; i++) {
|
|
|
|
int d = sr - res->string_map[i];
|
2001-08-13 20:01:37 +00:00
|
|
|
if (d >= 0 && d < 1024)
|
2001-08-15 06:01:38 +00:00
|
|
|
return ~(i * 1024 + d);
|
2001-08-13 20:01:37 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
strref_get_key (const void *_sr, void *notused)
|
2001-03-06 04:22:33 +00:00
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
strref_t *sr = (strref_t*)_sr;
|
|
|
|
|
2004-11-11 09:40:00 +00:00
|
|
|
// only static strings will ever be in the hash table
|
|
|
|
return sr->s.string;
|
2001-03-06 04:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-24 15:21:56 +00:00
|
|
|
strref_free (void *_sr, void *_res)
|
2001-03-06 04:22:33 +00:00
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
__auto_type res = (prstr_resources_t *) _res;
|
|
|
|
__auto_type sr = (strref_t *) _sr;
|
2001-03-06 04:22:33 +00:00
|
|
|
|
2010-01-13 06:42:26 +00:00
|
|
|
// Since this is called only by Hash_FlushTable, the memory pointed
|
2004-01-04 02:03:30 +00:00
|
|
|
// to by sr->string or sr->dstring has already been lost in the progs
|
|
|
|
// load/reload and thus there's no need to free it.
|
|
|
|
|
2001-03-06 04:22:33 +00:00
|
|
|
// free the string and ref only if it's not a static string
|
2020-02-24 15:21:56 +00:00
|
|
|
if (sr < res->static_strings
|
|
|
|
|| sr >= res->static_strings + res->num_strings) {
|
|
|
|
free_string_ref (res, sr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pr_strings_clear (progs_t *pr, void *data)
|
|
|
|
{
|
|
|
|
__auto_type res = (prstr_resources_t *) data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PR_RS_SLOTS; i++) {
|
2020-02-26 04:43:03 +00:00
|
|
|
if (res->return_strings[i].strref)
|
|
|
|
free_string_ref (res, res->return_strings[i].strref);
|
|
|
|
res->return_strings[i].strref = 0;
|
|
|
|
}
|
|
|
|
if (!res->rs_slot) {
|
|
|
|
strref_slot_t * const rs = res->return_strings;
|
|
|
|
for (i = 0; i < PR_RS_SLOTS; i++) {
|
|
|
|
rs[i].next = &rs[(i + 1) % PR_RS_SLOTS];
|
|
|
|
rs[i].prev = &rs[(i - 1 + PR_RS_SLOTS) % PR_RS_SLOTS].next;
|
|
|
|
}
|
|
|
|
res->rs_slot = rs;
|
2001-03-06 04:22:33 +00:00
|
|
|
}
|
2020-02-24 15:21:56 +00:00
|
|
|
|
|
|
|
pr->pr_string_resources = res;
|
|
|
|
pr->pr_xtstr = 0;
|
2001-03-06 04:22:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
static int
|
2001-03-06 04:22:33 +00:00
|
|
|
PR_LoadStrings (progs_t *pr)
|
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
prstr_resources_t *res = PR_Resources_Find (pr, "Strings");
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
char *end = pr->pr_strings + pr->progs->numstrings;
|
2001-09-10 12:56:23 +00:00
|
|
|
char *str = pr->pr_strings;
|
|
|
|
int count = 0;
|
2001-03-06 04:22:33 +00:00
|
|
|
|
2020-03-08 10:17:24 +00:00
|
|
|
pr->float_promoted = 0;
|
|
|
|
|
2001-03-06 04:22:33 +00:00
|
|
|
while (str < end) {
|
|
|
|
count++;
|
2020-03-08 10:17:24 +00:00
|
|
|
if (*str == '@' && pr->progs->version == PROG_VERSION) {
|
|
|
|
if (!strcmp (str, "@float_promoted@")) {
|
|
|
|
pr->float_promoted = 1;
|
|
|
|
}
|
|
|
|
}
|
2001-03-06 04:22:33 +00:00
|
|
|
str += strlen (str) + 1;
|
|
|
|
}
|
2004-01-03 08:43:57 +00:00
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
res->ds_mem.alloc = pr_strings_alloc;
|
|
|
|
res->ds_mem.free = pr_strings_free;
|
|
|
|
res->ds_mem.realloc = pr_strings_realloc;
|
|
|
|
res->ds_mem.data = pr;
|
|
|
|
|
|
|
|
if (res->strref_hash) {
|
|
|
|
Hash_FlushTable (res->strref_hash);
|
2001-03-06 04:22:33 +00:00
|
|
|
} else {
|
2020-02-24 15:21:56 +00:00
|
|
|
res->strref_hash = Hash_NewTable (1021, strref_get_key, strref_free,
|
2020-03-25 06:43:16 +00:00
|
|
|
res, pr->hashlink_freelist);
|
2020-02-24 15:21:56 +00:00
|
|
|
res->string_map = 0;
|
|
|
|
res->free_string_refs = 0;
|
|
|
|
res->dyn_str_size = 0;
|
2001-03-06 04:22:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
if (res->static_strings)
|
|
|
|
free (res->static_strings);
|
2020-03-08 12:29:31 +00:00
|
|
|
res->static_strings = calloc (count, sizeof (strref_t));
|
2001-03-06 04:22:33 +00:00
|
|
|
count = 0;
|
|
|
|
str = pr->pr_strings;
|
|
|
|
while (str < end) {
|
2020-02-24 15:21:56 +00:00
|
|
|
if (!Hash_Find (res->strref_hash, str)) {
|
|
|
|
res->static_strings[count].type = str_static;
|
|
|
|
res->static_strings[count].s.string = str;
|
|
|
|
Hash_Add (res->strref_hash, &res->static_strings[count]);
|
2004-11-02 04:59:00 +00:00
|
|
|
count++;
|
|
|
|
}
|
2001-03-06 04:22:33 +00:00
|
|
|
str += strlen (str) + 1;
|
|
|
|
}
|
2020-02-24 15:21:56 +00:00
|
|
|
res->num_strings = count;
|
2003-11-20 07:46:56 +00:00
|
|
|
return 1;
|
2001-03-06 04:22:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 04:43:03 +00:00
|
|
|
static void
|
|
|
|
requeue_strref (prstr_resources_t *res, strref_t *sr)
|
|
|
|
{
|
|
|
|
strref_slot_t *rs_slot = sr->rs_slot;
|
|
|
|
if (rs_slot->next != res->rs_slot) {
|
|
|
|
// this is the oldest slot, so advance res->rs_slot to the
|
|
|
|
// next oldest slot so this slot does not get reused just yet
|
|
|
|
if (res->rs_slot == rs_slot) {
|
|
|
|
res->rs_slot = rs_slot->next;
|
|
|
|
}
|
|
|
|
// unlink this slot from the chain
|
|
|
|
rs_slot->next->prev = rs_slot->prev;
|
|
|
|
*rs_slot->prev = rs_slot->next;
|
|
|
|
// link this slot just before the oldest slot: all the slots
|
|
|
|
// form a doubly linked circular list
|
|
|
|
rs_slot->prev = res->rs_slot->prev;
|
|
|
|
rs_slot->next = res->rs_slot;
|
|
|
|
*res->rs_slot->prev = rs_slot;
|
|
|
|
res->rs_slot->prev = &rs_slot->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-03 08:43:57 +00:00
|
|
|
static inline strref_t *
|
2020-02-24 15:21:56 +00:00
|
|
|
get_strref (prstr_resources_t *res, string_t num)
|
2001-03-06 04:22:33 +00:00
|
|
|
{
|
2003-04-17 00:01:48 +00:00
|
|
|
if (num < 0) {
|
2005-04-30 08:37:55 +00:00
|
|
|
strref_t *ref;
|
2007-04-08 01:22:00 +00:00
|
|
|
unsigned row = ~num / 1024;
|
2001-09-10 12:56:23 +00:00
|
|
|
|
2001-08-15 06:01:38 +00:00
|
|
|
num = ~num % 1024;
|
2001-08-13 20:01:37 +00:00
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
if (row >= res->dyn_str_size)
|
2002-10-29 05:07:10 +00:00
|
|
|
return 0;
|
2020-02-24 15:21:56 +00:00
|
|
|
ref = &res->string_map[row][num];
|
2005-04-30 08:37:55 +00:00
|
|
|
if (ref->type == str_free)
|
|
|
|
return 0;
|
|
|
|
return ref;
|
2004-01-03 08:43:57 +00:00
|
|
|
}
|
2005-04-30 08:37:55 +00:00
|
|
|
return 0;
|
2004-01-03 08:43:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 03:35:01 +00:00
|
|
|
static inline __attribute__((pure)) const char *
|
2007-04-08 01:22:00 +00:00
|
|
|
get_string (progs_t *pr, string_t num)
|
2004-01-03 08:43:57 +00:00
|
|
|
{
|
2020-02-26 04:43:03 +00:00
|
|
|
__auto_type res = pr->pr_string_resources;
|
2004-01-03 08:43:57 +00:00
|
|
|
if (num < 0) {
|
2020-02-26 04:43:03 +00:00
|
|
|
strref_t *ref = get_strref (res, num);
|
2004-01-03 08:43:57 +00:00
|
|
|
if (!ref)
|
|
|
|
return 0;
|
2004-11-11 09:40:00 +00:00
|
|
|
switch (ref->type) {
|
2020-02-26 04:43:03 +00:00
|
|
|
case str_return:
|
|
|
|
requeue_strref (res, ref);
|
2004-11-11 09:40:00 +00:00
|
|
|
case str_static:
|
|
|
|
case str_temp:
|
|
|
|
case str_dynamic:
|
|
|
|
return ref->s.string;
|
|
|
|
case str_mutable:
|
|
|
|
return ref->s.dstring->str;
|
2005-04-30 08:37:55 +00:00
|
|
|
case str_free:
|
|
|
|
break;
|
2004-11-11 09:40:00 +00:00
|
|
|
}
|
2020-03-08 12:29:31 +00:00
|
|
|
PR_Error (pr, "internal string error: %d", __LINE__);
|
2001-08-13 20:01:37 +00:00
|
|
|
} else {
|
2001-09-28 23:34:49 +00:00
|
|
|
if (num >= pr->pr_stringsize)
|
2002-10-29 05:07:10 +00:00
|
|
|
return 0;
|
2001-08-13 20:01:37 +00:00
|
|
|
return pr->pr_strings + num;
|
|
|
|
}
|
2002-10-29 05:07:10 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE qboolean
|
2007-04-06 00:47:41 +00:00
|
|
|
PR_StringValid (progs_t *pr, string_t num)
|
2002-10-29 05:07:10 +00:00
|
|
|
{
|
2020-02-26 17:07:53 +00:00
|
|
|
if (num >= 0) {
|
|
|
|
return num < pr->pr_stringsize;
|
|
|
|
}
|
2020-02-26 04:43:03 +00:00
|
|
|
return get_strref (pr->pr_string_resources, num) != 0;
|
2002-10-29 05:07:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 07:38:09 +00:00
|
|
|
VISIBLE qboolean
|
|
|
|
PR_StringMutable (progs_t *pr, string_t num)
|
|
|
|
{
|
|
|
|
strref_t *sr;
|
|
|
|
if (num >= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sr = get_strref (pr->pr_string_resources, num);
|
|
|
|
return sr && sr->type == str_mutable;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
2007-04-06 00:47:41 +00:00
|
|
|
PR_GetString (progs_t *pr, string_t num)
|
2002-10-29 05:07:10 +00:00
|
|
|
{
|
2004-01-03 08:43:57 +00:00
|
|
|
const char *str;
|
2001-09-28 23:34:49 +00:00
|
|
|
|
2002-10-29 05:07:10 +00:00
|
|
|
str = get_string (pr, num);
|
|
|
|
if (str)
|
|
|
|
return str;
|
2004-01-03 08:43:57 +00:00
|
|
|
PR_RunError (pr, "Invalid string offset %d", num);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE dstring_t *
|
2007-04-06 00:47:41 +00:00
|
|
|
PR_GetMutableString (progs_t *pr, string_t num)
|
2004-01-03 08:43:57 +00:00
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
strref_t *ref = get_strref (pr->pr_string_resources, num);
|
2004-01-03 08:43:57 +00:00
|
|
|
if (ref) {
|
2004-11-11 09:40:00 +00:00
|
|
|
if (ref->type == str_mutable)
|
|
|
|
return ref->s.dstring;
|
2004-01-03 08:43:57 +00:00
|
|
|
PR_RunError (pr, "not a dstring: %d", num);
|
|
|
|
}
|
|
|
|
PR_RunError (pr, "Invalid string offset: %d", num);
|
|
|
|
}
|
|
|
|
|
2020-03-09 13:16:19 +00:00
|
|
|
static inline void *
|
|
|
|
pr_strmalloc (progs_t *pr, size_t size)
|
|
|
|
{
|
|
|
|
return PR_Zone_Malloc (pr, size);
|
|
|
|
}
|
|
|
|
|
2004-11-02 04:59:00 +00:00
|
|
|
static inline char *
|
2007-04-08 01:22:00 +00:00
|
|
|
pr_stralloc (progs_t *pr, size_t len)
|
2004-11-02 04:59:00 +00:00
|
|
|
{
|
2020-03-09 13:16:19 +00:00
|
|
|
return pr_strmalloc (pr, len + 1);
|
2004-11-02 04:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
pr_strfree (progs_t *pr, char *s)
|
|
|
|
{
|
|
|
|
PR_Zone_Free (pr, s);
|
|
|
|
}
|
|
|
|
|
2004-01-03 08:43:57 +00:00
|
|
|
static inline char *
|
|
|
|
pr_strdup (progs_t *pr, const char *s)
|
|
|
|
{
|
2004-11-11 09:40:00 +00:00
|
|
|
char *new = pr_stralloc (pr, strlen (s));
|
2004-01-03 08:43:57 +00:00
|
|
|
strcpy (new, s);
|
|
|
|
return new;
|
2001-03-06 04:22:33 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 00:47:41 +00:00
|
|
|
VISIBLE string_t
|
2001-07-15 07:04:17 +00:00
|
|
|
PR_SetString (progs_t *pr, const char *s)
|
2001-03-06 04:22:33 +00:00
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
2004-11-02 04:59:00 +00:00
|
|
|
strref_t *sr;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2004-11-02 04:59:00 +00:00
|
|
|
if (!s)
|
|
|
|
s = "";
|
2020-02-24 15:21:56 +00:00
|
|
|
sr = Hash_Find (res->strref_hash, s);
|
2001-03-06 04:22:33 +00:00
|
|
|
|
2008-07-19 05:40:57 +00:00
|
|
|
if (__builtin_expect (!sr, 1)) {
|
2020-02-24 15:21:56 +00:00
|
|
|
sr = new_string_ref (res);
|
2004-11-11 09:40:00 +00:00
|
|
|
sr->type = str_static;
|
|
|
|
sr->s.string = pr_strdup(pr, s);
|
2020-02-24 15:21:56 +00:00
|
|
|
Hash_Add (res->strref_hash, sr);
|
2004-01-18 07:50:50 +00:00
|
|
|
}
|
2020-02-24 15:21:56 +00:00
|
|
|
return string_index (res, sr);
|
2004-01-07 05:22:57 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 00:47:41 +00:00
|
|
|
VISIBLE string_t
|
2004-01-07 05:22:57 +00:00
|
|
|
PR_SetReturnString (progs_t *pr, const char *s)
|
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
2004-01-07 05:22:57 +00:00
|
|
|
strref_t *sr;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
s = "";
|
2020-02-24 15:21:56 +00:00
|
|
|
if ((sr = Hash_Find (res->strref_hash, s))) {
|
2020-02-26 04:43:03 +00:00
|
|
|
if (sr->type == str_return && sr->rs_slot) {
|
|
|
|
requeue_strref (res, sr);
|
|
|
|
} else if ((sr->type == str_return && !sr->rs_slot)
|
|
|
|
|| (sr->type != str_return && sr->rs_slot)) {
|
2020-03-08 12:29:31 +00:00
|
|
|
PR_Error (pr, "internal string error: %d %d %p", __LINE__,
|
|
|
|
sr->type, sr->rs_slot);
|
2020-02-26 04:43:03 +00:00
|
|
|
}
|
2020-02-24 15:21:56 +00:00
|
|
|
return string_index (res, sr);
|
2004-01-07 05:22:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 04:43:03 +00:00
|
|
|
// grab the string ref from the oldest slot, or make a new one if the
|
2020-03-26 07:36:29 +00:00
|
|
|
// slot is empty or the string has been held
|
|
|
|
if ((sr = res->rs_slot->strref) && sr->type != str_dynamic) {
|
2020-02-26 04:43:03 +00:00
|
|
|
if (sr->type != str_return || sr->rs_slot != res->rs_slot) {
|
2020-03-08 12:29:31 +00:00
|
|
|
PR_Error (pr, "internal string error: %d", __LINE__);
|
2020-02-26 04:43:03 +00:00
|
|
|
}
|
2004-11-11 09:40:00 +00:00
|
|
|
pr_strfree (pr, sr->s.string);
|
2004-01-07 05:22:57 +00:00
|
|
|
} else {
|
2020-02-24 15:21:56 +00:00
|
|
|
sr = new_string_ref (res);
|
2020-03-31 11:38:41 +00:00
|
|
|
res->rs_slot->strref = sr;
|
2004-01-07 05:22:57 +00:00
|
|
|
}
|
2004-11-11 09:40:00 +00:00
|
|
|
sr->type = str_return;
|
2020-02-26 04:43:03 +00:00
|
|
|
sr->rs_slot = res->rs_slot;
|
2004-11-11 09:40:00 +00:00
|
|
|
sr->s.string = pr_strdup(pr, s);
|
2004-01-07 05:22:57 +00:00
|
|
|
|
2020-02-26 04:43:03 +00:00
|
|
|
// the oldest slot just became the newest, so advance to the next oldest
|
|
|
|
res->rs_slot = res->rs_slot->next;
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
return string_index (res, sr);
|
2004-01-07 05:22:57 +00:00
|
|
|
}
|
|
|
|
|
2007-04-08 01:22:00 +00:00
|
|
|
static inline string_t
|
2020-02-24 15:21:56 +00:00
|
|
|
pr_settempstring (progs_t *pr, prstr_resources_t *res, char *s)
|
2004-11-02 04:59:00 +00:00
|
|
|
{
|
|
|
|
strref_t *sr;
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
sr = new_string_ref (res);
|
2004-11-02 04:59:00 +00:00
|
|
|
sr->type = str_temp;
|
|
|
|
sr->s.string = s;
|
|
|
|
sr->next = pr->pr_xtstr;
|
|
|
|
pr->pr_xtstr = sr;
|
2020-02-24 15:21:56 +00:00
|
|
|
return string_index (res, sr);
|
2004-11-02 04:59:00 +00:00
|
|
|
}
|
|
|
|
|
2007-04-08 00:00:35 +00:00
|
|
|
VISIBLE string_t
|
2004-11-02 04:59:00 +00:00
|
|
|
PR_CatStrings (progs_t *pr, const char *a, const char *b)
|
|
|
|
{
|
2007-04-08 01:22:00 +00:00
|
|
|
size_t lena;
|
2004-11-02 04:59:00 +00:00
|
|
|
char *c;
|
|
|
|
|
|
|
|
lena = strlen (a);
|
|
|
|
|
|
|
|
c = pr_stralloc (pr, lena + strlen (b));
|
|
|
|
|
|
|
|
strcpy (c, a);
|
|
|
|
strcpy (c + lena, b);
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
return pr_settempstring (pr, pr->pr_string_resources, c);
|
2004-11-02 04:59:00 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 00:47:41 +00:00
|
|
|
VISIBLE string_t
|
2004-01-03 08:43:57 +00:00
|
|
|
PR_SetTempString (progs_t *pr, const char *s)
|
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
2004-01-03 08:43:57 +00:00
|
|
|
strref_t *sr;
|
|
|
|
|
2004-01-05 07:10:32 +00:00
|
|
|
if (!s)
|
|
|
|
return PR_SetString (pr, "");
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
if ((sr = Hash_Find (res->strref_hash, s))) {
|
|
|
|
return string_index (res, sr);
|
2004-11-11 09:40:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
return pr_settempstring (pr, res, pr_strdup (pr, s));
|
2004-11-11 09:40:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 13:16:19 +00:00
|
|
|
VISIBLE string_t
|
|
|
|
PR_AllocTempBlock (progs_t *pr, size_t size)
|
|
|
|
{
|
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
|
|
|
return pr_settempstring (pr, res, pr_strmalloc (pr, size));
|
|
|
|
}
|
|
|
|
|
2020-03-09 14:36:09 +00:00
|
|
|
VISIBLE void
|
|
|
|
PR_PushTempString (progs_t *pr, string_t num)
|
|
|
|
{
|
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
|
|
|
strref_t *ref = get_strref (res, num);
|
|
|
|
strref_t **temp_ref;
|
|
|
|
|
|
|
|
if (!ref || ref->type != str_temp) {
|
|
|
|
PR_Error (pr, "attempt to push a non-temp string");
|
|
|
|
}
|
|
|
|
for (temp_ref = &pr->pr_xtstr; *temp_ref; temp_ref = &(*temp_ref)->next) {
|
|
|
|
if (*temp_ref == ref) {
|
|
|
|
*temp_ref = ref->next;
|
|
|
|
ref->next = pr->pr_pushtstr;
|
|
|
|
pr->pr_pushtstr = ref;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PR_Error (pr, "attempt to push stale temp string");
|
|
|
|
}
|
|
|
|
|
2007-04-06 00:47:41 +00:00
|
|
|
VISIBLE string_t
|
2004-11-11 09:40:00 +00:00
|
|
|
PR_SetDynamicString (progs_t *pr, const char *s)
|
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
2004-11-11 09:40:00 +00:00
|
|
|
strref_t *sr;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return PR_SetString (pr, "");
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
if ((sr = Hash_Find (res->strref_hash, s))) {
|
|
|
|
return string_index (res, sr);
|
2004-11-11 09:40:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 15:21:56 +00:00
|
|
|
sr = new_string_ref (res);
|
2004-11-11 09:40:00 +00:00
|
|
|
sr->type = str_dynamic;
|
|
|
|
sr->s.string = pr_strdup (pr, s);
|
2020-02-24 15:21:56 +00:00
|
|
|
return string_index (res, sr);
|
2004-01-03 08:43:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 06:36:46 +00:00
|
|
|
VISIBLE void
|
2007-04-06 00:47:41 +00:00
|
|
|
PR_MakeTempString (progs_t *pr, string_t str)
|
2004-01-04 07:42:43 +00:00
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
|
|
|
strref_t *sr = get_strref (res, str);
|
2004-01-04 07:42:43 +00:00
|
|
|
|
|
|
|
if (!sr)
|
|
|
|
PR_RunError (pr, "invalid string %d", str);
|
2004-11-11 09:40:00 +00:00
|
|
|
if (sr->type != str_mutable)
|
|
|
|
PR_RunError (pr, "not a dstring: %d", str);
|
|
|
|
if (sr->s.dstring->str) {
|
|
|
|
sr->s.string = dstring_freeze (sr->s.dstring);
|
|
|
|
} else {
|
|
|
|
dstring_delete (sr->s.dstring);
|
2004-01-04 07:42:43 +00:00
|
|
|
}
|
2004-11-11 09:40:00 +00:00
|
|
|
if (!sr->s.string)
|
|
|
|
sr->s.string = pr_strdup (pr, "");
|
|
|
|
sr->type = str_temp;
|
2004-01-04 07:42:43 +00:00
|
|
|
sr->next = pr->pr_xtstr;
|
|
|
|
pr->pr_xtstr = sr;
|
|
|
|
}
|
|
|
|
|
2007-04-06 00:47:41 +00:00
|
|
|
VISIBLE string_t
|
2004-11-11 09:40:00 +00:00
|
|
|
PR_NewMutableString (progs_t *pr)
|
2004-01-03 08:43:57 +00:00
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
|
|
|
strref_t *sr = new_string_ref (res);
|
2004-11-11 09:40:00 +00:00
|
|
|
sr->type = str_mutable;
|
2020-02-24 15:21:56 +00:00
|
|
|
sr->s.dstring = _dstring_newstr (&res->ds_mem);
|
|
|
|
return string_index (res, sr);
|
2004-01-03 08:43:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 07:36:29 +00:00
|
|
|
VISIBLE void
|
|
|
|
PR_HoldString (progs_t *pr, string_t str)
|
|
|
|
{
|
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
|
|
|
strref_t *sr = get_strref (res, str);
|
|
|
|
|
|
|
|
if (sr) {
|
|
|
|
switch (sr->type) {
|
|
|
|
case str_temp:
|
|
|
|
case str_return:
|
|
|
|
break;
|
|
|
|
case str_static:
|
|
|
|
case str_mutable:
|
|
|
|
case str_dynamic:
|
|
|
|
// non-ephemeral string, no-op
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
PR_Error (pr, "internal string error: %d", __LINE__);
|
|
|
|
}
|
|
|
|
sr->type = str_dynamic;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!PR_StringValid (pr, str)) {
|
|
|
|
PR_RunError (pr, "attempt to hold invalid string %d", str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2007-04-06 00:47:41 +00:00
|
|
|
PR_FreeString (progs_t *pr, string_t str)
|
2004-01-03 08:43:57 +00:00
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
|
|
|
strref_t *sr = get_strref (res, str);
|
2004-01-03 08:43:57 +00:00
|
|
|
|
|
|
|
if (sr) {
|
2004-11-11 09:40:00 +00:00
|
|
|
switch (sr->type) {
|
|
|
|
case str_static:
|
|
|
|
case str_temp:
|
2020-03-26 06:37:20 +00:00
|
|
|
case str_return:
|
2004-11-11 09:40:00 +00:00
|
|
|
return;
|
|
|
|
case str_mutable:
|
|
|
|
dstring_delete (sr->s.dstring);
|
|
|
|
break;
|
|
|
|
case str_dynamic:
|
|
|
|
pr_strfree (pr, sr->s.string);
|
|
|
|
break;
|
|
|
|
default:
|
2020-03-08 12:29:31 +00:00
|
|
|
PR_Error (pr, "internal string error: %d", __LINE__);
|
2004-11-11 09:40:00 +00:00
|
|
|
}
|
2020-02-24 15:21:56 +00:00
|
|
|
free_string_ref (res, sr);
|
2004-01-03 08:43:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-26 06:37:20 +00:00
|
|
|
if (!PR_StringValid (pr, str)) {
|
|
|
|
PR_RunError (pr, "attempt to free invalid string %d", str);
|
|
|
|
}
|
2004-01-03 08:43:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 06:36:46 +00:00
|
|
|
VISIBLE void
|
2004-01-03 08:43:57 +00:00
|
|
|
PR_FreeTempStrings (progs_t *pr)
|
|
|
|
{
|
2020-02-24 15:21:56 +00:00
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
2004-01-03 08:43:57 +00:00
|
|
|
strref_t *sr, *t;
|
|
|
|
|
|
|
|
for (sr = pr->pr_xtstr; sr; sr = t) {
|
|
|
|
t = sr->next;
|
2020-03-26 07:36:29 +00:00
|
|
|
if (sr->type == str_dynamic) {
|
|
|
|
// the string has been held, so simply remove the ref from the
|
|
|
|
// queue
|
|
|
|
continue;
|
|
|
|
}
|
2004-11-11 09:40:00 +00:00
|
|
|
if (sr->type != str_temp)
|
2020-03-08 12:29:31 +00:00
|
|
|
PR_Error (pr, "internal string error: %d", __LINE__);
|
2020-02-24 15:21:56 +00:00
|
|
|
if (R_STRING (pr) < 0 && string_index (res, sr) == R_STRING (pr)
|
2010-01-13 06:42:59 +00:00
|
|
|
&& pr->pr_depth) {
|
2007-04-08 01:22:00 +00:00
|
|
|
prstack_t *frame = pr->pr_stack + pr->pr_depth - 1;
|
|
|
|
sr->next = frame->tstr;
|
|
|
|
frame->tstr = sr;
|
|
|
|
} else {
|
|
|
|
pr_strfree (pr, sr->s.string);
|
2020-02-24 15:21:56 +00:00
|
|
|
free_string_ref (res, sr);
|
2007-04-08 01:22:00 +00:00
|
|
|
}
|
2004-01-03 08:43:57 +00:00
|
|
|
}
|
|
|
|
pr->pr_xtstr = 0;
|
|
|
|
}
|
2004-01-04 07:42:43 +00:00
|
|
|
|
|
|
|
#define PRINT(t) \
|
|
|
|
switch ((doWidth << 1) | doPrecision) { \
|
|
|
|
case 3: \
|
|
|
|
dasprintf (result, tmp->str, current->minFieldWidth, \
|
|
|
|
current->precision, current->data.t##_var); \
|
|
|
|
break; \
|
|
|
|
case 2: \
|
|
|
|
dasprintf (result, tmp->str, current->minFieldWidth, \
|
2006-12-20 10:32:34 +00:00
|
|
|
current->data.t##_var); \
|
2004-01-04 07:42:43 +00:00
|
|
|
break; \
|
|
|
|
case 1: \
|
|
|
|
dasprintf (result, tmp->str, current->precision, \
|
|
|
|
current->data.t##_var); \
|
|
|
|
break; \
|
|
|
|
case 0: \
|
|
|
|
dasprintf (result, tmp->str, current->data.t##_var); \
|
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
This function takes as input a linked list of fmt_item_t representing
|
|
|
|
EVERYTHING to be printed. This includes text that is not affected by
|
|
|
|
formatting. A string without any formatting would wind up with only one
|
|
|
|
list item.
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-26 09:35:19 +00:00
|
|
|
I_DoPrint (dstring_t *tmp, dstring_t *result, fmt_item_t *formatting)
|
2004-01-04 07:42:43 +00:00
|
|
|
{
|
|
|
|
fmt_item_t *current = formatting;
|
|
|
|
|
|
|
|
while (current) {
|
|
|
|
qboolean doPrecision, doWidth;
|
|
|
|
|
|
|
|
doPrecision = -1 != current->precision;
|
|
|
|
doWidth = 0 != current->minFieldWidth;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
dsprintf (tmp, "%%%s%s%s%s%s%s%s",
|
|
|
|
(current->flags & FMT_ALTFORM) ? "#" : "", // hash
|
|
|
|
(current->flags & FMT_ZEROPAD) ? "0" : "", // zero padding
|
|
|
|
(current->flags & FMT_LJUSTIFY) ? "-" : "", // left justify
|
|
|
|
(current->flags & FMT_ADDBLANK) ? " " : "", // add space for +ve
|
|
|
|
(current->flags & FMT_ADDSIGN) ? "+" : "", // add sign always
|
|
|
|
doWidth ? "*" : "",
|
|
|
|
doPrecision ? ".*" : "");
|
|
|
|
|
|
|
|
switch (current->type) {
|
|
|
|
case 's':
|
|
|
|
dstring_appendstr (tmp, "s");
|
|
|
|
PRINT (string);
|
|
|
|
break;
|
2006-12-19 14:15:27 +00:00
|
|
|
case 'c':
|
|
|
|
dstring_appendstr (tmp, "c");
|
|
|
|
PRINT (integer);
|
|
|
|
break;
|
2004-01-04 07:42:43 +00:00
|
|
|
case 'i':
|
2006-12-20 08:45:49 +00:00
|
|
|
case 'd':
|
2004-11-11 09:40:00 +00:00
|
|
|
dstring_appendstr (tmp, "d");
|
|
|
|
PRINT (integer);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
dstring_appendstr (tmp, "x");
|
2004-01-04 07:42:43 +00:00
|
|
|
PRINT (integer);
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
if (current->flags & FMT_HEX)
|
2004-11-11 09:40:00 +00:00
|
|
|
dstring_appendstr (tmp, "x");
|
2004-01-04 07:42:43 +00:00
|
|
|
else
|
2004-11-11 09:40:00 +00:00
|
|
|
dstring_appendstr (tmp, "u");
|
2004-01-04 07:42:43 +00:00
|
|
|
PRINT (uinteger);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
dstring_appendstr (tmp, "f");
|
2020-02-14 07:38:37 +00:00
|
|
|
if (current->flags & FMT_LONG) {
|
|
|
|
PRINT (double);
|
|
|
|
} else {
|
|
|
|
PRINT (float);
|
|
|
|
}
|
2004-01-04 07:42:43 +00:00
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
dstring_appendstr (tmp, "g");
|
2020-02-14 07:38:37 +00:00
|
|
|
if (current->flags & FMT_LONG) {
|
|
|
|
PRINT (double);
|
|
|
|
} else {
|
|
|
|
PRINT (float);
|
|
|
|
}
|
2004-01-04 07:42:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static fmt_item_t *
|
2020-02-26 09:19:21 +00:00
|
|
|
new_fmt_item (prstr_resources_t *res)
|
2004-01-04 07:42:43 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
fmt_item_t *fi;
|
|
|
|
|
2020-02-26 09:19:21 +00:00
|
|
|
if (!res->free_fmt_items) {
|
|
|
|
res->free_fmt_items = malloc (16 * sizeof (fmt_item_t));
|
2004-01-04 07:42:43 +00:00
|
|
|
for (i = 0; i < 15; i++)
|
2020-02-26 09:19:21 +00:00
|
|
|
res->free_fmt_items[i].next = res->free_fmt_items + i + 1;
|
|
|
|
res->free_fmt_items[i].next = 0;
|
2004-01-04 07:42:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 09:19:21 +00:00
|
|
|
fi = res->free_fmt_items;
|
|
|
|
res->free_fmt_items = fi->next;
|
2004-01-04 07:42:43 +00:00
|
|
|
memset (fi, 0, sizeof (*fi));
|
|
|
|
fi->precision = -1;
|
|
|
|
return fi;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-26 09:19:21 +00:00
|
|
|
free_fmt_item (prstr_resources_t *res, fmt_item_t *fi)
|
2004-01-04 07:42:43 +00:00
|
|
|
{
|
2020-02-26 09:19:21 +00:00
|
|
|
fi->next = res->free_fmt_items;
|
|
|
|
res->free_fmt_items = fi;
|
2004-01-04 07:42:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 04:32:07 +00:00
|
|
|
struct fmt_state_s;
|
|
|
|
typedef void (*fmt_state_f) (struct fmt_state_s *);
|
|
|
|
|
|
|
|
typedef struct fmt_state_s {
|
|
|
|
fmt_state_f state;
|
|
|
|
prstr_resources_t *res;
|
|
|
|
progs_t *pr;
|
|
|
|
pr_type_t **args;
|
2020-04-01 02:19:56 +00:00
|
|
|
const char *c;
|
2020-04-01 04:32:07 +00:00
|
|
|
const char *msg;
|
2020-04-01 02:19:56 +00:00
|
|
|
fmt_item_t *fmt_items;
|
|
|
|
fmt_item_t **fi;
|
|
|
|
int fmt_count;
|
|
|
|
} fmt_state_t;
|
|
|
|
|
2020-04-01 04:32:07 +00:00
|
|
|
static inline void
|
|
|
|
fmt_append_item (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
(*state->fi)->next = new_fmt_item (state->res);
|
|
|
|
state->fi = &(*state->fi)->next;
|
|
|
|
}
|
|
|
|
|
2004-01-04 07:42:43 +00:00
|
|
|
#undef P_var
|
2020-04-01 04:32:07 +00:00
|
|
|
#define P_var(p,n,t) (state->args[n]->t##_var)
|
2020-02-14 09:15:34 +00:00
|
|
|
#undef P_DOUBLE
|
2020-04-01 04:32:07 +00:00
|
|
|
#define P_DOUBLE(p,n) (*(double *) (state->args[n]))
|
|
|
|
|
|
|
|
static void fmt_state_format (fmt_state_t *state);
|
|
|
|
static void fmt_state_flags (fmt_state_t *state);
|
|
|
|
static void fmt_state_var_field_width (fmt_state_t *state);
|
|
|
|
static void fmt_state_field_width (fmt_state_t *state);
|
|
|
|
static void fmt_state_precision (fmt_state_t *state);
|
|
|
|
static void fmt_state_var_precision (fmt_state_t *state);
|
|
|
|
static void fmt_state_fixed_precision (fmt_state_t *state);
|
|
|
|
static void fmt_state_modifiers (fmt_state_t *state);
|
|
|
|
static void fmt_state_conversion (fmt_state_t *state);
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_state_flags (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
state->c++; // skip over %
|
|
|
|
while (1) {
|
|
|
|
switch (*state->c) {
|
|
|
|
case '0':
|
|
|
|
(*state->fi)->flags |= FMT_ZEROPAD;
|
|
|
|
break;
|
|
|
|
case '#':
|
|
|
|
(*state->fi)->flags |= FMT_ALTFORM;
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
(*state->fi)->flags |= FMT_ADDBLANK;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
(*state->fi)->flags |= FMT_LJUSTIFY;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
(*state->fi)->flags |= FMT_ADDSIGN;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
state->state = fmt_state_var_field_width;
|
|
|
|
return;
|
|
|
|
case '.':
|
|
|
|
state->state = fmt_state_precision;
|
|
|
|
return;
|
|
|
|
case '1': case '2': case '3': case '4': case '5':
|
|
|
|
case '6': case '7': case '8': case '9':
|
|
|
|
state->state = fmt_state_field_width;
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
state->state = fmt_state_modifiers;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state->c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_state_var_field_width (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
// XXX record width
|
|
|
|
if (*++state->c == '.') {
|
|
|
|
state->state = fmt_state_precision;
|
|
|
|
} else {
|
|
|
|
state->state = fmt_state_modifiers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_state_field_width (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
while (isdigit ((byte )*state->c)) {
|
|
|
|
(*state->fi)->minFieldWidth *= 10;
|
|
|
|
(*state->fi)->minFieldWidth += *state->c++ - '0';
|
|
|
|
}
|
|
|
|
if (*state->c == '.') {
|
|
|
|
state->state = fmt_state_precision;
|
|
|
|
} else {
|
|
|
|
state->state = fmt_state_modifiers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_state_precision (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
state->c++; // skip over .
|
|
|
|
(*state->fi)->precision = 0;
|
|
|
|
if (isdigit ((byte )*state->c)) {
|
|
|
|
state->state = fmt_state_fixed_precision;
|
|
|
|
} else if (*state->c == '*') {
|
|
|
|
state->state = fmt_state_var_precision;
|
|
|
|
} else {
|
|
|
|
state->state = fmt_state_modifiers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_state_var_precision (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
// XXX record precision
|
|
|
|
state->state = fmt_state_modifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_state_fixed_precision (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
while (isdigit ((byte )*state->c)) {
|
|
|
|
(*state->fi)->precision *= 10;
|
|
|
|
(*state->fi)->precision += *state->c++ - '0';
|
|
|
|
}
|
|
|
|
state->state = fmt_state_modifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_state_modifiers (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
// no modifiers supported
|
|
|
|
state->state = fmt_state_conversion;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_state_conversion (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
progs_t *pr = state->pr;
|
|
|
|
char conv;
|
|
|
|
switch ((conv = *state->c++)) {
|
|
|
|
case '@':
|
|
|
|
// object
|
|
|
|
state->fmt_count++;
|
|
|
|
fmt_append_item (state);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
// entity
|
|
|
|
(*state->fi)->type = 'i';
|
|
|
|
(*state->fi)->data.integer_var =
|
|
|
|
P_EDICTNUM (pr, state->fmt_count);
|
|
|
|
|
|
|
|
state->fmt_count++;
|
|
|
|
fmt_append_item (state);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
case 'd':
|
|
|
|
case 'c':
|
|
|
|
// integer
|
|
|
|
(*state->fi)->type = conv;
|
|
|
|
(*state->fi)->data.integer_var = P_INT (pr, state->fmt_count);
|
|
|
|
|
|
|
|
state->fmt_count++;
|
|
|
|
fmt_append_item (state);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
// float or double
|
|
|
|
case 'g':
|
|
|
|
// float or double, no trailing zeroes, trim "."
|
|
|
|
// if nothing after
|
|
|
|
(*state->fi)->type = conv;
|
|
|
|
if (pr->float_promoted) {
|
|
|
|
(*state->fi)->flags |= FMT_LONG;
|
|
|
|
(*state->fi)->data.double_var
|
|
|
|
= P_DOUBLE (pr, state->fmt_count);
|
|
|
|
} else {
|
|
|
|
(*state->fi)->data.float_var
|
|
|
|
= P_FLOAT (pr, state->fmt_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
state->fmt_count++;
|
|
|
|
fmt_append_item (state);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
// pointer
|
|
|
|
(*state->fi)->flags |= FMT_ALTFORM;
|
|
|
|
(*state->fi)->type = 'x';
|
|
|
|
(*state->fi)->data.uinteger_var = P_UINT (pr, state->fmt_count);
|
|
|
|
|
|
|
|
state->fmt_count++;
|
|
|
|
fmt_append_item (state);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
// string
|
|
|
|
(*state->fi)->type = conv;
|
|
|
|
(*state->fi)->data.string_var = P_GSTRING (pr, state->fmt_count);
|
|
|
|
|
|
|
|
state->fmt_count++;
|
|
|
|
fmt_append_item (state);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
case 'q':
|
|
|
|
// vector
|
|
|
|
{
|
|
|
|
int i, count = 3;
|
|
|
|
int flags = (*state->fi)->flags;
|
|
|
|
int precision = (*state->fi)->precision;
|
|
|
|
unsigned minWidth = (*state->fi)->minFieldWidth;
|
|
|
|
|
|
|
|
(*state->fi)->flags = 0;
|
|
|
|
(*state->fi)->precision = -1;
|
|
|
|
(*state->fi)->minFieldWidth = 0;
|
|
|
|
|
|
|
|
if (conv == 'q')
|
|
|
|
count = 4;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (i == 0) {
|
|
|
|
(*state->fi)->type = 's';
|
|
|
|
(*state->fi)->data.string_var = "'";
|
|
|
|
} else {
|
|
|
|
(*state->fi)->type = 's';
|
|
|
|
(*state->fi)->data.string_var = " ";
|
|
|
|
}
|
|
|
|
fmt_append_item (state);
|
|
|
|
|
|
|
|
(*state->fi)->flags = flags;
|
|
|
|
(*state->fi)->precision = precision;
|
|
|
|
(*state->fi)->minFieldWidth = minWidth;
|
|
|
|
(*state->fi)->type = 'g';
|
|
|
|
(*state->fi)->data.float_var =
|
|
|
|
P_VECTOR (pr, state->fmt_count)[i];
|
|
|
|
|
|
|
|
fmt_append_item (state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*state->fi)->type = 's';
|
|
|
|
(*state->fi)->data.string_var = "'";
|
|
|
|
|
|
|
|
state->fmt_count++;
|
|
|
|
fmt_append_item (state);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
(*state->fi)->flags = 0;
|
|
|
|
(*state->fi)->precision = 0;
|
|
|
|
(*state->fi)->minFieldWidth = 0;
|
|
|
|
(*state->fi)->type = 's';
|
|
|
|
(*state->fi)->data.string_var = "%";
|
|
|
|
|
|
|
|
fmt_append_item (state);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
// integer, hex notation
|
|
|
|
(*state->fi)->type = conv;
|
|
|
|
(*state->fi)->data.uinteger_var = P_UINT (pr, state->fmt_count);
|
|
|
|
|
|
|
|
state->fmt_count++;
|
|
|
|
fmt_append_item (state);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state->state = fmt_state_format;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_state_format (fmt_state_t *state)
|
|
|
|
{
|
|
|
|
const char *l = state->c;
|
|
|
|
while (*state->c && *state->c != '%') {
|
|
|
|
state->c++;
|
|
|
|
}
|
|
|
|
if (state->c != l) {
|
|
|
|
// have some unformatted text to print
|
|
|
|
(*state->fi)->precision = state->c - l;
|
|
|
|
(*state->fi)->type = 's';
|
|
|
|
(*state->fi)->data.string_var = l;
|
|
|
|
if (*state->c) {
|
|
|
|
fmt_append_item (state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*state->c) {
|
|
|
|
state->state = fmt_state_flags;
|
|
|
|
} else {
|
|
|
|
state->state = 0; // finished
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-01-04 07:42:43 +00:00
|
|
|
PR_Sprintf (progs_t *pr, dstring_t *result, const char *name,
|
|
|
|
const char *format, int count, pr_type_t **args)
|
|
|
|
{
|
2020-02-26 09:19:21 +00:00
|
|
|
prstr_resources_t *res = pr->pr_string_resources;
|
2020-04-01 02:19:56 +00:00
|
|
|
fmt_state_t state = { };
|
|
|
|
|
2020-04-01 04:32:07 +00:00
|
|
|
state.pr = pr;
|
|
|
|
state.res = res;
|
|
|
|
state.args = args;
|
2020-04-01 02:19:56 +00:00
|
|
|
state.fi = &state.fmt_items;
|
2020-04-01 04:32:07 +00:00
|
|
|
*state.fi = new_fmt_item (res);
|
|
|
|
state.c = format;
|
|
|
|
state.state = fmt_state_format;
|
2004-01-04 07:42:43 +00:00
|
|
|
|
|
|
|
if (!name)
|
2020-02-14 07:38:37 +00:00
|
|
|
name = "PR_Sprintf";
|
2004-01-04 07:42:43 +00:00
|
|
|
|
2020-04-01 04:32:07 +00:00
|
|
|
while (*state.c && state.state) {
|
|
|
|
state.state (&state);
|
2004-01-04 07:42:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 04:32:07 +00:00
|
|
|
if (state.state) {
|
|
|
|
state.msg = "Unexpected end of format string";
|
|
|
|
} else if (state.fmt_count != count) {
|
2020-04-01 02:19:56 +00:00
|
|
|
if (state.fmt_count > count)
|
2020-04-01 04:32:07 +00:00
|
|
|
state.msg = "Not enough arguments for format string.";
|
2004-01-04 07:42:43 +00:00
|
|
|
else
|
2020-04-01 04:32:07 +00:00
|
|
|
state.msg = "Too many arguments for format string.";
|
|
|
|
}
|
|
|
|
if (state.msg) {
|
|
|
|
dsprintf (res->print_str, "%s: %d %d", state.msg, state.fmt_count,
|
|
|
|
count);
|
|
|
|
state.msg = res->print_str->str;
|
2004-01-04 07:42:43 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2020-02-26 09:35:19 +00:00
|
|
|
dstring_clear (res->print_str);
|
2020-04-01 02:19:56 +00:00
|
|
|
I_DoPrint (res->print_str, result, state.fmt_items);
|
|
|
|
while (state.fmt_items) {
|
|
|
|
fmt_item_t *t = state.fmt_items->next;
|
|
|
|
free_fmt_item (res, state.fmt_items);
|
|
|
|
state.fmt_items = t;
|
2004-01-04 07:42:43 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
error:
|
2020-04-01 04:32:07 +00:00
|
|
|
PR_RunError (pr, "%s: %s", name, state.msg);
|
2004-01-04 07:42:43 +00:00
|
|
|
}
|
2020-02-24 15:21:56 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
PR_Strings_Init (progs_t *pr)
|
|
|
|
{
|
|
|
|
prstr_resources_t *res = calloc (1, sizeof (*res));
|
|
|
|
res->pr = pr;
|
2020-02-26 09:35:19 +00:00
|
|
|
res->print_str = dstring_new ();
|
2020-02-24 15:21:56 +00:00
|
|
|
|
|
|
|
PR_Resources_Register (pr, "Strings", res, pr_strings_clear);
|
|
|
|
PR_AddLoadFunc (pr, PR_LoadStrings);
|
|
|
|
}
|