-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.cpp
More file actions
113 lines (95 loc) · 3.13 KB
/
Copy patheditor.cpp
File metadata and controls
113 lines (95 loc) · 3.13 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
#include "editor.h"
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_dialog.h>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <cctype>
// Set file selected from dialog upon opening
void SDLCALL open_file_callback(void* userdata, const char* const* filelist, int filter)
{
EditorState* editor = static_cast<EditorState*>(userdata);
if (!filelist) {
SDL_Log("Error: %s", SDL_GetError());
return;
}
if (!*filelist) {
SDL_Log("No file selected.");
return;
}
editor->file_path = *filelist;
std::ifstream file(*filelist, std::ios::binary);
editor->bytes.assign((std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>()));
// Reset cursor/scroll/edit state for the newly loaded file
editor->byte_index = (size_t)-1;
editor->prev_byte_index = (size_t)-1;
editor->scroll_offset = 0;
editor->edited_bytes.clear();
editor->show_cursor_rect = false;
editor->show_edit_rect = false;
SDL_Log("Loaded file: %s (%zu bytes)", editor->file_path.c_str(), editor->bytes.size());
}
// Format bytes into Offset Hex ASCII string
std::string format_row(const std::vector<unsigned char>& bytes, size_t offset) {
std::ostringstream oss;
// Offset (8 hex digits)
oss << std::setw(8) << std::setfill('0') << std::hex << std::uppercase << offset << " ";
// Hex bytes (16 per row)
for (int i = 0; i < 16; ++i) {
if (offset + i < bytes.size()) {
oss << std::setw(2)
<< std::setfill('0')
<< std::hex
<< std::uppercase
<< static_cast<int>(bytes[offset + i])
<< " ";
} else {
oss << " ";
}
}
oss << " ";
// ASCII
for (int i = 0; i < 16; ++i) {
if (offset + i < bytes.size()) {
unsigned char c = bytes[offset + i];
if (std::isprint(c)) {
oss << c;
} else {
oss << '.';
}
}
}
return oss.str();
}
// Get byte index based on where the mouse clicks on the window
size_t get_byte_index(EditorState& editor, int mouse_x, int mouse_y)
{
constexpr int TOP_OFFSET = 34;
constexpr int ROW_H = 16;
constexpr int HEX_START_X = 110;
constexpr int HEX_CELL_W = 30;
int row = (mouse_y - TOP_OFFSET) / ROW_H;
int col = (mouse_x - HEX_START_X) / HEX_CELL_W;
if (row < 0 || col < 0 || col >= 16)
return (size_t)-1;
size_t index = (row + editor.scroll_offset) * 16 + col;
return index;
}
// Convert byte_index to hex string
std::string index_to_hex(EditorState& editor, size_t byte_index) {
unsigned char b = editor.bytes[byte_index];
std::ostringstream oss;
oss << std::hex << std::uppercase << std::setw(2)
<< std::setfill('0')
<< (int)b;
return oss.str();
}
bool save_file(const std::string& file_path, const std::vector<unsigned char>& bytes)
{
std::ofstream file(file_path, std::ios::binary);
if (!file) return false;
file.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
return true;
}