bool ext_matches_list(const char *name, const char **exts) { for (const char **p = exts; *p; p++) { if (ext_matches(name, *p)) return true; } return false; }
static int ext_matches_list(const char *name, const char **exts) { const char **p; for (p = exts; *p; p++) { if (ext_matches(name, *p)) return 1; } return 0; }
int main(int argc, char *argv[]) { FILE *fp; int i; format_srm(); memset(&files, 0, sizeof(files)); if (argc < 2) { Usage: printf("Usage:\n"); printf(" To create a srm file: %s [file.eep] [file.mpk] [file.sra] [file.fla]\n", argv[0]); printf(" To extract a srm file: %s <file.srm>\n\n", argv[0]); printf("Output files will be placed in the same directory of the input files.\n"); exit(EXIT_FAILURE); } for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help") || !strcmp(argv[i], "/?")) goto Usage; if (ext_matches("srm", argv[i])) strncpy(files.srm, argv[i], 512); if (ext_matches("eep", argv[i])) strncpy(files.eep, argv[i], 512); if (ext_matches("mpk", argv[i])) strncpy(files.mpk, argv[i], 512); if (ext_matches("sra", argv[i])) strncpy(files.sra, argv[i], 512); if (ext_matches("fla", argv[i])) strncpy(files.fla, argv[i], 512); } if (*files.srm) { puts("Running in srm extraction mode"); read_mem(files.srm, &srm, sizeof(srm)); if (!is_empty_mem(srm.eeprom, sizeof(srm.eeprom))) change_extension(files.eep, files.srm, ".eep"); if (!is_empty_mem((uint8_t*)srm.mempack, sizeof(srm.mempack))) change_extension(files.mpk, files.srm, ".mpk"); if (!is_empty_mem(srm.sram, sizeof(srm.sram))) change_extension(files.sra, files.srm, ".sra"); if (!is_empty_mem(srm.flashram, sizeof(srm.flashram))) change_extension(files.fla, files.srm, ".fla"); int over = ((*files.eep && access(files.eep, 0) != -1) || (*files.mpk && access(files.mpk, 0) != -1) || (*files.sra && access(files.sra, 0) != -1) || (*files.fla && access(files.fla, 0) != -1)); if (over && !overwrite_confirm("existing saves in the source directory")) { puts("existing saves unmodified."); exit(EXIT_SUCCESS); } if (*files.eep) write_mem(files.eep, srm.eeprom, sizeof(srm.eeprom)); if (*files.mpk) write_mem(files.mpk, srm.mempack, sizeof(srm.mempack)); if (*files.sra) write_mem(files.sra, srm.sram, sizeof(srm.sram)); if (*files.fla) write_mem(files.fla, srm.flashram, sizeof(srm.flashram)); } else { puts("srm file unspecified, running in srm creation mode"); if (!files.eep && !files.mpk && !files.sra && !files.fla) die("no input files."); /* pick the first filename */ if (!*files.srm) { if (*files.eep) change_extension(files.srm, files.eep, ".srm"); else if (*files.mpk) change_extension(files.srm, files.mpk, ".srm"); else if (*files.sra) change_extension(files.srm, files.sra, ".srm"); else if (*files.fla) change_extension(files.srm, files.fla, ".srm"); if (strlen(files.srm) + 3 > 512) die("path too long"); } if (*files.eep) read_mem(files.eep, srm.eeprom, sizeof(srm.eeprom)); if (*files.mpk) read_mem(files.mpk, srm.mempack, sizeof(srm.mempack)); if (*files.sra) read_mem(files.sra, srm.sram, sizeof(srm.sram)); if (*files.fla) read_mem(files.fla, srm.flashram, sizeof(srm.flashram)); int over = access(files.srm, 0 /*F_OK*/) != -1; if (over && !overwrite_confirm(files.srm)) { printf("%s unmodified.\n", files.srm); exit(EXIT_SUCCESS); } printf("Writting srm data to %s...\n", files.srm); fp = fopen(files.srm, "wb"); if (!fp) { perror(files.srm); exit(EXIT_FAILURE); } fwrite(srm.eeprom, sizeof(srm.eeprom), 1, fp); fwrite(srm.mempack, sizeof(srm.mempack), 1, fp); fwrite(srm.sram, sizeof(srm.sram), 1, fp); fwrite(srm.flashram, sizeof(srm.flashram), 1, fp); fclose(fp); } return EXIT_SUCCESS; }