2008-03-08 23:49:26 +01:00
|
|
|
// Copyright (c) 2008, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
2008-03-18 04:44:00 +01:00
|
|
|
// AEGISUB/ATHENASUB
|
2008-03-08 23:49:26 +01:00
|
|
|
//
|
|
|
|
// Website: http://www.aegisub.net
|
|
|
|
// Contact: mailto:amz@aegisub.net
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "section.h"
|
2008-03-18 04:44:00 +01:00
|
|
|
using namespace Athenasub;
|
2008-03-08 23:49:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Constructor
|
2008-08-03 18:17:35 +02:00
|
|
|
CSection::CSection(String _name)
|
2008-03-08 23:49:26 +01:00
|
|
|
{
|
|
|
|
name = _name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////
|
|
|
|
// Append an entry
|
2008-08-03 18:17:35 +02:00
|
|
|
void CSection::AddEntry(Entry entry,int pos)
|
2008-03-08 23:49:26 +01:00
|
|
|
{
|
2008-03-18 03:09:33 +01:00
|
|
|
// Insert to entry list
|
2008-03-14 03:02:48 +01:00
|
|
|
if (pos == -1) entries.push_back(entry);
|
|
|
|
else entries.insert(entries.begin()+pos,entry);
|
2008-03-18 03:09:33 +01:00
|
|
|
|
|
|
|
// Add to index too if it's indexable
|
|
|
|
if (entry->IsIndexable()) {
|
|
|
|
index[entry->GetIndexName()] = entry;
|
|
|
|
}
|
2008-03-08 23:49:26 +01:00
|
|
|
}
|
2008-03-13 01:51:31 +01:00
|
|
|
|
2008-03-18 03:09:33 +01:00
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
// Removes the nth entry
|
2008-08-03 18:17:35 +02:00
|
|
|
void CSection::RemoveEntryByIndex(size_t i)
|
2008-03-13 06:39:03 +01:00
|
|
|
{
|
2008-03-18 03:09:33 +01:00
|
|
|
// Get entry iterator and erase
|
2008-08-03 18:17:35 +02:00
|
|
|
std::vector<Entry>::iterator entry = entries.begin()+i;
|
2008-03-18 03:09:33 +01:00
|
|
|
entries.erase(entry);
|
|
|
|
|
|
|
|
// If it's indexable, remove it from index as well
|
|
|
|
if ((*entry)->IsIndexable()) index.erase((*entry)->GetIndexName());
|
2008-03-13 06:39:03 +01:00
|
|
|
}
|
|
|
|
|
2008-03-18 03:09:33 +01:00
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
// Removes an entry by its value
|
2008-08-03 18:17:35 +02:00
|
|
|
void CSection::RemoveEntry(Entry entry)
|
2008-03-13 06:39:03 +01:00
|
|
|
{
|
|
|
|
size_t len = entries.size();
|
|
|
|
for (size_t i=0;i<len;i++) {
|
|
|
|
if (entries[i] == entry) {
|
|
|
|
entries.erase(entries.begin()+i);
|
2008-03-18 03:09:33 +01:00
|
|
|
if (entry->IsIndexable()) index.erase(entry->GetIndexName());
|
2008-03-13 06:39:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-18 03:09:33 +01:00
|
|
|
|
|
|
|
////////////////////////////
|
|
|
|
// Retrieves entry by index
|
2008-08-03 18:17:35 +02:00
|
|
|
Entry CSection::GetEntry(size_t i) const
|
2008-03-16 03:20:34 +01:00
|
|
|
{
|
2008-03-18 03:09:33 +01:00
|
|
|
return entries[i];
|
2008-03-16 03:20:34 +01:00
|
|
|
}
|
|
|
|
|
2008-03-18 03:09:33 +01:00
|
|
|
|
2008-08-09 22:43:27 +02:00
|
|
|
//////////////////////////////////////
|
|
|
|
// Retrieves entry reference by index
|
|
|
|
Entry& CSection::GetEntryRef(size_t i)
|
|
|
|
{
|
|
|
|
return entries[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-18 03:09:33 +01:00
|
|
|
/////////////////////////
|
|
|
|
// Get number of entries
|
2008-08-03 18:17:35 +02:00
|
|
|
size_t CSection::GetEntryCount() const
|
2008-03-13 06:39:03 +01:00
|
|
|
{
|
|
|
|
return entries.size();
|
|
|
|
}
|
|
|
|
|
2008-03-13 01:51:31 +01:00
|
|
|
|
|
|
|
//////////////////
|
|
|
|
// Set a property
|
2008-08-03 18:17:35 +02:00
|
|
|
void CSection::SetProperty(const String &key,const String &value)
|
2008-03-13 01:51:31 +01:00
|
|
|
{
|
|
|
|
properties[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
// Removes a property
|
2008-08-03 18:17:35 +02:00
|
|
|
void CSection::UnsetProperty(const String &key)
|
2008-03-13 01:51:31 +01:00
|
|
|
{
|
|
|
|
std::map<String,String>::iterator iter = properties.find(key);
|
|
|
|
if (iter != properties.end()) properties.erase(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////
|
|
|
|
// Get a property's value
|
2008-08-03 18:17:35 +02:00
|
|
|
String CSection::GetProperty(const String &key) const
|
2008-03-13 01:51:31 +01:00
|
|
|
{
|
|
|
|
std::map<String,String>::const_iterator iter = properties.find(key);
|
|
|
|
if (iter != properties.end()) return iter->second;
|
2008-11-08 23:07:23 +01:00
|
|
|
else return "";
|
2008-03-13 01:51:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
// Checks if it has a property
|
2008-08-03 18:17:35 +02:00
|
|
|
bool CSection::HasProperty(const String &key) const
|
2008-03-13 01:51:31 +01:00
|
|
|
{
|
|
|
|
return properties.find(key) != properties.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
// Get property count
|
2008-08-03 18:17:35 +02:00
|
|
|
size_t CSection::GetPropertyCount() const
|
2008-03-13 01:51:31 +01:00
|
|
|
{
|
|
|
|
return properties.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////
|
|
|
|
// Get name of a property by index
|
2008-08-03 18:17:35 +02:00
|
|
|
String CSection::GetPropertyName(size_t n) const
|
2008-03-13 01:51:31 +01:00
|
|
|
{
|
|
|
|
std::map<String,String>::const_iterator iter=properties.begin();
|
|
|
|
for (size_t i=0 ; iter!=properties.end() ; iter++,i++) {
|
2008-03-18 03:09:33 +01:00
|
|
|
if (i == n) return iter->first;
|
2008-03-13 01:51:31 +01:00
|
|
|
}
|
2008-11-08 23:07:23 +01:00
|
|
|
return "";
|
2008-03-13 01:51:31 +01:00
|
|
|
}
|
2008-03-18 03:09:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////
|
|
|
|
// Retrieve from index
|
2008-08-03 18:17:35 +02:00
|
|
|
Entry CSection::GetFromIndex(String key) const
|
2008-03-18 03:09:33 +01:00
|
|
|
{
|
2008-08-03 18:17:35 +02:00
|
|
|
std::map<String,Entry>::const_iterator result = index.find(key);
|
2008-03-18 03:09:33 +01:00
|
|
|
if (result != index.end()) return result->second;
|
2008-08-03 18:17:35 +02:00
|
|
|
return Entry();
|
2008-03-18 03:09:33 +01:00
|
|
|
}
|