-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebsite.cpp
112 lines (95 loc) · 2.39 KB
/
Website.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Copyright(c) 2018 Daniel Arad.
//
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
// ---------------------------------------------------------------------
// File: Website.cpp
// ---------------------------------------------------------------------
//
#include "Website.h"
Website::Website(const std::string& path) : _path(path)
{
addFiles();
}
const std::vector<std::string> &Website::getTemplates() const
{
return _templates;
}
const std::string &Website::getAssets() const
{
return _assets;
}
const std::string& Website::getPath() const
{
return _path;
}
const std::map<std::string, std::string>& Website::getBadNames() const {
return _badNames;
}
void Website::addFiles()
{
std::vector<std::string> files; //files and directories
std::string fileType;
std::string fixFile;
unsigned long pos;
getFiles(files);
for(const std::string& file : files) // for(each value in vector : vector) - Added in c++11
{
fileType = getFileType(file);
if(fileType == "html")
{
pos = file.find('-');
if(pos != std::string::npos)
{
fixFile = file;
fixFile[pos] = '_';
_badNames[file] = fixFile;
}
_templates.push_back(file);
}
else if (fileType.empty() && (file == "assets" || file == "Assets"))
{
_assets = file;
}
else
{
std::cout << "Loose file: " + file << '\n';
}
std::cout << std::endl; //Space paragraph and flush buffer.
}
}
std::string Website::getFileType(const std::string& file)
{
std::size_t pos;
pos = file.find_last_of('.');
if(pos != std::string::npos)
{
return( file.substr(pos + 1, file.length()) ); //returns word AFTER dot.
}
return("");
}
void Website::getFiles(std::vector<std::string>& files)
{
std::string output; //store exec function output.
std::size_t pos;
std::string cmd = "cd " + _path + "; ls";
output = exec(cmd); //ls - shell command that lists unhidden files and directories in directory.
while( (pos = output.find('\n')) != std::string::npos )
{
files.push_back(output.substr(0, pos));
output = output.substr(pos + 1);
}
}
std::string Website::exec(const std::string& cmd) const
{
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
result += buffer.data();
}
return result;
}