Skip to content

Commit

Permalink
Merge pull request #38 from wtfbbqhax/msg-test
Browse files Browse the repository at this point in the history
Msg test
  • Loading branch information
wtfbbqhax authored Apr 25, 2018
2 parents caaf74b + d01917f commit 299faba
Show file tree
Hide file tree
Showing 14 changed files with 13,326 additions and 615 deletions.
12,012 changes: 12,012 additions & 0 deletions external/catch/catch.hpp

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/client/cl_cgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ bool CL_GetServerCommand( int serverCommandNumber )
Com_DPrintf( "serverCommand: %i : %s\n", serverCommandNumber, s );

rescan:
Cmd_TokenizeString( s );
Cmd_TokenizeString2(s, false);
cmd = Cmd_Argv(0);
argc = Cmd_Argc();

Expand Down Expand Up @@ -312,7 +312,7 @@ bool CL_GetServerCommand( int serverCommandNumber )
CL_ConfigstringModified();
// reparse the string, because CL_ConfigstringModified may have done
// another Cmd_TokenizeString()
Cmd_TokenizeString( s );
Cmd_TokenizeString2(s, false);
return true;
}

Expand All @@ -323,7 +323,7 @@ bool CL_GetServerCommand( int serverCommandNumber )
Con_ClearNotify();
// reparse the string, because Con_ClearNotify() may have done another
// Cmd_TokenizeString()
Cmd_TokenizeString( s );
Cmd_TokenizeString2(s, false);
::memset( cl.cmds, 0, sizeof( cl.cmds ) );
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/cl_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3552,7 +3552,7 @@ static void CL_ServerInfoPacket(netadr_t from, msg_t *msg)
{
int i, type;
char info[MAX_INFO_STRING];
char *infoString;
const char *infoString;
int prot;
const char *gamename;
bool gameMismatch;
Expand Down Expand Up @@ -3776,7 +3776,7 @@ static void CL_ConnectionlessPacket(netadr_t from, msg_t *msg)

const char *s = MSG_ReadStringLine(msg);

Cmd_TokenizeString(s);
Cmd_TokenizeString2(s, false);

const char *c = Cmd_Argv(0);

Expand Down
4 changes: 2 additions & 2 deletions src/client/cl_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ void CL_ParseGamestate( msg_t *msg ) {
int newnum;
entityState_t nullstate;
int cmd;
char *s;
const char *s;
char oldGame[MAX_QPATH];

clc.connectPacketCount = 0;
Expand Down Expand Up @@ -840,7 +840,7 @@ when it transitions a snapshot
=====================
*/
void CL_ParseCommandString( msg_t *msg ) {
char *s;
const char *s;
int seq;
int index;

Expand Down
171 changes: 171 additions & 0 deletions src/qcommon/CmdParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
===========================================================================
Copyright (C) 2018 GrangerHub
This file is part of Tremulous.
Tremulous is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Tremulous 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tremulous; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/

#include <iostream>
#include <vector>

using Args = std::vector<std::string>;

struct Parser {

Args args;

Args& Parse(std::string src)
{
args.clear();
token.clear();
state = prev_state = START;
scan = SCAN_START;

for (int ch : src)
{
if (!process(ch))
continue;

args.emplace_back(token);
token.clear();
}

// flush any partial states
process(-1);

if (token.size())
args.emplace_back(token);

return args;
}

private:

enum State {
START,
DQUOTE,
ESCAPE,
COMMENT_START,
ONELINE_COMMENT
};

enum ScanState {
SCAN_START,
SCAN_FOUND,
SCAN_BREAK,
SCAN_STOP
};

bool process(int ch)
{
if (ch == -1)
{
if (state == COMMENT_START)
token += '/';
else if (state == ESCAPE)
token += '\\';
else
return false;
return true;
}

switch (state) {
again:
case START:
if (isspace(ch)) {
if (scan == SCAN_FOUND)
scan = SCAN_BREAK;
break;
}
else if (ch == '"') {
state = DQUOTE;
break;
}
else if (ch == '/') {
state = COMMENT_START;
break;
}
else if (ch == '\\') {
prev_state = state;
state = ESCAPE;
break;
}

scan = SCAN_FOUND;
token += ch;
break;

// Begin comment scanner
case COMMENT_START:
if (ch == '/') {
scan = SCAN_STOP;
state = ONELINE_COMMENT;
break;
}

token += '/';
state = START;
scan = SCAN_FOUND;
goto again;

// Oneline comment ends the processing. We could optimize this
case ONELINE_COMMENT:
break;

// String literal
case DQUOTE:
if (ch == '\\') {
prev_state = state;
state = ESCAPE;
break;
}
else if (ch == '"') {
state = START;
scan = SCAN_BREAK;
break;
}

token += ch;
break;

case ESCAPE:
token += ch;
state = prev_state;
break;
}

if (scan == SCAN_BREAK) {
scan = SCAN_START;
state = START;
return true;
}

if (scan == SCAN_STOP) {
if (!token.empty())
return true;
return false;
}

return false;
}

State state;
State prev_state; // used to restore state after processing an escaped literal char.
ScanState scan;
std::string token;
};
Loading

0 comments on commit 299faba

Please sign in to comment.