#pragma once
#include "FractoriumCommon.h"
///
/// EmberFile class.
///
///
/// Class for representing an ember Xml file in memory.
/// It contains a filename and a vector of embers.
/// It also provides static helper functions for creating
/// default names for the file and the embers in it.
///
template
class EmberFile
{
public:
///
/// Empty constructor that does nothing.
///
EmberFile()
{
}
///
/// Default copy constructor.
///
/// The EmberFile object to copy
EmberFile(const EmberFile& emberFile)
{
EmberFile::operator=(emberFile);
}
///
/// Copy constructor to copy an EmberFile object of type U.
///
/// The EmberFile object to copy
template
EmberFile(const EmberFile& emberFile)
{
EmberFile::operator=(emberFile);
}
///
/// Default assignment operator.
///
/// The EmberFile object to copy
EmberFile& operator = (const EmberFile& emberFile)
{
if (this != &emberFile)
EmberFile::operator=(emberFile);
return *this;
}
///
/// Assignment operator to assign a EmberFile object of type U.
///
/// The EmberFile object to copy.
/// Reference to updated self
template
EmberFile& operator = (const EmberFile& emberFile)
{
m_Filename = emberFile.m_Filename;
CopyVec(m_Embers, emberFile.m_Embers);
return *this;
}
///
/// Clear the file name and the vector of embers.
///
void Clear()
{
m_Filename.clear();
m_Embers.clear();
}
///
/// Thin wrapper to get the size of the vector of embers.
///
size_t Size()
{
return m_Embers.size();
}
///
/// Delete the ember at the given index.
/// Will not delete anything if the size is already 1.
///
/// The index of the ember to delete
/// True if successfully deleted, else false.
bool Delete(size_t index)
{
if (Size() > 1 && index < Size())
{
m_Embers.erase(m_Embers.begin() + index);
return true;
}
else
return false;
}
///
/// Ensure all ember names are unique.
///
void MakeNamesUnique()
{
for (size_t i = 0; i < m_Embers.size(); i++)
{
for (size_t j = 0; j < m_Embers.size(); j++)
{
if (i != j && m_Embers[i].m_Name == m_Embers[j].m_Name)
{
m_Embers[j].m_Name = IncrementTrailingUnderscoreInt(QString::fromStdString(m_Embers[j].m_Name)).toStdString();
j = 0;
}
}
}
}
///
/// Return the default filename based on the current date/time.
///
/// The default filename
static QString DefaultFilename()
{
return "Flame_" + QDateTime(QDateTime::currentDateTime()).toString("yyyy-MM-dd-hhmmss");
}
///
/// Return a copy of the string which ends with _# where # is the
/// previous number at that position incremented by one.
/// If the original string did not end with _#, the returned
/// string will just have _1 appended to it.
///
/// The string to process
/// The original string with the number after the final _ character incremented by one
static QString IncrementTrailingUnderscoreInt(const QString& str)
{
bool ok = false;
size_t num = 0;
QString endSection;
QString ret = str;
int lastUnderscore = str.lastIndexOf('_');
if (lastUnderscore != -1)
{
endSection = str.section('_', -1);
num = endSection.toULongLong(&ok);
if (ok)
ret.chop(str.size() - lastUnderscore);
}
ret += "_" + QString::number(num + 1);
return ret;
}
///
/// Ensures a given input filename is unique by appending a count to the end.
///
/// The filename to ensure is unique
/// The passed in name if it was unique, else a uniquely made name.
static QString UniqueFilename(const QString& filename)
{
if (!QFile::exists(filename))
return filename;
QString newPath;
QFileInfo original(filename);
QString path = original.absolutePath() + '/';
QString base = original.completeBaseName();
QString extension = original.suffix();
do
{
base = IncrementTrailingUnderscoreInt(base);
newPath = path + base + "." + extension;
}
while (QFile::exists(newPath));
return newPath;
}
///
/// Return the default ember name based on the current date/time and
/// the ember's index in the file.
///
/// The index in the file of the ember
/// The default ember name
static QString DefaultEmberName(T i)
{
return DefaultFilename() + "_" + ToString(i);
}
QString m_Filename;
vector> m_Embers;
};