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

View File

@ -81,6 +81,7 @@ struct imgtool_partition_features
void imgtool_init(bool omit_untested, void (*warning)(const char *message));
void imgtool_exit(void);
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);
void imgtool_warn(const char *format, ...) ATTR_PRINTF(1,2);
char *imgtool_basename(char *filename);

View File

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

View File

@ -18,6 +18,7 @@
#define LIBRARY_H
#include <time.h>
#include <list>
#include "corestr.h"
#include "opresolv.h"
@ -330,9 +331,6 @@ char *imgtool_temp_str(void);
struct imgtool_module
{
imgtool_module *previous;
imgtool_module *next;
imgtool_class imgclass;
const char *name;
@ -379,6 +377,8 @@ namespace imgtool {
class library
{
public:
typedef std::list<std::unique_ptr<imgtool_module> > modulelist;
enum class sort_type
{
NAME,
@ -392,7 +392,7 @@ public:
void add(imgtool_get_info get_info);
// 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
void sort(sort_type sort);
@ -401,13 +401,14 @@ public:
const imgtool_module *findmodule(const char *module_name);
// module iteration
imgtool_module *iterate(const imgtool_module *module);
imgtool_module *index(int i);
const modulelist &modules() { return m_modules; }
private:
object_pool * m_pool;
imgtool_module * m_first;
imgtool_module * m_last;
object_pool * m_pool;
modulelist m_modules;
// internal lookup and iteration
modulelist::iterator find(const char *module_name);
// helpers
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[])
{
const imgtool_module *mod;
fprintf(stdout, "Image formats supported by imgtool:\n\n");
mod = imgtool_find_module(nullptr);
while(mod)
for (const auto &module : imgtool_get_modules())
{
fprintf(stdout, " %-25s%s\n", mod->name, mod->description);
mod = mod->next;
fprintf(stdout, " %-25s%s\n", module->name, module->description);
}
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)
{
size_t i;
imgtool_module *module;
/* list of modules that we drop */
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 (omit_untested)
{
module = nullptr;
while((module = library->iterate(module)) != nullptr)
for (auto &module : library->modules())
{
if (module->writing_untested)
{