/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
/************************************************************
* File name: List.chf
*            member functions of class List
************************************************************/
#include <list.h>

/******************** Public Functions ********************/
List::List(){
    void *fptr;
    if (m_dlcount==0) { //first time
        m_dlhandle = dlopen("liblist.dl", RTLD_LAZY);
        if(m_dlhandle == NULL) {
            printf("Error: %s(): dlopen(): %s\n", __func__, dlerror());
            return;
        };
    };
    fptr = dlsym(m_dlhandle, "List_List_chdl");
    if(fptr == NULL)  {
        printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
        return;
    };
    dlrunfun(fptr, &m_class, NULL);
    m_dlcount++;    
}

List::~List(){
    void *fptr;
    fptr = dlsym(m_dlhandle, "List_dList_chdl");
    if(fptr == NULL)  {
        printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
        return;
    };
    dlrunfun(fptr, NULL, NULL, m_class);
    if(m_dlcount==1) { //last instance
        if(dlclose(m_dlhandle)!=0)
            printf("Error: %s(): dlclose(): %s\n", __func__, dlerror());
    };
    m_dlcount--;
}


void List::prepend (int ssn, string_t fname, string_t lname) {
    void *fptr;

    fptr = dlsym(m_dlhandle, "List_prepend_chdl");
    if(fptr == NULL)  {
        printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
    };
    dlrunfun(fptr, NULL, prepend, m_class, ssn, fname, lname);
}



void List::print() {
    void *fptr;

    fptr = dlsym(m_dlhandle, "List_print_chdl");
    if(fptr == NULL)  {
        printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
    };
    dlrunfun(fptr, NULL, print, m_class);
}



int List::remove (int ssn) {
    void *fptr;
    int retval;

    fptr = dlsym(m_dlhandle, "List_remove_chdl");
    if(fptr == NULL)  {
        printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
        return -1;
    };
    dlrunfun(fptr, &retval, remove, m_class, ssn);
    return retval;
}
