#include "Bilder.h"
#include <DateiSystem.h>

// Inhalt der Bilder Klasse aus Bilder.h
// Konstruktor
Bilder::Bilder()
{
    bilder = new RCArray< Bild >();
    paths = new RCArray< Text >();
    ref = 1;
}

// Destruktor
Bilder::~Bilder()
{
    bilder->release();
    paths->release();
}

// nicht constant
Bild *Bilder::get( const char *path )
{
    c.lock();
    int index = 0;
    for( auto i = paths->getIterator(); i; i++, index++ )
    {
        if( i->istGleich( path ) )
        {
            c.unlock();
            return bilder->get( index );
        }
    }
    Text p( path );
    p.ersetzen( "\\", "/" );
    LTDBDatei dat;
    dat.setDatei( p.getTeilText( 0, p.positionVon( "/", p.anzahlVon( "/" ) - 1 ) ) );
    dat.leseDaten( 0 );
    Bild *b = dat.laden( 0, p.getTeilText( p.positionVon( "/", p.anzahlVon( "/" ) - 1 ) + 1 ) );
    if( !b )
    {
        c.unlock();
        return 0;
    }
    paths->add( new Text( path ) );
    bilder->add( b->getThis() );
    c.unlock();
    return b;
}

BilderV *Bilder::getThis()
{
    ref++;
    return this;
}

BilderV *Bilder::release()
{
    if( !--ref )
        delete this;
    return 0;
}