Help w/extension for a single file ANSI C source?

RJ nowhere at blue.com
Tue Apr 23 18:08:32 EDT 2002


Can anyone get me started on a Pyhton extension?
I'd like to be able to access the internal functions of an ANSI C program
which consists of just prog.h and prog.c (as below).
It compiles and runs w/Borland on MS and gcc on Linux, but I'm going around
in circles trying to make my first Python extension.
Otherwise I might next try making a dll with VS6 and using calldll.py, which
I at least have experience with.
Is SWIG the 'best' method for extensions? This doesn't need libraries and
does not have an __init function, just straight C.


/* *************************************** */
/*  prog.h - function prototypes  */
/* *************************************** */
#define HYPERDIAG           (int)'A'
#define RECTANGULAR         (int)'R'
#define LINEAR          (int)'L'
#define SPLINE          (int)'S'
#define CUSTOM          (int)'C'
int allocate_prog(int num_state,int *qnt_state,
           int num_resp, int num_cell,
           int memory, int rfield_shape, int collision_flag);
int train_prog(int prog_id, int *state, int *respns, int beta1, int beta2);
int prog_response(int prog_id, int *state, int *respns);
int clear_prog_weights(int prog_id);
int deallocate_prog(int prog_id);
int prog_memory_usage(int prog_id);
int get_prog(int prog_id, int index, int *buffer, int count);
int put_prog(int prog_id, int index, int *buffer, int count);


/*     prog.c */
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <malloc.h>
#include "prog.h"
#ifndef O_BINARY
#   define O_BINARY 0
#endif
#define TRUE  1
#define FALSE  0
/* ***********************************************************************
The following definitions affect static data storage size, but NOT execution
time.
**************************************************************************/#
define NUM_NETWORKS 8
#define MAX_STATE_SIZE 64
#define MAX_RESPONSE_SIZE 8
#define MAX_GEN_SIZE 256
#define RF_TABLE_SIZE 128
/* ************************************************************************
/**************************************************************************
 The following store the major specs and pointers for the current prog
**************************************************************************/
static int first_call = TRUE;   /* is set to FALSE after init      */
static int *ap_a[NUM_NETWORKS+1];   /* pointers to all prog memories   */
static int *ap;                 /* pointer to current prog memory  */
static int ap_size[NUM_NETWORKS+1]; /* sizes in bytes of prog memories */
static int collide;         /* TRUE/FALSE control of hash collisions */
static int *qnt;                /* pointer to input quantization   */
static int *disp;               /* pointer to the displacement vector */
static int *rfieldt;            /* pointer to the rfield function table */
static unsigned int rndseq[2048];   /* Random number table for hashing */
static int indexes[MAX_GEN_SIZE];   /* prog mapping indexes            */
static long rfield[MAX_GEN_SIZE];   /* RF function magnitudes */
static long sum2_rfield = 0;    /* summed squared RF function magnitudes */
/**************************************************************************
The following functions are used internally by the prog code and can not
be called externally!
 **************************************************************************/
static void stoap(int *state)
{
    int i, j, k;
    long index, min_d, test_d, rf_index;
    long sum;
    int hash_tag;
    static long base[MAX_STATE_SIZE], qstate[MAX_STATE_SIZE];
...
}

static void setup(int prog_id)
{
...
}

/**************************************************************************
The following prog functions CAN be called externally!
**********************/
int allocate_prog(int num_state,int *qnt_state,int num_resp,
                int num_cell,int memory,int field_shape, int collide_flag)
{
    int i,j;
    int *pntr;

    /* initialize data structures on first call to driver */

    if (first_call) {
        genmap();
        for (i = 0; i <= NUM_NETWORKS; i++) ap_a[i] = 0;
        first_call = FALSE;
    }
...
}

etc...


Ray Schumacher
http://RJS.org





More information about the Python-list mailing list