JetCore/__whiledir.cpp
2025-01-09 08:02:38 -08:00

45 lines
1.8 KiB
C++

#include "__whiledir.h"
#include "Exception.h"
#include "__mysql.h"
#include <iostream>
#include <filesystem>
#include <vector>
#include <algorithm>
namespace jet {
__whiledir::__whiledir(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, this) {
if(!keywordDefined("path"))
throw coreutils::Exception("whiledir tag must specify a path.");
if(keywordDefined("sort") && (resolveKeyword("sort") == "true")) {
std::vector<std::filesystem::directory_entry> entries;
for(auto const &entry : std::filesystem::directory_iterator(resolveKeyword("path").str()))
entries.push_back(entry);
std::sort(entries.begin(), entries.end(), [](const auto &a, const auto &b) { return a.path() < b.path(); });
for(const auto &entry : entries) {
if(keywordDefined("fullpath"))
global.variables[resolveKeyword("fullpath")] = entry.path();
if(keywordDefined("filename"))
global.variables[resolveKeyword("filename")] = entry.path().filename();
if(keywordDefined("filenamenoextension"))
global.variables[resolveKeyword("filenamenoextension")] = entry.path().stem();
processContainer(container);
container.reset();
}
} else {
for(auto const &entry : std::filesystem::directory_iterator(variables[resolveKeyword("path")].str())) {
if(keywordDefined("fullpath"))
global.variables[resolveKeyword("fullpath")] = entry.path();
if(keywordDefined("filename"))
global.variables[resolveKeyword("filename")] = entry.path().filename();
if(keywordDefined("filenamenoextension"))
global.variables[resolveKeyword("filenamenoextension")] = entry.path().stem();
processContainer(container);
container.reset();
}
}
}
}