-
Notifications
You must be signed in to change notification settings - Fork 9
/
filemisc.cpp
253 lines (202 loc) · 5.65 KB
/
filemisc.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
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
/*
This file is part of Retro Graphics Toolkit
Retro Graphics Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.
Retro Graphics Toolkit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Retro Graphics Toolkit. If not, see <http://www.gnu.org/licenses/>.
Copyright Sega16 (or whatever you wish to call me) (2012-2017)
*/
#include <cstdio>
#include <stdint.h>
#include <stdexcept>
#include <FL/fl_ask.H>
#include "errorMsg.h"
#include "gui.h"
#include "filemisc.h"
void saveStrifNot(FILE*fp, const char*str, const char*cmp) {
if (strcmp(cmp, str) != 0)
fputs(str, fp);
fputc(0, fp);
}
void fileToStr(FILE*fp, std::string&s, const char*defaultStr) {
char d = fgetc(fp);
if (d) {
s.clear();
do {
s.push_back(d);
} while ((d = fgetc(fp)));
} else
s.assign(defaultStr);
}
int clipboardAsk(void) {
return fl_choice("File or clipboard?", "File", "Clipboard", "Cancel");
}
fileType_t askSaveType(bool save, fileType_t def) {
return (fileType_t)MenuPopup(save ? "How would you like the file saved?" : "What type of file is this?", "Set if the file is saved as binary, C header, bex, or asm", 4, (unsigned)def, "Binary", "C header", "ASM", "BEX");
}
bool saveBinAsText(const void * ptr, size_t sizeBin, FILE * fp, fileType_t type, const char*comment, const char*label, int bits, boost::endian::order endian) {
/*!
This function saves binary data as plain text useful for C headers each byte is separated by a comma
To use the clipboard specify file as NULL
Returns True on success false on error
Type can be:
*/
if (type == fileType_t::tBinary && !fp)
throw std::invalid_argument("type cannot be tBinary when clipboard mode is enabled.");
uint8_t * dat8 = (uint8_t *)ptr;
uint16_t * dat16 = (uint16_t *)ptr;
uint32_t * dat32 = (uint32_t *)ptr;
char endc = ',';
std::string temp;
unsigned mask = (bits / 8) - 1;
char tmp[16];
if (mask) {
if (sizeBin & mask) {
fl_alert("Error file type unaligned to %d bits", bits);
return false;
}
sizeBin /= mask + 1;
}
switch (bits) {
case 8:
mask = 31;
break;
case 16:
mask = 15;
break;
case 32:
mask = 7;
break;
}
if (comment && type != fileType_t::tBinary) {
switch (type) {
case fileType_t::tCheader:
temp.assign("// ");
break;
case fileType_t::tASM:
temp.assign("; ");
break;
case fileType_t::tBEX:
temp.assign("' ");
break;
default:
show_default_error
}
temp.append(comment);
temp.push_back('\n');
}
char hexStr[4];
switch (type) {
case fileType_t::tCheader:
temp.append("#include <stdint.h>\n");
temp.append("const uint");
snprintf(tmp, 16, "%d", bits);
temp.append(tmp);
temp.append("_t ");
temp.append(label);
temp.append("[]={");
strncpy(hexStr, "0x", sizeof(hexStr));
break;
case fileType_t::tASM:
case fileType_t::tBEX:
temp.append(label);
temp.push_back(':');
strncpy(hexStr, "$", sizeof(hexStr));
break;
}
for (size_t x = 0; x < sizeBin; ++x) {
if ((x & mask) == 0) {
temp.push_back('\n');
switch (type) {
case fileType_t::tASM:
switch (bits) {
case 8:
temp.append("\tdc.b ");
break;
case 16:
temp.append("\tdc.w ");
break;
case 32:
temp.append("\tdc.l ");
break;
}
break;
case fileType_t::tBEX:
switch (bits) {
case 8:
temp.append("\tdata ");
break;
case 16:
temp.append("\tdataint ");
break;
case 32:
temp.append("\tdatalong ");
break;
}
break;
}
}
if (((x & mask) == mask) && (type != fileType_t::tCheader))
endc = 0;
else
endc = ',';
if (x == (sizeBin - 1))
endc = '\n';
switch (bits) {
case 8:
{
uint8_t datTmp = *dat8++;
if (type == fileType_t::tBinary)
fwrite(&datTmp, sizeof(uint8_t), 1, fp);
else
snprintf(tmp, sizeof(tmp), "%s%X", hexStr, datTmp);
}
break;
case 16:
{
uint16_t datTmp = *dat16++;
if (type == fileType_t::tBinary) {
if (endian == boost::endian::order::little)
boost::endian::conditional_reverse_inplace<boost::endian::order::native, boost::endian::order::little>(datTmp);
else if (endian == boost::endian::order::big)
boost::endian::conditional_reverse_inplace<boost::endian::order::native, boost::endian::order::big>(datTmp);
fwrite(&datTmp, sizeof(uint16_t), 1, fp);
} else
snprintf(tmp, sizeof(tmp), "%s%X", hexStr, datTmp);
}
break;
case 32:
{
uint32_t datTmp = *dat32++;
if (type == fileType_t::tBinary) {
if (endian == boost::endian::order::little)
boost::endian::conditional_reverse_inplace<boost::endian::order::native, boost::endian::order::little>(datTmp);
else if (endian == boost::endian::order::big)
boost::endian::conditional_reverse_inplace<boost::endian::order::native, boost::endian::order::big>(datTmp);
fwrite(&datTmp, sizeof(uint32_t), 1, fp);
} else
snprintf(tmp, sizeof(tmp), "%s%X", hexStr, datTmp);
}
break;
default:
show_default_error
}
temp.append(tmp);
if (endc)
temp.push_back(endc);
}
if (type == fileType_t::tCheader)
temp.append("};\n");
if (type != fileType_t::tBinary) {
if (fp)
fputs(temp.c_str(), fp);
else
Fl::copy(temp.c_str(), temp.length(), 1);
}
return true;
}