Debian Codegraph @

CodeLite/clFileName.cpp

raw
1 #include "clFileName.hpp"
2
3 #include "file_logger.h"
4 #include "fileutils.h"
5 #include "procutils.h"
6
7 namespace
8 {
9 /// helper method:
10 /// run `uname -s` command and cache the output
11 const wxString& __uname()
12 {
13 static wxString uname_output;
14 static bool firstTime = true;
15
16 if(firstTime) {
17 firstTime = false;
18 wxFileName uname;
19 if(FileUtils::FindExe("uname", uname)) {
20 firstTime = false;
21 clDEBUG() << "Running `uname -s`..." << endl;
22 wxString cmd;
23 cmd << uname.GetFullPath();
24 if(cmd.Contains(" ")) {
25 cmd.Prepend("\"").Append("\"");
26 }
27 cmd << " -s";
28 uname_output = ProcUtils::SafeExecuteCommand(cmd);
29 clDEBUG() << uname_output << endl;
30 }
31 }
32 return uname_output;
33 }
34
35 bool is_cygwin_env()
36 {
37 #ifdef __WXMSW__
38 wxString uname_output = __uname();
39 return uname_output.Lower().Contains("CYGWIN_NT");
40 #else
41 return false;
42 #endif
43 }
44 } // namespace
45
46 clFileName::clFileName() {}
47
48 clFileName::clFileName(const wxString& fullpath)
49 : wxFileName(FromCygwin(fullpath))
50 {
51 }
52
53 clFileName::clFileName(const wxString& dir, const wxString& name)
54 : wxFileName(FromCygwin(dir + wxFileName::GetPathSeparator() + name))
55 {
56 }
57
58 clFileName::~clFileName() {}
59
60 wxString clFileName::FromCygwin(const wxString& fullpath)
61 {
62 if(!is_cygwin_env()) {
63 return fullpath;
64 }
65 wxString tmppath;
66 if(fullpath.StartsWith("/cygdrive/")) {
67 // "/cygdrive/c/Users/eran/devl/codelite/CodeLite/clFileName.cpp"
68 tmppath = fullpath;
69 tmppath.Replace("/cygdrive/", "");
70 if(tmppath.IsEmpty()) {
71 return fullpath;
72 }
73
74 // c/Users/eran/devl/codelite/CodeLite/clFileName.cpp
75 wxString drive = tmppath[0];
76 tmppath.erase(0, 1);
77 // /Users/eran/devl/codelite/CodeLite/clFileName.cpp
78 tmppath.Prepend(drive.Upper() + ":");
79 // C:/Users/eran/devl/codelite/CodeLite/clFileName.cpp
80 return wxFileName(tmppath).GetFullPath();
81 }
82 return fullpath;
83 }
84
85 wxString clFileName::ToCygwin(const wxString& fullpath) { return ToCygwin(wxFileName(fullpath)); }
86 wxString clFileName::ToCygwin(const wxFileName& fullpath)
87 {
88 if(!is_cygwin_env()) {
89 return fullpath.GetFullPath();
90 }
91 wxFileName fn(fullpath);
92 if(fn.GetVolume().empty()) {
93 // non windows path
94 return fullpath.GetFullPath();
95 }
96 wxString drive = fn.GetVolume();
97 wxString cygwin_path = fn.GetFullPath();
98 if(cygwin_path.size() < 2) {
99 return fullpath.GetFullPath();
100 }
101
102 cygwin_path.erase(0, 2); // C:
103 cygwin_path.Replace("\\", "/");
104 cygwin_path.Prepend("/cygdrive/" + drive.Lower());
105 return cygwin_path;
106 }