BashSpark
Loading...
Searching...
No Matches
shell_tools.h
Go to the documentation of this file.
1
26#pragma once
27
28#include <string>
29#include <vector>
30
31#include "BashSpark/tools/utf.h"
33
34#ifdef __WIN64
35#include <windows.h>
36#elif defined(__posix__) || defined(__APPLE__)
37#include <unistd.h>
38#endif
39
40namespace bs {
46 inline std::vector<std::string> split_string(const std::string_view &oString) {
47 std::vector<std::string> vStrings;
48 std::size_t nStart = 0;
49 std::size_t nLength = 0;
50 for (std::size_t i = 0; i < oString.size(); ++i) {
51 if (
52 const char cChar = oString[i];
53 cChar == ' ' || cChar == '\n' || cChar == '\t'
54 ) {
55 // Append pending
56 if (nLength != 0)
57 vStrings.emplace_back(oString.substr(nStart, nLength));
58 // Reset buffer
59 nStart = i + 1;
60 nLength = 0;
61 } else {
62 // Continue searching new end
63 ++nLength;
64 }
65 }
66 // Append remainder
67 if (nLength != 0)
68 vStrings.emplace_back(oString.substr(nStart, nLength));
69 return vStrings;
70 }
71
77 inline void split_string(
78 std::vector<std::string> &vStrings,
79 const std::string_view &oString
80 ) {
81 std::size_t nStart = 0;
82 std::size_t nLength = 0;
83 for (std::size_t i = 0; i < oString.size(); ++i) {
84 if (
85 const char cChar = oString[i];
86 cChar == ' ' || cChar == '\n' || cChar == '\t'
87 ) {
88 // Append pending
89 if (nLength != 0)
90 vStrings.emplace_back(oString.substr(nStart, nLength));
91 // Reset buffer
92 nStart = i + 1;
93 nLength = 0;
94 } else {
95 // Continue searching new end
96 ++nLength;
97 }
98 }
99 // Append remainder
100 if (nLength != 0)
101 vStrings.emplace_back(oString.substr(nStart, nLength));
102 }
103
109 inline void insert_vector(std::vector<std::string> &vDestiny, std::vector<std::string> vOrigin) {
110 vDestiny.insert(
111 vDestiny.end(),
112 std::make_move_iterator(vOrigin.begin()),
113 std::make_move_iterator(vOrigin.end())
114 );
115 }
116
122 inline void concat_vector(ofakestream &oOstream, const std::vector<std::string> &vOrigin) {
123 if (!vOrigin.empty()) {
124 oOstream << vOrigin[0];
125 for (std::size_t i = 1; i < vOrigin.size(); ++i) {
126 oOstream.put(' ');
127 oOstream << vOrigin[i];
128 }
129 }
130 }
131
137 inline std::int64_t get_pid() {
138#ifdef __WIN64
139 return GetCurrentProcessId();
140#elif defined(__posix__) || defined(__APPLE__)
141 return getpid();
142#else
143 return 0;
144#endif
145 }
146
152 inline shell_keyword get_keyword_id(const std::string_view &oString) {
153 const static std::unordered_map<std::string_view, shell_keyword> s_mKeywords = {
154 {"function", shell_keyword::SK_FUNCTION},
155 {"if", shell_keyword::SK_IF},
156 {"then", shell_keyword::SK_THEN},
157 {"else", shell_keyword::SK_ELSE},
158 {"elif", shell_keyword::SK_ELIF},
159 {"fi", shell_keyword::SK_FI},
160 {"for", shell_keyword::SK_FOR},
161 {"in", shell_keyword::SK_IN},
162 {"while", shell_keyword::SK_WHILE},
163 {"until", shell_keyword::SK_UNTIL},
164 {"do", shell_keyword::SK_DO},
165 {"done", shell_keyword::SK_DONE},
166 {"continue", shell_keyword::SK_CONTINUE},
167 {"break", shell_keyword::SK_BREAK},
168 };
169 const auto pIter = s_mKeywords.find(oString);
170 if (pIter == s_mKeywords.end()) return shell_keyword::SK_NONE;
171 return pIter->second;
172 }
173
179 inline shell_keyword get_keyword_id(const std::string &oString) {
180 const static std::map<std::string, shell_keyword> s_mKeywords = {
181 {"function", shell_keyword::SK_FUNCTION},
182 {"if", shell_keyword::SK_IF},
183 {"then", shell_keyword::SK_THEN},
184 {"else", shell_keyword::SK_ELSE},
185 {"elif", shell_keyword::SK_ELIF},
186 {"fi", shell_keyword::SK_FI},
187 {"for", shell_keyword::SK_FOR},
188 {"in", shell_keyword::SK_IN},
189 {"while", shell_keyword::SK_WHILE},
190 {"until", shell_keyword::SK_UNTIL},
191 {"do", shell_keyword::SK_DO},
192 {"done", shell_keyword::SK_DONE}
193 };
194 const auto pIter = s_mKeywords.find(oString);
195 if (pIter == s_mKeywords.end()) return shell_keyword::SK_NONE;
196 return pIter->second;
197 }
198}
A class for output stream behavior with a character type.
Definition fakestream.h:263
ALWAYS_INLINE void put(const char_type cChar)
Writes a single character to the stream.
Definition fakestream.h:309
BashSpark main namespace.
Definition command.h:39
std::int64_t get_pid()
Gets the PID process of the shell On non recognized platforms returns 0.
Definition shell_tools.h:137
shell_keyword get_keyword_id(const std::string_view &oString)
Identifies shell special keywords.
Definition shell_tools.h:152
shell_keyword
Enumeration of shell keywords for scripting.
Definition shell_keyword.h:39
@ SK_FUNCTION
Start of a function definition.
@ SK_DO
Beginning of a block for loops and conditionals.
@ SK_NONE
No keyword (default value).
@ SK_WHILE
Start of a while loop.
@ SK_DONE
End of a do block or loop.
@ SK_FOR
Start of a for loop.
@ SK_CONTINUE
Continue statement.
@ SK_ELSE
Alternative block for when the 'if' condition is false.
@ SK_IN
Collection specified in a for loop.
@ SK_IF
Beginning of an if conditional statement.
@ SK_BREAK
Break staement.
@ SK_ELIF
Additional condition in an if-else chain.
@ SK_FI
End of an if-else construct.
@ SK_UNTIL
Loop continues until a condition is met.
@ SK_THEN
Block that executes if the 'if' condition is true.
void insert_vector(std::vector< std::string > &vDestiny, std::vector< std::string > vOrigin)
Moves appends the contents of the origin vector into the destination vector.
Definition shell_tools.h:109
void concat_vector(ofakestream &oOstream, const std::vector< std::string > &vOrigin)
Writes the contents of a vector<string> on an output stream.
Definition shell_tools.h:122
std::vector< std::string > split_string(const std::string_view &oString)
Splits a string into a vector.
Definition shell_tools.h:46
Defines class bs::shell_parser.
Provides simple tools for utf string manipulation.