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