/* Chrysalide - Outil d'analyse de fichiers binaires * pathname.c - équivalent Python du fichier "common/pathname.c" * * Copyright (C) 2015-2017 Cyrille Bagard * * This file is part of Chrysalide. * * Chrysalide is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Chrysalide is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "pathname.h" #include #include #include #include #include "../access.h" /* Calcule le chemin relatif entre deux fichiers donnés. */ static PyObject *py_build_relative_filename(PyObject *, PyObject *); /* Calcule le chemin absolu d'un fichier par rapport à un autre. */ static PyObject *py_build_absolute_filename(PyObject *, PyObject *); /****************************************************************************** * * * Paramètres : self = NULL car méthode statique. * * args = arguments fournis lors de l'appel à la fonction. * * * * Description : Calcule le chemin relatif entre deux fichiers donnés. * * * * Retour : Chemin relatif obtenu. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_build_relative_filename(PyObject *self, PyObject *args) { PyObject *result; /* Instance à retourner */ const char *ref; /* Fichier de référence */ const char *target; /* Fichier à cibler */ int ret; /* Bilan de lecture des args. */ char *relative; /* Chemin d'accès construit */ ret = PyArg_ParseTuple(args, "ss", &ref, &target); if (!ret) Py_RETURN_NONE; relative = build_relative_filename(ref, target); result = PyUnicode_FromString(relative); free(relative); return result; } /****************************************************************************** * * * Paramètres : self = NULL car méthode statique. * * args = arguments fournis lors de l'appel à la fonction. * * * * Description : Calcule le chemin absolu d'un fichier par rapport à un autre.* * * * Retour : Chemin absolu obtenu. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_build_absolute_filename(PyObject *self, PyObject *args) { PyObject *result; /* Instance à retourner */ const char *ref; /* Fichier de référence */ const char *target; /* Fichier à cibler */ int ret; /* Bilan de lecture des args. */ char *relative; /* Chemin d'accès construit */ ret = PyArg_ParseTuple(args, "ss", &ref, &target); if (!ret) Py_RETURN_NONE; relative = build_absolute_filename(ref, target); if (relative == NULL) { PyErr_SetString(PyExc_ValueError, _("Relative path is too deep.")); result = NULL; } else { result = PyUnicode_FromString(relative); free(relative); } return result; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_pathname_type(void) { static PyMethodDef py_pathname_methods[] = { { "build_relative_filename", py_build_relative_filename, METH_VARARGS | METH_STATIC, "build_relative_filename(ref, target, /)\n--\n\nCompute the relative path between two files." }, { "build_absolute_filename", py_build_absolute_filename, METH_VARARGS | METH_STATIC, "build_absolute_filename(ref, target, /)\n--\n\nCompute the absolute path for a file." }, { NULL } }; static PyTypeObject py_pathname_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.core.pathname", .tp_basicsize = sizeof(PyObject), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT, .tp_doc = "Path manipulations in Python for Chrysalide.", .tp_methods = py_pathname_methods }; return &py_pathname_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.core.pathname'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_pathname_is_registered(void) { PyTypeObject *type; /* Type Python pour 'pathname' */ PyObject *module; /* Module à recompléter */ int ret; /* Bilan d'un appel */ type = get_python_pathname_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { //type->tp_new = PyType_GenericNew; if (PyType_Ready(type) != 0) return false; module = get_access_to_python_module("pychrysalide.common"); Py_INCREF(type); ret = PyModule_AddObject(module, "pathname", (PyObject *)type); if (ret != 0) return false; } return true; }