mirror of
https://github.com/logicog/RTLPlayground.git
synced 2026-08-30 14:52:51 +08:00
Add mime-type of files and fix a bug with a terminating 0 for a file
This commit is contained in:
+40
-7
@@ -118,6 +118,35 @@ int addfile(const char *name, int addr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int hasSuffix(const char *str, const char *suffix)
|
||||||
|
{
|
||||||
|
if (!str || !suffix)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
size_t lenstr = strlen(str);
|
||||||
|
size_t lensuffix = strlen(suffix);
|
||||||
|
|
||||||
|
if (lensuffix > lenstr)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char *getMime(const char *name)
|
||||||
|
{
|
||||||
|
if (hasSuffix(name, ".html"))
|
||||||
|
return "text/html";
|
||||||
|
else if (hasSuffix(name, ".svg"))
|
||||||
|
return "image/svg+xml";
|
||||||
|
else if (hasSuffix(name, ".ico"))
|
||||||
|
return "image/svg+xml";
|
||||||
|
else if (hasSuffix(name, ".png"))
|
||||||
|
return "image/png";
|
||||||
|
return "text/plain";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int addidx(const char *name, int addr, int len)
|
int addidx(const char *name, int addr, int len)
|
||||||
{
|
{
|
||||||
char s[256];
|
char s[256];
|
||||||
@@ -131,9 +160,9 @@ int addidx(const char *name, int addr, int len)
|
|||||||
|
|
||||||
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_START_%s 0x%x\n", s, addr);
|
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_START_%s 0x%x\n", s, addr);
|
||||||
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_SIZE_%s %d\n", s, len);
|
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_SIZE_%s %d\n", s, len);
|
||||||
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, " {\"/%s\", FDATA_START_%s, FDATA_SIZE_%s},\n", name, s, s);
|
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, " {\"/%s\", FDATA_START_%s, FDATA_SIZE_%s, \"%s\"},\n", name, s, s, getMime(name));
|
||||||
if (!strcmp(name, "index.html"))
|
if (!strcmp(name, "index.html"))
|
||||||
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, " {\"/\", FDATA_START_%s, FDATA_SIZE_%s},\n", s, s);
|
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, " {\"/\", FDATA_START_%s, FDATA_SIZE_%s, \"text/html\"},\n", s, s);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,7 +264,7 @@ int main(int argc, char **argv)
|
|||||||
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#ifndef FDATA_DEFS_H\n\n");
|
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#ifndef FDATA_DEFS_H\n\n");
|
||||||
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_DEFS_H\n\n");
|
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#define FDATA_DEFS_H\n\n");
|
||||||
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#include <stdint.h>\n\n");
|
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "#include <stdint.h>\n\n");
|
||||||
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "struct f_data {\n __code char *file;\n uint32_t start;\n uint16_t len;\n};\n\n");
|
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "struct f_data {\n __code char *file;\n uint32_t start;\n uint16_t len;\n __code char *mime;\n};\n\n");
|
||||||
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "typedef uint16_t (*fcall_ptr)(void);\n\n");
|
defbuf_p += snprintf(&dbuf[defbuf_p], DEF_SIZE - defbuf_p, "typedef uint16_t (*fcall_ptr)(void);\n\n");
|
||||||
|
|
||||||
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "// This file is automatically generated, do not edit!\n\n");
|
ibuf_p += snprintf(&ibuf[ibuf_p], INDEX_SIZE - ibuf_p, "// This file is automatically generated, do not edit!\n\n");
|
||||||
@@ -273,8 +302,10 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
size_t data_read = addfile(pathbuffer, addr);
|
size_t data_read = addfile(pathbuffer, addr);
|
||||||
if (data_read) {
|
if (data_read) {
|
||||||
if (arguments.add_zero)
|
if (arguments.add_zero) {
|
||||||
buffer[arguments.address + data_read++ + 1] = '0';
|
buffer[addr + data_read + 1] = '\0';
|
||||||
|
data_read++;
|
||||||
|
}
|
||||||
printf("Data inserted from %s at 0x%x, size: %ld\n", pathbuffer, addr, data_read);
|
printf("Data inserted from %s at 0x%x, size: %ld\n", pathbuffer, addr, data_read);
|
||||||
}
|
}
|
||||||
int old_len = data_read;
|
int old_len = data_read;
|
||||||
@@ -288,8 +319,10 @@ int main(int argc, char **argv)
|
|||||||
size_t data_read = addfile(arguments.data_file, arguments.address);
|
size_t data_read = addfile(arguments.data_file, arguments.address);
|
||||||
|
|
||||||
if (data_read) {
|
if (data_read) {
|
||||||
if (arguments.add_zero)
|
if (arguments.add_zero) {
|
||||||
buffer[arguments.address + data_read++ + 1] = '0';
|
buffer[arguments.address + data_read + 1] = '\0';
|
||||||
|
data_read++;
|
||||||
|
}
|
||||||
printf("Data inserted at 0x%x, size: %ld\n", arguments.address, data_read);
|
printf("Data inserted at 0x%x, size: %ld\n", arguments.address, data_read);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user