[Imgtool] C++-ification of imgtool_forkent structure

This commit is contained in:
Nathan Woods 2017-07-29 09:45:16 -04:00 committed by Vas Crabb
parent 5d6815b6de
commit 7e8707f496
6 changed files with 93 additions and 60 deletions

View File

@ -580,7 +580,7 @@ imgtool::partition::partition(imgtool::image &image, const imgtool_class &imgcla
m_read_file = (imgtoolerr_t(*)(imgtool::partition &, const char *, const char *, imgtool::stream &)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_READ_FILE); m_read_file = (imgtoolerr_t(*)(imgtool::partition &, const char *, const char *, imgtool::stream &)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_READ_FILE);
m_write_file = (imgtoolerr_t(*)(imgtool::partition &, const char *, const char *, imgtool::stream &, util::option_resolution *)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_WRITE_FILE); m_write_file = (imgtoolerr_t(*)(imgtool::partition &, const char *, const char *, imgtool::stream &, util::option_resolution *)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_WRITE_FILE);
m_delete_file = (imgtoolerr_t(*)(imgtool::partition &, const char *)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_DELETE_FILE); m_delete_file = (imgtoolerr_t(*)(imgtool::partition &, const char *)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_DELETE_FILE);
m_list_forks = (imgtoolerr_t(*)(imgtool::partition &, const char *, imgtool_forkent *, size_t)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_LIST_FORKS); m_list_forks = (imgtoolerr_t(*)(imgtool::partition &, const char *, std::vector<imgtool::fork_entry> &forks)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_LIST_FORKS);
m_create_dir = (imgtoolerr_t(*)(imgtool::partition &, const char *)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_CREATE_DIR); m_create_dir = (imgtoolerr_t(*)(imgtool::partition &, const char *)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_CREATE_DIR);
m_delete_dir = (imgtoolerr_t(*)(imgtool::partition &, const char *)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_DELETE_DIR); m_delete_dir = (imgtoolerr_t(*)(imgtool::partition &, const char *)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_DELETE_DIR);
m_list_attrs = (imgtoolerr_t(*)(imgtool::partition &, const char *, uint32_t *, size_t)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_LIST_ATTRS); m_list_attrs = (imgtoolerr_t(*)(imgtool::partition &, const char *, uint32_t *, size_t)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_LIST_ATTRS);
@ -1873,7 +1873,7 @@ imgtoolerr_t imgtool::partition::delete_file(const char *fname)
// forks on an image // forks on an image
//------------------------------------------------- //-------------------------------------------------
imgtoolerr_t imgtool::partition::list_file_forks(const char *path, imgtool_forkent *ents, size_t len) imgtoolerr_t imgtool::partition::list_file_forks(const char *path, std::vector<imgtool::fork_entry> &forks)
{ {
imgtoolerr_t err; imgtoolerr_t err;
if (!m_list_forks) if (!m_list_forks)
@ -1885,7 +1885,9 @@ imgtoolerr_t imgtool::partition::list_file_forks(const char *path, imgtool_forke
if (err) if (err)
return err; return err;
err = m_list_forks(*this, cannonical_path.c_str(), ents, len); // call the callback
forks.clear();
err = m_list_forks(*this, cannonical_path.c_str(), forks);
if (err) if (err)
return err; return err;

View File

@ -158,7 +158,7 @@ namespace imgtool
imgtoolerr_t get_file(const char *filename, const char *fork, const char *dest, filter_getinfoproc filter); imgtoolerr_t get_file(const char *filename, const char *fork, const char *dest, filter_getinfoproc filter);
imgtoolerr_t put_file(const char *newfname, const char *fork, const char *source, util::option_resolution *opts, filter_getinfoproc filter); imgtoolerr_t put_file(const char *newfname, const char *fork, const char *source, util::option_resolution *opts, filter_getinfoproc filter);
imgtoolerr_t delete_file(const char *fname); imgtoolerr_t delete_file(const char *fname);
imgtoolerr_t list_file_forks(const char *path, imgtool_forkent *ents, size_t len); imgtoolerr_t list_file_forks(const char *path, std::vector<imgtool::fork_entry> &forks);
imgtoolerr_t create_directory(const char *path); imgtoolerr_t create_directory(const char *path);
imgtoolerr_t delete_directory(const char *path); imgtoolerr_t delete_directory(const char *path);
imgtoolerr_t list_file_attributes(const char *path, uint32_t *attrs, size_t len); imgtoolerr_t list_file_attributes(const char *path, uint32_t *attrs, size_t len);
@ -209,7 +209,7 @@ namespace imgtool
std::function<imgtoolerr_t(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &destf)> m_read_file; std::function<imgtoolerr_t(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &destf)> m_read_file;
std::function<imgtoolerr_t(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts)> m_write_file; std::function<imgtoolerr_t(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts)> m_write_file;
std::function<imgtoolerr_t(imgtool::partition &partition, const char *filename)> m_delete_file; std::function<imgtoolerr_t(imgtool::partition &partition, const char *filename)> m_delete_file;
std::function<imgtoolerr_t(imgtool::partition &partition, const char *path, imgtool_forkent *ents, size_t len)> m_list_forks; std::function<imgtoolerr_t(imgtool::partition &partition, const char *path, std::vector<imgtool::fork_entry> &forks)> m_list_forks;
std::function<imgtoolerr_t(imgtool::partition &partition, const char *path)> m_create_dir; std::function<imgtoolerr_t(imgtool::partition &partition, const char *path)> m_create_dir;
std::function<imgtoolerr_t(imgtool::partition &partition, const char *path)> m_delete_dir; std::function<imgtoolerr_t(imgtool::partition &partition, const char *path)> m_delete_dir;
std::function<imgtoolerr_t(imgtool::partition &partition, const char *path, uint32_t *attrs, size_t len)> m_list_attrs; std::function<imgtoolerr_t(imgtool::partition &partition, const char *path, uint32_t *attrs, size_t len)> m_list_attrs;

View File

@ -81,20 +81,59 @@ struct imgtool_chainent
uint64_t block; uint64_t block;
}; };
enum imgtool_forktype_t namespace imgtool
{ {
FORK_END, class fork_entry
FORK_DATA, {
FORK_RESOURCE, public:
FORK_ALTERNATE enum class type_t
}; {
DATA,
RESOURCE,
ALT
};
struct imgtool_forkent fork_entry(uint64_t size, type_t type = type_t::DATA)
{ : m_size(size)
imgtool_forktype_t type; , m_type(type)
uint64_t size; , m_name(default_name(type))
char forkname[64]; {
};
}
fork_entry(uint64_t size, std::string &&name)
: m_size(size)
, m_type(fork_entry::type_t::ALT)
, m_name(std::move(name))
{
}
fork_entry(const fork_entry &that) = default;
fork_entry(fork_entry &&that) = default;
uint64_t size() const { return m_size; }
type_t type() const { return m_type; }
const std::string &name() const { return m_name; }
private:
static std::string default_name(type_t type)
{
switch (type)
{
case type_t::DATA:
return std::string("");
case type_t::RESOURCE:
return std::string("RESOURCE_FORK");
default:
throw false;
}
}
uint64_t m_size;
type_t m_type;
std::string m_name;
};
}
struct imgtool_transfer_suggestion struct imgtool_transfer_suggestion
{ {
@ -290,7 +329,7 @@ union imgtoolinfo
imgtoolerr_t (*read_file) (imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &destf); imgtoolerr_t (*read_file) (imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &destf);
imgtoolerr_t (*write_file) (imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts); imgtoolerr_t (*write_file) (imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts);
imgtoolerr_t (*delete_file) (imgtool::partition &partition, const char *filename); imgtoolerr_t (*delete_file) (imgtool::partition &partition, const char *filename);
imgtoolerr_t (*list_forks) (imgtool::partition &partition, const char *path, imgtool_forkent *ents, size_t len); imgtoolerr_t (*list_forks) (imgtool::partition &partition, const char *path, std::vector<imgtool::fork_entry> &forks);
imgtoolerr_t (*create_dir) (imgtool::partition &partition, const char *path); imgtoolerr_t (*create_dir) (imgtool::partition &partition, const char *path);
imgtoolerr_t (*delete_dir) (imgtool::partition &partition, const char *path); imgtoolerr_t (*delete_dir) (imgtool::partition &partition, const char *path);
imgtoolerr_t (*list_attrs) (imgtool::partition &partition, const char *path, uint32_t *attrs, size_t len); imgtoolerr_t (*list_attrs) (imgtool::partition &partition, const char *path, uint32_t *attrs, size_t len);

View File

@ -5741,13 +5741,12 @@ static imgtoolerr_t mac_image_writefile(imgtool::partition &partition, const cha
static imgtoolerr_t mac_image_listforks(imgtool::partition &partition, const char *path, imgtool_forkent *ents, size_t len) static imgtoolerr_t mac_image_listforks(imgtool::partition &partition, const char *path, std::vector<imgtool::fork_entry> &forks)
{ {
imgtoolerr_t err; imgtoolerr_t err;
uint32_t parID; uint32_t parID;
mac_str255 filename; mac_str255 filename;
mac_dirent cat_info; mac_dirent cat_info;
int fork_num = 0;
imgtool::image &img(partition.image()); imgtool::image &img(partition.image());
struct mac_l2_imgref *image = get_imgref(img); struct mac_l2_imgref *image = get_imgref(img);
@ -5758,22 +5757,15 @@ static imgtoolerr_t mac_image_listforks(imgtool::partition &partition, const cha
if (cat_info.dataRecType != hcrt_File) if (cat_info.dataRecType != hcrt_File)
return IMGTOOLERR_FILENOTFOUND; return IMGTOOLERR_FILENOTFOUND;
/* specify data fork */ // specify data fork
ents[fork_num].type = FORK_DATA; forks.emplace_back(cat_info.dataLogicalSize, imgtool::fork_entry::type_t::DATA);
ents[fork_num].forkname[0] = '\0';
ents[fork_num].size = cat_info.dataLogicalSize;
fork_num++;
if (cat_info.rsrcLogicalSize > 0) if (cat_info.rsrcLogicalSize > 0)
{ {
/* specify the resource fork */ // specify the resource fork
ents[fork_num].type = FORK_RESOURCE; forks.emplace_back(cat_info.rsrcLogicalSize, imgtool::fork_entry::type_t::RESOURCE);
strcpy(ents[fork_num].forkname, "RESOURCE_FORK");
ents[fork_num].size = cat_info.rsrcLogicalSize;
fork_num++;
} }
ents[fork_num].type = FORK_END;
return IMGTOOLERR_SUCCESS; return IMGTOOLERR_SUCCESS;
} }

View File

@ -2,7 +2,7 @@
// copyright-holders:Raphael Nabet // copyright-holders:Raphael Nabet
/**************************************************************************** /****************************************************************************
macbin.c macbin.cpp
MacBinary filter for use with Mac and ProDOS drivers MacBinary filter for use with Mac and ProDOS drivers
@ -82,7 +82,6 @@ static imgtoolerr_t macbinary_readfile(imgtool::partition &partition, const char
imgtoolerr_t err; imgtoolerr_t err;
uint8_t header[128]; uint8_t header[128];
const char *basename; const char *basename;
int i;
uint32_t type_code = 0x3F3F3F3F; uint32_t type_code = 0x3F3F3F3F;
uint32_t creator_code = 0x3F3F3F3F; uint32_t creator_code = 0x3F3F3F3F;
@ -93,23 +92,32 @@ static imgtoolerr_t macbinary_readfile(imgtool::partition &partition, const char
uint8_t script_code = 0; uint8_t script_code = 0;
uint8_t extended_flags = 0; uint8_t extended_flags = 0;
imgtool_forkent fork_entries[4];
const imgtool_forkent *data_fork = NULL;
const imgtool_forkent *resource_fork = NULL;
uint32_t creation_time = 0; uint32_t creation_time = 0;
uint32_t lastmodified_time = 0; uint32_t lastmodified_time = 0;
imgtool_attribute attr_values[10]; imgtool_attribute attr_values[10];
/* get the forks */ // get the forks
err = partition.list_file_forks(filename, fork_entries, sizeof(fork_entries)); std::vector<imgtool::fork_entry> fork_entries;
err = partition.list_file_forks(filename, fork_entries);
if (err) if (err)
return err; return err;
for (i = 0; fork_entries[i].type != FORK_END; i++)
const imgtool::fork_entry *data_fork = nullptr;
const imgtool::fork_entry *resource_fork = nullptr;
for (const auto &entry : fork_entries)
{ {
if (fork_entries[i].type == FORK_DATA) switch (entry.type())
data_fork = &fork_entries[i]; {
else if (fork_entries[i].type == FORK_RESOURCE) case imgtool::fork_entry::type_t::DATA:
resource_fork = &fork_entries[i]; data_fork = &entry;
break;
case imgtool::fork_entry::type_t::RESOURCE:
resource_fork = &entry;
break;
default:
// do nothing
break;
}
} }
/* get the attributes */ /* get the attributes */
@ -144,8 +152,8 @@ static imgtoolerr_t macbinary_readfile(imgtool::partition &partition, const char
place_integer_be(header, 75, 2, coord_x); place_integer_be(header, 75, 2, coord_x);
place_integer_be(header, 77, 2, coord_y); place_integer_be(header, 77, 2, coord_y);
place_integer_be(header, 79, 2, finder_folder); place_integer_be(header, 79, 2, finder_folder);
place_integer_be(header, 83, 4, data_fork ? data_fork->size : 0); place_integer_be(header, 83, 4, data_fork ? data_fork->size() : 0);
place_integer_be(header, 87, 4, resource_fork ? resource_fork->size : 0); place_integer_be(header, 87, 4, resource_fork ? resource_fork->size() : 0);
place_integer_be(header, 91, 4, creation_time); place_integer_be(header, 91, 4, creation_time);
place_integer_be(header, 95, 4, lastmodified_time); place_integer_be(header, 95, 4, lastmodified_time);
place_integer_be(header, 101, 1, (finder_flags >> 0) & 0xFF); place_integer_be(header, 101, 1, (finder_flags >> 0) & 0xFF);
@ -164,7 +172,7 @@ static imgtoolerr_t macbinary_readfile(imgtool::partition &partition, const char
if (err) if (err)
return err; return err;
destf.fill(0, pad128(data_fork->size)); destf.fill(0, pad128(data_fork->size()));
} }
if (resource_fork) if (resource_fork)
@ -173,7 +181,7 @@ static imgtoolerr_t macbinary_readfile(imgtool::partition &partition, const char
if (err) if (err)
return err; return err;
destf.fill(0, pad128(resource_fork->size)); destf.fill(0, pad128(resource_fork->size()));
} }
return IMGTOOLERR_SUCCESS; return IMGTOOLERR_SUCCESS;

View File

@ -1813,13 +1813,12 @@ static imgtoolerr_t prodos_diskimage_deletefile(imgtool::partition &partition, c
static imgtoolerr_t prodos_diskimage_listforks(imgtool::partition &partition, const char *path, imgtool_forkent *ents, size_t len) static imgtoolerr_t prodos_diskimage_listforks(imgtool::partition &partition, const char *path, std::vector<imgtool::fork_entry> &forks)
{ {
imgtoolerr_t err; imgtoolerr_t err;
imgtool::image &image(partition.image()); imgtool::image &image(partition.image());
prodos_dirent ent; prodos_dirent ent;
prodos_direnum direnum; prodos_direnum direnum;
int fork_num = 0;
err = prodos_lookup_path(image, path, CREATE_NONE, &direnum, &ent); err = prodos_lookup_path(image, path, CREATE_NONE, &direnum, &ent);
if (err) if (err)
@ -1828,22 +1827,15 @@ static imgtoolerr_t prodos_diskimage_listforks(imgtool::partition &partition, co
if (is_dir_storagetype(ent.storage_type)) if (is_dir_storagetype(ent.storage_type))
return IMGTOOLERR_FILENOTFOUND; return IMGTOOLERR_FILENOTFOUND;
/* specify data fork */ // specify data fork
ents[fork_num].type = FORK_DATA; forks.emplace_back(ent.filesize[0], imgtool::fork_entry::type_t::DATA);
ents[fork_num].forkname[0] = '\0';
ents[fork_num].size = ent.filesize[0];
fork_num++;
if (is_extendedfile_storagetype(ent.storage_type)) if (is_extendedfile_storagetype(ent.storage_type))
{ {
/* specify the resource fork */ // specify the resource fork
ents[fork_num].type = FORK_RESOURCE; forks.emplace_back(ent.filesize[1], imgtool::fork_entry::type_t::RESOURCE);
strcpy(ents[fork_num].forkname, "RESOURCE_FORK");
ents[fork_num].size = ent.filesize[1];
fork_num++;
} }
ents[fork_num].type = FORK_END;
return IMGTOOLERR_SUCCESS; return IMGTOOLERR_SUCCESS;
} }