[Gambas-user] Issue accessing C Library from Gambas

Linus olivier.cruilles at yahoo.fr
Sat Oct 7 20:23:28 CEST 2023


Hello,

For long time I build Classes in Gambas to access to various C Libraries directly.
I did it for libssh4, GeoIp and others.
For now all worked fine and I was able to figure all cases of Args format to convert in Gambas.

Currently, I'm trying to build another Classes for RRDtool library but new cases of Args and of course new 'Segmentation Fault 11" error.

Only the function 'rrd_graph' fail and I don't know how to deal with it

	=> int rrd_graph(int, char **, char ***, int *, int *, FILE *, double *, double *)


Maybe someone can help me on it:

######## Classe RRDtoolClient ########

' Gambas class file

Export

  Event rrdError(ErrorMessage As String) ' librrd error

  ' Error return codes
  Private Enum
    RRD_OK = 0,
    RRD_ERROR = -1,
    RRD_PARAM = -10

  ' Declaration of librrd library
  Library "librrd"

  ' Fonction C of the library 'librrd.so'

  ' int rrd_create(int, char **);

  ' rrd_info_t *rrd_info(int, char **);
  ' rrd_info_t *rrd_info_push(rrd_info_t *, char *, rrd_info_type_t, rrd_infoval_t);
  ' void rrd_info_print(
  ' rrd_info_t * data);
  ' void rrd_info_free(rrd_info_t *); 
  ' int rrd_update(int, char **);
  ' rrd_info_t *rrd_update_v(int, char **);

  ' int rrd_graph(int, char **, char ***, int *, int *, FILE *, double *, double *);

  ' rrd_info_t *rrd_graph_v(int, char **);
  ' int rrd_fetch(int, char **, time_t *, time_t *, unsigned long *, unsigned long *, char ***, rrd_value_t **);
  ' int rrd_restore(int, char **);
  ' int rrd_dump(int, char **);
  ' int rrd_tune(int, char **);
  ' time_t rrd_last(int, char **);
  ' int rrd_lastupdate(int argc, char **argv);
  ' time_t rrd_first(int, char **);
  ' int rrd_resize(int, char **);
  ' char *rrd_strversion(void);
  ' double rrd_version(void);
  ' int rrd_xport(int, char **, int *, time_t *, time_t *, unsigned long *, unsigned long *, char ***, rrd_value_t **);
  ' int rrd_flushcached (int argc, char **argv);
  ' void rrd_freemem(void *mem);

  ' void   rrd_set_error_r  (rrd_context_t *, char *, ...)
  ' void   rrd_clear_error_r(rrd_context_t *)
  ' int    rrd_test_error_r (rrd_context_t *)
  ' char  *rrd_get_error_r  (rrd_context_t *)


  Private Extern rrd_create(nbArgs As Integer, rrdcreateOptions As Pointer) As Integer
  Private Extern rrd_update(nbArgs As Integer, rrdupdateOptions As Pointer) As Integer

  Private Extern rrd_lastupdate(nbArgs As Integer, rrdOptions As Pointer) As Integer

  Private Extern rrd_version() As Long
  Private Extern rrd_strversion() As String

  Private Extern rrd_clear_error()
  Private Extern rrd_get_error() As String

  Private Extern rrd_test_error() As Integer


==> Here is my issue


  ' int rrd_graph(int, char **, char ***, int *, int *, FILE *, double *, double *);

  Private Extern rrd_graph(nbArgs As Integer, rrdgraphOptions As Pointer, CalCpr As Pointer, xsize As Integer, ysize As Integer, FileType As Pointer, ymin As Float, ymax As Float) As Integer



Public Function rrdgraph(RRDfile As String, RRDGraphValue As String[]) As Integer

  ' Procedure to Create an RRD Graphic using
  ' C fonction rrd_graph()

  Dim rc As Integer
  Dim ErrorMess As String
  Dim RRDOption As New String[]
  Dim NbreOptions As Integer
  Dim Options As String
  Dim CalCpr As New String[]
  Dim PointerCalCpr As Pointer
  Dim xsize As Integer
  Dim ysize As Integer
  Dim FileStream As Pointer
  Dim ymin As Float
  Dim ymax As Float

  If RRDGraphValue = Null Then
    Return RRD_PARAM
  End If

  If RRDGraphValue = Null Then
    Return RRD_PARAM
  End If

  If Trim(RRDfile) = "" Then
    Return RRD_PARAM
  End If

  FileStream = Null

  RRDOption.Add("rrdgraph")
  '
  RRDOption.Add(RRDfile)
  '
  RRDOption.Insert(RRDGraphValue, RRDOption.Max + 1)

  RRDOption.Add("")


  '     char *rrdargs[] = {
  '         "rrdgraph",
  '   "mygraph.png",
  '   "-a", "PNG",
  '   "--title", "My Fancy Graph",
  '   "DEF:myval=myfile.rrd:myval:AVERAGE",
  '   "LINE2:myval#00CC00:The value",
  '   NULL
  '     };

  xsize = 600
  ysize = 300

  ymin = 10.0
  ymax = 50.0

  Options = RRDOption.Join(" ")
  NbreOptions = RRDOption.Max

  Print "rrdcreate: " & Options

  PointerCalCpr = CalCpr.Data

  rrd_clear_error()

  rc = rrd_graph(NbreOptions, RRDOption.Data, Pointer@(PointerCalCpr), xsize, ysize, FileStream, ymin, ymax)
  If rrd_test_error() Or rc <> 0 Then
    ErrorMess = rrd_get_error()
    $MessageErreur = "Error creating RRD file: " & ErrorMess & " (" & rc & ")"
    Raise rrdError($MessageErreur)
    Return RRD_ERROR
  End If

  Return rc


  ' Example:
  ' rrd_graph works the same, but has some additional parameters that are
  ' return values from the rrd_graph function. And this API changed from
  ' rrdtool 1.0 -> 1.2. So what I do is like this:
  ' 
  '     char **calcpr  = NULL;
  '     int rrdargcount, xsize, ysize, result;
  '     double ymin, ymax;
  '     char *rrdargs[] = {
  '         "rrdgraph",
  '   "mygraph.png",
  '   "-a", "PNG",
  '   "--title", "My Fancy Graph",
  '   "DEF:myval=myfile.rrd:myval:AVERAGE",
  '   "LINE2:myval#00CC00:The value",
  '   NULL
  '     };
  ' 
  '     optind = opterr = 0;
  '     rrd_clear_error();
  ' #ifdef RRDTOOL12
  '     result = rrd_graph(8, rrdargs, &calcpr, &xsize, &ysize, NULL, &ymin,
  ' &ymax);
  ' #else
  '     result = rrd_graph(8, rrdargs, &calcpr, &xsize, &ysize);
  ' #endif
  ' 
  '     /* Was it OK ? */
  '     if (rrd_test_error() || (result != 0)) {
  '         if (calcpr) {
  '             int i;
  '             for (i=0; (calcpr[i]); i++) free(calcpr[i]);
  '             calcpr = NULL;
  '         }
  ' 
  '   printf("Graph error: %s\n", rrd_get_error());
  '     }

End

I have attached the project into the Email




Thank you

Olivier Cruilles
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20231007/bcdfd033/attachment-0002.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gb.RRDClient-0.1.5.tar.gz
Type: application/x-gzip
Size: 12542 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20231007/bcdfd033/attachment-0001.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20231007/bcdfd033/attachment-0003.htm>


More information about the User mailing list