Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal static List<KeyValuePair<bool,string>> CrackCommandLine(string commandLine, out string exename, char esc = '\\')
- {
- exename = null;
- var result = new List<KeyValuePair<bool,string>>();
- var i = 0;
- var inQuote = false;
- var sb = new StringBuilder();
- while (i < commandLine.Length)
- {
- if (!inQuote)
- {
- if (sb.Length == 0)
- {
- if (commandLine[i] == '"')
- {
- inQuote = true;
- ++i;
- continue;
- }
- }
- var ws = false;
- while (char.IsWhiteSpace(commandLine[i]))
- {
- ws = true;
- ++i;
- }
- if (ws)
- {
- if (sb.Length > 0)
- {
- if (exename == null)
- {
- exename = sb.ToString();
- }
- else
- {
- result.Add(new KeyValuePair<bool, string>(false, sb.ToString()));
- }
- sb.Clear();
- }
- }
- else
- {
- sb.Append(commandLine[i]);
- ++i;
- }
- }
- else
- {
- if (i < commandLine.Length - 1 && commandLine[i] == esc && commandLine[i + 1] == '\"')
- {
- sb.Append('\"');
- i += 2;
- }
- else if (commandLine[i] == '\"')
- {
- if (exename == null)
- {
- exename = sb.ToString();
- }
- else
- {
- result.Add(new KeyValuePair<bool, string>(true, sb.ToString()));
- }
- sb.Clear();
- inQuote = false;
- ++i;
- continue;
- }
- sb.Append(commandLine[i]);
- ++i;
- }
- }
- if (sb.Length > 0)
- {
- if (exename == null)
- {
- exename = sb.ToString();
- }
- else
- {
- result.Add(new KeyValuePair<bool, string>(inQuote, sb.ToString()));
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement