-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandUtils.h
executable file
·120 lines (103 loc) · 2.88 KB
/
CommandUtils.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef SMASH_COMMANDUTILS_H_
#define SMASH_COMMANDUTILS_H_
#include <string>
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <sys/wait.h>
#include <iomanip>
#include <cstring>
#include <sstream>
const std::string WHITESPACE = " \n\r\t\f\v";
#if 0
#define FUNC_ENTRY() \
cout << __PRETTY_FUNCTION__ << " --> " << endl;
#define FUNC_EXIT() \
cout << __PRETTY_FUNCTION__ << " <-- " << endl;
#else
#define FUNC_ENTRY()
#define FUNC_EXIT()
#endif
// include for memset
namespace CommandUtils
{
inline std::string _ltrim(const std::string &s)
{
size_t start = s.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : s.substr(start);
}
inline std::string _rtrim(const std::string &s)
{
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
inline std::string _trim(const std::string &s)
{
return _rtrim(_ltrim(s));
}
inline bool _isBackgroundComamnd(const char *cmd_line)
{
const std::string str(cmd_line);
return str[str.find_last_not_of(WHITESPACE)] == '&';
}
inline void _removeBackgroundSign(std::string &cmd_line)
{
cmd_line = _trim(cmd_line);
if (cmd_line[cmd_line.length() - 1] == '&')
{
cmd_line = cmd_line.substr(0, cmd_line.length() - 1);
}
}
inline int _parseCommandLine(const char *cmd_line, char **args)
{
FUNC_ENTRY()
int i = 0;
std::string cmd_s = std::string(cmd_line);
_removeBackgroundSign(cmd_s);
std::istringstream iss(_trim(cmd_s).c_str());
for (std::string s; iss >> s;)
{
args[i] = (char *)malloc(s.length() + 1);
std::memset(args[i], 0, s.length() + 1);
std::strcpy(args[i], s.c_str());
args[++i] = NULL;
}
return i;
FUNC_EXIT()
}
inline std::vector<std::string> _split(const std::string &s, char delim)
{
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
inline std::vector<std::string> _split(const std::string &s)
{
std::vector<std::string> words;
std::istringstream iss(s);
std::string word;
while (iss >> word)
{
if (word[0] == '"')
{
std::string quoted;
std::getline(iss, quoted, '"');
word += quoted;
}
words.push_back(word);
}
return words;
}
inline bool is_digits(const std::string &str)
{
return str.find_first_not_of("0123456789") == std::string::npos;
}
}
#endif // SMASH_COMMANDUTILS_H_