mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2024-11-10 15:22:04 +00:00
dstring.[ch]:
create dsprintf and dvsprintf to do formated prints to dynamic strings. rest Use the new functions :)
This commit is contained in:
parent
f945907cff
commit
5a2538faaf
9 changed files with 89 additions and 83 deletions
|
@ -29,6 +29,10 @@
|
|||
#ifndef __dstring_h
|
||||
#define __dstring_h
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "QF/gcc_attr.h"
|
||||
|
||||
typedef struct dstring_s {
|
||||
unsigned long int size, truesize;
|
||||
char *str;
|
||||
|
@ -50,4 +54,8 @@ dstring_t *dstring_newstr (void);
|
|||
void dstring_appendstr (dstring_t *dstr, const char *str);
|
||||
void dstring_insertstr (dstring_t *dstr, const char *str, unsigned int pos);
|
||||
void dstring_clearstr (dstring_t *dstr);
|
||||
|
||||
int dvsprintf (dstring_t *dstr, const char *fmt, va_list args);
|
||||
int dsprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3)));
|
||||
|
||||
#endif // __dstring_h
|
||||
|
|
|
@ -107,7 +107,7 @@ SNDDMA_Init (void)
|
|||
}
|
||||
stereo = snd_stereo->int_val;
|
||||
if (!pcmname)
|
||||
pcmname = "plug:0,0";
|
||||
pcmname = "plughw:0,0";
|
||||
if ((err = snd_pcm_open (&pcm, pcmname,
|
||||
SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
|
||||
Sys_Printf ("Error: audio open error: %s\n", snd_strerror (err));
|
||||
|
|
|
@ -411,31 +411,23 @@ void
|
|||
C_Print (const char *fmt, va_list args)
|
||||
{
|
||||
char *s;
|
||||
static char *buffer;
|
||||
int mask, size, c, l, y;
|
||||
static int buffer_size, cr;
|
||||
static dstring_t *buffer;
|
||||
int mask, c, l, y;
|
||||
static int cr;
|
||||
|
||||
size = vsnprintf (buffer, buffer_size, fmt, args) + 1; // +1 for nul
|
||||
while (size <= 0 || size > buffer_size) {
|
||||
if (size > 0)
|
||||
buffer_size = (size + 1023) & ~1023; // 1k multiples
|
||||
else
|
||||
buffer_size += 1024;
|
||||
buffer = realloc (buffer, buffer_size);
|
||||
if (!buffer)
|
||||
Sys_Error ("console: could not allocate %d bytes\n",
|
||||
buffer_size);
|
||||
size = vsnprintf (buffer, buffer_size, fmt, args) + 1;
|
||||
}
|
||||
if (!buffer)
|
||||
buffer = dstring_new ();
|
||||
|
||||
dvsprintf (buffer, fmt, args);
|
||||
|
||||
// log all messages to file
|
||||
if (con_debuglog)
|
||||
Sys_DebugLog (va ("%s/qconsole.log", com_gamedir), "%s", buffer);
|
||||
Sys_DebugLog (va ("%s/qconsole.log", com_gamedir), "%s", buffer->str);
|
||||
|
||||
if (!con_initialized)
|
||||
return;
|
||||
|
||||
s = buffer;
|
||||
s = buffer->str;
|
||||
|
||||
if (s[0] == 1 || s[0] == 2) {
|
||||
mask = 128; // go to colored text
|
||||
|
@ -489,10 +481,10 @@ C_Print (const char *fmt, va_list args)
|
|||
}
|
||||
|
||||
// echo to debugging console
|
||||
if ((byte)buffer[0] > 2)
|
||||
fputs (buffer, stdout);
|
||||
else if ((byte)buffer[0])
|
||||
fputs (buffer + 1, stdout);
|
||||
if ((byte)buffer->str[0] > 2)
|
||||
fputs (buffer->str, stdout);
|
||||
else if ((byte)buffer->str[0])
|
||||
fputs (buffer->str + 1, stdout);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
@ -299,30 +299,18 @@ C_Shutdown (void)
|
|||
static void
|
||||
C_Print (const char *fmt, va_list args)
|
||||
{
|
||||
static unsigned char *buffer;
|
||||
static dstring_t *buffer;
|
||||
unsigned char *txt;
|
||||
int size;
|
||||
static int buffer_size;
|
||||
|
||||
size = vsnprintf (buffer, buffer_size, fmt, args) + 1; // +1 for nul
|
||||
//printf ("size = %d\n", size);
|
||||
while (size <= 0 || size > buffer_size) {
|
||||
if (size > 0)
|
||||
buffer_size = (size + 1023) & ~1023; // 1k multiples
|
||||
else
|
||||
buffer_size += 1024;
|
||||
buffer = realloc (buffer, buffer_size);
|
||||
if (!buffer)
|
||||
Sys_Error ("console: could not allocate %d bytes\n",
|
||||
buffer_size);
|
||||
size = vsnprintf (buffer, buffer_size, fmt, args) + 1;
|
||||
//printf ("size = %d\n", size);
|
||||
}
|
||||
if (!buffer)
|
||||
buffer = dstring_new ();
|
||||
|
||||
txt = buffer;
|
||||
dvsprintf (buffer, fmt, args);
|
||||
|
||||
txt = buffer->str;
|
||||
#ifdef HAVE_CURSES_H
|
||||
if (use_curses) {
|
||||
Con_BufferAddText (output_buffer, buffer);
|
||||
Con_BufferAddText (output_buffer, buffer->str);
|
||||
while (*txt)
|
||||
draw_fun_char (output, *txt++);
|
||||
wrefresh (output);
|
||||
|
|
|
@ -33,6 +33,8 @@ static const char rcsid[] =
|
|||
#include "QF/sys.h"
|
||||
#include "QF/dstring.h"
|
||||
|
||||
#include "compat.h"
|
||||
|
||||
dstring_t *
|
||||
dstring_new (void)
|
||||
{
|
||||
|
@ -135,3 +137,34 @@ dstring_clearstr (dstring_t *dstr)
|
|||
dstring_adjust (dstr);
|
||||
dstr->str[0] = 0;
|
||||
}
|
||||
|
||||
int
|
||||
dvsprintf (dstring_t *dstr, const char *fmt, va_list args)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = vsnprintf (dstr->str, dstr->truesize, fmt, args) + 1; // +1 for nul
|
||||
while (size <= 0 || size > dstr->truesize) {
|
||||
if (size > 0)
|
||||
dstr->size = (size + 1023) & ~1023; // 1k multiples
|
||||
else
|
||||
dstr->size = dstr->truesize + 1024;
|
||||
dstring_adjust (dstr);
|
||||
size = vsnprintf (dstr->str, dstr->truesize, fmt, args) + 1;
|
||||
}
|
||||
dstr->size = size;
|
||||
return size - 1;
|
||||
}
|
||||
|
||||
int
|
||||
dsprintf (dstring_t *dstr, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
int ret;
|
||||
|
||||
va_start (args, fmt);
|
||||
ret = dvsprintf (dstr, fmt, args);
|
||||
va_end (args);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -58,6 +58,7 @@ static const char rcsid[] =
|
|||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "QF/dstring.h"
|
||||
#include "QF/vfile.h"
|
||||
#include "QF/vfs.h"
|
||||
|
||||
|
@ -261,17 +262,15 @@ Qprintf (VFile *file, const char *fmt, ...)
|
|||
ret = vfprintf (file->file, fmt, args);
|
||||
#ifdef HAVE_ZLIB
|
||||
else {
|
||||
char buf[4096];
|
||||
static dstring_t *buf;
|
||||
|
||||
if (!buf)
|
||||
buf = dstring_new ();
|
||||
|
||||
va_start (args, fmt);
|
||||
#ifdef HAVE_VSNPRINTF
|
||||
(void) vsnprintf (buf, sizeof (buf), fmt, args);
|
||||
#else
|
||||
(void) vsprintf (buf, fmt, args);
|
||||
#endif
|
||||
dvsprintf (buf, fmt, args);
|
||||
va_end (args);
|
||||
ret = strlen (buf); /* some *snprintf don't return the nb
|
||||
of bytes written */
|
||||
ret = strlen (buf->str);
|
||||
if (ret > 0)
|
||||
ret = gzwrite (file->gzfile, buf, (unsigned) ret);
|
||||
}
|
||||
|
|
|
@ -60,6 +60,7 @@ static const char rcsid[] =
|
|||
#include <sys/types.h>
|
||||
|
||||
#include "QF/cvar.h"
|
||||
#include "QF/dstring.h"
|
||||
#include "QF/sys.h"
|
||||
|
||||
#include "compat.h"
|
||||
|
@ -185,18 +186,21 @@ Sys_SetErrPrintf (sys_printf_t func)
|
|||
void
|
||||
Sys_Print (FILE *stream, const char *fmt, va_list args)
|
||||
{
|
||||
char msg[MAXPRINTMSG];
|
||||
static dstring_t *msg;
|
||||
unsigned char *p;
|
||||
|
||||
vsnprintf (msg, sizeof (msg), fmt, args);
|
||||
if (!msg)
|
||||
msg = dstring_new ();
|
||||
|
||||
dvsprintf (msg, fmt, args);
|
||||
|
||||
#ifdef WIN32
|
||||
if (stream == stderr)
|
||||
MessageBox (NULL, msg, "Error", 0 /* MB_OK */ );
|
||||
MessageBox (NULL, msg->str, "Error", 0 /* MB_OK */ );
|
||||
#endif
|
||||
|
||||
/* translate to ASCII instead of printing [xx] --KB */
|
||||
for (p = (unsigned char *) msg; *p; p++)
|
||||
for (p = (unsigned char *) msg->str; *p; p++)
|
||||
putc (sys_char_map[*p], stream);
|
||||
|
||||
fflush (stream);
|
||||
|
|
|
@ -34,6 +34,7 @@ static const char rcsid[] =
|
|||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "QF/dstring.h"
|
||||
#include "QF/qtypes.h"
|
||||
#include "QF/sys.h"
|
||||
#include "QF/va.h"
|
||||
|
@ -47,30 +48,18 @@ static const char rcsid[] =
|
|||
does a varargs printf into a temp buffer, so I don't need to have
|
||||
varargs versions of all text functions.
|
||||
*/
|
||||
char *
|
||||
char *
|
||||
va (const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
static char *string;
|
||||
int size;
|
||||
static int string_size;
|
||||
static dstring_t *string;
|
||||
|
||||
if (!string)
|
||||
string = dstring_new ();
|
||||
|
||||
va_start (args, fmt);
|
||||
size = vsnprintf (string, string_size, fmt, args) + 1; // +1 for nul
|
||||
//printf ("size = %d\n", size);
|
||||
while (size <= 0 || size > string_size) {
|
||||
if (size > 0)
|
||||
string_size = (size + 1023) & ~1023; // 1k multiples
|
||||
else
|
||||
string_size += 1024;
|
||||
string = realloc (string, string_size);
|
||||
if (!string)
|
||||
Sys_Error ("console: could not allocate %d bytes\n",
|
||||
string_size);
|
||||
size = vsnprintf (string, string_size, fmt, args) + 1;
|
||||
//printf ("size = %d\n", size);
|
||||
}
|
||||
dvsprintf (string, fmt, args);
|
||||
va_end (args);
|
||||
|
||||
return string;
|
||||
return string->str;
|
||||
}
|
||||
|
|
|
@ -1025,14 +1025,10 @@ preprocess_file (const char *filename)
|
|||
temp++;
|
||||
|
||||
if (*sourcedir) {
|
||||
tempname->size = strlen (sourcedir) + 1 + strlen (temp) + 1;
|
||||
dstring_adjust (tempname);
|
||||
sprintf (tempname->str, "%s%c%s", sourcedir,
|
||||
PATH_SEPARATOR, temp);
|
||||
dsprintf (tempname, "%s%c%s", sourcedir,
|
||||
PATH_SEPARATOR, temp);
|
||||
} else {
|
||||
tempname->size = strlen (temp) + 2 + 1;
|
||||
dstring_adjust (tempname);
|
||||
sprintf (tempname->str, "%s.p", temp);
|
||||
dsprintf (tempname, "%s.p", temp);
|
||||
}
|
||||
free (basename);
|
||||
} else {
|
||||
|
@ -1044,10 +1040,7 @@ preprocess_file (const char *filename)
|
|||
}
|
||||
}
|
||||
|
||||
tempname->size = strlen (temp1) + 1
|
||||
+ strlen (temp2 ? temp2 + 1 : this_program) + 1;
|
||||
dstring_adjust (tempname);
|
||||
sprintf (tempname->str, "%s%c%sXXXXXX", temp1,
|
||||
dsprintf (tempname, "%s%c%sXXXXXX", temp1,
|
||||
PATH_SEPARATOR, temp2 ? temp2 + 1 : this_program);
|
||||
}
|
||||
build_cpp_args (filename, tempname->str);
|
||||
|
|
Loading…
Reference in a new issue