-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
87 lines (85 loc) · 2.15 KB
/
parser.h
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
#include <vector>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include <string.h>
#include <limits.h>
#include <vector>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
vector<string> PARSE_STRING(string input,char delim)
{
int pos=0;
int prev=0;
vector<char>tokens;
vector<string>command;
while(input[pos]!='\0')
{
if(input[pos]=='"')
{
tokens.push_back(input[pos]);
pos++;
while(input[pos]!='"'&& input[pos]!='\0')
{
tokens.push_back(input[pos]);
pos++;
}
tokens.push_back(input[pos]);
pos++;
}
else if(input[pos]=='\'')
{
tokens.push_back(input[pos]);
pos++;
while(input[pos]!='\''&& input[pos]!='\0')
{
tokens.push_back(input[pos]);
pos++;
}
tokens.push_back(input[pos]);
pos++;
}
else if(delim=='|' && input[pos]=='|' && input[pos+1]!='\0' && input[pos+1]=='|')
{
tokens.push_back(input[pos]);
pos++;
tokens.push_back(input[pos]);
pos++;
}
else if(input[pos]!=delim)
{
tokens.push_back(input[pos]);
pos++;
}
else
{
if(!tokens.empty())
{
// tokens.push_back('\0');
string s(tokens.begin(),tokens.end());
if(s.compare(" ")!=0)
{
command.push_back(s);
// cout<<"token = "<<s<<" and length = "<<s.length()<<endl;
}
tokens.clear();
}
pos++;
}
}
if(!tokens.empty())
{
// tokens.push_back('\0');
string s(tokens.begin(),tokens.end());
if(s.compare(" ")!=0)
{
command.push_back(s);
// cout<<"token = "<<s<<" and length = "<<s.length()<<endl;
}
tokens.clear();
}
return command;
}