From 5cfc1e5d9579b97872ff1ce90e131d56640603d7 Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Fri, 8 Nov 2019 14:43:10 +0000
Subject: [PATCH] Synched with master

---
 .binary_c_python_api.c.swp | Bin 1024 -> 0 bytes
 binary_c_python.c          |  62 ++++++++++++++++++++++++++++++-------
 binary_c_python.h          |  10 +++---
 binary_c_python_api.c      |  44 +++++++++++++-------------
 4 files changed, 78 insertions(+), 38 deletions(-)
 delete mode 100644 .binary_c_python_api.c.swp

diff --git a/.binary_c_python_api.c.swp b/.binary_c_python_api.c.swp
deleted file mode 100644
index df644636b4141ddb926e3f7ddb6072642942ddfa..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 1024
zcmYc?$V<%2S1{7E)H7y40>POK3@M3anJGwOxR_vdx<09S`DOXKo++t`0f{NP!Nw*A
tx`rm6;XZI}Ntt<xMV0Z%@dcG78Tonfi3OQ@$#B_G#%KtPh5)@o005nY7=Zu)

diff --git a/binary_c_python.c b/binary_c_python.c
index c5cf65d47..e09c5d3dd 100644
--- a/binary_c_python.c
+++ b/binary_c_python.c
@@ -156,6 +156,9 @@ static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args)
 
 static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args)
 {
+
+    // This function is an very bare example of how a function would look like.
+
     double var1, var2;
 
     /* Parse the input tuple */
@@ -224,27 +227,64 @@ static PyObject* binary_c_run_binary_with_log(PyObject *self, PyObject *args)
     else
     {
         char * buffer;
-        int nbytes;
+        char * error_buffer;
+        size_t nbytes;
         int out MAYBE_UNUSED = run_binary_with_log(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;
     }
 }
 
 static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args)
 {
-    /* Binary structures */
     char * buffer;
-    int nbytes;
+    char * error_buffer;
+    size_t nbytes;
     int out MAYBE_UNUSED = return_arglines(&buffer,
-                                      &nbytes);
+                                          &error_buffer,
+                                          &nbytes);
+
     /* copy the buffer to a python string */
-    PyObject * ret = Py_BuildValue("s", buffer);
-    free(buffer);
-    /* Return an object containing the arg list */
-    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;
 }
+
+
+
diff --git a/binary_c_python.h b/binary_c_python.h
index 29fbf7247..04a1a1257 100644
--- a/binary_c_python.h
+++ b/binary_c_python.h
@@ -15,11 +15,13 @@ int run_binary (char * argstring,
                 size_t * const nbytes);
 
 int run_binary_with_log (char * argstring,
-                char ** outstring,
-                int * nbytes);
+                char ** const outstring,
+                char ** const errorstring,
+                size_t * const nbytes);
 
-int return_arglines(char ** buffer,
-               int * nbytes);
+int return_arglines(char ** const outstring,
+                char ** const errorstring,
+                size_t * const nbytes);
 
 /* C macros */
 #define BINARY_C_APITEST_VERSION 0.1
diff --git a/binary_c_python_api.c b/binary_c_python_api.c
index 166aa6ee7..d160ef857 100644
--- a/binary_c_python_api.c
+++ b/binary_c_python_api.c
@@ -94,7 +94,6 @@ int run_binary(char * argstring,
                         &store,
                         &argstring,
                         -1);
-    printf("have new stardata %p\n",stardata);fflush(NULL);
     
     /* disable logging */
     snprintf(stardata->preferences->log_filename,
@@ -129,8 +128,9 @@ int run_binary(char * argstring,
     return 0;
 }
 
-int return_arglines(char ** buffer,
-                    int * nbytes)
+int return_arglines(char ** const buffer,
+               char ** const error_buffer,
+               size_t * const nbytes)
 {
     /* memory for N binary systems */
     struct libbinary_c_stardata_t *stardata;
@@ -157,8 +157,7 @@ int return_arglines(char ** buffer,
              "/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;
 
     /* List available arguments */
@@ -166,19 +165,24 @@ int return_arglines(char ** buffer,
 
     /* 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;
 }
 
 int run_binary_with_log(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;
@@ -192,32 +196,26 @@ int run_binary_with_log(char * argstring,
                         &store,
                         &argstring,
                         -1);
-    /* disable logging */
-    // snprintf(stardata->preferences->log_filename,
-    //          STRING_LENGTH-1,
-    //          "%s",
-    //          "/dev/null");
-    // snprintf(stardata->preferences->api_log_filename_prefix,
-    //          STRING_LENGTH-1,
-    //          "%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 */
     binary_c_evolve_for_dt(stardata,
                            stardata->model.max_evolution_time);
-
+        
     /* 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;
 }
-- 
GitLab