//
// List BN 2-Mai-98
//
//
//
//
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>

#include "WinMain.h"
#include "System.h"
#include "List.h"

#define __CFILE__ "List"
char LIST_acString[1024];

#define INDEXBLOCKANZAHL 100
#define DATENBLOCKANZAHL 100


// *******************************************************************
// vlInit
// *******************************************************************
List::vlInit (void)
{
VList_ *pStruktur;


pStruktur = &pVList;
pStruktur->nStatus = FALSE;
pStruktur->Daten = (VListDaten_ *)Mem_Alloc(sizeof(VListDaten_));
if(pStruktur->Daten!=NULL){
memset(pStruktur->Daten, 0, sizeof(VListDaten_));
pStruktur->Daten->pIndexBuffer = (char *)Mem_Alloc((INDEXBLOCKANZAHL + 1) * sizeof(long));
if(pStruktur->Daten->pIndexBuffer!=NULL){
memset(pStruktur->Daten->pIndexBuffer, 0, INDEXBLOCKANZAHL * sizeof(long));
pStruktur->Daten->lGroessepIndexBuffer = INDEXBLOCKANZAHL * sizeof(long);
pStruktur->Daten->lMaxAnzahlEintraegepIndexBuffer = INDEXBLOCKANZAHL;
pStruktur->Daten->nErsterDurchlauf = TRUE;
pStruktur->Daten->nOptimierungBisErstesDelet = TRUE;
pStruktur->nStatus = TRUE;
} else {
Mem_Free(pStruktur->Daten, sizeof(VListDaten_));
Mem_Free(pStruktur, sizeof(VList_));
sprintf(LIST_acString, "Can not get %d KBytes(1).", ((INDEXBLOCKANZAHL + 1) * sizeof(long) / 1024));
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
return(FALSE);
}
} else {
sprintf(LIST_acString, "Can not get %d KBytes(2).", (sizeof(VListDaten_) / 1024));
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
return(FALSE);
}
return(pStruktur->nStatus);
}

