Skip to content

Commit 1dff8e6

Browse files
committed
better paths operations, simplified example
1 parent 6d3fbac commit 1dff8e6

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

CppAsScript.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,28 @@
44

55
int main()
66
{
7-
cppProject PR;
8-
std::string download = PR.checkIfCompilerInstalled();
7+
cppProject project;
8+
std::string download = project.checkIfCompilerInstalled();
99
if(download.length()) {
1010
std::cout << "LLVM-CL not installed, please download and install at:\n" << download << "\n";
1111
return 1;
1212
}
13-
/// the path to project, we walk up because thet exe placed somehere deeply
14-
std::filesystem::path cur = std::filesystem::current_path().parent_path().parent_path().parent_path();
13+
/// Set searh paths for files if they passed in relative form
14+
/// 3 means that we add current folder of exe file and 3 parent folders
15+
/// this is done because exe is usually deeply inside projects folder
16+
project.addSearhPath(std::filesystem::current_path(), 3);
1517

16-
/// the path to the file to be compiled and executed
17-
std::filesystem::path test = cur;
18-
test.append("test/test.cpp");
19-
20-
/// the path to API reference include folder
21-
std::filesystem::path inc = cur;
22-
inc.append("api_test/");
23-
24-
/// set project settings, pass the file to execute
25-
PR.addFile(test.string()).addIncludeFolder(inc.string()).debug();
18+
/// addFile adds the path to the file to be compiled and executed
19+
/// addIncludeFolder adds the path to API reference include folder
20+
/// debug states that debug config compiled, PDB file will be compiled as well
21+
project.addFile("test/test.cpp").addIncludeFolder("api_test/").debug();
2622

2723
/// find and execute the main
28-
auto f = PR.bind<void()>("main");
24+
auto f = project.bind<void()>("main");
2925
if (f)f();
3026

3127
/// find and execute the test123
32-
auto f1 = PR.bind<void(int)>("test123");
28+
auto f1 = project.bind<void(int)>("test123");
3329
if (f1)f1(23);
3430

3531
return 0;

cppAsScript.h

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class cppBuilder;
1616
class cppProject {
1717
protected:
1818
std::vector<std::string> files;
19+
std::vector<std::filesystem::path> paths;
1920
std::string modulePath;
2021
std::string log;
2122
std::string _options;
@@ -28,6 +29,21 @@ class cppProject {
2829
cppProject();
2930
virtual ~cppProject();
3031

32+
33+
/**
34+
* \brief Add the path where we seek for files if the relative path passed
35+
* \param path the absolute path
36+
* \param parent_depth we add the path, then add parent path and so on, repeat this parent_depth count. It is useful if executable nested somewhere deeply into project folder
37+
*/
38+
void addSearhPath(const std::filesystem::path& path, int parent_depth = 0);
39+
40+
/**
41+
* \brief determines if path is local and tries to find the file in any folder defined previously with addSearhPath
42+
* \param path relative or absolute path
43+
* \return the file path if the file exists, empty path othervice
44+
*/
45+
std::filesystem::path findFile(const std::string& path);
46+
3147
/// Compile the project (if need).
3248
void recompileIfNeed();
3349

@@ -41,7 +57,7 @@ class cppProject {
4157
cppProject& addIncludeFolder(const std::string& path);
4258

4359
/// Add the text of cpp file to be compiled into the project
44-
cppProject& addSource(const char* cpp_text);
60+
cppProject& addSource(const std::string& cpp_text);
4561

4662
/// Compile (in future) with the speed optimization
4763
cppProject& speedOptimization();
@@ -60,7 +76,7 @@ class cppProject {
6076

6177
/// Pass the function name and get the function tat may be called later (till the project is loaded).
6278
template <class F>
63-
std::function<F> bind(const char* functionName);
79+
std::function<F> bind(const std::string& functionName);
6480

6581
/// returns the compile log
6682
std::string& compileLog();
@@ -79,13 +95,13 @@ class cppProject {
7995
};
8096

8197
template <class F>
82-
std::function<F> cppProject::bind(const char* functionName) {
98+
std::function<F> cppProject::bind(const std::string& functionName) {
8399
std::function<F> f1 = nullptr;
84100
if (!ref) {
85101
recompileIfNeed();
86102
}
87103
if (ref) {
88-
f1 = static_cast<F*>(ref->raw_ptr(functionName));
104+
f1 = static_cast<F*>(ref->raw_ptr(functionName.c_str()));
89105
}
90106
return f1;
91107
}
@@ -288,6 +304,28 @@ inline cppProject::~cppProject() {
288304
ref = nullptr;
289305
}
290306

307+
inline void cppProject::addSearhPath(const std::filesystem::path& path, int parent_depth) {
308+
paths.push_back(path);
309+
if(parent_depth && path.has_parent_path()) {
310+
addSearhPath(path.parent_path(), parent_depth - 1);
311+
}
312+
}
313+
314+
inline std::filesystem::path cppProject::findFile(const std::string& path) {
315+
std::filesystem::path p = path;
316+
if(p.is_absolute()) {
317+
if (exists(p))return p;
318+
} else {
319+
for(auto p:paths) {
320+
auto pt = p;
321+
pt.append(path);
322+
if (exists(pt))return pt;
323+
}
324+
}
325+
p.clear();
326+
return p;
327+
}
328+
291329
inline bool cppProject::valid() {
292330
return ref != nullptr;
293331
}
@@ -313,16 +351,18 @@ inline std::string& cppProject::includes() {
313351
}
314352

315353
inline cppProject& cppProject::addFile(const std::string& path) {
316-
filesList().push_back(path);
354+
auto fpath = findFile(path);
355+
if (!is_empty(fpath))filesList().push_back(fpath.string());
317356
return *this;
318357
}
319358

320359
inline cppProject& cppProject::addIncludeFolder(const std::string& path) {
321-
includes() += " /I " + path;
360+
auto fpath = findFile(path);
361+
if(exists(fpath)) includes() += std::string(" /I ") + "\"" + path + "\"";
322362
return *this;
323363
}
324364

325-
inline cppProject& cppProject::addSource(const char* cpp_text) {
365+
inline cppProject& cppProject::addSource(const std::string& cpp_text) {
326366
std::string fn = "temp_" + md5::hash(cpp_text) + ".cpp";
327367
std::string res = builder->pathInHeap(fn);
328368
std::ofstream f(res);

0 commit comments

Comments
 (0)