PhoenixFileParser  2.0.2
Set of tools to ease file parsing
Loading...
Searching...
No Matches
directory_replace_placeholder.cpp File Reference
#include <fstream>
#include <set>
#include "directory_replace_placeholder.h"
Include dependency graph for directory_replace_placeholder.cpp:

Go to the source code of this file.

Functions

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_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 holder.
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 holder (file only).

Function Documentation

◆ directory_replace_copyBinaryFile()

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).

Parameters
inputFile: source file
outputFile: destination file
Returns
true on success, false otherwise

Definition at line 44 of file directory_replace_placeholder.cpp.

44 {
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}

Referenced by directory_replace_placeholderFile().

Here is the caller graph for this function:

◆ directory_replace_isBinaryAsset()

bool directory_replace_isBinaryAsset ( const PPath & file)

Extensions of binary/image assets that must never go through text placeholder substitution : it is meaningless on such content (no XXXPlaceholderXXX token to substitute), unsafe (PPath::saveFileContent writes through a NUL-terminated C string, so binary content gets truncated at the first embedded '\0'), and prohibitively slow on large files (PString concatenation is not amortised, so replace() on a multi-megabyte blob is effectively quadratic).

Parameters
file: file to be checked
Returns
true if the file extension is a known binary/image asset

Definition at line 21 of file directory_replace_placeholder.cpp.

21 {
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}

Referenced by directory_replace_placeholderFile().

Here is the caller graph for this function:

◆ directory_replace_placeholder()

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 holder.

Parameters
outputDir: output directory
inputConfigDir: input configuration directory
mapReplacePlaceholder: map of placeholders and values to be replaced with
Returns
true on success, false otherwise

Definition at line 85 of file directory_replace_placeholder.cpp.

85 {
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_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_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...

References directory_replace_placeholder(), and directory_replace_placeholderFile().

Referenced by directory_replace_placeholder().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ directory_replace_placeholderFile()

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 holder (file only).

Parameters
outputDir: output directory
inputConfigDir: input configuration directory
mapReplacePlaceholder: map of placeholders and values to be replaced with
Returns
true on success, false otherwise

Definition at line 59 of file directory_replace_placeholder.cpp.

59 {
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}
bool directory_replace_isBinaryAsset(const PPath &file)
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).

References directory_replace_copyBinaryFile(), and directory_replace_isBinaryAsset().

Referenced by directory_replace_placeholder().

Here is the call graph for this function:
Here is the caller graph for this function: