idTech4.idCmdArgs.TokenizeString C# (CSharp) Method

TokenizeString() public method

Takes a string and breaks it up into arg tokens.
public TokenizeString ( string text, bool keepAsStrings ) : void
text string
keepAsStrings bool true to only seperate tokens from whitespace and comments, ignoring punctuation.
return void
		public void TokenizeString(string text, bool keepAsStrings)
		{
			// clear previous args.
			_args = new string[] { };

			if(text.Length == 0)
			{
				return;
			}

			idLexer lexer = new idLexer();
			lexer.LoadMemory(text, "idCmdSystem.TokenizeString");
			lexer.Options = LexerOptions.NoErrors | LexerOptions.NoWarnings | LexerOptions.NoStringConcatination | LexerOptions.AllowPathNames | LexerOptions.NoStringEscapeCharacters | LexerOptions.AllowIPAddresses | ((keepAsStrings == true) ? LexerOptions.OnlyStrings : 0);

			idToken token = null, number = null;
			List<string> newArgs = new List<string>();
			int len = 0, totalLength = 0;

			string tokenValue;

			while(true)
			{
				if(newArgs.Count == idE.MaxCommandArgs)
				{
					break; // this is usually something malicious.
				}

				if((token = lexer.ReadToken()) == null)
				{
					break;
				}

				tokenValue = token.ToString();

				if((keepAsStrings == false) && (tokenValue == "-"))
				{
					// check for negative numbers.
					if((number = lexer.CheckTokenType(TokenType.Number, 0)) != null)
					{
						token.Set("-" + number);
					}
				}

				// check for cvar expansion
				if(tokenValue == "$")
				{
					if((token = lexer.ReadToken()) == null)
					{
						break;
					}

					if(idE.CvarSystem.IsInitialized == true)
					{
						token.Set(idE.CvarSystem.GetString(token.ToString()));
					}
					else
					{
						token.Set("<unknown>");
					}
				}

				tokenValue = token.ToString();

				len = tokenValue.Length;
				totalLength += len + 1;

				// regular token
				newArgs.Add(tokenValue);
			}

			_args = newArgs.ToArray();
		}