/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* example for testing functions Ch_VaVarargs*() */
#include <ch.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>

void func(int n, ...);
void vfunc(int n, va_list ap_c);

EXPORTCH void vfunc_chdl(void *varg) {
    ChInterp_t interp;
    ChVaList_t ap;
    void * ap_c; /* equivalent to va_list ap_c */
    ChVaList_t ap_ch;
    int ii;
    void *memhandle; 

    Ch_VaStart (interp, ap, varg);
    ii = Ch_VaArg (interp, ap, int);
    ap_ch = Ch_VaArg (interp, ap, ChVaList_t);

    ap_c = Ch_VaVarArgsCreate(interp, ap_ch, &memhandle);
    vfunc(ii, ap_c);
    Ch_VaVarArgsDelete(interp, memhandle);

    Ch_VaEnd(interp, ap);
    return;
}

void func(int n, ...) {
    va_list ap;

    va_start(ap, n);
    vfunc(n, ap);
    va_end(ap);
}

void vfunc(int n, va_list ap_c) {
    char c1;
    int i1;
    double d1;

    printf("The first argument is %d\n", n);

    c1 = va_arg(ap_c, char);
    printf("\nc1 = %c\n", c1);

    i1 = va_arg(ap_c, int);
    printf("i1 = %d\n", i1);

    d1 = va_arg(ap_c, double);
    printf("d1 = %f\n", d1);

    return;
}
