Commit b8342867 authored by Robert Izzard's avatar Robert Izzard
Browse files

update for latest API including error_buffer

parent 5aaa5351
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -7,11 +7,12 @@ CC := gcc
LD      := gcc
PROGRAM := binary_c_python_api
MAKE    := /usr/bin/make
LIBS 	:= -lbinary_c `$(BINARY_C)/binary_c-config --libs` 
LIBS 	:= -lbinary_c $(shell $(BINARY_C)/binary_c-config --libs)
#`$(BINARY_C)/binary_c-config --libdirs_list` 
C_SRC   := binary_c_python_api.c
OBJECTS := $(C_SRC:.c=.o)
OBJ_FLAGS := -c
CFLAGS := -fPIC `$(BINARY_C)/binary_c-config --flags` -I$(BINARY_C)/src/ -I$(BINARY_C)/src/API 
CFLAGS := -fPIC $(shell $(BINARY_C)/binary_c-config --flags) -I$(BINARY_C)/src/ -I$(BINARY_C)/src/API 
SO_FLAGS := -shared -o
SO_NAME := libbinary_c_api.so

+0 −1
Original line number Diff line number Diff line
@@ -105,7 +105,6 @@ int run_binary(char * argstring,
             "%s",
             "/dev/null");
    stardata->preferences->internal_buffering = 2;
    stardata->preferences->internal_buffering_compression = 0;
    stardata->preferences->batchmode = BATCHMODE_LIBRARY;

    binary_c_evolve_for_dt(stardata,
+21 −4
Original line number Diff line number Diff line
@@ -176,13 +176,30 @@ static PyObject* binary_c_run_binary(PyObject *self, PyObject *args)
    else
    {
        char * buffer;
        int nbytes;
        char * error_buffer;
        size_t nbytes;
        int out MAYBE_UNUSED = run_binary(argstring,
                                          &buffer,
                                          &error_buffer,
                                          &nbytes);
        /* copy the buffer to a python string */
        PyObject * ret = Py_BuildValue("s", buffer);
        free(buffer);
        return ret;
        PyObject * return_string = Py_BuildValue("s", buffer);
        PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer);

        if(error_buffer != NULL && strlen(error_buffer)>0)
        {
            fprintf(stderr,
                    "Error in binary_c run : %s\n",
                    error_buffer);
        }
        
        Safe_free(buffer);
        Safe_free(error_buffer);

        /* 
         * TODO
         * return the return_error_string as well!
         */
        return return_string;
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -10,8 +10,9 @@

/* Binary_c's python API prototypes */
int run_binary (char * argstring,
                char ** outstring,
                int * nbytes);
                char ** const outstring,
                char ** const errorstring,
                size_t * const nbytes);

/* C macros */
#define BINARY_C_APITEST_VERSION 0.1
+18 −10
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ int stdoutwas;
int main(int argc,
         char * argv[])
{
    char * argstring = MALLOC(sizeof(char) * (size_t)STRING_LENGTH);
    char * argstring = Malloc(sizeof(char) * (size_t)STRING_LENGTH);
    snprintf(argstring,
             STRING_LENGTH,
             "binary_c M_1 %g M_2 %g separation %g orbital_period %g metallicity %g max_evolution_time %g\n",
@@ -62,9 +62,11 @@ int main(int argc,
             15000.0);

    char * buffer;
    int nbytes;
    char * error_buffer;
    size_t nbytes;
    int out = run_binary(argstring,
                         &buffer,
                         &error_buffer,
                         &nbytes);
    
    printf("output (binary_c returned %d)\n%s\n",out,buffer);
@@ -76,8 +78,9 @@ int main(int argc,


int run_binary(char * argstring,
               char ** buffer,
               int * nbytes)
               char ** const buffer,
               char ** const error_buffer,
               size_t * const nbytes)
{
    /* memory for N binary systems */
    struct libbinary_c_stardata_t *stardata;
@@ -91,6 +94,8 @@ int run_binary(char * argstring,
                        &store,
                        &argstring,
                        -1);
    printf("have new stardata %p\n",stardata);fflush(NULL);
    
    /* disable logging */
    snprintf(stardata->preferences->log_filename,
             STRING_LENGTH-1,
@@ -101,8 +106,7 @@ int run_binary(char * argstring,
             "%s",
             "/dev/null");
    /* output to strings */
    stardata->preferences->internal_buffering = 2;
    stardata->preferences->internal_buffering_compression = 0;
    stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
    stardata->preferences->batchmode = BATCHMODE_LIBRARY;

    /* do binary evolution */
@@ -112,11 +116,15 @@ int run_binary(char * argstring,
    /* get buffer pointer */
    binary_c_buffer_info(stardata,buffer,nbytes);
    
    /* get error buffer pointer */
    binary_c_error_buffer(stardata,error_buffer);
    
    /* set raw_buffer_size = -1 to prevent it being freed */
    stardata->tmpstore->raw_buffer_size = -1;
    
    /* free stardata (except the buffer) */
    binary_c_free_memory(&stardata,TRUE,TRUE,FALSE);
    binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
    binary_c_free_store_contents(store);
        
    return 0;
}
Loading