// *******************************************************************
// vlnew
// *******************************************************************
List::vlnew (void *pBuffer, long lLaenge)
{
int nRet=FALSE;
long lMerker;
long lWert;
char *pTmpBuffer;
long lpTmpBufferGroesse;


if(pVList.nStatus==TRUE){
if(pVList.Daten->nErsterDurchlauf==TRUE){
pVList.Daten->lGroesseEintrag = lLaenge;
pVList.Daten->pDatenBuffer = (char *)Mem_Alloc((DATENBLOCKANZAHL + 1) * pVList.Daten->lGroesseEintrag);
if(pVList.Daten->pDatenBuffer!=NULL){
memset(pVList.Daten->pDatenBuffer, 0, (DATENBLOCKANZAHL + 1) * pVList.Daten->lGroesseEintrag);
pVList.Daten->nErsterDurchlauf = FALSE;
pVList.Daten->lGroessepDatenBuffer = DATENBLOCKANZAHL * pVList.Daten->lGroesseEintrag;
pVList.Daten->lMaxAnzahlEintraegepDatenBuffer = DATENBLOCKANZAHL;
pVList.Daten->lAnzahlEintraege = 1;
pVList.Daten->lAktuellerEintrag = 0;
lWert = (long)(pVList.Daten->pDatenBuffer + pVList.Daten->lAktuellerEintrag * pVList.Daten->lGroesseEintrag);
memcpy(pBuffer, &lWert, sizeof(long));
memcpy((pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), &pVList.Daten->lAktuellerEintrag, sizeof(long));
nRet = TRUE;
} else {
vlDeInit();
sprintf(LIST_acString, "Can not get %d KBytes(3).", (((DATENBLOCKANZAHL + 1) * pVList.Daten->lGroesseEintrag) / 1024));
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
} else {
lMerker = pVList.Daten->lAktuellerEintrag;
lpTmpBufferGroesse = (pVList.Daten->lMaxAnzahlEintraegepIndexBuffer + 1) * sizeof(long);
pTmpBuffer = (char *)Mem_Alloc(lpTmpBufferGroesse);
if(pTmpBuffer!=NULL){
memset(pTmpBuffer, 0, (pVList.Daten->lMaxAnzahlEintraegepIndexBuffer + 1) * sizeof(long));
pVList.Daten->lAktuellerEintrag = 0;
while((pVList.Daten->lAktuellerEintrag<=pVList.Daten->lAnzahlEintraege)){
memcpy(&lWert, (pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), sizeof(long));
if(lWert!=-1){
*(pTmpBuffer + (lWert * sizeof(long))) = TRUE;
}
pVList.Daten->lAktuellerEintrag++;
}
pVList.Daten->lAktuellerEintrag = 0;
while((pVList.Daten->lAktuellerEintrag<pVList.Daten->lAnzahlEintraege) && ((long *)(pTmpBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long))!=0)){
pVList.Daten->lAktuellerEintrag++;
}
if(pVList.Daten->lAktuellerEintrag<(pVList.Daten->lMaxAnzahlEintraegepIndexBuffer - 2)){
memcpy((pVList.Daten->pIndexBuffer + (lMerker + 2) * sizeof(long)), (pVList.Daten->pIndexBuffer + (lMerker + 1) * sizeof(long)), ((pVList.Daten->lMaxAnzahlEintraegepIndexBuffer - lMerker - 2) * sizeof(long)));
memcpy((pVList.Daten->pIndexBuffer + (lMerker + 1) * sizeof(long)), &pVList.Daten->lAktuellerEintrag, sizeof(long));

lWert = (long)(pVList.Daten->pDatenBuffer + pVList.Daten->lAktuellerEintrag * pVList.Daten->lGroesseEintrag);
memcpy(pBuffer, &lWert, sizeof(long));
pVList.Daten->lAnzahlEintraege++;

nRet = TRUE;
} else {
pVList.Daten->pDatenBuffer = (char *)Mem_ReAlloc(pVList.Daten->pDatenBuffer, (long)(pVList.Daten->lMaxAnzahlEintraegepDatenBuffer * pVList.Daten->lGroesseEintrag), (long)((pVList.Daten->lMaxAnzahlEintraegepDatenBuffer + DATENBLOCKANZAHL) * pVList.Daten->lGroesseEintrag));
if(pVList.Daten->pDatenBuffer!=NULL){
memset(pVList.Daten->pDatenBuffer + (pVList.Daten->lMaxAnzahlEintraegepDatenBuffer * pVList.Daten->lGroesseEintrag), 0, DATENBLOCKANZAHL * pVList.Daten->lGroesseEintrag);
pVList.Daten->lMaxAnzahlEintraegepDatenBuffer += DATENBLOCKANZAHL;

pVList.Daten->pIndexBuffer = (char *)Mem_ReAlloc(pVList.Daten->pIndexBuffer, (long)((pVList.Daten->lMaxAnzahlEintraegepIndexBuffer + 1) * sizeof(long)), (long)((pVList.Daten->lMaxAnzahlEintraegepIndexBuffer + INDEXBLOCKANZAHL + 1) * sizeof(long)));
if(pVList.Daten->pIndexBuffer!=NULL){
pVList.Daten->lMaxAnzahlEintraegepIndexBuffer += INDEXBLOCKANZAHL;
pVList.Daten->lGroessepIndexBuffer += (long)((pVList.Daten->lMaxAnzahlEintraegepIndexBuffer + 1) * sizeof(long));

pVList.Daten->lGroessepDatenBuffer = (long)(pVList.Daten->lMaxAnzahlEintraegepIndexBuffer * pVList.Daten->lGroesseEintrag);
memcpy((pVList.Daten->pIndexBuffer + (lMerker + 2) * sizeof(long)), (pVList.Daten->pIndexBuffer + (lMerker + 1) * sizeof(long)), ((pVList.Daten->lMaxAnzahlEintraegepIndexBuffer - lMerker - 2) * sizeof(long)));
lWert = pVList.Daten->lAktuellerEintrag;
memcpy((pVList.Daten->pIndexBuffer + (lMerker + 1) * sizeof(long)), &lWert, sizeof(long));

lWert = (long)(pVList.Daten->pDatenBuffer + (long)(pVList.Daten->lAktuellerEintrag * pVList.Daten->lGroesseEintrag));
memcpy(pBuffer, &lWert, sizeof(long));
pVList.Daten->lAnzahlEintraege++;
nRet = TRUE;
} else {
pVList.Daten->lAktuellerEintrag = lMerker;
sprintf(LIST_acString, "Can not get %d KBytes(4).", ((pVList.Daten->pIndexBuffer, (long)((pVList.Daten->lMaxAnzahlEintraegepIndexBuffer + 1) * sizeof(long)), (long)((pVList.Daten->lMaxAnzahlEintraegepIndexBuffer + INDEXBLOCKANZAHL + 1) * sizeof(long))) / 1024));
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
} else {
pVList.Daten->lAktuellerEintrag = lMerker;
}

}
Mem_Free(pTmpBuffer, lpTmpBufferGroesse);
} else {
sprintf(LIST_acString, "Can not get %d KBytes(5).", (lpTmpBufferGroesse / 1024));
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
}
} else {
sprintf(LIST_acString, "%s", "[List.vlnew] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlnext
// *******************************************************************
List::vlnext (void *pBuffer)
{
int nRet=FALSE;
long lWert;


if(pVList.nStatus==TRUE){
if((pVList.Daten->lAktuellerEintrag + 1)<pVList.Daten->lAnzahlEintraege){
pVList.Daten->lAktuellerEintrag++;
memcpy(&lWert, (pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), sizeof(long));
lWert = (long)(pVList.Daten->pDatenBuffer + lWert * pVList.Daten->lGroesseEintrag);
memcpy(pBuffer, &lWert, sizeof(long));
nRet = TRUE;
}
} else {
sprintf(LIST_acString, "%s", "[List.vlnext] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlprev
// *******************************************************************
List::vlprev (void *pBuffer)
{
int nRet=FALSE;
long lWert;


if(pVList.nStatus==TRUE){
if((pVList.Daten->lAktuellerEintrag/* - 1*/)>0){ // war >1 BN 13.08.98
pVList.Daten->lAktuellerEintrag--;
memcpy(&lWert, (pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), sizeof(long));
lWert = (long)(pVList.Daten->pDatenBuffer + lWert * pVList.Daten->lGroesseEintrag);
memcpy(pBuffer, &lWert, sizeof(long));
nRet = TRUE;
}
} else {
sprintf(LIST_acString, "%s", "[List.vlprev] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlnum
// *******************************************************************
List::vlnum (long lPosition, void *pBuffer)
{
int nRet=FALSE;
long lWert;


if(pVList.nStatus==TRUE){
if(lPosition>0 && lPosition<(pVList.Daten->lAnzahlEintraege + 1)){
pVList.Daten->lAktuellerEintrag = lPosition - 1;
memcpy(&lWert, (pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), sizeof(long));
lWert = (long)(pVList.Daten->pDatenBuffer + lWert * pVList.Daten->lGroesseEintrag);
memcpy(pBuffer, &lWert, sizeof(long));
nRet = TRUE;
}
} else {
sprintf(LIST_acString, "%s", "[List.vlnum] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlreq
// *******************************************************************
List::vlreq (void *pBuffer, long *lPosition)
{
int nRet=FALSE;
long lWert;


if(pVList.nStatus==TRUE){
if(pVList.Daten->lAktuellerEintrag>=0){ // war >0 BN 27.5.98
*(lPosition) = pVList.Daten->lAktuellerEintrag + 1;
memcpy(&lWert, (pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), sizeof(long));
lWert = (long)(pVList.Daten->pDatenBuffer + lWert * pVList.Daten->lGroesseEintrag);
memcpy(pBuffer, &lWert, sizeof(long));
nRet = TRUE;
} else {
*(lPosition) = 0;
}
} else {
sprintf(LIST_acString, "%s", "[List.vlreq] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlroot
// *******************************************************************
List::vlroot (void)
{
int nRet=FALSE;


if(pVList.nStatus==TRUE){
pVList.Daten->lAktuellerEintrag = -1;
nRet = TRUE;
} else {
sprintf(LIST_acString, "%s", "[List.vlroot] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vldelet
// *******************************************************************
List::vldelet (void *pBuffer)
{
int nRet=FALSE;


if(pVList.nStatus==TRUE){
pVList.Daten->nOptimierungBisErstesDelet = FALSE;
memcpy((pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), (pVList.Daten->pIndexBuffer + (pVList.Daten->lAktuellerEintrag + 1) * sizeof(long)), ((pVList.Daten->lMaxAnzahlEintraegepIndexBuffer - pVList.Daten->lAktuellerEintrag - 1) * sizeof(long)));
pVList.Daten->lAnzahlEintraege--;
vlread (pBuffer);
nRet = TRUE;
} else {
sprintf(LIST_acString, "%s", "[List.vldelet] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vllast
// *******************************************************************
List::vllast (void *pBuffer)
{
int nRet=FALSE;
long lWert;


if(pVList.nStatus==TRUE){
pVList.Daten->lAktuellerEintrag = pVList.Daten->lAnzahlEintraege - 1;
memcpy(&lWert, (pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), sizeof(long));
lWert = (long)(pVList.Daten->pDatenBuffer + lWert * pVList.Daten->lGroesseEintrag);
memcpy(pBuffer, &lWert, sizeof(long));
nRet = TRUE;
} else {
sprintf(LIST_acString, "%s", "[List.vllast] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlDeInit
// *******************************************************************
List::vlDeInit (void)
{
int nRet=FALSE;


if(pVList.nStatus==TRUE){
if(pVList.Daten->pDatenBuffer!=NULL){
Mem_Free(pVList.Daten->pDatenBuffer, pVList.Daten->lGroessepDatenBuffer);
}
if(pVList.Daten->pIndexBuffer!=NULL){
Mem_Free(pVList.Daten->pIndexBuffer, pVList.Daten->lGroessepIndexBuffer);
}
if(pVList.Daten!=NULL){
Mem_Free(pVList.Daten, sizeof(VListDaten_));
}
nRet = TRUE;
} else {
sprintf(LIST_acString, "%s", "[List.vlDeInit] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlanz
// *******************************************************************
List::vlanz (long *lAnzahl)
{
int nRet=FALSE;


if(pVList.nStatus==TRUE){
*(lAnzahl) = pVList.Daten->lAnzahlEintraege;
nRet = TRUE;
} else {
sprintf(LIST_acString, "%s", "[List.vlanz] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlswap
// *******************************************************************
List::vlswap (long lZielPosition, long lQuellPosition)
{
int nRet=FALSE;
char *pBuffer;
long lWert1, lWert2;


if(pVList.nStatus==TRUE){
lZielPosition++;
lQuellPosition++;
if((lZielPosition>0 && lZielPosition<pVList.Daten->lAnzahlEintraege + 1) && (lQuellPosition>0 && lQuellPosition<pVList.Daten->lAnzahlEintraege + 1)){
pBuffer = (char *)Mem_Alloc(pVList.Daten->lGroesseEintrag);
if(pBuffer!=NULL){
memcpy(&lWert1, (pVList.Daten->pIndexBuffer + lZielPosition * sizeof(long)), sizeof(long));
memcpy(pBuffer, (pVList.Daten->pDatenBuffer + lWert1 * pVList.Daten->lGroesseEintrag), pVList.Daten->lGroesseEintrag);

memcpy(&lWert1, (pVList.Daten->pIndexBuffer + lZielPosition * sizeof(long)), sizeof(long));
memcpy(&lWert2, (pVList.Daten->pIndexBuffer + lQuellPosition * sizeof(long)), sizeof(long));
memcpy((pVList.Daten->pDatenBuffer + lWert1 * pVList.Daten->lGroesseEintrag),(pVList.Daten->pDatenBuffer + lWert2 * pVList.Daten->lGroesseEintrag), pVList.Daten->lGroesseEintrag);

memcpy(&lWert1, (pVList.Daten->pIndexBuffer + lQuellPosition * sizeof(long)), sizeof(long));
memcpy((pVList.Daten->pDatenBuffer + lWert1 * pVList.Daten->lGroesseEintrag), pBuffer, pVList.Daten->lGroesseEintrag);

nRet = TRUE;
Mem_Free(pBuffer, pVList.Daten->lGroesseEintrag);
}
}
} else {
sprintf(LIST_acString, "%s", "[List.vlswap] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlread
// *******************************************************************
List::vlread (void *pBuffer)
{
int nRet=FALSE;
long lWert;


if(pVList.nStatus==TRUE){
if(pVList.Daten->lAktuellerEintrag>=0 && pVList.Daten->lAktuellerEintrag<pVList.Daten->lAnzahlEintraege){
memcpy(&lWert, (pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), sizeof(long));
lWert = (long)(pVList.Daten->pDatenBuffer + lWert * pVList.Daten->lGroesseEintrag);
memcpy(pBuffer, &lWert, sizeof(long));
nRet = TRUE;
}
} else {
sprintf(LIST_acString, "%s", "[List.vlread] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}

// *******************************************************************
// vlmemset
// *******************************************************************
List::vlmemset (void *pBuffer)
{
int nRet=FALSE;
long lWert;


if(pVList.nStatus==TRUE){
if(pVList.Daten->lAktuellerEintrag>=0 && pVList.Daten->lAktuellerEintrag<pVList.Daten->lAnzahlEintraege){
memcpy(&lWert, (pVList.Daten->pIndexBuffer + pVList.Daten->lAktuellerEintrag * sizeof(long)), sizeof(long));
lWert = (long)(pVList.Daten->pDatenBuffer + lWert * pVList.Daten->lGroesseEintrag);
memset((pVList.Daten->pDatenBuffer + lWert * pVList.Daten->lGroesseEintrag), 0, pVList.Daten->lGroesseEintrag);
memcpy(pBuffer, &lWert, sizeof(long));
nRet = TRUE;
}
} else {
sprintf(LIST_acString, "%s", "[List.vlmemset] pVList.nStatus==FALSE");
AusnahmeFehler(LIST_acString, __CFILE__, __LINE__);
}
return(nRet);
}