Refactored #7 out into class files like I should have to begin with.
This commit is contained in:
parent
7ef32aafcb
commit
2139a01252
21
2022/7/File.cpp
Normal file
21
2022/7/File.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include "File.h"
|
||||
|
||||
File::File(const std::string &set_filename, unsigned long set_size)
|
||||
{
|
||||
this->size = set_size;
|
||||
this->filename = set_filename;
|
||||
}
|
||||
|
||||
File::~File()
|
||||
{
|
||||
}
|
||||
|
||||
const std::string& File::Filename() const
|
||||
{
|
||||
return filename;
|
||||
}
|
||||
|
||||
unsigned long File::Size() const
|
||||
{
|
||||
return size;
|
||||
}
|
19
2022/7/File.h
Normal file
19
2022/7/File.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef ADVENT_DVEREB_FILE_H
|
||||
#define ADVENT_DVEREB_FILE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class File {
|
||||
public:
|
||||
File(const std::string &set_filename, unsigned long set_size);
|
||||
virtual ~File();
|
||||
|
||||
const std::string& Filename() const;
|
||||
virtual unsigned long Size() const;
|
||||
|
||||
protected:
|
||||
unsigned long size;
|
||||
std::string filename;
|
||||
};
|
||||
|
||||
#endif
|
58
2022/7/Folder.cpp
Normal file
58
2022/7/Folder.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#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;
|
||||
}
|
22
2022/7/Folder.h
Normal file
22
2022/7/Folder.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef ADVENT_DVEREB_FOLDER_H
|
||||
#define ADVENT_DVEREB_FOLDER_H
|
||||
|
||||
#include "File.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Folder : public File {
|
||||
public:
|
||||
Folder(const std::string &filename);
|
||||
|
||||
unsigned long Size() const override;
|
||||
void AddFile(const std::string &filename, unsigned long size);
|
||||
void AddFolder(const std::string &name);
|
||||
const std::vector<File*>& Files() const;
|
||||
|
||||
private:
|
||||
std::vector<File*> files;
|
||||
};
|
||||
|
||||
#endif
|
@ -1 +0,0 @@
|
||||
../starter/Makefile
|
2
2022/7/Makefile
Normal file
2
2022/7/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
a.out: main.cpp File.h File.cpp Folder.h Folder.cpp SplitStr.h
|
||||
clang++ -std=c++2b -g -O0 main.cpp File.cpp Folder.cpp
|
22
2022/7/SplitStr.h
Normal file
22
2022/7/SplitStr.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef ADVENT_DVEREB_SPLITSTR_H
|
||||
#define ADVENT_DVEREB_SPLITSTR_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
std::vector<std::string> SplitStr(std::string str, const std::string &delim)
|
||||
{
|
||||
std::vector<std::string> rtn;
|
||||
|
||||
for(auto split = str.find(delim); split != std::string::npos; split = str.find(delim))
|
||||
{
|
||||
rtn.push_back(str.substr(0, split));
|
||||
str = str.substr(split + delim.size()); // chop off beginning
|
||||
}
|
||||
if(str.size())
|
||||
rtn.push_back(str);
|
||||
|
||||
return rtn;
|
||||
}
|
||||
|
||||
#endif
|
116
2022/7/main.cpp
116
2022/7/main.cpp
@ -1,112 +1,13 @@
|
||||
#include "Folder.h"
|
||||
#include "SplitStr.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> SplitStr(std::string str, const std::string &delim)
|
||||
{
|
||||
std::vector<std::string> rtn;
|
||||
|
||||
for(auto split = str.find(delim); split != std::string::npos; split = str.find(delim))
|
||||
{
|
||||
rtn.push_back(str.substr(0, split));
|
||||
str = str.substr(split + delim.size()); // chop off beginning
|
||||
}
|
||||
if(str.size())
|
||||
rtn.push_back(str);
|
||||
|
||||
return rtn;
|
||||
}
|
||||
|
||||
class File {
|
||||
public:
|
||||
File(const std::string &set_filename, unsigned long set_size)
|
||||
{
|
||||
this->size = set_size;
|
||||
this->filename = set_filename;
|
||||
}
|
||||
|
||||
virtual ~File()
|
||||
{
|
||||
}
|
||||
|
||||
const std::string& Filename() const
|
||||
{
|
||||
return filename;
|
||||
}
|
||||
virtual unsigned long Size() const
|
||||
{
|
||||
return size;
|
||||
}
|
||||
|
||||
protected:
|
||||
unsigned long size;
|
||||
std::string filename;
|
||||
};
|
||||
|
||||
class Folder : public File {
|
||||
public:
|
||||
Folder(const std::string &filename)
|
||||
: File(filename, 0)
|
||||
{
|
||||
}
|
||||
|
||||
unsigned long Size() const override
|
||||
{
|
||||
unsigned long size = 0;
|
||||
for(const auto *file : files)
|
||||
size += file->Size();
|
||||
return size;
|
||||
}
|
||||
|
||||
void 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 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*>& Files() const
|
||||
{
|
||||
return files;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<File*> files;
|
||||
};
|
||||
|
||||
void DebugOutput(File *file)
|
||||
{
|
||||
static int indent = -1;
|
||||
@ -169,16 +70,15 @@ void PartTwo(File *file, unsigned long at_least, File *&smallest)
|
||||
return;
|
||||
}
|
||||
|
||||
int main()
|
||||
void Parse(Folder &root)
|
||||
{
|
||||
std::ifstream ifs("data.txt");
|
||||
if(!ifs.is_open())
|
||||
{
|
||||
std::cerr << "Missing data.txt." << std::endl;
|
||||
return -1;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
Folder root("/");
|
||||
std::stack<Folder*> dir_stack;
|
||||
dir_stack.push(&root);
|
||||
|
||||
@ -238,6 +138,12 @@ int main()
|
||||
dir_stack.top()->AddFile(values[1], std::atol(values[0].c_str()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Folder root("/");
|
||||
Parse(root);
|
||||
|
||||
// Step 1.5: Debug output:
|
||||
DebugOutput(&root);
|
||||
|
Loading…
Reference in New Issue
Block a user