imgtool_stream *stream_open(const char *fname, int read_or_write) { static const UINT32 write_modes[] = { OPEN_FLAG_READ, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, OPEN_FLAG_READ | OPEN_FLAG_WRITE, OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE }; imgtool_stream *s = nullptr; char c; /* maybe we are just a ZIP? */ const char *ext = strrchr(fname, '.'); if (ext && !core_stricmp(ext, ".zip")) return stream_open_zip(fname, nullptr, read_or_write); util::core_file::ptr f = nullptr; auto const filerr = util::core_file::open(fname, write_modes[read_or_write], f); if (filerr != osd_file::error::NONE) { if (!read_or_write) { int const len = strlen(fname); /* can't open the file; try opening ZIP files with other names */ std::vector<char> buf(len + 1); strcpy(&buf[0], fname); for (int i = len-1; !s && (i >= 0); i--) { if ((buf[i] == '\\') || (buf[i] == '/')) { c = buf[i]; buf[i] = '\0'; s = stream_open_zip(&buf[0], &buf[i + 1], read_or_write); buf[i] = c; } } if (s) return s; } /* ah well, it was worth a shot */ return nullptr; } imgtool_stream::ptr imgfile(new imgtool_stream(read_or_write ? false : true, std::move(f))); /* Normal file */ imgfile->name = fname; return imgfile.release(); }
imgtool_stream *stream_open(const char *fname, int read_or_write) { file_error filerr; const char *ext; imgtool_stream *imgfile = NULL; static const UINT32 write_modes[] = { OPEN_FLAG_READ, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, OPEN_FLAG_READ | OPEN_FLAG_WRITE, OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE }; core_file *f = NULL; char *buf = NULL; int len, i; imgtool_stream *s = NULL; char c; /* maybe we are just a ZIP? */ ext = strrchr(fname, '.'); if (ext && !mame_stricmp(ext, ".zip")) return stream_open_zip(fname, NULL, read_or_write); filerr = core_fopen(fname, write_modes[read_or_write], &f); if (filerr != FILERR_NONE) { if (!read_or_write) { len = strlen(fname); /* can't open the file; try opening ZIP files with other names */ buf = (char*)malloc(len + 1); if (!buf) goto error; strcpy(buf, fname); for(i = len-1; !s && (i >= 0); i--) { if ((buf[i] == '\\') || (buf[i] == '/')) { c = buf[i]; buf[i] = '\0'; s = stream_open_zip(buf, buf + i + 1, read_or_write); buf[i] = c; } } free(buf); buf = NULL; if (s) return s; } /* ah well, it was worth a shot */ goto error; } imgfile = (imgtool_stream *)malloc(sizeof(struct _imgtool_stream)); if (!imgfile) goto error; /* Normal file */ memset(imgfile, 0, sizeof(*imgfile)); imgfile->imgtype = IMG_FILE; imgfile->position = 0; imgfile->filesize = core_fsize(f); imgfile->write_protect = read_or_write ? 0 : 1; imgfile->u.file = f; imgfile->name = fname; return imgfile; error: if (imgfile != NULL) free((void *) imgfile); if (f != NULL) core_fclose(f); if (buf) free(buf); return (imgtool_stream *) NULL; }