2004-10-15 00:35:53 +00:00
|
|
|
/* An implementation of some 'standard' functions */
|
|
|
|
|
|
|
|
/* We use this with msvc too, because msvc's implementations of
|
|
|
|
_snprintf and _vsnprint differ - besides, this way we can make
|
|
|
|
sure qvms handle all the printf stuff that dlls do*/
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2019-01-20 16:26:50 +00:00
|
|
|
#define longlong long
|
|
|
|
|
2004-10-15 00:35:53 +00:00
|
|
|
/*
|
|
|
|
this is a fairly basic implementation.
|
|
|
|
don't expect it to do much.
|
|
|
|
You can probably get a better version from somewhere.
|
|
|
|
*/
|
2013-05-11 14:02:55 +00:00
|
|
|
int Q_vsnprintf(char *buffer, size_t maxlen, const char *format, va_list vargs)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
|
|
|
int tokens=0;
|
2018-08-04 19:00:19 +00:00
|
|
|
const char *string_;
|
2004-10-15 00:35:53 +00:00
|
|
|
char *string;
|
|
|
|
char tempbuffer[64];
|
2015-06-29 23:46:31 +00:00
|
|
|
char sign;
|
2019-01-20 16:26:50 +00:00
|
|
|
unsigned longlong uint_;
|
|
|
|
longlong int_;
|
2018-08-04 19:00:19 +00:00
|
|
|
float float_;
|
2004-10-15 00:35:53 +00:00
|
|
|
int i;
|
2013-06-23 18:43:59 +00:00
|
|
|
int use0s;
|
2015-10-27 15:20:15 +00:00
|
|
|
int width, useprepad, plus;
|
|
|
|
int precision;
|
2019-01-20 16:26:50 +00:00
|
|
|
int lengthspec;
|
|
|
|
int base, firstletter;
|
2004-10-15 00:35:53 +00:00
|
|
|
|
|
|
|
if (!maxlen)
|
|
|
|
return 0;
|
|
|
|
maxlen--;
|
|
|
|
|
|
|
|
while(*format)
|
|
|
|
{
|
|
|
|
switch(*format)
|
|
|
|
{
|
|
|
|
case '%':
|
2013-06-29 16:01:07 +00:00
|
|
|
plus = 0;
|
2015-10-27 15:20:15 +00:00
|
|
|
width= 0;
|
|
|
|
precision=-1;
|
2006-01-21 00:07:47 +00:00
|
|
|
useprepad=0;
|
2015-10-27 15:20:15 +00:00
|
|
|
use0s= 0;
|
2019-01-20 16:26:50 +00:00
|
|
|
lengthspec = 0;
|
2004-10-15 00:35:53 +00:00
|
|
|
retry:
|
|
|
|
switch(*(++format))
|
|
|
|
{
|
2006-01-21 00:07:47 +00:00
|
|
|
case '-':
|
|
|
|
useprepad=true;
|
|
|
|
goto retry;
|
2013-06-29 16:01:07 +00:00
|
|
|
case '+':
|
|
|
|
plus = true;
|
|
|
|
goto retry;
|
2015-10-27 15:20:15 +00:00
|
|
|
case '.':
|
|
|
|
precision = 0;
|
|
|
|
while (format[1] >= '0' && format[1] <= '9')
|
|
|
|
precision = precision*10+*++format-'0';
|
|
|
|
goto retry;
|
2019-01-20 16:26:50 +00:00
|
|
|
case 'l':
|
|
|
|
case 'h':
|
|
|
|
case 'j':
|
|
|
|
case 'z':
|
|
|
|
case 't':
|
|
|
|
case 'L':
|
|
|
|
lengthspec = (lengthspec<<8) | *format;
|
|
|
|
goto retry;
|
2004-10-15 00:35:53 +00:00
|
|
|
case '0':
|
2015-10-27 15:20:15 +00:00
|
|
|
if (!width)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
|
|
|
use0s=true;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
2015-10-27 15:20:15 +00:00
|
|
|
width=width*10+*format-'0';
|
2004-10-15 00:35:53 +00:00
|
|
|
goto retry;
|
|
|
|
case '%': /*emit a %*/
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
|
|
|
*buffer++ = *format;
|
|
|
|
break;
|
|
|
|
case 's':
|
2019-01-20 16:26:50 +00:00
|
|
|
if (!lengthspec)
|
|
|
|
string_ = va_arg(vargs, char *);
|
|
|
|
else
|
|
|
|
goto badformat;
|
2018-08-04 19:00:19 +00:00
|
|
|
if (!string_)
|
|
|
|
string_ = "(null)";
|
2015-10-27 15:20:15 +00:00
|
|
|
if (width)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
2018-08-04 19:00:19 +00:00
|
|
|
while (*string_ && width--)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
2018-08-04 19:00:19 +00:00
|
|
|
*buffer++ = *string_++;
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-04 19:00:19 +00:00
|
|
|
while (*string_)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
2018-08-04 19:00:19 +00:00
|
|
|
*buffer++ = *string_++;
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tokens++;
|
|
|
|
break;
|
|
|
|
case 'c':
|
2019-01-20 16:26:50 +00:00
|
|
|
if (!lengthspec)
|
|
|
|
int_ = va_arg(vargs, int);
|
|
|
|
else
|
|
|
|
goto badformat;
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
2018-08-04 19:00:19 +00:00
|
|
|
*buffer++ = int_;
|
2004-10-15 00:35:53 +00:00
|
|
|
tokens++;
|
|
|
|
break;
|
2016-07-12 00:40:13 +00:00
|
|
|
case 'p':
|
|
|
|
if (1)
|
2019-01-20 16:26:50 +00:00
|
|
|
{
|
|
|
|
if (!lengthspec)
|
|
|
|
uint_ = (size_t)va_arg(vargs, void*);
|
|
|
|
else
|
|
|
|
goto badformat;
|
|
|
|
}
|
2016-07-12 00:40:13 +00:00
|
|
|
else
|
2005-11-30 00:47:59 +00:00
|
|
|
case 'x':
|
2019-01-20 16:26:50 +00:00
|
|
|
case 'X':
|
|
|
|
case 'u':
|
|
|
|
{
|
|
|
|
if (!lengthspec)
|
|
|
|
uint_ = va_arg(vargs, unsigned int);
|
|
|
|
else if (lengthspec == 'l')
|
|
|
|
uint_ = va_arg(vargs, long);
|
|
|
|
else if (lengthspec == (('l'<<8)|'l'))
|
|
|
|
uint_ = va_arg(vargs, longlong);
|
|
|
|
else if (lengthspec == 'z')
|
|
|
|
int_ = va_arg(vargs, size_t);
|
|
|
|
else
|
|
|
|
goto badformat;
|
|
|
|
}
|
|
|
|
base = (*format=='u')?10:16;
|
|
|
|
firstletter = (*format=='X')?'A':'a';
|
2005-11-30 00:47:59 +00:00
|
|
|
i = sizeof(tempbuffer)-2;
|
2013-05-03 04:29:36 +00:00
|
|
|
tempbuffer[i+1] = '\0';
|
2018-08-04 19:00:19 +00:00
|
|
|
while(uint_)
|
2005-11-30 00:47:59 +00:00
|
|
|
{
|
2019-01-20 16:26:50 +00:00
|
|
|
tempbuffer[i] = (uint_%base) + '0';
|
2005-12-01 01:16:55 +00:00
|
|
|
if (tempbuffer[i] > '9')
|
2019-01-20 16:26:50 +00:00
|
|
|
tempbuffer[i] = tempbuffer[i] - ':' + firstletter;
|
|
|
|
uint_/=base;
|
2005-11-30 00:47:59 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
string = tempbuffer+i+1;
|
|
|
|
|
|
|
|
if (!*string)
|
|
|
|
{
|
|
|
|
i=61;
|
|
|
|
string = tempbuffer+i+1;
|
|
|
|
string[0] = '0';
|
|
|
|
string[1] = '\0';
|
|
|
|
}
|
|
|
|
|
2015-10-27 15:20:15 +00:00
|
|
|
width -= 62-i;
|
|
|
|
while (width>0)
|
2005-11-30 00:47:59 +00:00
|
|
|
{
|
|
|
|
string--;
|
|
|
|
if (use0s)
|
|
|
|
*string = '0';
|
|
|
|
else
|
|
|
|
*string = ' ';
|
2015-10-27 15:20:15 +00:00
|
|
|
width--;
|
2005-11-30 00:47:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (*string)
|
|
|
|
{
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2005-11-30 00:47:59 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
|
|
|
*buffer++ = *string++;
|
|
|
|
}
|
|
|
|
tokens++;
|
|
|
|
break;
|
2004-10-15 00:35:53 +00:00
|
|
|
case 'd':
|
|
|
|
case 'i':
|
2019-01-20 16:26:50 +00:00
|
|
|
if (!lengthspec)
|
|
|
|
int_ = va_arg(vargs, int);
|
|
|
|
else if (lengthspec == 'l')
|
|
|
|
int_ = va_arg(vargs, long);
|
|
|
|
else if (lengthspec == (('l'<<8)|'l'))
|
|
|
|
int_ = va_arg(vargs, longlong);
|
|
|
|
// else if (lengthspec == (('h'<<8)|'h'))
|
|
|
|
// int_ = va_arg(vargs, char);
|
|
|
|
// else if (lengthspec == 'h')
|
|
|
|
// int_ = va_arg(vargs, short);
|
|
|
|
// else if (lengthspec == 'j')
|
|
|
|
// int_ = va_arg(vargs, intmax_t);
|
|
|
|
else if (lengthspec == 'z')
|
|
|
|
int_ = va_arg(vargs, size_t);
|
|
|
|
// else if (lengthspec == 't')
|
|
|
|
// int_ = va_arg(vargs, ptrdiff_t);
|
|
|
|
else
|
|
|
|
goto badformat;
|
|
|
|
base = 10;
|
2006-01-21 00:07:47 +00:00
|
|
|
if (useprepad)
|
|
|
|
{
|
2013-06-23 18:43:59 +00:00
|
|
|
/*
|
2018-08-04 19:00:19 +00:00
|
|
|
if (int_ >= 1000)
|
2006-01-21 00:07:47 +00:00
|
|
|
useprepad = 4;
|
2018-08-04 19:00:19 +00:00
|
|
|
else if (int_ >= 100)
|
2006-01-21 00:07:47 +00:00
|
|
|
useprepad = 3;
|
2018-08-04 19:00:19 +00:00
|
|
|
else if (int_ >= 10)
|
2006-01-21 00:07:47 +00:00
|
|
|
useprepad = 2;
|
2018-08-04 19:00:19 +00:00
|
|
|
else if (int_ >= 0)
|
2006-01-21 00:07:47 +00:00
|
|
|
useprepad = 1;
|
2018-08-04 19:00:19 +00:00
|
|
|
else if (int_ <= -1000)
|
2006-01-21 00:07:47 +00:00
|
|
|
useprepad = 5;
|
2018-08-04 19:00:19 +00:00
|
|
|
else if (int_ <= -100)
|
2006-01-21 00:07:47 +00:00
|
|
|
useprepad = 4;
|
2018-08-04 19:00:19 +00:00
|
|
|
else if (int_ <= -10)
|
2006-01-21 00:07:47 +00:00
|
|
|
useprepad = 3;
|
|
|
|
else
|
|
|
|
useprepad = 2;
|
|
|
|
|
|
|
|
useprepad = precision - useprepad;
|
|
|
|
Con_Printf("add %i chars\n", useprepad);
|
|
|
|
while (useprepad>0)
|
|
|
|
{
|
|
|
|
if (--maxlen < 0)
|
|
|
|
{*buffer++='\0';return tokens;}
|
|
|
|
*buffer++ = ' ';
|
|
|
|
useprepad--;
|
|
|
|
}
|
|
|
|
Con_Printf("%i bytes left\n", maxlen);
|
|
|
|
*/
|
2013-06-23 18:43:59 +00:00
|
|
|
}
|
2018-08-04 19:00:19 +00:00
|
|
|
if (int_ < 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
2015-06-29 23:46:31 +00:00
|
|
|
sign = '-';
|
2018-08-04 19:00:19 +00:00
|
|
|
int_ *= -1;
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
2013-06-29 16:01:07 +00:00
|
|
|
else if (plus)
|
2015-06-29 23:46:31 +00:00
|
|
|
sign = '+';
|
|
|
|
else
|
|
|
|
sign = 0;
|
2004-10-15 00:35:53 +00:00
|
|
|
i = sizeof(tempbuffer)-2;
|
|
|
|
tempbuffer[sizeof(tempbuffer)-1] = '\0';
|
2018-08-04 19:00:19 +00:00
|
|
|
while(int_)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
2019-01-20 16:26:50 +00:00
|
|
|
tempbuffer[i--] = int_%base + '0';
|
|
|
|
if (tempbuffer[i] > '9')
|
|
|
|
tempbuffer[i] = tempbuffer[i] - ':' + 'a';
|
|
|
|
int_/=base;
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
2015-06-29 23:46:31 +00:00
|
|
|
if (sign)
|
|
|
|
tempbuffer[i--] = sign;
|
2004-10-15 00:35:53 +00:00
|
|
|
string = tempbuffer+i+1;
|
|
|
|
|
|
|
|
if (!*string)
|
|
|
|
{
|
|
|
|
i=61;
|
|
|
|
string = tempbuffer+i+1;
|
|
|
|
string[0] = '0';
|
|
|
|
string[1] = '\0';
|
|
|
|
}
|
|
|
|
|
2015-10-27 15:20:15 +00:00
|
|
|
width -= 62-i;
|
|
|
|
/* while (width>0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
|
|
|
string--;
|
2006-01-21 00:07:47 +00:00
|
|
|
*string = ' ';
|
2015-10-27 15:20:15 +00:00
|
|
|
width--;
|
2006-01-21 00:07:47 +00:00
|
|
|
}
|
|
|
|
*/
|
2015-10-27 15:20:15 +00:00
|
|
|
while(width>0)
|
2006-01-21 00:07:47 +00:00
|
|
|
{
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2006-01-21 00:07:47 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
2004-10-15 00:35:53 +00:00
|
|
|
if (use0s)
|
2006-01-21 00:07:47 +00:00
|
|
|
*buffer++ = '0';
|
2004-10-15 00:35:53 +00:00
|
|
|
else
|
2006-01-21 00:07:47 +00:00
|
|
|
*buffer++ = ' ';
|
2015-10-27 15:20:15 +00:00
|
|
|
width--;
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (*string)
|
|
|
|
{
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
|
|
|
*buffer++ = *string++;
|
|
|
|
}
|
|
|
|
tokens++;
|
|
|
|
break;
|
|
|
|
case 'f':
|
2019-01-20 16:26:50 +00:00
|
|
|
if (!lengthspec)
|
|
|
|
float_ = (float)va_arg(vargs, double);
|
|
|
|
else if (lengthspec == 'L')
|
|
|
|
float_ = (float)va_arg(vargs, long double);
|
|
|
|
else
|
|
|
|
goto badformat;
|
2004-10-15 00:35:53 +00:00
|
|
|
//integer part.
|
2019-01-20 16:26:50 +00:00
|
|
|
int_ = (longlong)float_;
|
2018-08-04 19:00:19 +00:00
|
|
|
if (int_ < 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
|
|
|
*buffer++ = '-';
|
2018-08-04 19:00:19 +00:00
|
|
|
int_ *= -1;
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
|
|
|
i = sizeof(tempbuffer)-2;
|
|
|
|
tempbuffer[sizeof(tempbuffer)-1] = '\0';
|
2018-08-04 19:00:19 +00:00
|
|
|
if (!int_)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
2005-07-20 11:50:00 +00:00
|
|
|
tempbuffer[i--] = '0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-04 19:00:19 +00:00
|
|
|
while(int_)
|
2005-07-20 11:50:00 +00:00
|
|
|
{
|
2018-08-04 19:00:19 +00:00
|
|
|
tempbuffer[i--] = int_%10 + '0';
|
|
|
|
int_/=10;
|
2005-07-20 11:50:00 +00:00
|
|
|
}
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
|
|
|
string = tempbuffer+i+1;
|
|
|
|
while (*string)
|
|
|
|
{
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
|
|
|
*buffer++ = *string++;
|
|
|
|
}
|
|
|
|
|
2018-08-04 19:00:19 +00:00
|
|
|
int_ = sizeof(tempbuffer)-2-i;
|
2004-10-15 00:35:53 +00:00
|
|
|
|
|
|
|
//floating point part.
|
2019-01-20 16:26:50 +00:00
|
|
|
float_ -= (longlong)float_;
|
2004-10-15 00:35:53 +00:00
|
|
|
i = 0;
|
|
|
|
tempbuffer[i++] = '.';
|
2015-10-27 15:20:15 +00:00
|
|
|
if (precision < 0)
|
|
|
|
precision = 7;
|
2019-01-20 16:26:50 +00:00
|
|
|
while(float_ - (longlong)float_)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
2015-10-27 15:20:15 +00:00
|
|
|
if (i > precision) //remove the excess presision.
|
2004-10-15 00:35:53 +00:00
|
|
|
break;
|
|
|
|
|
2018-08-04 19:00:19 +00:00
|
|
|
float_*=10;
|
2019-01-20 16:26:50 +00:00
|
|
|
tempbuffer[i++] = (longlong)float_%10 + '0';
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
|
|
|
if (i == 1) //no actual fractional part
|
|
|
|
{
|
|
|
|
tokens++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//concatinate to our string
|
|
|
|
tempbuffer[i] = '\0';
|
|
|
|
string = tempbuffer;
|
|
|
|
while (*string)
|
|
|
|
{
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
|
|
|
*buffer++ = *string++;
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens++;
|
|
|
|
break;
|
|
|
|
default:
|
2019-01-20 16:26:50 +00:00
|
|
|
badformat:
|
2018-08-04 19:00:19 +00:00
|
|
|
string_ = "ERROR IN FORMAT";
|
|
|
|
while (*string_)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
2018-08-04 19:00:19 +00:00
|
|
|
*buffer++ = *string_++;
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
2019-01-20 16:26:50 +00:00
|
|
|
*buffer++='\0';
|
|
|
|
return tokens;
|
2004-10-15 00:35:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-03-31 04:21:08 +00:00
|
|
|
if (maxlen-- == 0)
|
2004-10-15 00:35:53 +00:00
|
|
|
{*buffer++='\0';return tokens;}
|
|
|
|
*buffer++ = *format;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
{*buffer++='\0';return tokens;}
|
|
|
|
}
|
|
|
|
|
2013-05-11 14:02:55 +00:00
|
|
|
int Q_snprintf(char *buffer, size_t maxlen, const char *format, ...)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
|
|
|
int p;
|
|
|
|
va_list argptr;
|
|
|
|
|
|
|
|
va_start (argptr, format);
|
2013-06-23 18:43:59 +00:00
|
|
|
p = Q_vsnprintf (buffer, maxlen, format,argptr);
|
2004-10-15 00:35:53 +00:00
|
|
|
va_end (argptr);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef Q3_VM
|
2013-05-11 14:02:55 +00:00
|
|
|
//libc functions that are actually properly supported on all other platforms (c89)
|
2004-10-15 00:35:53 +00:00
|
|
|
int strlen(const char *s)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
while(*s++)
|
|
|
|
len++;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2005-11-27 01:12:16 +00:00
|
|
|
int strncmp (const char *s1, const char *s2, int count)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (!count--)
|
|
|
|
return 0;
|
|
|
|
if (*s1 != *s2)
|
|
|
|
return -1; // strings not equal
|
|
|
|
if (!*s1)
|
|
|
|
return 0; // strings are equal
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-27 03:18:43 +00:00
|
|
|
int strnicmp(const char *s1, const char *s2, int count)
|
|
|
|
{
|
|
|
|
char c1, c2;
|
|
|
|
char ct;
|
|
|
|
while(*s1)
|
|
|
|
{
|
|
|
|
if (!count--)
|
|
|
|
return 0;
|
|
|
|
c1 = *s1;
|
|
|
|
c2 = *s2;
|
|
|
|
if (c1 != c2)
|
|
|
|
{
|
|
|
|
if (c1 >= 'a' && c1 <= 'z') c1 = c1-'a' + 'A';
|
|
|
|
if (c2 >= 'a' && c2 <= 'z') c2 = c2-'a' + 'A';
|
|
|
|
if (c1 != c2)
|
|
|
|
return c1<c2?-1:1;
|
|
|
|
}
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
if (*s2) //s2 was longer.
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-27 01:12:16 +00:00
|
|
|
int strcmp(const char *s1, const char *s2)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
|
|
|
while(*s1)
|
|
|
|
{
|
|
|
|
if (*s1 != *s2)
|
2005-11-27 03:18:43 +00:00
|
|
|
return *s1<*s2?-1:1;
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
if (*s2) //s2 was longer.
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stricmp(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
char c1, c2;
|
|
|
|
char ct;
|
|
|
|
while(*s1)
|
|
|
|
{
|
|
|
|
c1 = *s1;
|
|
|
|
c2 = *s2;
|
|
|
|
if (c1 != c2)
|
|
|
|
{
|
|
|
|
if (c1 >= 'a' && c1 <= 'z') c1 = c1-'a' + 'A';
|
|
|
|
if (c2 >= 'a' && c2 <= 'z') c2 = c2-'a' + 'A';
|
|
|
|
if (c1 != c2)
|
|
|
|
return c1<c2?-1:1;
|
|
|
|
}
|
2004-10-15 00:35:53 +00:00
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
if (*s2) //s2 was longer.
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-27 01:12:16 +00:00
|
|
|
char *strstr(char *str, const char *sub)
|
2004-10-15 00:35:53 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char *p2;
|
|
|
|
int l = strlen(sub)-1;
|
|
|
|
if (l < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
if (*str == *sub)
|
|
|
|
{
|
|
|
|
if (!strncmp (str+1, sub+1, l))
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-11-27 01:12:16 +00:00
|
|
|
char *strchr(char *str, char sub)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char *p2;
|
|
|
|
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
if (*str == sub)
|
|
|
|
return str;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int atoi(char *str)
|
|
|
|
{
|
|
|
|
int sign;
|
2005-11-28 00:05:17 +00:00
|
|
|
int num = 0;
|
2005-11-27 01:12:16 +00:00
|
|
|
int base = 10;
|
|
|
|
|
|
|
|
while(*(unsigned char*)str < ' ' && *str)
|
|
|
|
str++;
|
|
|
|
|
|
|
|
if (*str == '-')
|
|
|
|
{
|
|
|
|
sign = -1;
|
|
|
|
str++;
|
|
|
|
}
|
2005-11-30 01:02:10 +00:00
|
|
|
else sign = 1;
|
2005-11-27 01:12:16 +00:00
|
|
|
|
|
|
|
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
|
|
|
|
{
|
|
|
|
base = 16;
|
|
|
|
str += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if (*str >= '0' && *str <= '9')
|
|
|
|
num = num*base + (*str - '0');
|
|
|
|
else if (*str >= 'a' && *str <= 'a'+base-10)
|
|
|
|
num = num*base + (*str - 'a')+10;
|
|
|
|
else if (*str >= 'A' && *str <= 'A'+base-10)
|
|
|
|
num = num*base + (*str - 'A')+10;
|
|
|
|
else break; //bad char
|
2005-11-30 01:02:10 +00:00
|
|
|
str++;
|
2005-11-27 01:12:16 +00:00
|
|
|
}
|
|
|
|
return num*sign;
|
|
|
|
}
|
2005-11-30 01:02:10 +00:00
|
|
|
|
2005-11-28 00:05:17 +00:00
|
|
|
float atof(char *str)
|
|
|
|
{
|
|
|
|
int sign;
|
|
|
|
float num = 0.0f;
|
|
|
|
float unit = 1;
|
|
|
|
|
|
|
|
while(*(unsigned char*)str < ' ' && *str)
|
|
|
|
str++;
|
|
|
|
|
|
|
|
if (*str == '-')
|
|
|
|
{
|
|
|
|
sign = -1;
|
|
|
|
str++;
|
|
|
|
}
|
2005-11-30 01:02:10 +00:00
|
|
|
else sign = 1;
|
2005-11-28 00:05:17 +00:00
|
|
|
|
|
|
|
while(1)
|
|
|
|
{//each time we find a new digit, increase the value of the previous digets by a factor of ten, and add the new
|
|
|
|
if (*str >= '0' && *str <= '9')
|
|
|
|
num = num*10 + (*str - '0');
|
|
|
|
else break; //bad char
|
2005-11-30 01:02:10 +00:00
|
|
|
str++;
|
2005-11-28 00:05:17 +00:00
|
|
|
}
|
|
|
|
if (*str == '.')
|
|
|
|
{ //each time we find a new digit, decrease the value of the following digits.
|
2005-12-01 01:16:55 +00:00
|
|
|
str++;
|
2005-11-28 00:05:17 +00:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if (*str >= '0' && *str <= '9')
|
|
|
|
{
|
|
|
|
unit /= 10;
|
|
|
|
num = num + (*str - '0')*unit;
|
|
|
|
}
|
|
|
|
else break; //bad char
|
2005-11-30 01:02:10 +00:00
|
|
|
str++;
|
2005-11-28 00:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return num*sign;
|
|
|
|
}
|
2005-11-27 01:12:16 +00:00
|
|
|
|
|
|
|
void strcpy(char *d, const char *s)
|
|
|
|
{
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
*d++ = *s++;
|
|
|
|
}
|
|
|
|
*d='\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static long randx = 1;
|
|
|
|
void srand(unsigned int x)
|
|
|
|
{
|
|
|
|
randx = x;
|
|
|
|
}
|
|
|
|
int getseed(void)
|
|
|
|
{
|
|
|
|
return randx;
|
|
|
|
}
|
|
|
|
int rand(void)
|
|
|
|
{
|
|
|
|
return(((randx = randx*1103515245 + 12345)>>16) & 077777);
|
|
|
|
}
|
2004-10-15 00:35:53 +00:00
|
|
|
#endif
|
2005-07-20 11:50:00 +00:00
|
|
|
|
2017-10-12 12:02:25 +00:00
|
|
|
void Q_strlncpy(char *d, const char *s, int sizeofd, int lenofs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
sizeofd--;
|
|
|
|
if (sizeofd < 0)
|
|
|
|
return; //this could be an error
|
|
|
|
|
|
|
|
for (i=0; lenofs-- > 0; i++)
|
|
|
|
{
|
|
|
|
if (i == sizeofd)
|
|
|
|
break;
|
|
|
|
*d++ = *s++;
|
|
|
|
}
|
|
|
|
*d='\0';
|
|
|
|
}
|
|
|
|
|
2013-05-11 14:02:55 +00:00
|
|
|
void Q_strlcpy(char *d, const char *s, int n)
|
2005-07-20 11:50:00 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
n--;
|
|
|
|
if (n < 0)
|
|
|
|
return; //this could be an error
|
|
|
|
|
|
|
|
for (i=0; *s; i++)
|
|
|
|
{
|
|
|
|
if (i == n)
|
|
|
|
break;
|
|
|
|
*d++ = *s++;
|
|
|
|
}
|
|
|
|
*d='\0';
|
|
|
|
}
|
2013-06-23 02:33:52 +00:00
|
|
|
void Q_strlcat(char *d, const char *s, int n)
|
|
|
|
{
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
int dlen = strlen(d);
|
|
|
|
int slen = strlen(s)+1;
|
2013-06-23 18:43:59 +00:00
|
|
|
if (slen > (n-1)-dlen)
|
|
|
|
slen = (n-1)-dlen;
|
|
|
|
memcpy(d+dlen, s, slen);
|
2013-06-23 02:33:52 +00:00
|
|
|
d[n - 1] = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-07-12 00:40:13 +00:00
|
|
|
|
|
|
|
char *Plug_Info_ValueForKey (const char *s, const char *key, char *out, size_t outsize)
|
|
|
|
{
|
|
|
|
int isvalue = 0;
|
|
|
|
const char *start;
|
|
|
|
char *oout = out;
|
|
|
|
*out = 0;
|
|
|
|
if (*s != '\\')
|
|
|
|
return out; //gah, get lost with your corrupt infostrings.
|
|
|
|
|
|
|
|
start = ++s;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
while(s[0] == '\\' && s[1] == '\\')
|
|
|
|
s+=2;
|
|
|
|
if (s[0] != '\\' && *s)
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//okay, it terminates here
|
|
|
|
isvalue = !isvalue;
|
|
|
|
if (isvalue)
|
|
|
|
{
|
2018-08-04 19:00:19 +00:00
|
|
|
if (strlen(key) == (size_t)(s - start) && !strncmp(start, key, s - start))
|
2016-07-12 00:40:13 +00:00
|
|
|
{
|
|
|
|
s++;
|
|
|
|
while (outsize --> 1)
|
|
|
|
{
|
|
|
|
if (s[0] == '\\' && s[1] == '\\')
|
|
|
|
s++;
|
|
|
|
else if (s[0] == '\\' || !s[0])
|
|
|
|
break;
|
|
|
|
*out++ = *s++;
|
|
|
|
}
|
|
|
|
*out++ = 0;
|
|
|
|
return oout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*s)
|
|
|
|
start = ++s;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return oout;
|
|
|
|
}
|