Esempio n. 1
0
/**
 *  fügt einen Eintrag hinzu.
 *
 *  @param[in] name   Variablenname
 *  @param[in] value  Wert
 */
void libsiedler2::ArchivItem_Ini::addValue(const std::string& name, const std::string& value)
{
    ArchivItem_Text* item = dynamic_cast<ArchivItem_Text*>( getAllocator().create(BOBTYPE_TEXT) );
    item->setText(value);
    item->setName(name);

    push(item);
}
Esempio n. 2
0
void libsiedler2::ArchivItem_Ini::setValue(const std::string& name, const std::string& value)
{
    ArchivItem_Text* item = dynamic_cast<ArchivItem_Text*>(find(name));
    if(item)
    {
        // setText überschreibt Namen, daher nochmals setzen
        item->setText(value);
        item->setName(name);
    }
    else
    {
        // nicht gefunden, also hinzufügen
        addValue(name, value);
    }
}
Esempio n. 3
0
/**
 *  lädt die INI-Daten aus einer Datei.
 *
 *  @param[in] file    Dateihandle der Datei
 *
 *  @return liefert Null bei Erfolg, ungleich Null bei Fehler
 */
int libsiedler2::ArchivItem_Ini::load(std::istream& file)
{
    if(!file)
        return 1;

    std::string section;
    std::getline(file, section);

    size_t lr = section.find('\r');
    if (lr != std::string::npos) section.erase(lr, 1);
    size_t ln = section.find('\n');
    if (ln != std::string::npos) section.erase(ln, 1);

    if(!section.empty())
    {
        size_t pos = section.find('[');
        if(pos == std::string::npos)
            throw std::runtime_error("Invalid section");
        section = section.substr(pos + 1);
        pos = section.find(']');
        if(pos == std::string::npos)
            throw std::runtime_error("Invalid section");
        section = section.substr(0, pos);
    }
    if(section.empty() && !file.eof())
        return 4;

    setName(section);

    if(!file)
        return 0;

    while(true)
    {
        std::string entry;
        if(!std::getline(file, entry))
            break;
        lr = entry.find('\015');
        if (lr != std::string::npos) entry.erase(lr, 1);
        ln = section.find('\n');
        if (ln != std::string::npos) entry.erase(ln, 1);
        if(entry.empty())
            break;

        size_t pos = entry.find('=');
        if(pos == std::string::npos)
            throw std::runtime_error("Invalid value");
        std::string name = entry.substr(0, pos);
        std::string value = entry.substr(pos + 1);

        if(name.empty() || value.empty())
            continue;

        ArchivItem_Text* item = dynamic_cast<ArchivItem_Text*>( getAllocator().create(BOBTYPE_TEXT) );
        item->setText(value);
        item->setName(name);

        push(item);
    }

    return 0;
}