Changed a number of declarations in Imgtool to use std::string instead of 'const char *'

This commit is contained in:
Nathan Woods 2016-12-11 10:30:22 -05:00
parent acc42e344d
commit b995c53b91
4 changed files with 35 additions and 40 deletions

View File

@ -881,7 +881,7 @@ char *imgtool_temp_str(void)
***************************************************************************/ ***************************************************************************/
imgtoolerr_t imgtool::image::internal_open(const imgtool_module *module, const char *fname, imgtoolerr_t imgtool::image::internal_open(const imgtool_module *module, const std::string &filename,
int read_or_write, util::option_resolution *createopts, imgtool::image::ptr &outimg) int read_or_write, util::option_resolution *createopts, imgtool::image::ptr &outimg)
{ {
imgtoolerr_t err; imgtoolerr_t err;
@ -908,7 +908,7 @@ imgtoolerr_t imgtool::image::internal_open(const imgtool_module *module, const c
} }
// open the stream // open the stream
stream = imgtool::stream::open(fname, read_or_write); stream = imgtool::stream::open(filename, read_or_write);
if (!stream) if (!stream)
{ {
err = (imgtoolerr_t)(IMGTOOLERR_FILENOTFOUND | IMGTOOLERR_SRC_IMAGEFILE); err = (imgtoolerr_t)(IMGTOOLERR_FILENOTFOUND | IMGTOOLERR_SRC_IMAGEFILE);
@ -964,7 +964,7 @@ done:
// open - open an image // open - open an image
//------------------------------------------------- //-------------------------------------------------
imgtoolerr_t imgtool::image::open(const imgtool_module *module, const char *filename, int read_or_write, ptr &outimg) imgtoolerr_t imgtool::image::open(const imgtool_module *module, const std::string &filename, int read_or_write, ptr &outimg)
{ {
read_or_write = read_or_write ? OSD_FOPEN_RW : OSD_FOPEN_READ; read_or_write = read_or_write ? OSD_FOPEN_RW : OSD_FOPEN_READ;
return internal_open(module, filename, read_or_write, nullptr, outimg); return internal_open(module, filename, read_or_write, nullptr, outimg);
@ -975,7 +975,7 @@ imgtoolerr_t imgtool::image::open(const imgtool_module *module, const char *file
// imgtool::image::open_byname - open an image // imgtool::image::open_byname - open an image
//------------------------------------------------- //-------------------------------------------------
imgtoolerr_t imgtool::image::open(const std::string &modulename, const char *filename, int read_or_write, ptr &outimg) imgtoolerr_t imgtool::image::open(const std::string &modulename, const std::string &filename, int read_or_write, ptr &outimg)
{ {
const imgtool_module *module; const imgtool_module *module;
@ -1016,7 +1016,7 @@ imgtool::image::~image()
// create - creates an image // create - creates an image
//------------------------------------------------- //-------------------------------------------------
imgtoolerr_t imgtool::image::create(const imgtool_module *module, const char *fname, imgtoolerr_t imgtool::image::create(const imgtool_module *module, const std::string &filename,
util::option_resolution *opts, ptr &image) util::option_resolution *opts, ptr &image)
{ {
std::unique_ptr<util::option_resolution> alloc_resolution; std::unique_ptr<util::option_resolution> alloc_resolution;
@ -1033,7 +1033,7 @@ imgtoolerr_t imgtool::image::create(const imgtool_module *module, const char *fn
opts = alloc_resolution.get(); opts = alloc_resolution.get();
} }
return internal_open(module, fname, OSD_FOPEN_RW_CREATE, opts, image); return internal_open(module, filename, OSD_FOPEN_RW_CREATE, opts, image);
} }
@ -1041,7 +1041,7 @@ imgtoolerr_t imgtool::image::create(const imgtool_module *module, const char *fn
// create - creates an image // create - creates an image
//------------------------------------------------- //-------------------------------------------------
imgtoolerr_t imgtool::image::create(const std::string &modulename, const char *fname, util::option_resolution *opts, ptr &image) imgtoolerr_t imgtool::image::create(const std::string &modulename, const std::string &filename, util::option_resolution *opts, ptr &image)
{ {
const imgtool_module *module; const imgtool_module *module;
@ -1049,7 +1049,7 @@ imgtoolerr_t imgtool::image::create(const std::string &modulename, const char *f
if (!module) if (!module)
return (imgtoolerr_t)(IMGTOOLERR_MODULENOTFOUND | IMGTOOLERR_SRC_MODULE); return (imgtoolerr_t)(IMGTOOLERR_MODULENOTFOUND | IMGTOOLERR_SRC_MODULE);
return create(module, fname, opts, image); return create(module, filename, opts, image);
} }
@ -1057,11 +1057,11 @@ imgtoolerr_t imgtool::image::create(const std::string &modulename, const char *f
// create - creates an image // create - creates an image
//------------------------------------------------- //-------------------------------------------------
imgtoolerr_t imgtool::image::create(const imgtool_module *module, const char *fname, imgtoolerr_t imgtool::image::create(const imgtool_module *module, const std::string &filename,
util::option_resolution *opts) util::option_resolution *opts)
{ {
std::unique_ptr<image> image; std::unique_ptr<image> image;
return create(module, fname, opts, image); return create(module, filename, opts, image);
} }
@ -1069,10 +1069,10 @@ imgtoolerr_t imgtool::image::create(const imgtool_module *module, const char *fn
// create - creates an image // create - creates an image
//------------------------------------------------- //-------------------------------------------------
imgtoolerr_t imgtool::image::create(const std::string &modulename, const char *fname, util::option_resolution *opts) imgtoolerr_t imgtool::image::create(const std::string &modulename, const std::string &filename, util::option_resolution *opts)
{ {
std::unique_ptr<image> image; std::unique_ptr<image> image;
return create(modulename, fname, opts, image); return create(modulename, filename, opts, image);
} }
@ -2416,8 +2416,9 @@ imgtool::directory::directory(imgtool::partition &partition)
// enumerating files on a partition // enumerating files on a partition
//------------------------------------------------- //-------------------------------------------------
imgtoolerr_t imgtool::directory::open(imgtool::partition &partition, const char *path, imgtool::directory::ptr &outenum) imgtoolerr_t imgtool::directory::open(imgtool::partition &partition, const std::string &path_string, imgtool::directory::ptr &outenum)
{ {
const char *path = path_string.c_str();
imgtoolerr_t err = (imgtoolerr_t)IMGTOOLERR_SUCCESS; imgtoolerr_t err = (imgtoolerr_t)IMGTOOLERR_SUCCESS;
imgtool::directory::ptr enumeration; imgtool::directory::ptr enumeration;
char *alloc_path = nullptr; char *alloc_path = nullptr;

View File

@ -98,12 +98,12 @@ namespace imgtool
~image(); ~image();
static imgtoolerr_t identify_file(const char *filename, imgtool_module **modules, size_t count); static imgtoolerr_t identify_file(const char *filename, imgtool_module **modules, size_t count);
static imgtoolerr_t open(const imgtool_module *module, const char *filename, int read_or_write, ptr &outimg); static imgtoolerr_t open(const imgtool_module *module, const std::string &filename, int read_or_write, ptr &outimg);
static imgtoolerr_t open(const std::string &modulename, const char *filename, int read_or_write, ptr &outimg); static imgtoolerr_t open(const std::string &modulename, const std::string &filename, int read_or_write, ptr &outimg);
static imgtoolerr_t create(const imgtool_module *module, const char *fname, util::option_resolution *opts, ptr &image); static imgtoolerr_t create(const imgtool_module *module, const std::string &filename, util::option_resolution *opts, ptr &image);
static imgtoolerr_t create(const std::string &modulename, const char *fname, util::option_resolution *opts, ptr &image); static imgtoolerr_t create(const std::string &modulename, const std::string &filename, util::option_resolution *opts, ptr &image);
static imgtoolerr_t create(const imgtool_module *module, const char *fname, util::option_resolution *opts); static imgtoolerr_t create(const imgtool_module *module, const std::string &filename, util::option_resolution *opts);
static imgtoolerr_t create(const std::string &modulename, const char *fname, util::option_resolution *opts); static imgtoolerr_t create(const std::string &modulename, const std::string &filename, util::option_resolution *opts);
static uint64_t rand(); static uint64_t rand();
std::string info(); std::string info();
@ -129,7 +129,7 @@ namespace imgtool
// better C++ adoption (e.g. - std::unique_ptr<>, std:move() etc) // better C++ adoption (e.g. - std::unique_ptr<>, std:move() etc)
bool m_okay_to_close; bool m_okay_to_close;
static imgtoolerr_t internal_open(const imgtool_module *module, const char *fname, static imgtoolerr_t internal_open(const imgtool_module *module, const std::string &filename,
int read_or_write, util::option_resolution *createopts, imgtool::image::ptr &outimg); int read_or_write, util::option_resolution *createopts, imgtool::image::ptr &outimg);
}; };
} }
@ -244,7 +244,7 @@ namespace imgtool
~directory(); ~directory();
// methods // methods
static imgtoolerr_t open(imgtool::partition &partition, const char *path, ptr &outenum); static imgtoolerr_t open(imgtool::partition &partition, const std::string &path, ptr &outenum);
imgtoolerr_t get_next(imgtool_dirent &ent); imgtoolerr_t get_next(imgtool_dirent &ent);
// accessors // accessors

View File

@ -24,7 +24,6 @@
imgtool::stream::stream(bool wp) imgtool::stream::stream(bool wp)
: imgtype(IMG_FILE) : imgtype(IMG_FILE)
, write_protect(wp) , write_protect(wp)
, name(nullptr)
, position(0) , position(0)
, filesize(0) , filesize(0)
, file() , file()
@ -40,7 +39,6 @@ imgtool::stream::stream(bool wp)
imgtool::stream::stream(bool wp, util::core_file::ptr &&f) imgtool::stream::stream(bool wp, util::core_file::ptr &&f)
: imgtype(IMG_FILE) : imgtype(IMG_FILE)
, write_protect(wp) , write_protect(wp)
, name(nullptr)
, position(0) , position(0)
, filesize(f->size()) , filesize(f->size())
, file(std::move(f)) , file(std::move(f))
@ -56,7 +54,6 @@ imgtool::stream::stream(bool wp, util::core_file::ptr &&f)
imgtool::stream::stream(bool wp, std::size_t size) imgtool::stream::stream(bool wp, std::size_t size)
: imgtype(IMG_MEM) : imgtype(IMG_MEM)
, write_protect(wp) , write_protect(wp)
, name(nullptr)
, position(0) , position(0)
, filesize(size) , filesize(size)
, file() , file()
@ -72,7 +69,6 @@ imgtool::stream::stream(bool wp, std::size_t size)
imgtool::stream::stream(bool wp, std::size_t size, void *buf) imgtool::stream::stream(bool wp, std::size_t size, void *buf)
: imgtype(IMG_MEM) : imgtype(IMG_MEM)
, write_protect(wp) , write_protect(wp)
, name(nullptr)
, position(0) , position(0)
, filesize(size) , filesize(size)
, file() , file()
@ -96,13 +92,13 @@ imgtool::stream::~stream()
// open_zip // open_zip
//------------------------------------------------- //-------------------------------------------------
imgtool::stream::ptr imgtool::stream::open_zip(const char *zipname, const char *subname, int read_or_write) imgtool::stream::ptr imgtool::stream::open_zip(const std::string &zipname, const char *subname, int read_or_write)
{ {
if (read_or_write) if (read_or_write)
return imgtool::stream::ptr(); return imgtool::stream::ptr();
/* check to see if the file exists */ /* check to see if the file exists */
FILE *f = fopen(zipname, "r"); FILE *f = fopen(zipname.c_str(), "r");
if (!f) if (!f)
return imgtool::stream::ptr(); return imgtool::stream::ptr();
fclose(f); fclose(f);
@ -139,7 +135,7 @@ imgtool::stream::ptr imgtool::stream::open_zip(const char *zipname, const char *
// open // open
//------------------------------------------------- //-------------------------------------------------
imgtool::stream::ptr imgtool::stream::open(const char *fname, int read_or_write) imgtool::stream::ptr imgtool::stream::open(const std::string &filename, int read_or_write)
{ {
static const uint32_t write_modes[] = static const uint32_t write_modes[] =
{ {
@ -151,22 +147,22 @@ imgtool::stream::ptr imgtool::stream::open(const char *fname, int read_or_write)
imgtool::stream::ptr s; imgtool::stream::ptr s;
char c; char c;
/* maybe we are just a ZIP? */ // maybe we are just a ZIP?
const char *ext = strrchr(fname, '.'); std::string ext = core_filename_extract_extension(filename);
if (ext && !core_stricmp(ext, ".zip")) if (!core_stricmp(ext.c_str(), ".zip"))
return open_zip(fname, nullptr, read_or_write); return open_zip(filename, nullptr, read_or_write);
util::core_file::ptr f = nullptr; util::core_file::ptr f = nullptr;
auto const filerr = util::core_file::open(fname, write_modes[read_or_write], f); auto const filerr = util::core_file::open(filename, write_modes[read_or_write], f);
if (filerr != osd_file::error::NONE) if (filerr != osd_file::error::NONE)
{ {
if (!read_or_write) if (!read_or_write)
{ {
int const len = strlen(fname); int const len = filename.size();
/* can't open the file; try opening ZIP files with other names */ /* can't open the file; try opening ZIP files with other names */
std::vector<char> buf(len + 1); std::vector<char> buf(len + 1);
strcpy(&buf[0], fname); strcpy(&buf[0], filename.c_str());
for (int i = len-1; !s && (i >= 0); i--) for (int i = len-1; !s && (i >= 0); i--)
{ {
@ -189,8 +185,7 @@ imgtool::stream::ptr imgtool::stream::open(const char *fname, int read_or_write)
imgtool::stream::ptr imgfile(new imgtool::stream(read_or_write ? false : true, std::move(f))); imgtool::stream::ptr imgfile(new imgtool::stream(read_or_write ? false : true, std::move(f)));
/* Normal file */ // normal file
imgfile->name = fname;
return imgfile; return imgfile;
} }

View File

@ -23,7 +23,7 @@ namespace imgtool
~stream(); ~stream();
static imgtool::stream::ptr open(const char *fname, int read_or_write); /* similar params to mame_fopen */ static imgtool::stream::ptr open(const std::string &filename, int read_or_write); /* similar params to mame_fopen */
static imgtool::stream::ptr open_write_stream(int filesize); static imgtool::stream::ptr open_write_stream(int filesize);
static imgtool::stream::ptr open_mem(void *buf, size_t sz); static imgtool::stream::ptr open_mem(void *buf, size_t sz);
@ -61,7 +61,6 @@ namespace imgtool
imgtype_t imgtype; imgtype_t imgtype;
bool write_protect; bool write_protect;
const char *name; // needed for clear
std::uint64_t position; std::uint64_t position;
std::uint64_t filesize; std::uint64_t filesize;
@ -75,7 +74,7 @@ namespace imgtool
stream(bool wp, std::size_t size, void *buf); stream(bool wp, std::size_t size, void *buf);
// private methods // private methods
static stream::ptr open_zip(const char *zipname, const char *subname, int read_or_write); static stream::ptr open_zip(const std::string &zipname, const char *subname, int read_or_write);
}; };
} }