99
1010#include < cstdlib>
1111#include < vector>
12- # include < dirent.h >
13- #include < libgen.h >
12+
13+ #include < filesystem >
1414
1515namespace scene_rdl2 {
1616namespace rdl2 {
1717
18- using util::Args;
18+ static const std::string sRaasRender (" raas_render" );
19+ static const std::string sOsPathSep (" :" );
1920
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- }
21+ using util::Args;
2822
2923std::string
3024DsoFinder::guessDsoPath ()
3125{
3226 std::string dsoPath = " " ;
33- dirent** nameList;
34-
27+
3528 // First, search PATH for raas_render executable
3629 const std::string pathEnv = util::getenv<std::string>(" PATH" );
3730 if (pathEnv.empty ()) {
3831 return " " ;
3932 }
40-
41- size_t found = pathEnv.find (' :' );
42- int numFound;
43- std::string path;
33+ size_t found = pathEnv.find (sOsPathSep );
34+ std::filesystem::path path;
4435 if (found == std::string::npos) { // single path
45- path = pathEnv;
46- numFound = scandir (path.c_str (), &nameList, isMatching, alphasort);
36+ path = std::filesystem::path (pathEnv).make_preferred ();
37+
38+ if (std::filesystem::exists (path)) {
39+ for (auto const & dirEntry : std::filesystem::directory_iterator (path)) {
40+ if (dirEntry.path ().filename ().string () == sRaasRender ) {
41+ break ;
42+ }
43+ }
44+ }
4745 } else {
4846 int counter = 0 ;
47+ bool pathFound = false ;
4948 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 ) {
49+ path = std::filesystem::path (pathEnv.substr (counter, found - counter)).make_preferred ();
50+ if (std::filesystem::exists (path)) {
51+ for ( const auto & dirEntry : std::filesystem::directory_iterator (path)) {
52+ std::string file = dirEntry.path ().stem ().string ();
53+ if (file == sRaasRender ) {
54+ pathFound = true ;
55+ break ;
56+ }
57+ }
58+ }
59+ if (pathFound) {
5360 break ;
5461 }
5562 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);
63+ found = pathEnv.find (sOsPathSep , found + 1 );
6364 }
6465 }
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);
7966
67+ if (!path.empty ()) {
68+ dsoPath = std::filesystem::path (std::filesystem::absolute (path.parent_path ()) / " rdl2dso" ).make_preferred ().string ();
69+ }
8070 return dsoPath;
8171}
8272
@@ -85,14 +75,14 @@ std::string DsoFinder::find() {
8575 std::string dsoPathString = " ." ; // Search '.' path first
8676 if (const char * const dsoPathEnvVar = util::getenv<const char *>(" RDL2_DSO_PATH" )) {
8777 // append dso path as sourced from RDL2_DSO_PATH
88- dsoPathString += " : " + std::string (dsoPathEnvVar);
78+ dsoPathString += sOsPathSep + std::string (dsoPathEnvVar);
8979 }
9080
9181 // finally, guess dso path based on location of raas_render
9282 std::string guessedDsoPath = guessDsoPath ();
9383 if (!guessedDsoPath.empty ()) {
9484 // append dso path as sourced from location of raas_render executable
95- dsoPathString += " : " + guessedDsoPath;
85+ dsoPathString += sOsPathSep + guessedDsoPath;
9686 }
9787
9888 return dsoPathString;
@@ -125,7 +115,7 @@ std::string DsoFinder::parseDsoPath(int argc, char* argv[]) {
125115
126116 if (!dsoPath.empty ()) {
127117 // prepend dso path as sourced from command line
128- return dsoPath + " : " + findPath;
118+ return dsoPath + sOsPathSep + findPath;
129119 }
130120
131121 return findPath;
0 commit comments