Made code more sane. Now allocates the exact space needed for things.

This commit is contained in:
Xnoe 2020-07-10 05:31:50 +01:00 committed by Elsie
parent 07ee41aa97
commit 95d8d76fa7

View File

@ -6,20 +6,24 @@
namespace fs = std::filesystem;
const int MAX_FILE_SIZE = 65536;
char* read(const char* name) {
std::ifstream file_stream(name, std::ios_base::binary);
int count = 0;
file_stream.seekg(0, file_stream.end);
int length = file_stream.tellg();
file_stream.seekg(0, file_stream.beg);
char* data = (char*) calloc(length, 1);
file_stream.read(data, length);
file_stream.close();
return data;
}
string32 readfile(const char* name) {
std::fstream file_stream;
file_stream.open(name);
char* data = new char [MAX_FILE_SIZE];
int count(0);
while (file_stream)
data[count++] = file_stream.get();
file_stream.close();
data[--count] = 0;
char* data = read(name);
string32 file = string32 (data);
delete data;
free (data);
return file;
}
@ -31,14 +35,16 @@ int main() {
for (const auto & file : fs::recursive_directory_iterator(path)) {
if (file.is_regular_file()) {
if (file.path().stem().string().at(0) != '_') {
string32 newFile = layout;
newFile.replace("{{ content }}", readfile(file.path().c_str()));
if (file.path().stem().string().at(0) == '_')
continue;
std::string ext = file.path().extension().string();
fs::create_directories("output" + file.path().parent_path().string().substr(6));
std::ofstream outfile("output" + file.path().string().substr(6));
outfile << newFile;
if (ext == ".html")
outfile << layout.replace("{{ content }}", readfile(file.path().c_str()));
else
outfile << read(file.path().c_str());
outfile.close();
}
}
}
}