Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
39 #include "TextParsers.h"
42 TextParsers g_TextParser;
44 static int g_ini_chartable1[255] = {0};
45 static int g_ws_chartable[255] = {0};
47 bool IsWhitespace(const char *stream)
49 return g_ws_chartable[(unsigned)*stream] == 1;
52 TextParsers::TextParsers()
54 g_ini_chartable1[(unsigned)'_'] = 1;
55 g_ini_chartable1[(unsigned)'-'] = 1;
56 g_ini_chartable1[(unsigned)','] = 1;
57 g_ini_chartable1[(unsigned)'+'] = 1;
58 g_ini_chartable1[(unsigned)'.'] = 1;
59 g_ini_chartable1[(unsigned)'$'] = 1;
60 g_ini_chartable1[(unsigned)'?'] = 1;
61 g_ini_chartable1[(unsigned)'/'] = 1;
62 g_ws_chartable[(unsigned)'\n'] = 1;
63 g_ws_chartable[(unsigned)'\v'] = 1;
64 g_ws_chartable[(unsigned)'\r'] = 1;
65 g_ws_chartable[(unsigned)'\t'] = 1;
66 g_ws_chartable[(unsigned)'\f'] = 1;
67 g_ws_chartable[(unsigned)' '] = 1;
70 void TextParsers::OnSourceModAllInitialized()
72 g_ShareSys.AddInterface(NULL, this);
75 unsigned int TextParsers::GetUTF8CharBytes(const char *stream)
77 return _GetUTF8CharBytes(stream);
89 bool CharStreamReader(void *stream, char *buffer, size_t maxlength, unsigned int *read)
91 CharStream *srdr = (CharStream *)stream;
93 const char *ptr = srdr->curpos;
94 for (size_t i=0; i<maxlength; i++)
103 *read = ptr - srdr->curpos;
110 SMCParseError TextParsers::ParseString_SMC(const char *stream,
111 ITextListener_SMC *smc,
115 CharStream srdr = { stream };
117 return ParseStream_SMC(&srdr, CharStreamReader, smc, line, col);
124 bool FileStreamReader(void *stream, char *buffer, size_t maxlength, unsigned int *read)
126 size_t num = fread(buffer, 1, maxlength, (FILE *)stream);
128 *read = static_cast<unsigned int>(num);
130 if (num == 0 && feof((FILE *)stream))
135 return (ferror((FILE *)stream) == 0);
138 SMCParseError TextParsers::ParseFile_SMC(const char *file, ITextListener_SMC *smc, unsigned int *line, unsigned int *col)
140 FILE *fp = fopen(file, "rt");
144 return SMCParse_StreamOpen;
147 SMCParseError result = ParseStream_SMC(fp, FileStreamReader, smc, line, col);
155 * Raw parsing of streams with helper functions
160 StringInfo() : quoted(false), ptr(NULL), end(NULL), special(false) { }
167 const char *FixupString(StringInfo &data)
180 /* A string will never have beginning whitespace because we ignore it in the stream.
181 * Furthermore, if there is trailing whitespace, the end ptr will point to it, so it is valid
182 * to overwrite! Lastly, the last character must be whitespace or a comment/invalid character.
187 /* Do some extra work on strings that have special quoted characters. */
190 char *outptr = data.ptr;
191 size_t len = data.end - data.ptr;
194 for (size_t i=0; i<len; i++)
196 if (data.ptr[i] == '\\' && i < len - 1)
198 /* Resolve the next character. */
200 if (data.ptr[i] == 'n')
203 } else if (data.ptr[i] == 't') {
205 } else if (data.ptr[i] == 'r') {
207 } else if (data.ptr[i] != '\\'
208 && data.ptr[i] != '"') {
209 /* This character is invalid, so go back one */
213 *outptr++ = data.ptr[i];
224 const char *rotate(StringInfo info[3])
226 if (info[2].ptr != NULL)
231 if (info[0].ptr != NULL)
235 info[0] = StringInfo();
241 void scrap(StringInfo info[3])
243 info[2] = StringInfo();
244 info[1] = StringInfo();
245 info[0] = StringInfo();
248 void reloc(StringInfo &data, unsigned int bytes)
260 char *lowstring(StringInfo info[3])
262 for (int i=2; i>=0; i--)
273 SMCParseError TextParsers::ParseStream_SMC(void *stream,
275 ITextListener_SMC *smc,
280 char *reparse_point = NULL;
281 char *parse_point = in_buf;
282 char *line_begin = in_buf;
284 unsigned int curline = 1;
285 unsigned int curtok = 0;
286 unsigned int curlevel = 0;
287 bool in_quote = false;
288 bool ignoring = false;
289 bool eol_comment = false;
290 bool ml_comment = false;
292 SMCParseError err = SMCParse_Okay;
296 StringInfo strings[3];
297 StringInfo emptystring;
299 smc->ReadSMC_ParseStart();
301 while (srdr(stream, parse_point, sizeof(in_buf) - (parse_point - line_begin) - 1, &read))
308 /* :TODO: do this outside of the main loop somehow
309 * This checks for BOM markings
312 in_buf[0] == (char)0xEF &&
313 in_buf[1] == (char)0xBB &&
314 in_buf[2] == (char)0xBF)
316 /* Move EVERYTHING down :\ */
317 memmove(in_buf, &in_buf[3], read - 3);
323 read += (parse_point - reparse_point);
324 parse_point = reparse_point;
325 reparse_point = NULL;
328 for (i=0; i<read; i++)
333 /* If we got a newline, there's a lot of things that could have happened in the interim.
334 * First, let's make sure the staged strings are rotated.
338 strings[0].end = &parse_point[i];
339 if (rotate(strings) != NULL)
341 err = SMCParse_InvalidTokens;
346 /* Next, let's clear some line-based values that may no longer have meaning */
349 if (ignoring && !ml_comment)
354 /* Pass the raw line onto the listener */
355 if ((res=smc->ReadSMC_RawLine(line_begin, curline)) != SMCParse_Continue)
357 err = (res == SMCParse_HaltFail) ? SMCParse_Custom : SMCParse_Okay;
361 /* Now we check the sanity of our staged strings! */
366 err = SMCParse_InvalidProperty1;
369 /* Assume the next string is a property and pass the info on. */
370 if ((res=smc->ReadSMC_KeyValue(
371 FixupString(strings[2]),
372 FixupString(strings[1]),
374 strings[1].quoted)) != SMCParse_Continue)
376 err = (res == SMCParse_HaltFail) ? SMCParse_Custom : SMCParse_Okay;
382 /* Change the states for the next line */
385 line_begin = &parse_point[i+1]; //Note: safe because this gets relocated later
386 } else if (ignoring) {
389 /* If i was 0, this case is impossible due to reparsing */
390 if ((i != 0) && c == '"' && parse_point[i-1] != '\\')
392 /* If we reached a quote in an ignore phase,
393 * we're staging a string and we must rotate it out.
398 strings[0].end = &parse_point[i];
399 strings[0].quoted = true;
400 if (rotate(strings) != NULL)
402 /* If we rotated too many strings, there was too much crap on one line */
403 err = SMCParse_InvalidTokens;
406 } else if (c == '\\') {
407 strings[0].special = true;
410 reparse_point = &parse_point[i];
414 } else if (ml_comment) {
417 /* Check if we need to get more input first */
420 reparse_point = &parse_point[i];
423 if (parse_point[i+1] == '/')
427 /* We should not be staging anything right now. */
428 assert(strings[0].ptr == NULL);
429 /* Advance the input stream so we don't choke on this token */
436 /* Check if we're whitespace or not */
437 if (!g_ws_chartable[(unsigned)c])
439 bool restage = false;
440 /* Check various special tokens:
447 if (c == ';' || c == '/')
449 /* If it's a line-based comment (that is, ; or //)
450 * we will need to scrap everything until the end of the line.
456 /* If we reached the end of the look-ahead, we need to re-check our input.
457 * Breaking out will force this to be the new reparse point!
459 reparse_point = &parse_point[i];
462 if (parse_point[i + 1] == '/')
464 /* standard comment */
468 } else if (parse_point[i+1] == '*') {
469 /* inline comment - start ignoring */
472 /* yes, we restage, meaning that:
473 * STR/ *stuff* /ING (space because ml comments don't nest in C++)
474 * will not generate 'STRING', but rather 'STR' and 'ING'.
475 * This should be a rare occurrence and is done here for convenience.
484 } else if (c == '{') {
485 /* If we are staging a string, we must rotate here */
488 /* We have unacceptable tokens on this line */
489 if (rotate(strings) != NULL)
491 err = SMCParse_InvalidSection1;
495 /* Sections must always be alone */
496 if (strings[2].ptr != NULL)
498 err = SMCParse_InvalidSection1;
500 } else if (strings[1].ptr == NULL) {
501 err = SMCParse_InvalidSection2;
504 if ((res=smc->ReadSMC_NewSection(FixupString(strings[1]), strings[1].quoted))
505 != SMCParse_Continue)
507 err = (res == SMCParse_HaltFail) ? SMCParse_Custom : SMCParse_Okay;
510 strings[1] = emptystring;
512 } else if (c == '}') {
513 /* Unlike our matching friend, this can be on the same line as something prior */
514 if (rotate(strings) != NULL)
516 err = SMCParse_InvalidSection3;
523 err = SMCParse_InvalidProperty1;
526 if ((res=smc->ReadSMC_KeyValue(
527 FixupString(strings[2]),
528 FixupString(strings[1]),
531 != SMCParse_Continue)
533 err = (res == SMCParse_HaltFail) ? SMCParse_Custom : SMCParse_Okay;
536 } else if (strings[1].ptr) {
537 err = SMCParse_InvalidSection3;
539 } else if (!curlevel) {
540 err = SMCParse_InvalidSection4;
543 /* Now it's safe to leave the section */
545 if ((res=smc->ReadSMC_LeavingSection()) != SMCParse_Continue)
547 err = (res == SMCParse_HaltFail) ? SMCParse_Custom : SMCParse_Okay;
551 } else if (c == '"') {
552 /* If we get a quote mark, we always restage, but we need to do it beforehand */
555 strings[0].end = &parse_point[i];
556 if (rotate(strings) != NULL)
558 err = SMCParse_InvalidTokens;
562 strings[0].ptr = &parse_point[i];
565 } else if (!strings[0].ptr) {
566 /* If we have no string, we must start one */
567 strings[0].ptr = &parse_point[i];
569 if (restage && strings[0].ptr)
571 strings[0].end = &parse_point[i];
572 if (rotate(strings) != NULL)
574 err = SMCParse_InvalidTokens;
579 /* If we're eating a string and get whitespace, we need to restage.
580 * (Note that if we are quoted, this is being ignored)
585 * The specification says the second string in a pair does not need to be quoted.
586 * Thus, we check if there's already a string on the stack.
587 * If there's a newline, we always rotate so the newline has an empty starter.
591 /* There's no string, so we must move this one down and eat up another */
592 strings[0].end = &parse_point[i];
594 } else if (!strings[1].quoted) {
595 err = SMCParse_InvalidTokens;
602 /* Advance which token we're on */
606 if (line_begin != in_buf)
608 /* The line buffer has advanced, so it's safe to copy N bytes back to the beginning.
609 * What's N? N is the lowest point we're currently relying on.
611 char *stage = lowstring(strings);
612 if (!stage || stage > line_begin)
616 unsigned int bytes = read - (stage - parse_point);
618 /* It is now safe to delete everything before the staged point */
619 memmove(in_buf, stage, bytes);
621 /* Calculate the number of bytes in the new buffer */
622 bytes = stage - in_buf;
623 /* Relocate all the cached pointers to our new base */
625 reloc(strings[0], bytes);
626 reloc(strings[1], bytes);
627 reloc(strings[2], bytes);
630 reparse_point -= bytes;
634 parse_point = &parse_point[read];
635 parse_point -= bytes;
637 } else if (read == sizeof(in_buf) - 1) {
638 err = SMCParse_TokenOverflow;
643 /* If we're done parsing and there are tokens left over... */
646 err = SMCParse_InvalidSection5;
648 } else if (strings[0].ptr || strings[1].ptr) {
649 err = SMCParse_InvalidTokens;
653 smc->ReadSMC_ParseEnd(false, false);
655 return SMCParse_Okay;
663 smc->ReadSMC_ParseEnd(true, (err == SMCParse_Custom));
678 bool TextParsers::ParseFile_INI(const char *file, ITextListener_INI *ini_listener, unsigned int *line, unsigned int *col)
680 FILE *fp = fopen(file, "rt");
681 unsigned int curline = 0;
696 char *ptr, *save_ptr;
704 if (fgets(buffer, sizeof(buffer), fp) == NULL)
709 //:TODO: this will only run once, so find a nice way to move it out of the while loop
710 /* If this is the first line, check the first three bytes for BOM */
712 buffer[0] == (char)0xEF &&
713 buffer[1] == (char)0xBB &&
714 buffer[2] == (char)0xBF)
716 /* We have a UTF-8 marked file... skip these bytes */
722 /***************************************************
723 * We preprocess the string before parsing tokens! *
724 ***************************************************/
726 /* First strip beginning whitespace */
727 while (*ptr != '\0' && g_ws_chartable[(unsigned)*ptr] != 0)
739 /* Now search for comment characters */
742 for (size_t i=0; i<len; i++,ptr++)
757 /* Terminate the string here */
777 /* Lastly, strip ending whitespace off */
778 for (size_t i=len-1; i>=0 && i<len; i--)
780 if (g_ws_chartable[(unsigned)ptr[i]])
794 if (!ini_listener->ReadINI_RawLine(ptr, &curtok))
801 bool invalid_tokens = false;
802 bool got_bracket = false;
803 bool extra_tokens = false;
808 for (size_t i=1; i<len; i++)
815 if (mbtowc(&wc, &ptr[i], len-i) != -1)
817 alnum = (iswalnum(wc) != 0);
818 i += _GetUTF8CharBytes(&ptr[i]) - 1;
821 alnum = (isalnum(c) != 0) || (g_ini_chartable1[(unsigned)c] != 0);
825 /* First check - is this a bracket? */
830 /* If this isn't the last character... */
839 /* n...No! Continue copying. */
840 invalid_tokens = true;
845 /* Tell the handler */
846 if (!ini_listener->ReadINI_NewSection(&ptr[1], invalid_tokens, got_bracket, extra_tokens, &curtok))
852 char *val_ptr = NULL;
854 size_t first_space = 0;
855 bool invalid_tokens = false;
856 bool equal_token = false;
861 for (size_t i=0; i<len; i++)
865 /* is this an invalid char? */
868 if (mbtowc(&wc, &ptr[i], len-i) != -1)
870 alnum = (iswalnum(wc) != 0);
871 i += _GetUTF8CharBytes(&ptr[i]) - 1;
874 alnum = (isalnum(c) != 0) || (g_ini_chartable1[(unsigned)c] != 0);
879 if (g_ws_chartable[(unsigned)c])
881 /* if it's a space, keep track of the first occurring space */
889 /* if it's an equal sign, we're done with the key */
892 /* remove excess whitespace */
893 key_ptr[first_space] = '\0';
895 /* remove the equal sign */
898 if (ptr[++i] != '\0')
900 /* If this isn't the end, set next pointer */
906 /* Mark that we got something invalid! */
907 invalid_tokens = true;
914 /* Now we need to parse the value, if any */
917 /* eat up spaces! there shouldn't be any h*/
918 while ((*val_ptr != '\0') && g_ws_chartable[(unsigned)*val_ptr] != 0)
922 if (*val_ptr == '\0')
927 /* Do we have an initial quote? If so, the parsing rules change! */
928 if (*val_ptr == '"' && *val_ptr != '\0')
930 len = strlen(val_ptr);
931 if (val_ptr[len-1] == '"')
934 val_ptr[--len] = '\0';
942 curtok = val_ptr - buffer;
943 if (!ini_listener->ReadINI_KeyValue(key_ptr, val_ptr, invalid_tokens, equal_token, quotes, &curtok))
976 const char *TextParsers::GetSMCErrorString(SMCParseError err)
978 static const char *s_errors[] =
981 "Stream failed to open",
982 "Stream returned read error",
984 "Un-quoted section has invalid tokens",
985 "Section declared without header",
986 "Section declared with unknown tokens",
987 "Section ending without a matching section beginning",
988 "Section beginning without a matching ending",
989 "Line contained too many invalid tokens",
990 "Token buffer overflowed",
991 "A property was declared outside of a section",
994 if (err < SMCParse_Okay || err > SMCParse_InvalidProperty1)
999 return s_errors[err];