-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.cpp
103 lines (90 loc) · 1.97 KB
/
common.cpp
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
#include "common.hpp"
void ascii( u8 character )
{
printf("%c", (character<0x1e || character>0x7f) ? '.' : character);
}
void hexdump( char * pointer , u32 length )
{
u32 go_to = 0;
#ifdef SHORT_DUMP
go_to = 10;
#else
go_to = (length + 0xF) >> 4;
#endif
#ifdef SAFETY
if ( go_to > 16 )
go_to = 16;
#endif
u8 * temp1 = NULL;
u8 * temp2 = NULL;
temp1 = (u8*)pointer;
temp2 = (u8*)pointer;
for ( u32 j = 0 ; j < go_to ; j++ )
{
for ( u32 i = 0 ; i < 0x10 ; i++ )
printf("%02x ", *temp1++);
printf(" ");
for ( u32 i = 0 ; i < 0x10 ; i++ )
ascii(*temp2++);
printf("\n");
}
}
void printStringVector( vector<string> lines )
{
for(u32 ii = 0; ii < lines.size(); ii++)
cout << lines[ii] << endl;
}
void printCharVector( vector<char> line )
{
for(u32 ii = 0; ii < line.size(); ii++)
cout << line[ii];
cout << endl;
}
void stripCarriageReturns( string& StringToModify )
{
if(StringToModify.empty()) return;
int startIndex = StringToModify.find_first_not_of("\r");
int endIndex = StringToModify.find_last_not_of("\r");
string tempString = StringToModify;
StringToModify.erase();
StringToModify = tempString.substr(startIndex, (endIndex - startIndex + 1));
}
u32 ReadFile(const char* filename, char* buffer) {
ifstream save(filename, ios::in);
if(!save)
{
cout << "File ";
cout << filename << "could not be opened" << endl;
exit(EXIT_FAILURE);
}
save.seekg(0, ifstream::end);
u32 saveSize = save.tellg();
save.seekg(0);
buffer = new char[saveSize];
save.read(buffer, saveSize);
save.close();
return saveSize;
}
vector<char> readLine1(char * buffer, int len)
{
vector<char> line;
for(char * spot = buffer; spot < buffer + len; spot++)
if( *spot != '\n' )
line.push_back(*spot);
else
return line;
return line;
}
#if 0
string readLine2(const char * buffer, int len)
{
string line;
line.empty();
for(const char* spot = buffer; spot < buffer + len; spot++)
if( *spot != '\n' )
line.append(*spot);
else
return line;
return line;
}
#endif