55
66namespace PathUtils
77{
8- std::string programDirectory;
9- std::string assetsDirectory;
8+ std::string programDirectory;
9+ std::string assetsDirectory;
1010}
1111
1212void PathUtils::SetProgramDirectory (const std::string& executableFilePath)
1313{
14- programDirectory = GetFolderPath (executableFilePath);
15- assetsDirectory = programDirectory + " assets/" ;
16- if (!std::filesystem::exists (assetsDirectory))
17- {
18- // asset directory is different when launching from visual studio
19- std::string parent = programDirectory;
20- parent = GetFolderPath (parent.substr (0 , parent.length () - 1 ));
21- parent = GetFolderPath (parent.substr (0 , parent.length () - 1 ));
22- parent = GetFolderPath (parent.substr (0 , parent.length () - 1 ));
14+ programDirectory = GetFolderPath (executableFilePath);
15+ assetsDirectory = programDirectory + " assets/" ;
16+ if (!std::filesystem::exists (assetsDirectory))
17+ {
18+ // asset directory is different when launching from visual studio
19+ std::string parent = programDirectory;
20+ parent = GetFolderPath (parent.substr (0 , parent.length () - 1 ));
21+ parent = GetFolderPath (parent.substr (0 , parent.length () - 1 ));
22+ parent = GetFolderPath (parent.substr (0 , parent.length () - 1 ));
2323
24- assetsDirectory = parent + " assets/" ;
25- }
26- if (!std::filesystem::exists (assetsDirectory))
27- std::cout << " [Path utils] Warning, could not locate assets folder\n " ;
28- else
29- std::cout << " [Path utils] Assets directory set to: " << assetsDirectory << std::endl;
24+ assetsDirectory = parent + " assets/" ;
25+ }
26+ if (!std::filesystem::exists (assetsDirectory))
27+ std::cout << " [Path utils] Warning, could not locate assets folder\n " ;
28+ else
29+ std::cout << " [Path utils] Assets directory set to: " << assetsDirectory << std::endl;
3030}
3131
3232const std::string& PathUtils::GetProgramDirectory ()
3333{
34- return programDirectory;
34+ return programDirectory;
3535}
3636
3737const std::string& PathUtils::GetAssetsDirectory ()
3838{
39- return assetsDirectory;
39+ return assetsDirectory;
4040}
4141
4242std::string PathUtils::GetRelativePath (const std::string& fileRelativeTo, const std::string& targetPath)
4343{
44- std::string out = " " ;
44+ std::string out = " " ;
4545
46- std::vector<std::string> pathA, pathB;
46+ std::vector<std::string> pathA, pathB;
4747
48- int i = fileRelativeTo.length () - 1 , iEnd = fileRelativeTo.length ();
49- int j = targetPath.length () - 1 , jEnd = targetPath.length ();
50- while (i > -1 )
51- {
52- if (fileRelativeTo[i] == ' /' || fileRelativeTo[i] == ' \\ ' )
53- {
54- pathA.insert (pathA.begin (), fileRelativeTo.substr (i + 1 , iEnd - i - 1 ));
55- iEnd = i;
56- }
57- i--;
58- }
59- while (j > -1 )
60- {
61- if (targetPath[j] == ' /' || targetPath[j] == ' \\ ' )
62- {
63- pathB.insert (pathB.begin (), targetPath.substr (j + 1 , jEnd - j - 1 ));
64- jEnd = j;
65- }
66- j--;
67- }
48+ int i = fileRelativeTo.length () - 1 , iEnd = fileRelativeTo.length ();
49+ int j = targetPath.length () - 1 , jEnd = targetPath.length ();
50+ while (i > -1 )
51+ {
52+ if (fileRelativeTo[i] == ' /' || fileRelativeTo[i] == ' \\ ' )
53+ {
54+ pathA.insert (pathA.begin (), fileRelativeTo.substr (i + 1 , iEnd - i - 1 ));
55+ iEnd = i;
56+ }
57+ i--;
58+ }
59+ while (j > -1 )
60+ {
61+ if (targetPath[j] == ' /' || targetPath[j] == ' \\ ' )
62+ {
63+ pathB.insert (pathB.begin (), targetPath.substr (j + 1 , jEnd - j - 1 ));
64+ jEnd = j;
65+ }
66+ j--;
67+ }
6868
69- i = 0 ; j = 0 ;
70- while (pathA[i].compare (pathB[j]) == 0 ) { i++; j++; }
69+ i = 0 ; j = 0 ;
70+ while (pathA[i].compare (pathB[j]) == 0 ) { i++; j++; }
7171
72- while (i < pathA.size () - 1 )
73- {
74- out += " ../" ;
75- i++;
76- }
77- while (j < pathB.size ())
78- {
79- out += pathB[j];
80- if (j != pathB.size () - 1 )
81- out += ' /' ;
82- j++;
83- }
84- return out;
72+ while (i < pathA.size () - 1 )
73+ {
74+ out += " ../" ;
75+ i++;
76+ }
77+ while (j < pathB.size ())
78+ {
79+ out += pathB[j];
80+ if (j != pathB.size () - 1 )
81+ out += ' /' ;
82+ j++;
83+ }
84+ return out;
8585}
8686
8787std::string PathUtils::GetFolderPath (const std::string& filePath)
8888{
89- int i = filePath.length () - 1 ;
90- for (; i > -1 && filePath[i] != ' /' && filePath[i] != ' \\ ' ; i--) {};
91- return filePath.substr (0 , i + 1 );
89+ int i = filePath.length () - 1 ;
90+ for (; i > -1 && filePath[i] != ' /' && filePath[i] != ' \\ ' ; i--) {};
91+ return filePath.substr (0 , i + 1 );
9292}
0 commit comments