99
1010#include < cstdlib>
1111#include < vector>
12- #include < dirent.h>
13- #include < libgen.h>
12+
13+ #include < filesystem>
14+ namespace fs = std::filesystem;
1415
1516namespace scene_rdl2 {
1617namespace rdl2 {
1718
18- using util::Args;
19+ const std::string g_raasRender (" raas_render" );
20+ const std::string g_osPathSep (" :" );
1921
20- int isMatching (const dirent* entry) {
21- std::string name = " raas_render" ;
22- if (name == std::string (entry->d_name )) {
23- return 1 ;
24- }
25-
26- return 0 ;
27- }
22+ using util::Args;
2823
2924std::string
3025DsoFinder::guessDsoPath ()
3126{
3227 std::string dsoPath = " " ;
33- dirent** nameList;
34-
28+
3529 // First, search PATH for raas_render executable
3630 const std::string pathEnv = util::getenv<std::string>(" PATH" );
3731 if (pathEnv.empty ()) {
3832 return " " ;
3933 }
40-
41- size_t found = pathEnv.find (' :' );
42- int numFound;
43- std::string path;
34+ size_t found = pathEnv.find (g_osPathSep);
35+ fs::path path;
4436 if (found == std::string::npos) { // single path
45- path = pathEnv;
46- numFound = scandir (path.c_str (), &nameList, isMatching, alphasort);
37+ path = fs::path (pathEnv).make_preferred ();
38+
39+ if (fs::exists (path)) {
40+ for (auto const & dirEntry : std::filesystem::directory_iterator (path)) {
41+ if (dirEntry.path ().filename ().string () == g_raasRender) {
42+ break ;
43+ }
44+ }
45+ }
4746 } else {
4847 int counter = 0 ;
48+ bool pathFound = false ;
4949 while (found != std::string::npos) {
50- path = pathEnv.substr (counter, found - counter);
51- numFound = scandir (path.c_str (), &nameList, isMatching, alphasort);
52- if (numFound > 0 ) {
50+ path = fs::path (pathEnv.substr (counter, found - counter)).make_preferred ();
51+ if (fs::exists (path)) {
52+ for ( const auto & dirEntry : std::filesystem::directory_iterator (path)) {
53+ std::string file = dirEntry.path ().stem ().string ();
54+ if (file == g_raasRender) {
55+ pathFound = true ;
56+ break ;
57+ }
58+ }
59+ }
60+ if (pathFound) {
5361 break ;
5462 }
5563 counter = found + 1 ;
56- found = pathEnv.find (' :' , found + 1 );
57- }
58-
59- if (numFound <= 0 ) { // Haven't found raas_render yet
60- // Process last path
61- path = pathEnv.substr (counter);
62- numFound = scandir (path.c_str (), &nameList, isMatching, alphasort);
64+ found = pathEnv.find (g_osPathSep, found + 1 );
6365 }
6466 }
65-
66- if (numFound > 0 ) {
67- // We found raas_render, now construct path to rdl2dso
68- // This assumes that the immediate parent directory is /bin
69- char * buf = realpath (path.c_str (), NULL ); // Resolve any relative links
70- dsoPath = std::string (dirname (buf)) + " /" + " rdl2dso" ;
71- free (buf);
72- }
73-
74- // clean up
75- /* while (numFound--) {
76- free(nameList[numFound]);
77- }*/
78- free (nameList);
7967
68+ if (!path.empty ()) {
69+ dsoPath = fs::path (fs::absolute (path.parent_path ()) / " rdl2dso" ).make_preferred ().string ();
70+ }
8071 return dsoPath;
8172}
8273
@@ -85,14 +76,14 @@ std::string DsoFinder::find() {
8576 std::string dsoPathString = " ." ; // Search '.' path first
8677 if (const char * const dsoPathEnvVar = util::getenv<const char *>(" RDL2_DSO_PATH" )) {
8778 // append dso path as sourced from RDL2_DSO_PATH
88- dsoPathString += " : " + std::string (dsoPathEnvVar);
79+ dsoPathString += g_osPathSep + std::string (dsoPathEnvVar);
8980 }
9081
9182 // finally, guess dso path based on location of raas_render
9283 std::string guessedDsoPath = guessDsoPath ();
9384 if (!guessedDsoPath.empty ()) {
9485 // append dso path as sourced from location of raas_render executable
95- dsoPathString += " : " + guessedDsoPath;
86+ dsoPathString += g_osPathSep + guessedDsoPath;
9687 }
9788
9889 return dsoPathString;
@@ -125,7 +116,7 @@ std::string DsoFinder::parseDsoPath(int argc, char* argv[]) {
125116
126117 if (!dsoPath.empty ()) {
127118 // prepend dso path as sourced from command line
128- return dsoPath + " : " + findPath;
119+ return dsoPath + g_osPathSep + findPath;
129120 }
130121
131122 return findPath;
0 commit comments