-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathhex-edit.cpp
More file actions
267 lines (228 loc) · 8.53 KB
/
hex-edit.cpp
File metadata and controls
267 lines (228 loc) · 8.53 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#if defined(Hiro_HexEdit)
@implementation CocoaHexEditTableView
- (void)mouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSInteger row = [self rowAtPoint:point];
NSInteger column = [self columnAtPoint:point];
[super mouseDown:event]; // handle selection
if (row >= 0 && column >= 0) {
// Begin editing the clicked cell
NSTableColumn *tableColumn = [self tableColumns][column];
NSString *identifier = tableColumn.identifier;
// Skip editing if column is not editable (e.g., Address/Char)
if (![identifier isEqualToString:@"Address"] && ![identifier isEqualToString:@"Char"]) {
[self editColumn:column row:row withEvent:event select:YES];
}
}
}
@end
@implementation CocoaHexEdit
-(id) initWith:(hiro::mHexEdit&)hexEditReference {
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
hexEdit = &hexEditReference;
if (tableView = [[CocoaHexEditTableView alloc] initWithFrame:[self bounds]]) {
[tableView setDelegate:self];
[tableView setDataSource:self];
// [tableView setHeaderView:nil];
[tableView setUsesAlternatingRowBackgroundColors:true];
[tableView setGridStyleMask:(NSTableViewDashedHorizontalGridLineMask | NSTableViewSolidVerticalGridLineMask)];
[tableView setRowHeight:14.0];
// remove padding and stuff to fit all 16 columns
[tableView setIntercellSpacing:NSMakeSize(0, 0)];
// [tableView setStyle:NSTableViewStylePlain];
NSTableColumn* addressCol = [[NSTableColumn alloc] initWithIdentifier:@"Address"];
addressCol.editable = NO;
addressCol.title = @" Address";
[tableView addTableColumn:addressCol];
addressCol.width = 60;
for (int i = 0; i < hexEdit->columns(); i++) {
NSTableColumn* col = [[NSTableColumn alloc] initWithIdentifier:[NSString stringWithFormat:@"%d",
i]];
col.title = [NSString stringWithFormat:@"0%x", i];
col.width = 21;
[tableView addTableColumn:col];
}
NSTableColumn* charCol = [[NSTableColumn alloc] initWithIdentifier:@"Char"];
charCol.title = @" Plain Text";
charCol.editable = NO;
[tableView addTableColumn:charCol];
// must programmatically do this so the table shows up.
[self setDocumentView:tableView];
[self setHasVerticalScroller:YES];
[self setHasHorizontalScroller:YES];
}
}
return self;
}
- (NSTableView *) tableView {
return tableView;
}
- (hiro::mHexEdit *) hexEdit {
return hexEdit;
}
- (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView {
return (max(1, hexEdit->length()) + hexEdit->columns() - 1) / hexEdit->columns();
}
-(id) tableView:(NSTableView *) tableView
objectValueForTableColumn:(NSTableColumn *) tableColumn
row:(NSInteger) row {
// return a string with the content of (row, column)
// u32 address = hexEdit->address() + row * hexEdit->columns();
u32 address = row * hexEdit->columns();
// address only
if ([[tableColumn identifier] isEqualToString:@"Address"]) {
return [NSString stringWithUTF8String:hex(address, 8L).data()];
} else if ([[tableColumn identifier] isEqualToString:@"Char"]){
// create the char string by concatenating
NSMutableString *output = [NSMutableString stringWithCapacity:hexEdit->columns()];
for (auto column : range(hexEdit->columns())) {
if (address < hexEdit->length()) {
u8 data = hexEdit->doRead(address++);
[output appendString:[NSString stringWithFormat:@"%c",
(data >= 0x20 && data <= 0x7e ? (char)data : '.')]];
}
}
return output;
} else {
// return only the single byte at the correct position
NSInteger columnNumber = [tableColumn.identifier integerValue];
address += columnNumber;
if (address < hexEdit->length()) {
u8 data = hexEdit->doRead(address);
switch(hexEdit->base()) {
case 2: {
NSMutableString *binary = [NSMutableString stringWithCapacity:8];
for(int bit = 7; bit >= 0; bit--) {
[binary appendFormat:@"%c", (data & (1 << bit)) ? '1' : '0'];
}
return binary;
}
case 8:
return [NSString stringWithFormat:@"%03o", data];
case 16:
return [NSString stringWithFormat:@"%02X", data];
default:
@throw [NSException exceptionWithName:@"InvalidBaseException"
reason:@"The base for the Hex Editor was neither 2, 8, nor 16."
userInfo:@{
@"filename": @"hiro/cocoa/widget/hex-edit.cpp"}];
}
} else {
return @" ";
}
}
}
-(void) tableView:(NSTableView *) tableView
setObjectValue:(id) object
forTableColumn:(NSTableColumn *) tableColumn
row:(NSInteger) row {
// when table is edited, modify underlying data source
NSInteger colNumber = [tableColumn.identifier integerValue];
u32 address = row * hexEdit->columns() + colNumber;
if (address < hexEdit->length()) {
NSString* newVal = (NSString *)object;
newVal = [newVal stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Make sure code converts Single Digit by appending zero.
unsigned int data = 0;
BOOL boolSucceed = YES;
switch(hexEdit->base()) {
case 2: {
if (newVal.length > 8) {
newVal = [newVal substringToIndex:8]; // truncate to 8 chars max for Binary
}
// Converts Binary to Integer
for (NSUInteger i = 0; i < newVal.length; i++) {
unichar digit = [newVal characterAtIndex:i];
if (digit != '0' && digit != '1') {
boolSucceed = NO; break;
}
data = data * 2 + (digit - '0');
}
break;
}
case 8:{
if (newVal.length > 3) {
newVal = [newVal substringToIndex:3]; // truncate to 3 chars max for Octal
}
// Converts Octal to Integer
for (NSUInteger i = 0; i < newVal.length; i++) {
unichar digit = [newVal characterAtIndex:i];
if (digit < '0' || digit > '7') {
boolSucceed = NO; break;
}
data = data * 8 + (digit - '0');
}
data &= 0xFF;
break;
}
case 16:{
if (newVal.length > 2) {
newVal = [newVal substringToIndex:2]; // truncate to 2 chars max
}
NSScanner* hexScanner = [NSScanner scannerWithString:newVal];
// Converts Hex to Integer
boolSucceed = [hexScanner scanHexInt:&data];
break;
}
default:
@throw [NSException exceptionWithName:@"InvalidBaseException"
reason:@"The base for the Hex Editor was neither 2, 8, nor 16."
userInfo:@{
@"filename": @"hiro/cocoa/widget/hex-edit.cpp"}];
}
if (boolSucceed) {
// this.......is probably ok....???
hexEdit->doWrite(address, (u8)data);
}
}
[tableView reloadData];
}
- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
return YES;
}
@end
namespace hiro {
auto pHexEdit::construct() -> void {
cocoaView = cocoaHexEdit = [[CocoaHexEdit alloc] initWith:self()];
pWidget::construct();
for (NSTableColumn *column in cocoaHexEdit.tableView.tableColumns) {
if (@available(macOS 10.15, *)) {
NSTextFieldCell *cell = column.dataCell;
NSTableHeaderCell *headerCell = column.headerCell;
// Set the font for the data cell.
cell.font = [NSFont monospacedSystemFontOfSize:10 weight:NSFontWeightRegular];
}
}
update();
}
auto pHexEdit::destruct() -> void {
[cocoaView removeFromSuperview];
}
auto pHexEdit::setAddress(u32 offset) -> void {
mHexEdit* hexEdit = [cocoaHexEdit hexEdit];
int row = offset / hexEdit->columns();
[[cocoaHexEdit tableView] scrollRowToVisible:row];
}
auto pHexEdit::setBackgroundColor(Color color) -> void {
update();
}
auto pHexEdit::setBase(u8 base) -> void {
update();
}
auto pHexEdit::setColumns(u32 columns) -> void {
update();
}
auto pHexEdit::setForegroundColor(Color color) -> void {
update();
}
auto pHexEdit::setLength(u32 length) -> void {
update();
}
auto pHexEdit::setRows(u32 rows) -> void {
update();
}
auto pHexEdit::update() -> void {
[[cocoaHexEdit tableView] reloadData];
}
}
#endif