]> git.lyx.org Git - lyx.git/blob - src/support/os_win32.C
bug 183
[lyx.git] / src / support / os_win32.C
1 // os_win32.C copyright "Ruurd A. Reitsma" <R.A.Reitsma@wbmt.tudelft.nl>
2
3 // Various OS specific functions
4 #include <config.h>
5
6 #include "os.h"
7 #include "support/filetools.h"
8 #include "support/lstrings.h"
9 #include "debug.h"
10
11 #include <windows.h>
12 #include <io.h>
13 #include <sys/cygwin.h>
14
15
16 string os::binpath_ = string();
17 string os::binname_ = string();
18 string os::tmpdir_ = string();
19 os::shell_type os::_shell = os::UNIX;
20 unsigned long os::cp_ = 0;
21
22 void os::init(int * /* argc */, char ** argv[]) {
23         static bool initialized = false;
24         if (initialized) return;
25         initialized = true;
26         string tmp = *argv[0];
27         binname_ = OnlyFilename(tmp);
28         tmp = ExpandPath(tmp); // This expands ./ and ~/
29
30         if (!is_absolute_path(tmp)) {
31                 string binsearchpath = GetEnvPath("PATH");
32                 // This will make "src/lyx" work always :-)
33                 binsearchpath += ";.";
34                 tmp = *argv[0];
35                 tmp = FileOpenSearch(binsearchpath, tmp);
36         }
37
38         tmp = MakeAbsPath(OnlyPath(tmp));
39
40         // In case we are running in place and compiled with shared libraries
41         if (suffixIs(tmp, "/.libs/"))
42                 tmp.erase(tmp.length()-6, string::npos);
43         binpath_ = tmp;
44 }
45
46 void os::warn(string mesg) {
47         MessageBox(0, mesg.c_str(), "LyX error",
48         MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL);
49         }
50
51 string os::current_root() {
52         return string("/");
53 }
54
55 string::size_type os::common_path(string const &p1, string const &p2) {
56         string::size_type i = 0,
57                         p1_len = p1.length(),
58                         p2_len = p2.length();
59         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i])) ++i;
60         if ((i < p1_len && i < p2_len)
61             || (i < p1_len && p1[i] != '/' && i == p2_len)
62             || (i < p2_len && p2[i] != '/' && i == p1_len)) {
63                 if (i) --i;     // here was the last match
64                 while (i && p1[i] != '/') --i;
65         }
66         return i;
67 }
68
69 string os::slashify_path(string p) {
70                 return subst(p, '\\', '/');
71 }
72
73 string os::external_path(string const & p) {
74         string dos_path=p;
75         if (is_absolute_path(p)) {
76                 char dp[255];
77                 cygwin_conv_to_full_win32_path(p.c_str(), dp);
78                 dos_path=subst(dp,'\\','/');
79         }
80         lyxerr[Debug::LATEX]
81                 << "<Win32 path correction> ["
82                 << p << "]->>["
83                 << dos_path << "]" << endl;
84         return dos_path;
85 }
86
87
88 // (Claus H.) Parsing the latex log file in an Win32 environment all
89 // files are mentioned in Win32/DOS syntax. Because LyX uses the dep file
90 // entries to check if any file has been changed we must retranslate
91 // the Win32/DOS pathnames into Cygwin pathnames.
92 string os::internal_path(string const &p) {
93         char pp[256];
94         cygwin_conv_to_posix_path(p.c_str(), pp);
95         string const posix_path = MakeLatexName(pp);
96         lyxerr[Debug::DEPEND]
97                 << "<Win32 path correction> ["
98                 << p << "]->>["
99                 << posix_path << "]" << endl;
100         return posix_path;
101 }
102
103 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
104 // Therefore an absolute path could be either a pathname starting
105 // with a slash (Unix) or a pathname starting with a drive letter
106 // followed by a colon. Because a colon is not valid in pathes in Unix
107 // and at another location in Win32 testing just for the existance
108 // of the colon in the 2nd position seems to be enough!
109 bool os::is_absolute_path(string const & p)
110 {
111         if (p.empty())
112                 return false;
113
114         bool isDosPath = (p.length() > 1 && p[1] == ':');
115         bool isUnixPath = (p[0] == '/');
116
117         return isDosPath | isUnixPath;
118 }