-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.hpp
executable file
·182 lines (143 loc) · 4.08 KB
/
util.hpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#pragma once
#include <string>
#include <vector>
#include <map>
#include <cstdint>
//////////////////////////////////////////////////////////////////////////////
// Color codes for output formatting
enum ColorTag_t {
// Styles
S_BOLD, SN_BOLD,
S_DIM, SN_DIM,
S_ITALIC, NS_ITALIC,
S_ULINE, NS_ULINE,
S_BLINK, NS_BLINK,
// Colors
FG_BLACK, BG_BLACK,
FG_RED, BG_RED,
FG_GREEN, BG_GREEN,
FG_YELLOW, BG_YELLOW,
FG_BLUE, BG_BLUE,
FG_MAGENTA, BG_MAGENTA,
FG_CYAN, BG_CYAN,
FG_WHITE, BG_WHITE,
FG_DEFAULT, BG_DEFAULT,
FG_RESET, BG_RESET,
NULLSTR
};
/**
* @brief Get ansi color code corresponding to a tag
*
* @return std::string tag
*/
char* ansicode(ColorTag_t tag);
//////////////////////////////////////////////////////////////////////////////
// Throwing messages (error, warning, success)
/**
* @brief Throws error generated in the std::cerr stream
*
* @param er_code error code
* @param message error message
* @param exit flag that tells weather to exit immediately
*/
void throwError(std::string er_code, std::string message, bool Exit = false);
/**
* @brief Throws warning generated by assembler in the std::cerr stream
*
* @param wr_code warning code
* @param message Warning message
*/
void throwWarning(std::string wr_code, std::string message);
/**
* @brief Displays a success message
*
* @param message Success message
*/
void throwSuccessMessage(std::string message, bool Exit = false);
//////////////////////////////////////////////////////////////////////////////
// String stripping utility functions
// see: https://www.techiedelight.com/trim-string-cpp-remove-leading-trailing-spaces/#:~:text=We%20can%20use%20combination%20of,functions%20to%20trim%20the%20string.
const std::string WHITESPACE = " \n\r\t\f\v";
/**
* @brief removes preceeding whitespaces in a string
*
* @param s string
* @return std::string
*/
std::string lStrip(const std::string& s);
/**
* @brief removes succeeding whitespaces in a string
*
* @param s string
* @return std::string
*/
std::string rStrip(const std::string& s);
/**
* @brief removes preceding & succeeding whitespaces in a string
*
* @param s string
* @return std::string
*/
std::string strip(const std::string& s);
//////////////////////////////////////////////////////////////////////////////
// String Tokenizing
/**
* @brief splits a string accordint to delimiter
*
* @param txt input string
* @param strs vector of strings parts
* @param ch delimiter
* @return size_t
*/
size_t tokenize(const std::string &txt, std::vector<std::string> &strs, char ch);
//////////////////////////////////////////////////////////////////////////////
// File I/O
/**
* @brief Resolves environment variable in path specified using ${VAR} syntax
*
* @param path input path
* @return std::string resolved oath
*/
std::string resolve_envvar_in_path(std::string path);
/**
* @brief reads a binary file
*
* @param memfile filepath
* @return std::vector<char> contents
*/
std::vector<char> fReadBin(std::string memfile);
/**
* @brief Reads a file and returns its contents
*
* @param filepath Filepath
* @return Vector of strings containing file contents
*/
std::vector<std::string> fRead (std::string filepath);
/**
* @brief Write to a file
*
* @param filepath Filepath
*/
void fWrite (std::vector<std::string> data, std::string filepath);
//////////////////////////////////////////////////////////////////////////////
// Misc
/**
* @brief Get the Stdout From shell Command
*
* @param cmd shell command to execute
* @param get_output if true, returns stdout, else moves on(even if command isn't still complete)
* @return std::string command output
*/
std::string GetStdoutFromCommand(std::string cmd, bool get_output);
struct DisassembledLine
{
uint32_t instr;
std::string disassembly;
};
/**
* @brief Get the Disassembly of input file using riscv objdump
*
* @param dis std::map<uint32_t, std::string> map of disassembly
* @param filename input filename
*/
void getDisassembly(std::map<uint32_t, DisassembledLine> *dis, std::string filename);