summaryrefslogtreecommitdiff
path: root/src/q_shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/q_shared.c')
-rw-r--r--src/q_shared.c649
1 files changed, 340 insertions, 309 deletions
diff --git a/src/q_shared.c b/src/q_shared.c
index a54765d..353993a 100644
--- a/src/q_shared.c
+++ b/src/q_shared.c
@@ -8,7 +8,7 @@ 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.
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
@@ -23,90 +23,86 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
vec3_t vec3_origin = { 0, 0, 0 };
-void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
+void AngleVectors(vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
{
float angle;
float sr, sp, sy, cr, cp, cy;
- angle = angles[YAW] * (M_PI*2 / 360);
+ angle = angles[YAW] * (M_PI * 2 / 360);
sy = sin(angle);
cy = cos(angle);
- angle = angles[PITCH] * (M_PI*2 / 360);
+ angle = angles[PITCH] * (M_PI * 2 / 360);
sp = sin(angle);
cp = cos(angle);
- angle = angles[ROLL] * (M_PI*2 / 360);
+ angle = angles[ROLL] * (M_PI * 2 / 360);
sr = sin(angle);
cr = cos(angle);
- if (forward)
- {
- forward[0] = cp*cy;
- forward[1] = cp*sy;
+ if (forward) {
+ forward[0] = cp * cy;
+ forward[1] = cp * sy;
forward[2] = -sp;
}
- if (right)
- {
- right[0] = (-1*sr*sp*cy+-1*cr*-sy);
- right[1] = (-1*sr*sp*sy+-1*cr*cy);
- right[2] = -1*sr*cp;
+ if (right) {
+ right[0] = (-1 * sr * sp * cy + -1 * cr * -sy);
+ right[1] = (-1 * sr * sp * sy + -1 * cr * cy);
+ right[2] = -1 * sr * cp;
}
- if (up)
- {
- up[0] = (cr*sp*cy+-sr*-sy);
- up[1] = (cr*sp*sy+-sr*cy);
- up[2] = cr*cp;
+ if (up) {
+ up[0] = (cr * sp * cy + -sr * -sy);
+ up[1] = (cr * sp * sy + -sr * cy);
+ up[2] = cr * cp;
}
}
-vec_t VectorNormalize (vec3_t v)
+vec_t VectorNormalize(vec3_t v)
{
float length, ilength;
- length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
- length = sqrt (length); // FIXME
+ length = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
+ length = sqrt(length); // FIXME
- if (length)
- {
- ilength = 1/length;
+ if (length) {
+ ilength = 1 / length;
v[0] *= ilength;
v[1] *= ilength;
v[2] *= ilength;
}
-
+
return length;
}
-vec_t VectorNormalize2 (vec3_t v, vec3_t out)
+vec_t VectorNormalize2(vec3_t v, vec3_t out)
{
float length, ilength;
- length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
- length = sqrt (length); // FIXME
+ length = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
+ length = sqrt(length); // FIXME
- if (length)
- {
- ilength = 1/length;
- out[0] = v[0]*ilength;
- out[1] = v[1]*ilength;
- out[2] = v[2]*ilength;
+ if (length) {
+ ilength = 1 / length;
+ out[0] = v[0] * ilength;
+ out[1] = v[1] * ilength;
+ out[2] = v[2] * ilength;
}
-
+
return length;
}
-void ClearBounds (vec3_t mins, vec3_t maxs) {
+void ClearBounds(vec3_t mins, vec3_t maxs)
+{
mins[0] = mins[1] = mins[2] = 99999;
maxs[0] = maxs[1] = maxs[2] = -99999;
}
-void AddPointToBounds (const vec3_t v, vec3_t mins, vec3_t maxs) {
+void AddPointToBounds(const vec3_t v, vec3_t mins, vec3_t maxs)
+{
int i;
vec_t val;
- for (i=0 ; i<3 ; i++)
- {
+ for (i = 0; i < 3; i++) {
val = v[i];
if (val < mins[i])
mins[i] = val;
@@ -115,7 +111,8 @@ void AddPointToBounds (const vec3_t v, vec3_t mins, vec3_t maxs) {
}
}
-void UnionBounds( vec3_t a[2], vec3_t b[2], vec3_t c[2] ) {
+void UnionBounds(vec3_t a[2], vec3_t b[2], vec3_t c[2])
+{
c[0][0] = b[0][0] < a[0][0] ? b[0][0] : a[0][0];
c[0][1] = b[0][1] < a[0][1] ? b[0][1] : a[0][1];
c[0][2] = b[0][2] < a[0][2] ? b[0][2] : a[0][2];
@@ -130,19 +127,19 @@ void UnionBounds( vec3_t a[2], vec3_t b[2], vec3_t c[2] ) {
RadiusFromBounds
=================
*/
-vec_t RadiusFromBounds (const vec3_t mins, const vec3_t maxs) {
+vec_t RadiusFromBounds(const vec3_t mins, const vec3_t maxs)
+{
int i;
vec3_t corner;
vec_t a, b;
- for (i=0 ; i<3 ; i++)
- {
+ for (i = 0; i < 3; i++) {
a = Q_fabs(mins[i]);
b = Q_fabs(maxs[i]);
corner[i] = a > b ? a : b;
}
- return VectorLength (corner);
+ return VectorLength(corner);
}
//====================================================================================
@@ -154,16 +151,17 @@ static const char hexchars[] = "0123456789ABCDEF";
COM_SkipPath
============
*/
-char *COM_SkipPath( const char *pathname ) {
+char *COM_SkipPath(const char *pathname)
+{
char *last;
- if( !pathname ) {
- Com_Error( ERR_FATAL, "%s: NULL", __func__ );
+ if (!pathname) {
+ Com_Error(ERR_FATAL, "%s: NULL", __func__);
}
-
+
last = (char *)pathname;
- while( *pathname ) {
- if( *pathname == '/' )
+ while (*pathname) {
+ if (*pathname == '/')
last = (char *)pathname + 1;
pathname++;
}
@@ -175,18 +173,19 @@ char *COM_SkipPath( const char *pathname ) {
COM_StripExtension
============
*/
-void COM_StripExtension( const char *in, char *out, size_t size ) {
+void COM_StripExtension(const char *in, char *out, size_t size)
+{
char *s;
- Q_strlcpy( out, in, size );
+ Q_strlcpy(out, in, size);
- s = out + strlen( out );
-
- while( s != out ) {
- if( *s == '/' ) {
+ s = out + strlen(out);
+
+ while (s != out) {
+ if (*s == '/') {
break;
}
- if( *s == '.' ) {
+ if (*s == '.') {
*s = 0;
break;
}
@@ -199,22 +198,23 @@ void COM_StripExtension( const char *in, char *out, size_t size ) {
COM_FileExtension
============
*/
-char *COM_FileExtension( const char *in ) {
+char *COM_FileExtension(const char *in)
+{
const char *s;
const char *last;
- if( !in ) {
- Com_Error( ERR_FATAL, "%s: NULL", __func__ );
+ if (!in) {
+ Com_Error(ERR_FATAL, "%s: NULL", __func__);
}
- s = in + strlen( in );
+ s = in + strlen(in);
last = s;
-
- while( s != in ) {
- if( *s == '/' ) {
+
+ while (s != in) {
+ if (*s == '/') {
break;
}
- if( *s == '.' ) {
+ if (*s == '.') {
return (char *)s;
}
s--;
@@ -228,25 +228,24 @@ char *COM_FileExtension( const char *in ) {
COM_FileBase
============
*/
-void COM_FileBase (char *in, char *out)
+void COM_FileBase(char *in, char *out)
{
char *s, *s2;
-
+
s = in + strlen(in) - 1;
-
+
while (s != in && *s != '.')
s--;
-
- for (s2 = s ; s2 != in && *s2 != '/' ; s2--)
- ;
-
- if (s-s2 < 2)
+
+ for (s2 = s; s2 != in && *s2 != '/'; s2--)
+ ;
+
+ if (s - s2 < 2)
out[0] = 0;
- else
- {
+ else {
s--;
- strncpy (out,s2+1, s-s2);
- out[s-s2] = 0;
+ strncpy(out, s2 + 1, s - s2);
+ out[s - s2] = 0;
}
}
@@ -257,12 +256,13 @@ COM_FilePath
Returns the path up to, but not including the last /
============
*/
-void COM_FilePath( const char *in, char *out, size_t size ) {
+void COM_FilePath(const char *in, char *out, size_t size)
+{
char *s;
- Q_strlcpy( out, in, size );
- s = strrchr( out, '/' );
- if( s ) {
+ Q_strlcpy(out, in, size);
+ s = strrchr(out, '/');
+ if (s) {
*s = 0;
} else {
*out = 0;
@@ -278,22 +278,23 @@ if path doesn't have .EXT, append extension
(extension should include the .)
==================
*/
-size_t COM_DefaultExtension( char *path, const char *ext, size_t size ) {
+size_t COM_DefaultExtension(char *path, const char *ext, size_t size)
+{
char *src;
size_t len;
- if( *path ) {
- len = strlen( path );
+ if (*path) {
+ len = strlen(path);
src = path + len - 1;
- while( *src != '/' && src != path ) {
- if( *src == '.' )
+ while (*src != '/' && src != path) {
+ if (*src == '.')
return len; // it has an extension
src--;
}
}
- len = Q_strlcat( path, ext, size );
+ len = Q_strlcat(path, ext, size);
return len;
}
@@ -305,68 +306,72 @@ Returns true if the given string is valid representation
of floating point number.
==================
*/
-qboolean COM_IsFloat( const char *s ) {
+qboolean COM_IsFloat(const char *s)
+{
int c, dot = '.';
- if( *s == '-' ) {
+ if (*s == '-') {
s++;
}
- if( !*s ) {
+ if (!*s) {
return qfalse;
}
do {
c = *s++;
- if( c == dot ) {
+ if (c == dot) {
dot = 0;
- } else if( !Q_isdigit( c ) ) {
+ } else if (!Q_isdigit(c)) {
return qfalse;
}
- } while( *s );
+ } while (*s);
return qtrue;
}
-qboolean COM_IsUint( const char *s ) {
+qboolean COM_IsUint(const char *s)
+{
int c;
- if( !*s ) {
+ if (!*s) {
return qfalse;
}
do {
c = *s++;
- if( !Q_isdigit( c ) ) {
+ if (!Q_isdigit(c)) {
return qfalse;
}
- } while( *s );
+ } while (*s);
return qtrue;
}
-qboolean COM_IsPath( const char *s ) {
+qboolean COM_IsPath(const char *s)
+{
int c;
- if( !*s ) {
+ if (!*s) {
return qfalse;
}
do {
c = *s++;
- if( !Q_ispath( c ) ) {
+ if (!Q_ispath(c)) {
return qfalse;
}
- } while( *s );
+ } while (*s);
return qtrue;
}
-qboolean COM_IsWhite( const char *s ) {
+qboolean COM_IsWhite(const char *s)
+{
int c;
- while( *s ) {
+ while (*s) {
c = *s++;
- if( Q_isgraph( c ) ) {
+ if (Q_isgraph(c)) {
return qfalse;
}
}
@@ -374,12 +379,14 @@ qboolean COM_IsWhite( const char *s ) {
return qtrue;
}
-int QDECL SortStrcmp( const void *p1, const void *p2 ) {
- return strcmp( *( const char ** )p1, *( const char ** )p2 );
+int QDECL SortStrcmp(const void *p1, const void *p2)
+{
+ return strcmp(*(const char **)p1, *(const char **)p2);
}
-int QDECL SortStricmp( const void *p1, const void *p2 ) {
- return Q_stricmp( *( const char ** )p1, *( const char ** )p2 );
+int QDECL SortStricmp(const void *p1, const void *p2)
+{
+ return Q_stricmp(*(const char **)p1, *(const char **)p2);
}
/*
@@ -390,17 +397,18 @@ Operates inplace, normalizing high-bit and removing unprintable characters.
Returns final number of characters, not including the NUL character.
================
*/
-size_t COM_strclr( char *s ) {
+size_t COM_strclr(char *s)
+{
char *p;
int c;
size_t len;
p = s;
len = 0;
- while( *s ) {
+ while (*s) {
c = *s++;
c &= 127;
- if( Q_isprint( c ) ) {
+ if (Q_isprint(c)) {
*p++ = c;
len++;
}
@@ -420,18 +428,19 @@ varargs versions of all text functions.
FIXME: make this buffer size safe someday
============
*/
-char *va( const char *format, ... ) {
+char *va(const char *format, ...)
+{
va_list argptr;
static char buffers[2][0x2800];
static int index;
index ^= 1;
-
- va_start( argptr, format );
- Q_vsnprintf( buffers[index], sizeof( buffers[0] ), format, argptr );
- va_end( argptr );
- return buffers[index];
+ va_start(argptr, format);
+ Q_vsnprintf(buffers[index], sizeof(buffers[0]), format, argptr);
+ va_end(argptr);
+
+ return buffers[index];
}
static char com_token[4][MAX_TOKEN_CHARS];
@@ -445,7 +454,8 @@ Parse a token out of a string.
Handles C and C++ comments.
==============
*/
-char *COM_Parse( const char **data_p ) {
+char *COM_Parse(const char **data_p)
+{
int c;
int len;
const char *data;
@@ -454,35 +464,35 @@ char *COM_Parse( const char **data_p ) {
data = *data_p;
len = 0;
s[0] = 0;
-
- if( !data ) {
+
+ if (!data) {
*data_p = NULL;
return s;
}
-
+
// skip whitespace
skipwhite:
- while( ( c = *data ) <= ' ' ) {
- if( c == 0 ) {
+ while ((c = *data) <= ' ') {
+ if (c == 0) {
*data_p = NULL;
return s;
}
data++;
}
-
+
// skip // comments
- if( c == '/' && data[1] == '/' ) {
+ if (c == '/' && data[1] == '/') {
data += 2;
- while( *data && *data != '\n' )
+ while (*data && *data != '\n')
data++;
goto skipwhite;
}
// skip /* */ comments
- if( c == '/' && data[1] == '*' ) {
+ if (c == '/' && data[1] == '*') {
data += 2;
- while( *data ) {
- if( data[0] == '*' && data[1] == '/' ) {
+ while (*data) {
+ if (data[0] == '*' && data[1] == '/') {
data += 2;
break;
}
@@ -492,15 +502,15 @@ skipwhite:
}
// handle quoted strings specially
- if( c == '\"' ) {
+ if (c == '\"') {
data++;
- while( 1 ) {
+ while (1) {
c = *data++;
- if( c == '\"' || !c ) {
+ if (c == '\"' || !c) {
goto finish;
}
- if( len < MAX_TOKEN_CHARS - 1 ) {
+ if (len < MAX_TOKEN_CHARS - 1) {
s[len++] = c;
}
}
@@ -508,12 +518,12 @@ skipwhite:
// parse a regular word
do {
- if( len < MAX_TOKEN_CHARS - 1 ) {
+ if (len < MAX_TOKEN_CHARS - 1) {
s[len++] = c;
}
data++;
c = *data;
- } while( c > 32 );
+ } while (c > 32);
finish:
s[len] = 0;
@@ -532,41 +542,42 @@ Non-contiguous line feeds are preserved.
Returns resulting data length.
==============
*/
-int COM_Compress( char *data ) {
+int COM_Compress(char *data)
+{
int c, n = 0;
char *s = data, *d = data;
- while( *s ) {
- // skip whitespace
- if( *s <= ' ' ) {
+ while (*s) {
+ // skip whitespace
+ if (*s <= ' ') {
n = ' ';
do {
c = *s++;
- if( c == '\n' ) {
+ if (c == '\n') {
n = '\n';
}
- if( !c ) {
+ if (!c) {
goto finish;
}
- } while( *s <= ' ' );
+ } while (*s <= ' ');
}
-
+
// skip // comments
- if( s[0] == '/' && s[1] == '/' ) {
+ if (s[0] == '/' && s[1] == '/') {
n = ' ';
s += 2;
- while( *s && *s != '\n' ) {
+ while (*s && *s != '\n') {
s++;
}
continue;
}
// skip /* */ comments
- if( s[0] == '/' && s[1] == '*' ) {
+ if (s[0] == '/' && s[1] == '*') {
n = ' ';
s += 2;
- while( *s ) {
- if( s[0] == '*' && s[1] == '/' ) {
+ while (*s) {
+ if (s[0] == '*' && s[1] == '/') {
s += 2;
break;
}
@@ -576,27 +587,27 @@ int COM_Compress( char *data ) {
}
// add whitespace character
- if( n ) {
+ if (n) {
*d++ = n;
n = 0;
}
// handle quoted strings specially
- if( *s == '\"' ) {
+ if (*s == '\"') {
s++;
*d++ = '\"';
do {
c = *s++;
- if( !c ) {
+ if (!c) {
goto finish;
}
*d++ = c;
- } while( c != '\"' );
+ } while (c != '\"');
continue;
}
// handle line feed escape
- if( *s == '\\' && s[1] == '\n' ) {
+ if (*s == '\\' && s[1] == '\n') {
s += 2;
continue;
}
@@ -604,7 +615,7 @@ int COM_Compress( char *data ) {
// parse a regular word
do {
*d++ = *s++;
- } while( *s > ' ' );
+ } while (*s > ' ');
}
finish:
@@ -621,62 +632,65 @@ finish:
============================================================================
*/
-int Q_strncasecmp( const char *s1, const char *s2, size_t n ) {
+int Q_strncasecmp(const char *s1, const char *s2, size_t n)
+{
int c1, c2;
-
+
do {
c1 = *s1++;
c2 = *s2++;
- if( !n-- )
+ if (!n--)
return 0; /* strings are equal until end point */
-
- if( c1 != c2 ) {
- c1 = Q_tolower( c1 );
- c2 = Q_tolower( c2 );
- if( c1 < c2 )
+
+ if (c1 != c2) {
+ c1 = Q_tolower(c1);
+ c2 = Q_tolower(c2);
+ if (c1 < c2)
return -1;
- if( c1 > c2 )
+ if (c1 > c2)
return 1; /* strings not equal */
}
- } while( c1 );
-
+ } while (c1);
+
return 0; /* strings are equal */
}
-int Q_strcasecmp( const char *s1, const char *s2 ) {
+int Q_strcasecmp(const char *s1, const char *s2)
+{
int c1, c2;
-
+
do {
c1 = *s1++;
c2 = *s2++;
-
- if( c1 != c2 ) {
- c1 = Q_tolower( c1 );
- c2 = Q_tolower( c2 );
- if( c1 < c2 )
+
+ if (c1 != c2) {
+ c1 = Q_tolower(c1);
+ c2 = Q_tolower(c2);
+ if (c1 < c2)
return -1;
- if( c1 > c2 )
+ if (c1 > c2)
return 1; /* strings not equal */
}
- } while( c1 );
-
+ } while (c1);
+
return 0; /* strings are equal */
}
-char *Q_strcasestr( const char *s1, const char *s2 ) {
+char *Q_strcasestr(const char *s1, const char *s2)
+{
size_t l1, l2;
- l2 = strlen( s2 );
- if( !l2 ) {
- return ( char * )s1;
+ l2 = strlen(s2);
+ if (!l2) {
+ return (char *)s1;
}
- l1 = strlen( s1 );
- while( l1 >= l2 ) {
+ l1 = strlen(s1);
+ while (l1 >= l2) {
l1--;
- if( !Q_strncasecmp( s1, s2, l2 ) ) {
- return ( char * )s1;
+ if (!Q_strncasecmp(s1, s2, l2)) {
+ return (char *)s1;
}
s1++;
}
@@ -691,12 +705,13 @@ Q_strlcpy
Returns length of the source string.
===============
*/
-size_t Q_strlcpy( char *dst, const char *src, size_t size ) {
- size_t ret = strlen( src );
+size_t Q_strlcpy(char *dst, const char *src, size_t size)
+{
+ size_t ret = strlen(src);
- if( size ) {
+ if (size) {
size_t len = ret >= size ? size - 1 : ret;
- memcpy( dst, src, len );
+ memcpy(dst, src, len);
dst[len] = 0;
}
@@ -710,14 +725,15 @@ Q_strlcat
Returns length of the source and destinations strings combined.
===============
*/
-size_t Q_strlcat( char *dst, const char *src, size_t size ) {
- size_t ret, len = strlen( dst );
+size_t Q_strlcat(char *dst, const char *src, size_t size)
+{
+ size_t ret, len = strlen(dst);
- if( len >= size ) {
- Com_Error( ERR_FATAL, "%s: already overflowed", __func__ );
+ if (len >= size) {
+ Com_Error(ERR_FATAL, "%s: already overflowed", __func__);
}
- ret = Q_strlcpy( dst + len, src, size - len );
+ ret = Q_strlcpy(dst + len, src, size - len);
ret += len;
return ret;
@@ -732,23 +748,24 @@ excluding trailing '\0'. If the returned value is equal to or greater than
buffer size, resulting string is truncated.
===============
*/
-size_t Q_concat( char *dest, size_t size, ... ) {
+size_t Q_concat(char *dest, size_t size, ...)
+{
va_list argptr;
const char *s;
size_t len, total = 0;
- va_start( argptr, size );
- while( ( s = va_arg( argptr, const char * ) ) != NULL ) {
- len = strlen( s );
- if( total + len < size ) {
- memcpy( dest, s, len );
+ va_start(argptr, size);
+ while ((s = va_arg(argptr, const char *)) != NULL) {
+ len = strlen(s);
+ if (total + len < size) {
+ memcpy(dest, s, len);
dest += len;
}
total += len;
}
- va_end( argptr );
+ va_end(argptr);
- if( size ) {
+ if (size) {
*dest = 0;
}
@@ -764,35 +781,36 @@ excluding trailing '\0'. If the returned value is equal to or greater than
buffer size, resulting string is truncated.
===============
*/
-size_t Q_vsnprintf( char *dest, size_t size, const char *fmt, va_list argptr ) {
+size_t Q_vsnprintf(char *dest, size_t size, const char *fmt, va_list argptr)
+{
int ret;
- if( size > INT_MAX ) {
- Com_Error( ERR_FATAL, "%s: bad buffer size", __func__ );
+ if (size > INT_MAX) {
+ Com_Error(ERR_FATAL, "%s: bad buffer size", __func__);
}
#ifdef _WIN32
// work around broken M$ C runtime semantics.
- if( !size ) {
+ if (!size) {
return 0;
}
- ret = _vsnprintf( dest, size - 1, fmt, argptr );
- if( ret >= size - 1 ) {
+ ret = _vsnprintf(dest, size - 1, fmt, argptr);
+ if (ret >= size - 1) {
dest[size - 1] = 0;
}
#else
- ret = vsnprintf( dest, size, fmt, argptr );
+ ret = vsnprintf(dest, size, fmt, argptr);
#endif
// this shouldn't happen
- if( ret < 0 ) {
- if( size ) {
+ if (ret < 0) {
+ if (size) {
*dest = 0;
}
ret = 0;
}
- return ( size_t )ret;
+ return (size_t)ret;
}
/*
@@ -804,14 +822,15 @@ excluding trailing '\0'. If buffer size is 0, this function does nothing
and returns 0.
===============
*/
-size_t Q_vscnprintf( char *dest, size_t size, const char *fmt, va_list argptr ) {
+size_t Q_vscnprintf(char *dest, size_t size, const char *fmt, va_list argptr)
+{
size_t ret;
-
- if( !size ) {
+
+ if (!size) {
return 0;
}
- ret = Q_vsnprintf( dest, size, fmt, argptr );
+ ret = Q_vsnprintf(dest, size, fmt, argptr);
return ret >= size ? size - 1 : ret;
}
@@ -825,13 +844,14 @@ excluding trailing '\0'. If the returned value is equal to or greater than
buffer size, resulting string is truncated.
===============
*/
-size_t Q_snprintf( char *dest, size_t size, const char *fmt, ... ) {
+size_t Q_snprintf(char *dest, size_t size, const char *fmt, ...)
+{
va_list argptr;
size_t ret;
- va_start( argptr, fmt );
- ret = Q_vsnprintf( dest, size, fmt, argptr );
- va_end( argptr );
+ va_start(argptr, fmt);
+ ret = Q_vsnprintf(dest, size, fmt, argptr);
+ va_end(argptr);
return ret;
}
@@ -845,22 +865,24 @@ excluding trailing '\0'. If buffer size is 0, this function does nothing
and returns 0.
===============
*/
-size_t Q_scnprintf( char *dest, size_t size, const char *fmt, ... ) {
+size_t Q_scnprintf(char *dest, size_t size, const char *fmt, ...)
+{
va_list argptr;
size_t ret;
- va_start( argptr, fmt );
- ret = Q_vscnprintf( dest, size, fmt, argptr );
- va_end( argptr );
+ va_start(argptr, fmt);
+ ret = Q_vscnprintf(dest, size, fmt, argptr);
+ va_end(argptr);
return ret;
}
-char *Q_strchrnul( const char *s, int c ) {
- while( *s && *s != c ) {
+char *Q_strchrnul(const char *s, int c)
+{
+ while (*s && *s != c) {
s++;
}
- return ( char * )s;
+ return (char *)s;
}
/*
@@ -871,12 +893,13 @@ Copies no more than 'size' bytes stopping when 'c' character is found.
Returns pointer to next byte after 'c' in 'dst', or NULL if 'c' was not found.
===============
*/
-void *Q_memccpy( void *dst, const void *src, int c, size_t size ) {
+void *Q_memccpy(void *dst, const void *src, int c, size_t size)
+{
byte *d = dst;
const byte *s = src;
- while( size-- ) {
- if( ( *d++ = *s++ ) == c ) {
+ while (size--) {
+ if ((*d++ = *s++) == c) {
return d;
}
}
@@ -884,21 +907,22 @@ void *Q_memccpy( void *dst, const void *src, int c, size_t size ) {
return NULL;
}
-void Q_setenv( const char *name, const char *value ) {
+void Q_setenv(const char *name, const char *value)
+{
#ifdef _WIN32
- if( !value ) {
+ if (!value) {
value = "";
}
-#if( _MSC_VER >= 1400 )
- _putenv_s( name, value );
+#if (_MSC_VER >= 1400)
+ _putenv_s(name, value);
#else
- _putenv( va( "%s=%s", name, value ) );
+ _putenv(va("%s=%s", name, value));
#endif
#else // _WIN32
- if( value ) {
- setenv( name, value, 1 );
+ if (value) {
+ setenv(name, value, 1);
} else {
- unsetenv( name );
+ unsetenv(name);
}
#endif // !_WIN32
}
@@ -919,20 +943,21 @@ Searches the string for the given
key and returns the associated value, or an empty string.
===============
*/
-char *Info_ValueForKey( const char *s, const char *key ) {
+char *Info_ValueForKey(const char *s, const char *key)
+{
static char value[4][MAX_INFO_STRING]; // use 4 buffers so compares
- // work without stomping on each other
+ // work without stomping on each other
static int valueindex;
char pkey[MAX_INFO_STRING];
char *o;
valueindex++;
- if( *s == '\\' )
+ if (*s == '\\')
s++;
- while( 1 ) {
+ while (1) {
o = pkey;
- while( *s != '\\' ) {
- if( !*s )
+ while (*s != '\\') {
+ if (!*s)
return "";
*o++ = *s++;
}
@@ -940,15 +965,15 @@ char *Info_ValueForKey( const char *s, const char *key ) {
s++;
o = value[valueindex & 3];
- while( *s != '\\' && *s ) {
+ while (*s != '\\' && *s) {
*o++ = *s++;
}
*o = 0;
- if( !strcmp( key, pkey ) )
+ if (!strcmp(key, pkey))
return value[valueindex & 3];
- if( !*s )
+ if (!*s)
return "";
s++;
}
@@ -961,31 +986,32 @@ char *Info_ValueForKey( const char *s, const char *key ) {
Info_RemoveKey
==================
*/
-void Info_RemoveKey( char *s, const char *key ) {
+void Info_RemoveKey(char *s, const char *key)
+{
char *start;
char pkey[MAX_INFO_STRING];
char *o;
- while( 1 ) {
+ while (1) {
start = s;
- if( *s == '\\' )
+ if (*s == '\\')
s++;
o = pkey;
- while( *s != '\\' ) {
- if( !*s )
+ while (*s != '\\') {
+ if (!*s)
return;
*o++ = *s++;
}
*o = 0;
s++;
- while( *s != '\\' && *s ) {
+ while (*s != '\\' && *s) {
s++;
}
- if( !strcmp( key, pkey ) ) {
+ if (!strcmp(key, pkey)) {
o = start; // remove this part
- while( *s ) {
+ while (*s) {
*o++ = *s++;
}
*o = 0;
@@ -993,7 +1019,7 @@ void Info_RemoveKey( char *s, const char *key ) {
continue; // search for duplicates
}
- if( !*s )
+ if (!*s)
return;
}
@@ -1009,37 +1035,38 @@ can mess up the server's parsing.
Also checks the length of keys/values and the whole string.
==================
*/
-qboolean Info_Validate( const char *s ) {
+qboolean Info_Validate(const char *s)
+{
size_t len, total;
int c;
-
+
total = 0;
- while( 1 ) {
+ while (1) {
//
// validate key
//
- if( *s == '\\' ) {
+ if (*s == '\\') {
s++;
- if( ++total == MAX_INFO_STRING ) {
+ if (++total == MAX_INFO_STRING) {
return qfalse; // oversize infostring
}
}
- if( !*s ) {
+ if (!*s) {
return qfalse; // missing key
}
len = 0;
- while( *s != '\\' ) {
+ while (*s != '\\') {
c = *s++;
- if( !Q_isprint( c ) || c == '\"' || c == ';' ) {
+ if (!Q_isprint(c) || c == '\"' || c == ';') {
return qfalse; // illegal characters
}
- if( ++len == MAX_INFO_KEY ) {
+ if (++len == MAX_INFO_KEY) {
return qfalse; // oversize key
}
- if( ++total == MAX_INFO_STRING ) {
+ if (++total == MAX_INFO_STRING) {
return qfalse; // oversize infostring
}
- if( !*s ) {
+ if (!*s) {
return qfalse; // missing value
}
}
@@ -1048,25 +1075,25 @@ qboolean Info_Validate( const char *s ) {
// validate value
//
s++;
- if( ++total == MAX_INFO_STRING ) {
+ if (++total == MAX_INFO_STRING) {
return qfalse; // oversize infostring
}
- if( !*s ) {
+ if (!*s) {
return qfalse; // missing value
}
len = 0;
- while( *s != '\\' ) {
+ while (*s != '\\') {
c = *s++;
- if( !Q_isprint( c ) || c == '\"' || c == ';' ) {
+ if (!Q_isprint(c) || c == '\"' || c == ';') {
return qfalse; // illegal characters
}
- if( ++len == MAX_INFO_VALUE ) {
+ if (++len == MAX_INFO_VALUE) {
return qfalse; // oversize value
}
- if( ++total == MAX_INFO_STRING ) {
+ if (++total == MAX_INFO_STRING) {
return qfalse; // oversize infostring
}
- if( !*s ) {
+ if (!*s) {
return qtrue; // end of string
}
}
@@ -1080,18 +1107,19 @@ qboolean Info_Validate( const char *s ) {
Info_SubValidate
============
*/
-size_t Info_SubValidate( const char *s ) {
+size_t Info_SubValidate(const char *s)
+{
size_t len;
int c;
len = 0;
- while( *s ) {
+ while (*s) {
c = *s++;
c &= 127; // strip high bits
- if( c == '\\' || c == '\"' || c == ';' ) {
+ if (c == '\\' || c == '\"' || c == ';') {
return SIZE_MAX; // illegal characters
}
- if( ++len == MAX_QPATH ) {
+ if (++len == MAX_QPATH) {
return MAX_QPATH; // oversize value
}
}
@@ -1104,45 +1132,46 @@ size_t Info_SubValidate( const char *s ) {
Info_SetValueForKey
==================
*/
-qboolean Info_SetValueForKey( char *s, const char *key, const char *value ) {
+qboolean Info_SetValueForKey(char *s, const char *key, const char *value)
+{
char newi[MAX_INFO_STRING], *v;
size_t l, kl, vl;
int c;
// validate key
- kl = Info_SubValidate( key );
- if( kl >= MAX_QPATH ) {
+ kl = Info_SubValidate(key);
+ if (kl >= MAX_QPATH) {
return qfalse;
}
// validate value
- vl = Info_SubValidate( value );
- if( vl >= MAX_QPATH ) {
+ vl = Info_SubValidate(value);
+ if (vl >= MAX_QPATH) {
return qfalse;
}
- Info_RemoveKey( s, key );
- if( !vl ) {
+ Info_RemoveKey(s, key);
+ if (!vl) {
return qtrue;
}
- l = strlen( s );
- if( l + kl + vl + 2 >= MAX_INFO_STRING ) {
+ l = strlen(s);
+ if (l + kl + vl + 2 >= MAX_INFO_STRING) {
return qfalse;
}
newi[0] = '\\';
- memcpy( newi + 1, key, kl );
+ memcpy(newi + 1, key, kl);
newi[kl + 1] = '\\';
- memcpy( newi + kl + 2, value, vl + 1 );
+ memcpy(newi + kl + 2, value, vl + 1);
// only copy ascii values
s += l;
v = newi;
- while( *v ) {
+ while (*v) {
c = *v++;
c &= 127; // strip high bits
- if( Q_isprint( c ) )
+ if (Q_isprint(c))
*s++ = c;
}
*s = 0;
@@ -1155,50 +1184,51 @@ qboolean Info_SetValueForKey( char *s, const char *key, const char *value ) {
Info_NextPair
==================
*/
-void Info_NextPair( const char **string, char *key, char *value ) {
+void Info_NextPair(const char **string, char *key, char *value)
+{
char *o;
const char *s;
*value = *key = 0;
s = *string;
- if( !s ) {
+ if (!s) {
return;
}
- if( *s == '\\' )
+ if (*s == '\\')
s++;
- if( !*s ) {
+ if (!*s) {
*string = NULL;
return;
}
-
+
o = key;
- while( *s && *s != '\\' ) {
+ while (*s && *s != '\\') {
*o++ = *s++;
}
-
+
*o = 0;
- if( !*s ) {
+ if (!*s) {
*string = NULL;
return;
}
o = value;
s++;
- while( *s && *s != '\\' ) {
+ while (*s && *s != '\\') {
*o++ = *s++;
}
*o = 0;
- if( *s ) {
+ if (*s) {
s++;
}
*string = s;
-
+
}
/*
@@ -1206,21 +1236,22 @@ void Info_NextPair( const char **string, char *key, char *value ) {
Info_Print
==================
*/
-void Info_Print( const char *infostring ) {
+void Info_Print(const char *infostring)
+{
char key[MAX_INFO_STRING];
char value[MAX_INFO_STRING];
- while( infostring ) {
- Info_NextPair( &infostring, key, value );
-
- if( !key[0] ) {
+ while (infostring) {
+ Info_NextPair(&infostring, key, value);
+
+ if (!key[0]) {
break;
}
- if( value[0] ) {
- Com_Printf( "%-20s %s\n", key, value );
+ if (value[0]) {
+ Com_Printf("%-20s %s\n", key, value);
} else {
- Com_Printf( "%-20s <MISSING VALUE>\n", key );
+ Com_Printf("%-20s <MISSING VALUE>\n", key);
}
}
}