Imgtool: Changed the Imgtool module list to be std::list

This commit is contained in:
Nathan Woods 2016-09-05 11:30:21 -04:00
parent 3534b0090a
commit 9a3ac8f6b8
6 changed files with 53 additions and 114 deletions

View File

@ -255,6 +255,15 @@ const imgtool_module *imgtool_find_module(const char *modulename)
} }
/*-------------------------------------------------
imgtool_find_module - looks up a module
-------------------------------------------------*/
const imgtool::library::modulelist &imgtool_get_modules()
{
return global_imgtool_library->modules();
}
/*------------------------------------------------- /*-------------------------------------------------
imgtool_get_module_features - retrieves a imgtool_get_module_features - retrieves a
@ -385,7 +394,6 @@ imgtoolerr_t imgtool_identify_file(const char *fname, imgtool_module **modules,
{ {
imgtoolerr_t err = IMGTOOLERR_SUCCESS; imgtoolerr_t err = IMGTOOLERR_SUCCESS;
imgtool::library *library = global_imgtool_library.get(); imgtool::library *library = global_imgtool_library.get();
imgtool_module *module = nullptr;
imgtool_module *insert_module; imgtool_module *insert_module;
imgtool_module *temp_module; imgtool_module *temp_module;
size_t i = 0; size_t i = 0;
@ -418,15 +426,15 @@ imgtoolerr_t imgtool_identify_file(const char *fname, imgtool_module **modules,
extension++; extension++;
/* iterate through all modules */ /* iterate through all modules */
while((module = library->iterate(module)) != nullptr) for (const auto &module : library->modules())
{ {
if (!extension || image_find_extension(module->extensions, extension)) if (!extension || image_find_extension(module->extensions, extension))
{ {
err = evaluate_module(fname, module, &val); err = evaluate_module(fname, module.get(), &val);
if (err) if (err)
goto done; goto done;
insert_module = module; insert_module = module.get();
for (i = 0; (val > 0.0f) && (i < count); i++) for (i = 0; (val > 0.0f) && (i < count); i++)
{ {
if (val > values[i]) if (val > values[i])
@ -927,7 +935,6 @@ int imgtool_validitychecks(void)
{ {
int error = 0; int error = 0;
imgtoolerr_t err = (imgtoolerr_t)IMGTOOLERR_SUCCESS; imgtoolerr_t err = (imgtoolerr_t)IMGTOOLERR_SUCCESS;
const imgtool_module *module = nullptr;
imgtool_module_features features; imgtool_module_features features;
int created_library = FALSE; int created_library = FALSE;
@ -937,9 +944,9 @@ int imgtool_validitychecks(void)
created_library = TRUE; created_library = TRUE;
} }
while((module = global_imgtool_library->iterate(module)) != nullptr) for (const auto &module : global_imgtool_library->modules())
{ {
features = imgtool_get_module_features(module); features = imgtool_get_module_features(module.get());
if (!module->name) if (!module->name)
{ {

View File

@ -81,6 +81,7 @@ struct imgtool_partition_features
void imgtool_init(bool omit_untested, void (*warning)(const char *message)); void imgtool_init(bool omit_untested, void (*warning)(const char *message));
void imgtool_exit(void); void imgtool_exit(void);
const imgtool_module *imgtool_find_module(const char *modulename); const imgtool_module *imgtool_find_module(const char *modulename);
const imgtool::library::modulelist &imgtool_get_modules();
imgtool_module_features imgtool_get_module_features(const imgtool_module *module); imgtool_module_features imgtool_get_module_features(const imgtool_module *module);
void imgtool_warn(const char *format, ...) ATTR_PRINTF(1,2); void imgtool_warn(const char *format, ...) ATTR_PRINTF(1,2);
char *imgtool_basename(char *filename); char *imgtool_basename(char *filename);

View File

@ -10,6 +10,7 @@
****************************************************************************/ ****************************************************************************/
#include <string.h> #include <string.h>
#include <algorithm>
#include "imgtool.h" #include "imgtool.h"
#include "library.h" #include "library.h"
@ -27,8 +28,6 @@ library::library()
m_pool = pool_alloc_lib(nullptr); m_pool = pool_alloc_lib(nullptr);
if (!m_pool) if (!m_pool)
throw std::bad_alloc(); throw std::bad_alloc();
m_first = nullptr;
m_last = nullptr;
} }
@ -48,18 +47,12 @@ library::~library()
void library::add_class(const imgtool_class *imgclass) void library::add_class(const imgtool_class *imgclass)
{ {
imgtool_module *module;
char *s1, *s2; char *s1, *s2;
// allocate the module and place it in the chain // allocate the module and place it in the chain
module = (imgtool_module *)imgtool_library_malloc(sizeof(*module)); m_modules.emplace_back(std::unique_ptr<imgtool_module>(new imgtool_module));
imgtool_module *module = m_modules.back().get();
memset(module, 0, sizeof(*module)); memset(module, 0, sizeof(*module));
module->previous = m_last;
if (m_last)
m_last->next = module;
else
m_first = module;
m_last = module;
// extensions have a weird format // extensions have a weird format
s1 = imgtool_get_info_string(imgclass, IMGTOOLINFO_STR_FILE_EXTENSIONS); s1 = imgtool_get_info_string(imgclass, IMGTOOLINFO_STR_FILE_EXTENSIONS);
@ -139,26 +132,11 @@ void library::add(imgtool_get_info get_info)
// unlink // unlink
//------------------------------------------------- //-------------------------------------------------
const imgtool_module *library::unlink(const char *module) void library::unlink(const char *module_name)
{ {
imgtool_module *m; modulelist::iterator iter = find(module_name);
imgtool_module **previous; if (iter != m_modules.end())
imgtool_module **next; m_modules.erase(iter);
for (m = m_first; m; m = m->next)
{
if (!core_stricmp(m->name, module))
{
previous = m->previous ? &m->previous->next : &m_first;
next = m->next ? &m->next->previous : &m_last;
*previous = m->next;
*next = m->previous;
m->previous = nullptr;
m->next = nullptr;
return m;
}
}
return nullptr;
} }
@ -188,40 +166,24 @@ int library::module_compare(const imgtool_module *m1, const imgtool_module *m2,
void library::sort(sort_type sort) void library::sort(sort_type sort)
{ {
imgtool_module *m1; auto compare = [&](const std::unique_ptr<imgtool_module> &a, const std::unique_ptr<imgtool_module> &b)
imgtool_module *m2;
imgtool_module *target;
imgtool_module **before;
imgtool_module **after;
for (m1 = m_first; m1; m1 = m1->next)
{ {
target = m1; return module_compare(a.get(), b.get(), sort) < 0;
for (m2 = m1->next; m2; m2 = m2->next) };
{ m_modules.sort(compare);
while(module_compare(target, m2, sort) > 0) }
target = m2;
}
if (target != m1)
{
// unlink the target
before = target->previous ? &target->previous->next : &m_first;
after = target->next ? &target->next->previous : &m_last;
*before = target->next;
*after = target->previous;
// now place the target before m1 //-------------------------------------------------
target->previous = m1->previous; // find
target->next = m1; //-------------------------------------------------
before = m1->previous ? &m1->previous->next : &m_first;
*before = target;
m1->previous = target;
// since we changed the order, we have to replace ourselves library::modulelist::iterator library::find(const char *module_name)
m1 = target; {
} return std::find_if(
} m_modules.begin(),
m_modules.end(),
[&](std::unique_ptr<imgtool_module> &module) { return !strcmp(module->name, module_name); });
} }
@ -231,36 +193,10 @@ void library::sort(sort_type sort)
const imgtool_module *library::findmodule(const char *module_name) const imgtool_module *library::findmodule(const char *module_name)
{ {
const imgtool_module *module; modulelist::iterator iter = find(module_name);
return iter != m_modules.end()
module = m_first; ? iter->get()
while(module && module_name && strcmp(module->name, module_name)) : nullptr;
module = module->next;
return module;
}
//-------------------------------------------------
// iterate
//-------------------------------------------------
imgtool_module *library::iterate(const imgtool_module *module)
{
return module ? module->next : m_first;
}
//-------------------------------------------------
// index
//-------------------------------------------------
imgtool_module *library::index(int i)
{
imgtool_module *module;
module = m_first;
while(module && i--)
module = module->next;
return module;
} }

View File

@ -18,6 +18,7 @@
#define LIBRARY_H #define LIBRARY_H
#include <time.h> #include <time.h>
#include <list>
#include "corestr.h" #include "corestr.h"
#include "opresolv.h" #include "opresolv.h"
@ -330,9 +331,6 @@ char *imgtool_temp_str(void);
struct imgtool_module struct imgtool_module
{ {
imgtool_module *previous;
imgtool_module *next;
imgtool_class imgclass; imgtool_class imgclass;
const char *name; const char *name;
@ -379,6 +377,8 @@ namespace imgtool {
class library class library
{ {
public: public:
typedef std::list<std::unique_ptr<imgtool_module> > modulelist;
enum class sort_type enum class sort_type
{ {
NAME, NAME,
@ -392,7 +392,7 @@ public:
void add(imgtool_get_info get_info); void add(imgtool_get_info get_info);
// seeks out and removes a module from an imgtool library // seeks out and removes a module from an imgtool library
const imgtool_module *unlink(const char *module); void unlink(const char *module_name);
// sorts an imgtool library // sorts an imgtool library
void sort(sort_type sort); void sort(sort_type sort);
@ -401,13 +401,14 @@ public:
const imgtool_module *findmodule(const char *module_name); const imgtool_module *findmodule(const char *module_name);
// module iteration // module iteration
imgtool_module *iterate(const imgtool_module *module); const modulelist &modules() { return m_modules; }
imgtool_module *index(int i);
private: private:
object_pool * m_pool; object_pool * m_pool;
imgtool_module * m_first; modulelist m_modules;
imgtool_module * m_last;
// internal lookup and iteration
modulelist::iterator find(const char *module_name);
// helpers // helpers
void add_class(const imgtool_class *imgclass); void add_class(const imgtool_class *imgclass);

View File

@ -707,15 +707,11 @@ done:
static int cmd_listformats(const struct command *c, int argc, char *argv[]) static int cmd_listformats(const struct command *c, int argc, char *argv[])
{ {
const imgtool_module *mod;
fprintf(stdout, "Image formats supported by imgtool:\n\n"); fprintf(stdout, "Image formats supported by imgtool:\n\n");
mod = imgtool_find_module(nullptr); for (const auto &module : imgtool_get_modules())
while(mod)
{ {
fprintf(stdout, " %-25s%s\n", mod->name, mod->description); fprintf(stdout, " %-25s%s\n", module->name, module->description);
mod = mod->next;
} }
return 0; return 0;

View File

@ -30,7 +30,6 @@ static void (*const modules[])(const imgtool_class *imgclass, UINT32 state, unio
imgtoolerr_t imgtool_create_cannonical_library(bool omit_untested, std::unique_ptr<imgtool::library> &library) imgtoolerr_t imgtool_create_cannonical_library(bool omit_untested, std::unique_ptr<imgtool::library> &library)
{ {
size_t i; size_t i;
imgtool_module *module;
/* list of modules that we drop */ /* list of modules that we drop */
static const char *const irrelevant_modules[] = static const char *const irrelevant_modules[] =
@ -55,8 +54,7 @@ imgtoolerr_t imgtool_create_cannonical_library(bool omit_untested, std::unique_p
// if we are omitting untested, go through and block out the functionality in question // if we are omitting untested, go through and block out the functionality in question
if (omit_untested) if (omit_untested)
{ {
module = nullptr; for (auto &module : library->modules())
while((module = library->iterate(module)) != nullptr)
{ {
if (module->writing_untested) if (module->writing_untested)
{ {