-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.h
108 lines (93 loc) · 2.6 KB
/
functions.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
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
#include <iostream>
#include <vector>
enum RedirectionType {
NONE,
READ_FROM,
WRITE_TO
};
enum TokenType {
EXECUTABLE,
ARGUMENT,
PIPE,
REDIRECT_OUT,
REDIRECT_IN,
REDIRECT_FILE
};
struct Redirection {
RedirectionType type;
std::string fileName;
};
struct Command {
std::string fileToExecute;
std::vector<std::string> args;
std::vector<Redirection> redirections;
};
struct Token {
TokenType type;
std::string data;
};
/**
* @breif Takes a lines string and returns a list of tokens parsed from string
*
* @param line
* @return std::vector<Token>
*/
std::vector<Token> parseLine(const std::string& line);
/**
* @breif Given a line the shell needs to execute,
* this function determines if it should be done in the background or not
*
* @param line line given to the shell
* @return true this line should be handled in the background
* @return false this line should be handled in the forground
*/
bool shouldLineBeDoneInBackground(const std::string& line);
/**
* @breif Given a list of tokens this function returns a list of commands (that need to be exec'ed and piped)
*
* @param line line givin to the shell
* @return std::vector<Command> list of commands that need to be further parsed
*/
std::vector<Command> tokensToCommands(const std::vector<Token>& tokens);
///// GENERAL HELPERS
/**
* @breif Poor mans right trim.
* Takes a string and returns the string with any trailing spaces or newlines removed
*
* @param line string to rtrim
* @return std::string trimmed string
*/
std::string rtrim(const std::string& line);
/**
* @breif Poor mans left trim.
* Takes a string and returns the string with any leading spaces or newlines removed
*
* @param line string to ltrim
* @return std::string trimmed string
*/
std::string ltrim(const std::string& line);
/**
* @breif Takes a string and splits the string into two at the first sight of splitAt (unless its in quotes).
*
* @param line
* @param splitAt
* @return std::vector<std::string>
*/
std::vector<std::string> splitIgnoreQuotes(const std::string& line, const char& splitAt);
/**
* @breif Takes a string and splits the string into two at the first sight of splitAt (unless its in quotes).
*
* @param line
* @param splitAt
* @return std::vector<std::string>
*/
std::vector<std::string> splitIgnoreQuotes(const std::string& line, const std::vector<char>& splitAt);
/**
* @breif Remove Quotes from string if there are quotes
*
*/
std::string removeQuotes(const std::string& str);
#endif