From b9465ab7f636471ab4905be72ded80dbd4d8c267 Mon Sep 17 00:00:00 2001 From: "elias.bachaalany" Date: Wed, 21 Oct 2009 11:51:31 +0000 Subject: [PATCH] diskio.i: wrapped enumerate_files and enumerate_system_files --- swig/diskio.i | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/swig/diskio.i b/swig/diskio.i index 913b5a2..4646838 100644 --- a/swig/diskio.i +++ b/swig/diskio.i @@ -1,5 +1,6 @@ // TODO: These could be wrapped %ignore enumerate_files; +%rename (enumerate_files) py_enumerate_files; %ignore enumerate_system_files; %ignore ioport_bit_t; %ignore ioport_bits_t; @@ -41,6 +42,20 @@ %include "diskio.hpp" +%{ +// +int idaapi py_enumerate_files_cb(const char *file, void *ud) +{ + PyObject *py_file = PyString_FromString(file); + PyObject *py_ret = PyObject_CallFunctionObjArgs((PyObject *)ud, py_file, NULL); + int r = (py_ret == 0 || !PyNumber_Check(py_ret)) ? 1 /* stop enumeration on failure */ : PyInt_AsLong(py_ret); + Py_XDECREF(py_file); + Py_XDECREF(py_ret); + return r; +} +// +%} + %inline %{ // class loader_input_t @@ -280,5 +295,31 @@ public: return Py_BuildValue("c", ch); } }; + + +PyObject *py_enumerate_files(PyObject *path, PyObject *fname, PyObject *callback) +{ + do + { + if (!PyString_Check(path) || !PyString_Check(fname) || !PyCallable_Check(callback)) + break; + const char *_path = PyString_AsString(path); + const char *_fname = PyString_AsString(fname); + if (_path == NULL || _fname == NULL) + break; + char answer[MAXSTR]; + answer[0] = '\0'; + int r = enumerate_files(answer, sizeof(answer), _path, _fname, py_enumerate_files_cb, callback); + return Py_BuildValue("(is)", r, answer); + } while (false); + Py_RETURN_NONE; +} // %} + +%pythoncode %{ +# +def enumerate_system_files(subdir, fname, callback): + return enumerate_files(idadir(subdir), fname, callback) +# +%}