Added a Save Dump As... feature to save a memory zone to a file
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / SaveDumpAsWin.cpp
1 //
2 // SaveDumpAsWin.cpp - Save Dump function
3 //
4 // by Jean-Paul Mari
5 //
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7 //
8 // Who When What
9 // --- ---------- -----------------------------------------------------------
10 // JPM 04/10/2019 Created this file
11 //
12
13 // STILL TO DO:
14 //
15
16 #include "debugger/SaveDumpAsWin.h"
17 #include "jaguar.h"
18 #include "debugger/DBGManager.h"
19 #include "m68000/m68kinterface.h"
20 #include "settings.h"
21
22
23 //
24 SaveDumpAsWindow::SaveDumpAsWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Dialog),
25 layout(new QVBoxLayout),
26 maddress(new QLineEdit),
27 msize(new QLineEdit),
28 savedump(new SaveDumpAsInfo),
29 save(new QPushButton(tr("Save")))
30 {
31 setWindowTitle(tr("Save Dump As..."));
32
33 maddress->setPlaceholderText("0x<value>, decimal value or symbol name");
34 msize->setPlaceholderText("0x<value>, or decimal value");
35
36 QHBoxLayout * hbox1 = new QHBoxLayout;
37 hbox1->addWidget(maddress);
38 hbox1->addWidget(msize);
39 hbox1->addWidget(save);
40
41 layout->addLayout(hbox1);
42 setLayout(layout);
43
44 connect(save, SIGNAL(clicked()), this, SLOT(SaveDumpAs()));
45 }
46
47
48 //
49 SaveDumpAsWindow::~SaveDumpAsWindow(void)
50 {
51 }
52
53
54 //
55 void SaveDumpAsWindow::keyPressEvent(QKeyEvent * e)
56 {
57 if (e->key() == Qt::Key_Escape)
58 {
59 hide();
60 }
61 else
62 {
63 if (e->key() == Qt::Key_Return)
64 {
65 SaveDumpAs();
66 }
67 }
68 }
69
70
71 // Save Dump
72 void SaveDumpAsWindow::SaveDumpAs(void)
73 {
74 if (SelectAddress() && SelectSize())
75 {
76 QString fileName = QFileDialog::getSaveFileName(this, tr("Save dump"), "", tr("Save dump files (*.bin)"));
77
78 if (fileName.size())
79 {
80 if (FILE *File = fopen((char *)fileName.toUtf8().data(), "wb"))
81 {
82 if (fwrite((void *)&jagMemSpace[savedump->Adr], savedump->Size, 1, File) == 1)
83 {
84 fclose(File);
85 }
86 }
87 }
88 }
89 }
90
91
92 // Select size
93 bool SaveDumpAsWindow::SelectSize(void)
94 {
95 bool ok = false;
96 size_t len;
97 QString newSize;
98 size_t s;
99
100 QPalette p = msize->palette();
101 newSize = msize->text();
102
103 if ((len = newSize.size()))
104 {
105 if ((len > 1) && (newSize.at(0) == QChar('0')) && (newSize.at(1) == QChar('x')))
106 {
107 s = newSize.toUInt(&ok, 16);
108 }
109 else
110 {
111 s = newSize.toUInt(&ok, 10);
112 }
113
114 // Check validity size
115 if (ok && s && (s < 0xffffff))
116 {
117 // In all cases, consider size as valid
118 savedump->Size = s;
119 p.setColor(QPalette::Text, Qt::darkYellow);
120 }
121 else
122 {
123 // Size is not valid
124 ok = false;
125 p.setColor(QPalette::Text, Qt::red);
126 }
127 }
128 else
129 {
130 // Size has not be set
131 p.setColor(QPalette::Text, Qt::darkRed);
132 }
133
134 msize->setPalette(p);
135 return ok ? true : false;
136 }
137
138
139 // Select address
140 // Address can be an hexa, decimal or a symbol name
141 bool SaveDumpAsWindow::SelectAddress(void)
142 {
143 bool ok = false;
144 size_t len;
145 QString newAddress;
146 size_t adr;
147
148 QPalette p = maddress->palette();
149 newAddress = maddress->text();
150
151 if ((len = newAddress.size()))
152 {
153 if ((len > 1) && (newAddress.at(0) == QChar('0')) && (newAddress.at(1) == QChar('x')))
154 {
155 adr = newAddress.toUInt(&ok, 16);
156 }
157 else
158 {
159 if (!(adr = DBGManager_GetAdrFromSymbolName(newAddress.toLatin1().data())))
160 {
161 adr = newAddress.toUInt(&ok, 10);
162 }
163 else
164 {
165 ok = true;
166 }
167 }
168
169 // Check validity address
170 if (ok && (adr < 0xffffff))
171 {
172 // In all cases, consider address as valid
173 savedump->Adr = adr;
174 p.setColor(QPalette::Text, Qt::darkYellow);
175 }
176 else
177 {
178 // Address is not valid
179 p.setColor(QPalette::Text, Qt::red);
180 }
181 }
182 else
183 {
184 // Address has not be set
185 p.setColor(QPalette::Text, Qt::darkRed);
186 }
187
188 maddress->setPalette(p);
189 return ok ? true : false;
190 }