// SOLEL.C

// This file includes methods for:

// C_SOLEL

#include <solipeg.g>
#include <solipeg.rsg>
#include <hwim.h>



#pragma METHOD_CALL


/************************************/
/* External Library Class Methods   */
/************************************/

// Initialize an external library for loading
METHOD INT solel_el_init(   PR_SOLEL *  self,
                            TEXT *      strFilename,
                            TEXT *      strReqID,
                            WORD        wMaxLibVer ) {
    TEXT *      strLibID = NULL ;
    INT         iRet ;
    INT         iLp ;
    TEXT        buf[33] ;

    // Create an intance of the rscfile class
    self->solel.pRsc = p_new(CAT_SOLIPEG_OLIB, C_RSCFILE) ;
    p_send3(self->solel.pRsc, O_RS_INIT, strFilename) ;

    // Check the library ID - should match strReqID
    p_send4(self->solel.pRsc, O_RS_READ, 1, &strLibID) ;
    iRet = p_scmp(strLibID, strReqID) ;
    p_free(strLibID) ;
    if (iRet)
        return(1) ;

    // Read the version number
    p_send4(self->solel.pRsc, O_RS_READ_BUF, 2, &self->solel.wLibVer) ;
    if ( self->solel.wLibVer > wMaxLibVer )
        return(1) ;

    // Read the library size
    p_send4(self->solel.pRsc, O_RS_READ_BUF, 4, &self->solel.bLibSize) ;

    // Create a container for the names
    self->solel.pData = p_new(CAT_SOLIPEG_OLIB, C_VASTR) ;
    p_send3(self->solel.pData, O_VA_INIT, 32) ;

    // Start after the header and load all the names
    for ( iLp = 5 ; iLp < 5 + self->solel.bLibSize ; iLp++ ) {
        // Read the name into a local buffer
        p_send4(    self->solel.pRsc,
                    O_RS_READ_BUF,
                    iLp,
                    buf ) ;
        // Insert it into the list
        p_send5(    self->solel.pData,
                    O_VA_INSERTM,
                    self->solel.pData->varoot.nrec,
                    buf,
                    1 ) ;
    }

    return(0) ;
}

// Read a game by number
METHOD VOID solel_el_read(  PR_SOLEL *  self,
                            VOID **     ppBuf,
                            INT         iIndex ) {
    // Just a simple shell around the resource read service
    // Note the use of bLibSize member
    p_send4(    self->solel.pRsc,
                O_RS_READ,
                5 + self->solel.bLibSize + iIndex,
                ppBuf ) ;
}

