Advent-of-Code/2022/7/Folder.cpp

59 lines
1.3 KiB
C++
Raw Permalink Normal View History

#include "Folder.h"
#include <algorithm>
#include <iostream>
Folder::Folder(const std::string &filename)
: File(filename, 0)
{
}
unsigned long Folder::Size() const
{
unsigned long size = 0;
for(const auto *file : files)
size += file->Size();
return size;
}
void Folder::AddFile(const std::string &filename,
unsigned long size)
{
// make sure the file doesn't already exist:
if(std::find_if(files.begin(), files.end(),
[&](const File *file) {
return file->Filename() == filename;
}) != files.end())
{
std::cerr << "Folder \"" << this->Filename() << "\" already contains "
<< "the file \"" << filename << "\"." << std::endl;
exit(-1);
}
// increment this folder's size total:
this->size += size;
// add the file:
files.push_back(new File(filename, size));
}
void Folder::AddFolder(const std::string &name)
{
if(std::find_if(files.begin(), files.end(),
[&](const File *file) {
return file->Filename() == name;
}) != files.end())
{
std::cout << "NOTE: Folder \"" << this->Filename() << "\" already contains "
<< "the subfolder \"" << name << "\"." << std::endl;
// exit(-1);
}
files.push_back(new Folder(name));
}
const std::vector<File*>& Folder::Files() const
{
return files;
}