-
Notifications
You must be signed in to change notification settings - Fork 21
/
util.cc
149 lines (133 loc) · 3.55 KB
/
util.cc
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
// util.h -- May 10, 2009
// by geohot
// part of "The Embedded Disassembler"
// released under GPLv3, see http://gplv3.fsf.org/
#include <cstdio>
#include <string>
#include <sstream>
#include <iomanip>
#include "data.h"
#include "util.h"
using namespace std;
using namespace eda;
uint32_t eda::stoi(const string& num) {
istringstream in;
in.str(num);
uint32_t ret;
if(num.substr(0,2)=="0x")
in >> std::hex >> ret;
else if(num.find_first_of("abcdefABCDEF") != string::npos)
in >> std::hex >> ret;
else
in >> std::dec >> ret;
return ret;
}
std::string eda::MakeWellFormedXML(const std::string& in) {
string ret;
for(int i = 0; i<in.length(); i++) {
switch(in[i]) {
case '>': ret += ">"; break;
case '<': ret += "<"; break;
case '&': ret += "&"; break;
default: ret+=in[i]; break;
}
}
return ret;
}
bool eda::file_to_string(const std::string& filename, std::string* out) {
FILE* f = fopen(filename.c_str(), "rb");
if (f==0) return false;
fseek(f, 0, SEEK_END);
int size = ftell(f);
fseek(f, 0, SEEK_SET);
char* dat = (char*)malloc(size);
int read = fread(dat, 1, size, f);
out->assign(dat, size);
delete [] dat;
return read == size;
}
int eda::find_matching(const std::string& s, int start, char open, char close) {
//cout << s << endl;
int count = 0;
for (int i = start; i < s.length(); i++) {
if (s[i] == close) {
if (count == 1) {
//cout << "returning " << i << endl;
return i;
}
else count--;
} else if (s[i] == open) {
count++;
}
}
LOG(WARNING) << "error in brackets";
return -1;
}
void eda::SerializeToXML(ostringstream& out, const vector<int>* v, string name, string item) {
out << "<" << name << ">";
if (v != NULL) {
for (int i = 0; i < v->size(); i++) {
out << "<" << item << ">";
out << (*v)[i];
out << "</" << item << ">";
}
}
out << "</" << name << ">";
}
vector<int> eda::FetchGAIsFromAddresses(const vector<Address*>& a) {
vector<int> ret;
for(vector<Address*>::const_iterator it = a.begin(); it != a.end(); ++it) {
ret.push_back((*it)->get_gai());
}
return ret;
}
void eda::StringSplit(const char* a, const string& in, vector<string>* argv) {
size_t start=in.find_first_not_of(a,0);
size_t end=in.find_first_of(a, start);
while(end!=std::string::npos || start!=std::string::npos)
{
argv->push_back(in.substr(start,end-start));
start=in.find_first_not_of(a,end);
end=in.find_first_of(a, start);
}
}
void eda::StringSplit(char a, const string& in, vector<string>* argv) {
size_t start=in.find_first_not_of(a,0);
size_t end=in.find_first_of(a, start);
while(end!=std::string::npos || start!=std::string::npos)
{
argv->push_back(in.substr(start,end-start));
start=in.find_first_not_of(a,end);
end=in.find_first_of(a, start);
}
}
// Returns things like 9 and 0x4F
string eda::immed(uint32_t data) {
ostringstream o;
if(data < 10) o << data;
else o << hex << "0x" << data;
return o.str();
}
// Signed
string eda::immed_signed(int32_t data) {
ostringstream o;
if(data<0) {
o << "-";
data = data * -1;
}
o << immed(data);
return o.str();
}
eda::Logging::Logging(int level, const char* file, const char* pretty_function, const char* function, int line_number) {
switch(level) {
case 0:
std::cout << file << "(" << line_number << "): ";
break;
case 1:
case 2:
std::cout << file << "(" << line_number << ")--" << pretty_function << ": ";
break;
case DEBUG:
std::cout << "DEBUG: ";
}
}