1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: dll-agnostic routines (no dll dependencies here)
//
// $NoKeywords: $
//=============================================================================//
// Author: Matthew D. Campbell ([email protected]), 2003
#include "cbase.h"
#include <ctype.h>
#include "shared_util.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
static char s_shared_token[ 1500 ];
static char s_shared_quote = '\"';
//--------------------------------------------------------------------------------------------------------------
char * SharedVarArgs(const char *format, ...)
{
va_list argptr;
const int BufLen = 1024;
const int NumBuffers = 4;
static char string[NumBuffers][BufLen];
static int curstring = 0;
curstring = ( curstring + 1 ) % NumBuffers;
va_start (argptr, format);
V_vsprintf_safe( string[curstring], format, argptr );
va_end (argptr);
return string[curstring];
}
//--------------------------------------------------------------------------------------------------------------
char * BufPrintf(char *buf, int& len, const char *fmt, ...)
{
if (len <= 0)
return NULL;
va_list argptr;
va_start(argptr, fmt);
_vsnprintf(buf, len, fmt, argptr);
buf[ len - 1 ] = 0;
va_end(argptr);
len -= strlen(buf);
return buf + strlen(buf);
}
//--------------------------------------------------------------------------------------------------------------
wchar_t * BufWPrintf(wchar_t *buf, int& len, const wchar_t *fmt, ...)
{
if (len <= 0)
return NULL;
va_list argptr;
va_start(argptr, fmt);
#ifdef WIN32
_vsnwprintf(buf, len, fmt, argptr);
#else
vswprintf( buf, len, fmt, argptr );
#endif
buf[ len - 1 ] = 0;
va_end(argptr);
len -= wcslen(buf);
return buf + wcslen(buf);
}
//--------------------------------------------------------------------------------------------------------------
const wchar_t * NumAsWString( int val )
{
const int BufLen = 16;
static wchar_t buf[BufLen];
int len = BufLen;
BufWPrintf( buf, len, L"%d", val );
return buf;
}
//--------------------------------------------------------------------------------------------------------------
const char * NumAsString( int val )
{
const int BufLen = 16;
static char buf[BufLen];
int len = BufLen;
BufPrintf( buf, len, "%d", val );
return buf;
}
//--------------------------------------------------------------------------------------------------------
/**
* Returns the token parsed by SharedParse()
*/
char *SharedGetToken( void )
{
return s_shared_token;
}
//--------------------------------------------------------------------------------------------------------
/**
* Returns the token parsed by SharedParse()
*/
void SharedSetQuoteChar( char c )
{
s_shared_quote = c;
}
//--------------------------------------------------------------------------------------------------------
/**
* Parse a token out of a string
*/
const char *SharedParse( const char *data )
{
int c;
int len;
len = 0;
s_shared_token[0] = 0;
if (!data)
return NULL;
// skip whitespace
skipwhite:
while ( (c = *data) <= ' ')
{
if (c == 0)
return NULL; // end of file;
data++;
}
// skip // comments
if (c=='/' && data[1] == '/')
{
while (*data && *data != '\n')
data++;
goto skipwhite;
}
// handle quoted strings specially
if (c == s_shared_quote)
{
data++;
while (1)
{
c = *data++;
if (c==s_shared_quote || !c)
{
s_shared_token[len] = 0;
return data;
}
s_shared_token[len] = c;
len++;
}
}
// parse single characters
if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c == ',' )
{
s_shared_token[len] = c;
len++;
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;
}
//--------------------------------------------------------------------------------------------------------
/**
* Returns true if additional data is waiting to be processed on this line
*/
bool SharedTokenWaiting( const char *buffer )
{
const char *p;
p = buffer;
while ( *p && *p!='\n')
{
if ( !isspace( *p ) || isalnum( *p ) )
return true;
p++;
}
return false;
}
|