summaryrefslogtreecommitdiffstats
path: root/src/extension/fringtools.cpp
blob: 88ec037507e335dafb7bbc38b0c64cd570a521c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <Python.h>

#include <dirent.h>
#include <iostream>
#include <string>
#include <vector>

/*

==============================================================================
This is the original function in python

def _build_tree(self,path):
    l = []
    for fn in os.listdir(path):
        try: p = os.path.join(path, fn)
        except: continue
        s = os.lstat(p)
        if stat.S_ISDIR(s.st_mode):
            l.append( (fn, self._build_tree(p), s.st_size) )
        elif stat.S_ISREG(s.st_mode):
            l.append( (fn, None, s.st_size) )
    return sum_list(os.path.split(path)[-1], l, 0)

==============================================================================

Instead of using SumList, the new build_tree will use a tuple

*/

/// recursive function to build a list system with directory information
PyObject* build_tree( std::string path, const bool& showHidden) {
    PyObject* l = PyList_New(0);
    DIR* d = opendir(path.c_str());
    if(d == 0) Py_RETURN_NONE;

    unsigned long total = 0;
    struct dirent *e;
	while((e = readdir(d)) != NULL) {

        std::string fn = e->d_name;

        if(!showHidden) if(e->d_name[0] == '.') continue;
        if(fn == "." || fn == "..") continue;
		fn = path+"/" +fn;

        struct stat s;
		lstat(fn.c_str(), &s);

		if(S_ISDIR(s.st_mode)) {
            PyObject* sub = build_tree(fn,showHidden);
            PyTuple_SetItem(sub,0,PyString_FromString(e->d_name));
            PyList_Append(l, sub );
            total += PyLong_AsUnsignedLong( PyTuple_GET_ITEM(sub,2) );
		} else if (S_ISREG(s.st_mode)) {
            PyList_Append(l, Py_BuildValue("(sOk)",e->d_name,Py_None,s.st_size) );
            total += s.st_size;
		}

	}
	closedir(d);
    return Py_BuildValue("(sOk)", path.c_str(), l, total );
}

/// initial function call.
static PyObject* fringtools_build_tree(PyObject *self, PyObject *args) {
    const char *path;
    int show_hidden = 1;
    if (!PyArg_ParseTuple(args, "si", &path, &show_hidden)) return NULL;
    return build_tree(std::string(path),(show_hidden==1));
}

//==============================================================================
// module init
//==============================================================================

static PyMethodDef FringtoolsMethods[] = {
    {"build_tree",  fringtools_build_tree, METH_VARARGS, "Walk Tree."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC initfringtools(void) {
    (void) Py_InitModule("fringtools", FringtoolsMethods);
}

int main(int argc, char *argv[]) {
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    initfringtools();
    return 0;
}