From 7e8707f496cd7a4bbf6b01d131f40eab836614d3 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Sat, 29 Jul 2017 09:45:16 -0400 Subject: [PATCH] [Imgtool] C++-ification of imgtool_forkent structure --- src/tools/imgtool/imgtool.cpp | 8 ++-- src/tools/imgtool/imgtool.h | 4 +- src/tools/imgtool/library.h | 65 ++++++++++++++++++++++------ src/tools/imgtool/modules/mac.cpp | 18 +++----- src/tools/imgtool/modules/macbin.cpp | 40 ++++++++++------- src/tools/imgtool/modules/prodos.cpp | 18 +++----- 6 files changed, 93 insertions(+), 60 deletions(-) diff --git a/src/tools/imgtool/imgtool.cpp b/src/tools/imgtool/imgtool.cpp index b1b32e6c632..335a824ad41 100644 --- a/src/tools/imgtool/imgtool.cpp +++ b/src/tools/imgtool/imgtool.cpp @@ -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_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_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 &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_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); @@ -1873,7 +1873,7 @@ imgtoolerr_t imgtool::partition::delete_file(const char *fname) // 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 &forks) { imgtoolerr_t err; if (!m_list_forks) @@ -1885,7 +1885,9 @@ imgtoolerr_t imgtool::partition::list_file_forks(const char *path, imgtool_forke if (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) return err; diff --git a/src/tools/imgtool/imgtool.h b/src/tools/imgtool/imgtool.h index 1e973b30c70..1a63ccd2399 100644 --- a/src/tools/imgtool/imgtool.h +++ b/src/tools/imgtool/imgtool.h @@ -158,7 +158,7 @@ namespace imgtool 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 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 &forks); imgtoolerr_t create_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); @@ -209,7 +209,7 @@ namespace imgtool std::function m_read_file; std::function m_write_file; std::function m_delete_file; - std::function m_list_forks; + std::function &forks)> m_list_forks; std::function m_create_dir; std::function m_delete_dir; std::function m_list_attrs; diff --git a/src/tools/imgtool/library.h b/src/tools/imgtool/library.h index c64f02a33d5..160d6952a83 100644 --- a/src/tools/imgtool/library.h +++ b/src/tools/imgtool/library.h @@ -81,20 +81,59 @@ struct imgtool_chainent uint64_t block; }; -enum imgtool_forktype_t +namespace imgtool { - FORK_END, - FORK_DATA, - FORK_RESOURCE, - FORK_ALTERNATE -}; + class fork_entry + { + public: + enum class type_t + { + DATA, + RESOURCE, + ALT + }; -struct imgtool_forkent -{ - imgtool_forktype_t type; - uint64_t size; - char forkname[64]; -}; + fork_entry(uint64_t size, type_t type = type_t::DATA) + : m_size(size) + , m_type(type) + , m_name(default_name(type)) + { + + } + + 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 { @@ -290,7 +329,7 @@ union imgtoolinfo 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 (*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 &forks); imgtoolerr_t (*create_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); diff --git a/src/tools/imgtool/modules/mac.cpp b/src/tools/imgtool/modules/mac.cpp index f94246c5e7f..58af4872257 100644 --- a/src/tools/imgtool/modules/mac.cpp +++ b/src/tools/imgtool/modules/mac.cpp @@ -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 &forks) { imgtoolerr_t err; uint32_t parID; mac_str255 filename; mac_dirent cat_info; - int fork_num = 0; imgtool::image &img(partition.image()); 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) return IMGTOOLERR_FILENOTFOUND; - /* specify data fork */ - ents[fork_num].type = FORK_DATA; - ents[fork_num].forkname[0] = '\0'; - ents[fork_num].size = cat_info.dataLogicalSize; - fork_num++; + // specify data fork + forks.emplace_back(cat_info.dataLogicalSize, imgtool::fork_entry::type_t::DATA); if (cat_info.rsrcLogicalSize > 0) { - /* specify the resource fork */ - ents[fork_num].type = FORK_RESOURCE; - strcpy(ents[fork_num].forkname, "RESOURCE_FORK"); - ents[fork_num].size = cat_info.rsrcLogicalSize; - fork_num++; + // specify the resource fork + forks.emplace_back(cat_info.rsrcLogicalSize, imgtool::fork_entry::type_t::RESOURCE); } - ents[fork_num].type = FORK_END; return IMGTOOLERR_SUCCESS; } diff --git a/src/tools/imgtool/modules/macbin.cpp b/src/tools/imgtool/modules/macbin.cpp index 604b16f6196..c937ff22163 100644 --- a/src/tools/imgtool/modules/macbin.cpp +++ b/src/tools/imgtool/modules/macbin.cpp @@ -2,7 +2,7 @@ // copyright-holders:Raphael Nabet /**************************************************************************** - macbin.c + macbin.cpp 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; uint8_t header[128]; const char *basename; - int i; uint32_t type_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 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 lastmodified_time = 0; imgtool_attribute attr_values[10]; - /* get the forks */ - err = partition.list_file_forks(filename, fork_entries, sizeof(fork_entries)); + // get the forks + std::vector fork_entries; + err = partition.list_file_forks(filename, fork_entries); if (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) - data_fork = &fork_entries[i]; - else if (fork_entries[i].type == FORK_RESOURCE) - resource_fork = &fork_entries[i]; + switch (entry.type()) + { + case imgtool::fork_entry::type_t::DATA: + data_fork = &entry; + break; + case imgtool::fork_entry::type_t::RESOURCE: + resource_fork = &entry; + break; + default: + // do nothing + break; + } } /* 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, 77, 2, coord_y); place_integer_be(header, 79, 2, finder_folder); - 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, 83, 4, data_fork ? data_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, 95, 4, lastmodified_time); 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) return err; - destf.fill(0, pad128(data_fork->size)); + destf.fill(0, pad128(data_fork->size())); } if (resource_fork) @@ -173,7 +181,7 @@ static imgtoolerr_t macbinary_readfile(imgtool::partition &partition, const char if (err) return err; - destf.fill(0, pad128(resource_fork->size)); + destf.fill(0, pad128(resource_fork->size())); } return IMGTOOLERR_SUCCESS; diff --git a/src/tools/imgtool/modules/prodos.cpp b/src/tools/imgtool/modules/prodos.cpp index c9914f8e511..4c01612283f 100644 --- a/src/tools/imgtool/modules/prodos.cpp +++ b/src/tools/imgtool/modules/prodos.cpp @@ -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 &forks) { imgtoolerr_t err; imgtool::image &image(partition.image()); prodos_dirent ent; prodos_direnum direnum; - int fork_num = 0; err = prodos_lookup_path(image, path, CREATE_NONE, &direnum, &ent); if (err) @@ -1828,22 +1827,15 @@ static imgtoolerr_t prodos_diskimage_listforks(imgtool::partition &partition, co if (is_dir_storagetype(ent.storage_type)) return IMGTOOLERR_FILENOTFOUND; - /* specify data fork */ - ents[fork_num].type = FORK_DATA; - ents[fork_num].forkname[0] = '\0'; - ents[fork_num].size = ent.filesize[0]; - fork_num++; + // specify data fork + forks.emplace_back(ent.filesize[0], imgtool::fork_entry::type_t::DATA); if (is_extendedfile_storagetype(ent.storage_type)) { - /* specify the resource fork */ - ents[fork_num].type = FORK_RESOURCE; - strcpy(ents[fork_num].forkname, "RESOURCE_FORK"); - ents[fork_num].size = ent.filesize[1]; - fork_num++; + // specify the resource fork + forks.emplace_back(ent.filesize[1], imgtool::fork_entry::type_t::RESOURCE); } - ents[fork_num].type = FORK_END; return IMGTOOLERR_SUCCESS; }