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