IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Conversion extension C en PHP


Sujet :

Langage PHP

  1. #1
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Points
    364
    Par défaut Conversion extension C en PHP
    Bonjour,

    Je suis en train de développer un générateur d'extension PHP en Python à partir d'un fichier gabarit XML.

    J’aimerais simplement savoir si je suis sur la bonne voie, la question ne porte pas sur le générateur python mais sur le fichier PHP que je génère.

    Il y en a 4 que je génère :
    - config.m4
    - config.w32
    - module.c
    - php_module.c

    Ma question porte d'abord sur le fichier php_module.c, je voudrais savoir s'il est correctement généré (honnêtement j'ai du mal à comprendre les fonctions de l'API ZEND). Je me suis basé sur différents tutoriels et j’ai aussi regardé les différentes extensions déjà intégré à PHP, voici mon code généré :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
     
    #ifndef FREEAGE_MODULE_MODULE_INCLUDED
    #define FREEAGE_MODULE_MODULE_INCLUDED
     
    #define PHP_MODULE_EXTNAME	"module"
    #define PHP_MODULE_EXTVER	"1.0"
     
    #ifdef HAVE_CONFIG_H
    #include "config.h"
    #endif
     
    #include "php.h"
    #include "net_types.h"
    #include <stdarg.h>
     
    /* Registration module inputs.	*/
    extern zend_module_entry module_module_entry;
    #define phpext_module_ptr &module_module_entry
     
    PHP_MINIT_FUNCTION(module);
    PHP_MINFO_FUNCTION(module);
     
    /* Declaration of functions to create.	*/
    PHP_FUNCTION(test_module);
    PHP_FUNCTION(test_module_result);
    PHP_FUNCTION(test_sql);
    PHP_FUNCTION(test_sql_result);
     
    #ifdef __cplusplus
    extern "C" {
    #endif
     
    ZEND_BEGIN_MODULE_GLOBALS(module)
     
    struct net_struct;
    struct net_packet;
    struct net_func_instance;
    struct net_func_callbacks;
    struct net_client;
     
    /* test_module params declaration.	*/
    struct test_module_params
    {
    	net_int_t arg; /*< An argument.	*/
    };
     
    /* test_module result declaration.	*/
    struct test_module_result
    {
    	net_int_t ret; /*< A result.	*/
    };
     
    /* A test function.	*/
    /*
     * \param client A pointer to the client structure.
     * \param callbacks An optional pointer to the instance callbacks.
     * \param arg An argument.
     * \return A pointer to a function instance, or NULL if an error occured.
     */
    struct net_func_instance *
    test_module ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_int_t arg );
     
    /* Pop a result from an instance of the function test_module.	*/
    /*
     * \see test_module
     * \param instance A pointer to an instance of the function test_module.
     * \return A pointer to a result or NULL if no results are available.
     */
    const struct test_module_result *
    test_module_result ( struct net_func_instance * instance );
     
    /* test_sql params declaration.	*/
    struct test_sql_params
    {
    	net_char_t name [32]; /*< The test argument.	*/
    };
     
    /* test_sql result declaration.	*/
    struct test_sql_result
    {
    	net_int_t intval; /*< An integer.	*/
    	net_float_t floatval; /*< A float.	*/
    	net_char_t strval [32]; /*< A string.	*/
    };
     
    /* A function to test the sql client.	*/
    /*
     * \param client A pointer to the client structure.
     * \param callbacks An optional pointer to the instance callbacks.
     * \param name The test argument.
     * \return A pointer to a function instance, or NULL if an error occured.
     */
    struct net_func_instance *
    test_sql ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_char_t name [32] );
     
    /* Pop a result from an instance of the function test_sql.	*/
    /*
     * \see test_sql
     * \param instance A pointer to an instance of the function test_sql.
     * \return A pointer to a result or NULL if no results are available.
     */
    const struct test_sql_result *
    test_sql_result ( struct net_func_instance * instance );
     
    ZEND_END_MODULE_GLOBALS(module)
     
    #ifdef ZTS
    #define MODULE_G(v) TSRMG(module_globals_id, zend_module_globals*, v)
    #else
    #define MODULE_G(v) (module_globals.v)
    #endif
     
    ZEND_EXTERN_MODULE_GLOBALS(module)
     
    #ifdef __cplusplus
    }
    #endif
     
    #endif /* FREEAGE_MODULE_MODULE_INCLUDED */
    Avant de m'attaquer à la génération du fichier d'implémentation module.c, ce fichier déjà est-il bon ? Y a-t-il des erreurs ?

    Merci d'avance.

  2. #2
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Points
    364
    Par défaut
    Pour aider voici mon header d'origine :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #ifndef FREEAGE_MODULE_MODULE_INCLUDED
    #define FREEAGE_MODULE_MODULE_INCLUDED
     
    #include "net_types.h"
    #include <stdarg.h>
     
    #ifdef __cplusplus
    extern "C" {
    #endif
     
    struct net_struct;
    struct net_packet;
    struct net_func_instance;
    struct net_func_callbacks;
    struct net_client;
     
    /// test_module params declaration.
    struct test_module_params
    {
    	net_int_t arg; ///< An argument.
    };
     
    /// test_module result declaration.
    struct test_module_result
    {
    	net_int_t ret; ///< A result.
    };
     
    /// A test function.
    /**
     * \param client A pointer to the client structure.
     * \param callbacks An optional pointer to the instance callbacks.
     * \param arg An argument.
     * \return A pointer to a function instance, or NULL if an error occured.
     */
    struct net_func_instance *
    test_module ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_int_t arg );
     
    /// Pop a result from an instance of the function test_module.
    /**
     * \see test_module
     * \param instance A pointer to an instance of the function test_module.
     * \return A pointer to a result or NULL if no results are available.
     */
    const struct test_module_result *
    test_module_result ( struct net_func_instance * instance );
     
     
    /// test_sql params declaration.
    struct test_sql_params
    {
    	net_char_t name [32]; ///< The test argument.
    };
     
    /// test_sql result declaration.
    struct test_sql_result
    {
    	net_int_t intval; ///< An integer.
    	net_float_t floatval; ///< A float.
    	net_char_t strval [32]; ///< A string.
    };
     
    /// A function to test the sql client.
    /**
     * \param client A pointer to the client structure.
     * \param callbacks An optional pointer to the instance callbacks.
     * \param name The test argument.
     * \return A pointer to a function instance, or NULL if an error occured.
     */
    struct net_func_instance *
    test_sql ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_char_t name [32] );
     
    /// Pop a result from an instance of the function test_sql.
    /**
     * \see test_sql
     * \param instance A pointer to an instance of the function test_sql.
     * \return A pointer to a result or NULL if no results are available.
     */
    const struct test_sql_result *
    test_sql_result ( struct net_func_instance * instance );
     
     
    #ifdef __cplusplus
    }
    #endif
     
    #endif /* FREEAGE_MODULE_MODULE_INCLUDED */
    et mon .c d'origine :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
     
    #include "module.h"
    #include "net_endian.h"
    #include "net_struct.h"
    #include "net_func.h"
    #include "net_client.h"
    #include <stddef.h>
    #include <string.h>
     
    /// test_module params formatting function.
    /**
     * \param target A pointer to the structure.
     * \param ap     A list of parameters.
     */
    void
    test_module_params_format_fn ( struct test_module_params * target, va_list ap );
     
    /// test_module params byte swapping function.
    /**
     * This function byte swap the structure in-place.
     * \param target A pointer to the structure.
     */
    void
    test_module_params_bswap_fn ( struct test_module_params * target );
     
    /// test_module params format definition.
    static const struct net_struct test_module_params_format = {
    	1, sizeof(struct test_module_params),
    	NULL, /* not used by client */
    	(net_struct_format_fn_t)test_module_params_format_fn,
    	(net_struct_bswap_fn_t)test_module_params_bswap_fn,
    	{
    		{ NET_TYPE_INT, 1, offsetof(struct test_module_params, arg) }
    	}
    };
     
    void
    test_module_params_format_fn ( struct test_module_params * target, va_list ap )
    {
    	{
    		target->arg = va_arg(ap, net_int_t);
    	}
    }
     
    void
    test_module_params_bswap_fn ( struct test_module_params * target )
    {
    	net_bswap_int(&target->arg);
    }
     
    /// test_module result byte swapping function.
    /**
     * This function byte swap the structure in-place.
     * \param target A pointer to the structure.
     */
    void
    test_module_result_bswap_fn ( struct test_module_result * target );
     
    /// test_module result format definition.
    static const struct net_struct test_module_result_format = {
    	1, sizeof(struct test_module_result),
    	NULL, /* not used by client */
    	NULL, /* not used for results */
    	(net_struct_bswap_fn_t)test_module_result_bswap_fn,
    	{
    		{ NET_TYPE_INT, 1, offsetof(struct test_module_result, ret) }
    	}
    };
     
    void
    test_module_result_bswap_fn ( struct test_module_result * target )
    {
    	net_bswap_int(&target->ret);
    }
     
    /// test_module function definition.
    static const struct net_func test_module_func_def = {
    	1,
    	0,
    	&test_module_params_format,
    	&test_module_result_format
    };
     
    struct net_func_instance *
    test_module ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_int_t arg )
    {
    	return net_client_call_function(client, &test_module_func_def, callbacks, arg);
    }
     
    const struct test_module_result *
    test_module_result ( struct net_func_instance * instance )
    {
    	return (const struct test_module_result *)net_func_pop_result(instance);
    }
     
     
    /// test_sql params formatting function.
    /**
     * \param target A pointer to the structure.
     * \param ap     A list of parameters.
     */
    void
    test_sql_params_format_fn ( struct test_sql_params * target, va_list ap );
     
    /// test_sql params byte swapping function.
    /**
     * This function byte swap the structure in-place.
     * \param target A pointer to the structure.
     */
    void
    test_sql_params_bswap_fn ( struct test_sql_params * target );
     
    /// test_sql params format definition.
    static const struct net_struct test_sql_params_format = {
    	1, sizeof(struct test_sql_params),
    	NULL, /* not used by client */
    	(net_struct_format_fn_t)test_sql_params_format_fn,
    	(net_struct_bswap_fn_t)test_sql_params_bswap_fn,
    	{
    		{ NET_TYPE_CHAR, 32, offsetof(struct test_sql_params, name) }
    	}
    };
     
    void
    test_sql_params_format_fn ( struct test_sql_params * target, va_list ap )
    {
    	{
    		const char * source = va_arg(ap, const char *);
    		strncpy(target->name, source, 32);
    	}
    }
     
    void
    test_sql_params_bswap_fn ( struct test_sql_params * target )
    {
    }
     
    /// test_sql result byte swapping function.
    /**
     * This function byte swap the structure in-place.
     * \param target A pointer to the structure.
     */
    void
    test_sql_result_bswap_fn ( struct test_sql_result * target );
     
    /// test_sql result format definition.
    static const struct net_struct test_sql_result_format = {
    	3, sizeof(struct test_sql_result),
    	NULL, /* not used by client */
    	NULL, /* not used for results */
    	(net_struct_bswap_fn_t)test_sql_result_bswap_fn,
    	{
    		{ NET_TYPE_INT, 1, offsetof(struct test_sql_result, intval) },
    		{ NET_TYPE_FLOAT, 1, offsetof(struct test_sql_result, floatval) },
    		{ NET_TYPE_CHAR, 32, offsetof(struct test_sql_result, strval) }
    	}
    };
     
    void
    test_sql_result_bswap_fn ( struct test_sql_result * target )
    {
    	net_bswap_int(&target->intval);
    	net_bswap_float(&target->floatval);
    }
     
    /// test_sql function definition.
    static const struct net_func test_sql_func_def = {
    	1,
    	1,
    	&test_sql_params_format,
    	&test_sql_result_format
    };
     
    struct net_func_instance *
    test_sql ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_char_t name [32] )
    {
    	return net_client_call_function(client, &test_sql_func_def, callbacks, name);
    }
     
    const struct test_sql_result *
    test_sql_result ( struct net_func_instance * instance )
    {
    	return (const struct test_sql_result *)net_func_pop_result(instance);
    }

  3. #3
    Membre chevronné Avatar de nosferapti
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    1 157
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 1 157
    Points : 1 895
    Points
    1 895
    Par défaut
    Regarde du coté de cet outil qui pourra peut-être t'aider à faire une extension :
    http://www.swig.org/

  4. #4
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Points
    364
    Par défaut
    Ça ne me semble pas utile pour mon problème mais merci quand même.

  5. #5
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Points
    364
    Par défaut
    Je vais simplifier ma question.

    Comment transformer cette fonction pour l'intégrer dans une extension php ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    struct net_func_instance *
    test_module ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_int_t arg )
    {
    	return net_client_call_function(client, &test_module_func_def, callbacks, arg);
    }
    Comme ceci?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    ZEND_BEGIN_ARG_INFO_EX(arginfo_test_module) // Que mettre ici ? 
    // et ici ? 
    ZEND_END_ARG_INFO()
     
    const zend_function_entry module_functions[] = 
    {
    	PHP_FE(test_module, arginfo_test_module)  // ca c'est bon 
    	{NULL, NULL, NULL}
    };
     
     
    PHP_FUNCTION(test_module)
    {
    	return net_client_call_function(client, &test_module_func_def, callbacks, arg);
    }

  6. #6
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Points
    364
    Par défaut
    J'ai cru comprendre qu'il fallait utiliser zend_parse_parameters pour pouvoir passer les paramètres au moteur, j'ai donc modifié ma fonction comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    ZEND_BEGIN_ARG_INFO_EX(arginfo_test_module) // Que mettre ici ? 
    // et ici ? 
    ZEND_END_ARG_INFO()
     
    const zend_function_entry module_functions[] = 
    {
    	PHP_FE(test_module, arginfo_test_module)  // ca c'est bon je pense
    	{NULL, NULL, NULL}
    };
     
     
    PHP_FUNCTION(test_module)
    {
    struct net_client * client;
    	const struct net_func_callbacks * callbacks;
    	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", // que mettre ici ? ) == FAILURE)
    	{
    		RETURN_FALSE();
    	}
    	return net_client_call_function(client, &test_module_func_def, callbacks, arg);
    }
    Mais les structures sont-elles possibles en PHP? Et que mettre à la suite de zend_parse_parameters ?

    Merci d'avance car la je galère

  7. #7
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Points
    364
    Par défaut
    Voici aujourd'hui ma fonction :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    /* struct net_func_instance *
    test_module ( struct net_client * client, const struct net_func_callbacks * callbacks, const net_int_t arg ) */
    PHP_FUNCTION(test_module)
    {
    	struct net_client * client;
    	const struct net_func_callbacks * callbacks;
    	const net_int_t arg;
    	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrl", &client, &callbacks, &arg) == FAILURE)
    	{
    		RETURN_FALSE();
    	}
    	return net_client_call_function(client, &test_module_func_def, callbacks, arg);
    }

Discussions similaires

  1. [PHP-JS] Conversion fonction de temps PHP vers javascript
    Par Flynt dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 25/04/2008, 17h46
  2. Librairie mysql.so, extension MySQL pour PHP manquante
    Par nyko_kliko dans le forum Mandriva / Mageia
    Réponses: 6
    Dernier message: 07/09/2007, 11h00
  3. Installation extension HTTP pour PHP
    Par toxyko dans le forum Apache
    Réponses: 3
    Dernier message: 07/12/2006, 09h04
  4. Réponses: 1
    Dernier message: 15/05/2006, 22h22
  5. Conversion d un fichier php en ini
    Par gilles641 dans le forum Langage
    Réponses: 1
    Dernier message: 09/08/2005, 16h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo