/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
/* File name: summary.ch */
#include<dlfcn.h>
#include<array.h>
double summary(array double &a);

double summary(array double &a) {
  void *handle, *fptr;
  double retval;
  int dim = (int)shape(shape(a));
  array int ext[dim];
  int i, totnum = 1;
  double sums;

  ext = shape(a);
  for(i = 0; i < dim; i++) {
     totnum = totnum * ext[i];
  }
  array double aa[totnum];
  aa = (array double [totnum])a; /* cast all data types to double */

  handle = dlopen("libvlaarg.dl", RTLD_LAZY);
  if(handle == NULL) {
     printf("Error: %s(): dlopen(): %s\n", __func__, dlerror());
     return NaN;
  }

  fptr = dlsym(handle, "summary_chdl");
  if(fptr == NULL)  {
     printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
     return NaN;
  }
  dlrunfun(fptr, &retval, NULL, aa, dim, ext);

  if(dlclose(handle)!=0) {
     printf("Error: %s(): dlclose(): %s\n", __func__, dlerror());
     return NaN;
  }
  return retval;
}

int main() {
  array double a1[2][3] = { 1, 5, 3,
                            5, 6, 7};
  int b1[3] = { 1, 5, 3};

  printf("summary = %f\n", summary(a1));
  printf("summary = %f\n", summary(b1));
  return 0;
}
