PhoenixFileParser  2.0.2
Set of tools to ease file parsing
Loading...
Searching...
No Matches
directory_replace_placeholder.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : LGPL-3.0-only
5****************************************/
6
7
8#include <fstream>
9#include <set>
10
12
18
21bool directory_replace_isBinaryAsset(const PPath & file){
22 static const std::set<PString> setBinaryExtension{
23 //images
24 "png", "jpg", "jpeg", "gif", "bmp", "ico", "svg", "webp", "tiff", "tif",
25 //fonts
26 "woff", "woff2", "ttf", "otf", "eot",
27 //archives
28 "zip", "gz", "tar", "7z", "bz2", "xz",
29 //documents
30 "pdf",
31 //audio/video
32 "mp3", "mp4", "wav", "ogg", "avi", "mov",
33 //compiled/binary artefacts
34 "so", "dll", "dylib", "a", "o", "exe", "class", "pyc"
35 };
36 return setBinaryExtension.find(file.getExtension().toLower()) != setBinaryExtension.end();
37}
38
40
44bool directory_replace_copyBinaryFile(const PPath & inputFile, const PPath & outputFile){
45 std::ifstream in(inputFile.c_str(), std::ios::binary);
46 if(!in.is_open()){return false;}
47 std::ofstream out(outputFile.c_str(), std::ios::binary);
48 if(!out.is_open()){return false;}
49 out << in.rdbuf();
50 return static_cast<bool>(out);
51}
52
54
59bool directory_replace_placeholderFile(const PPath & outputDir, const PPath & inputConfigDir, const PMapString & mapReplacePlaceholder){
60 bool b(true);
61 PVecPath vecFileInputDir = inputConfigDir.getAllFileInDir();
62 for(PVecPath::iterator it(vecFileInputDir.begin()); it != vecFileInputDir.end(); ++it){
63 PPath relativeFile = it->replace(mapReplacePlaceholder);
64 PPath fullFile = inputConfigDir / *it;
65 PPath outputFile = outputDir / relativeFile;
67 //Binary/image assets are copied verbatim, the file name/path may still use placeholders, but the content must not be touched
68 b &= directory_replace_copyBinaryFile(fullFile, outputFile);
69 continue;
70 }
71 PString fileContent = fullFile.loadFileContent();
72 //TODO : this could be optimised with the dico_replace_var algorithm, but this is enough for now
73 PString replacedContent = fileContent.replace(mapReplacePlaceholder);
74 b &= outputFile.saveFileContent(replacedContent);
75 }
76 return b;
77}
78
80
85bool directory_replace_placeholder(const PPath & outputDir, const PPath & inputConfigDir, const PMapString & mapReplacePlaceholder){
86 if(!outputDir.createDirectory()){
87 std::cerr << "directory_replace_placeholder : cannot create dir '"<<outputDir<<"'" << std::endl;
88 return false;
89 }
90 //Let's get content of inputConfigDir
91 PVecPath vecDirInputDir = inputConfigDir.getAllDirectoryInDir();
92 bool b(true);
93 for(PVecPath::iterator itDir(vecDirInputDir.begin()); itDir != vecDirInputDir.end(); ++itDir){
94 PPath relativeDir = itDir->replace(mapReplacePlaceholder);
95 PPath fullInputDir = inputConfigDir / *itDir;
96 PPath fullOutputDir = outputDir / relativeDir;
97 b &= fullOutputDir.createDirectory();
98
99 b &= directory_replace_placeholderFile(fullOutputDir, fullInputDir, mapReplacePlaceholder);
100 b &= directory_replace_placeholder(fullOutputDir, fullInputDir, mapReplacePlaceholder);
101 }
102 b &= directory_replace_placeholderFile(outputDir, inputConfigDir, mapReplacePlaceholder);
103 if(!b){
104 std::cerr << "directory_replace_placeholder : cannot create output directories architecture" << std::endl;
105 std::cerr << "\tinputConfigDir = '"<<inputConfigDir<<"'" << std::endl;
106 std::cerr << "\toutputDir = '"<<outputDir<<"'" << std::endl;
107 return false;
108 }
109 return b;
110}
bool directory_replace_isBinaryAsset(const PPath &file)
bool directory_replace_placeholder(const PPath &outputDir, const PPath &inputConfigDir, const PMapString &mapReplacePlaceholder)
Replace placeholder in a directory and its content by respect to the given map of value for all place...
bool directory_replace_copyBinaryFile(const PPath &inputFile, const PPath &outputFile)
Copy a file byte for byte, with no text processing of any kind (safe for binary content).
bool directory_replace_placeholderFile(const PPath &outputDir, const PPath &inputConfigDir, const PMapString &mapReplacePlaceholder)
Replace placeholder in a directory and its content by respect to the given map of value for all place...