ReGameDLL_CS/regamedll/game_shared/shared_util.cpp

211 lines
3.5 KiB
C++
Raw Normal View History

2015-06-30 12:46:07 +03:00
#include "precompiled.h"
2017-10-12 17:50:56 +03:00
char s_shared_token[1500];
char s_shared_quote = '\"';
2015-06-30 12:46:07 +03:00
NOXREF wchar_t *SharedWVarArgs(wchar_t *format, ...)
2015-06-30 12:46:07 +03:00
{
va_list argptr;
const int BufLen = 1024;
const int NumBuffers = 4;
static wchar_t string[NumBuffers][BufLen];
static int curstring = 0;
curstring = (curstring + 1) % NumBuffers;
va_start(argptr, format);
Q_vsnwprintf(string[curstring], BufLen, format, argptr);
va_end(argptr);
return string[curstring];
2015-06-30 12:46:07 +03:00
}
char *SharedVarArgs(char *format, ...)
{
va_list argptr;
const int BufLen = 1024;
const int NumBuffers = 4;
static char string[NumBuffers][BufLen];
2015-06-30 12:46:07 +03:00
static int curstring = 0;
curstring = (curstring + 1) % NumBuffers;
va_start(argptr, format);
Q_vsnprintf(string[curstring], BufLen, format, argptr);
2015-06-30 12:46:07 +03:00
va_end(argptr);
return string[curstring];
2015-06-30 12:46:07 +03:00
}
char *BufPrintf(char *buf, int &len, const char *fmt, ...)
{
2015-09-16 23:19:21 +03:00
va_list argptr;
if (len > 0)
{
va_start(argptr, fmt);
Q_vsnprintf(buf, len, fmt, argptr);
va_end(argptr);
len -= Q_strlen(buf);
return buf + Q_strlen(buf);
}
2017-10-12 17:50:56 +03:00
return nullptr;
2015-06-30 12:46:07 +03:00
}
wchar_t *BufWPrintf(wchar_t *buf, int &len, const wchar_t *fmt, ...)
2015-06-30 12:46:07 +03:00
{
if (len <= 0)
2017-10-12 17:50:56 +03:00
return nullptr;
va_list argptr;
va_start(argptr, fmt);
Q_vsnwprintf(buf, len, fmt, argptr);
va_end(argptr);
len -= wcslen(buf);
return buf + wcslen(buf);
2015-06-30 12:46:07 +03:00
}
NOXREF const wchar_t *NumAsWString(int val)
2015-06-30 12:46:07 +03:00
{
const int BufLen = 16;
const int NumBuffers = 4;
static wchar_t string[NumBuffers][BufLen];
static int curstring = 0;
curstring = (curstring + 1) % NumBuffers;
int len = BufLen;
BufWPrintf(string[curstring], len, L"%d", val);
return string[curstring];
2015-06-30 12:46:07 +03:00
}
const char *NumAsString(int val)
{
const int BufLen = 16;
const int NumBuffers = 4;
static char string[NumBuffers][BufLen];
2015-06-30 12:46:07 +03:00
static int curstring = 0;
int len = 16;
2017-10-12 17:50:56 +03:00
curstring = (curstring + 1) % NumBuffers;
2015-06-30 12:46:07 +03:00
BufPrintf(string[curstring], len, "%d", val);
return string[curstring];
}
// Returns the token parsed by SharedParse()
char *SharedGetToken()
2015-06-30 12:46:07 +03:00
{
return s_shared_token;
}
// Returns the token parsed by SharedParse()
NOXREF void SharedSetQuoteChar(char c)
{
s_shared_quote = c;
}
// Parse a token out of a string
2017-10-12 17:50:56 +03:00
char *SharedParse(char *data)
2015-06-30 12:46:07 +03:00
{
int c;
int len;
len = 0;
s_shared_token[0] = '\0';
if (!data)
2017-10-12 17:50:56 +03:00
return nullptr;
// skip whitespace
skipwhite:
while ((c = *data) <= ' ')
{
if (c == 0)
{
// end of file;
2017-10-12 17:50:56 +03:00
return nullptr;
}
data++;
}
2017-10-12 17:50:56 +03:00
// skip // comments till the next line
if (c == '/' && data[1] == '/')
{
while (*data && *data != '\n')
data++;
2017-10-12 17:50:56 +03:00
// start over new line
goto skipwhite;
}
2017-10-12 17:50:56 +03:00
// handle quoted strings specially: copy till the end or another quote
if (c == s_shared_quote)
{
2017-10-12 17:50:56 +03:00
// skip starting quote
data++;
while (true)
{
2017-10-12 17:50:56 +03:00
// get char and advance
c = *data++;
if (c == s_shared_quote || !c)
{
s_shared_token[len] = '\0';
return data;
}
2017-10-12 17:50:56 +03:00
s_shared_token[len++] = c;
}
}
// parse single characters
if (c == '{' || c == '}'|| c == ')'|| c == '(' || c == '\'' || c == ',')
{
2017-10-12 17:50:56 +03:00
s_shared_token[len++] = c;
s_shared_token[len] = '\0';
return data + 1;
}
// parse a regular word
do
{
s_shared_token[len] = c;
data++;
len++;
c = *data;
if (c == '{' || c == '}'|| c == ')'|| c == '(' || c == '\'' || c == ',')
break;
}
while (c > 32);
s_shared_token[len] = '\0';
return data;
2015-06-30 12:46:07 +03:00
}
// Returns true if additional data is waiting to be processed on this line
2017-10-12 17:50:56 +03:00
bool SharedTokenWaiting(const char *buffer)
2015-06-30 12:46:07 +03:00
{
const char *p;
p = buffer;
2017-10-12 17:50:56 +03:00
while (*p && *p != '\n')
{
2017-10-12 17:50:56 +03:00
if (!isspace(*p) || isalnum(*p))
return true;
p++;
}
return false;
2015-06-30 12:46:07 +03:00
}