]> git.lyx.org Git - lyx.git/blob - src/support/os_win32.C
Re-enable compilation with Cygwin.
[lyx.git] / src / support / os_win32.C
1 /**
2  * \file os_win32.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Various OS specific functions
11  */
12
13 #include <config.h>
14
15 #include "support/os.h"
16 #include "support/lstrings.h"
17
18 #include "debug.h"
19
20 #include <windows.h>
21 #include <io.h>
22
23 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
24 # include <sys/cygwin.h>
25
26 #elif defined(_WIN32)
27 # include <direct.h> // _getdrive
28 #endif
29
30 using std::endl;
31 using std::string;
32
33
34 namespace lyx {
35 namespace support {
36 namespace os {
37
38 void init(int, char *[])
39 {}
40
41
42 string current_root()
43 {
44 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
45         return string("/");
46
47 #else
48         // _getdrive returns the current drive (1=A, 2=B, and so on).
49         char const drive = ::_getdrive() + 'A' - 1;
50         return string(1, drive) + ":/";
51 #endif
52 }
53
54
55 string::size_type common_path(string const & p1, string const & p2)
56 {
57         string::size_type i = 0;
58         string::size_type       p1_len = p1.length();
59         string::size_type       p2_len = p2.length();
60         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
61                 ++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         {
66                 if (i)
67                         --i;     // here was the last match
68                 while (i && p1[i] != '/')
69                         --i;
70         }
71         return i;
72 }
73
74
75 string external_path(string const & p)
76 {
77         string dos_path;
78
79 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
80         // Translate from cygwin path syntax to dos path syntax
81         if (is_absolute_path(p)) {
82                 char dp[PATH_MAX];
83                 cygwin_conv_to_full_win32_path(p.c_str(), dp);
84                 dos_path = !dp ? "" : dp;
85         }
86
87         else return p;
88 #else // regular Win32
89         dos_path = p;
90 #endif
91         
92         //No backslashes in LaTeX files
93         dos_path = subst(dos_path,'\\','/');
94
95         lyxerr[Debug::LATEX]
96                 << "<Win32 path correction> ["
97                 << p << "]->>["
98                 << dos_path << ']' << endl;
99         return dos_path;
100 }
101
102
103 // (Claus H.) Parsing the latex log file in an Win32 environment all
104 // files are mentioned in Win32/DOS syntax. Because LyX uses the dep file
105 // entries to check if any file has been changed we must retranslate
106 // the Win32/DOS pathnames into Cygwin pathnames.
107 string internal_path(string const & p)
108 {
109 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
110         char posix_path[PATH_MAX];
111         posix_path[0] = '\0';
112         cygwin_conv_to_posix_path(p.c_str(), posix_path);
113         return posix_path;
114 #else
115         return subst(p,"\\","/");
116 #endif
117 }
118
119
120 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
121 // Therefore an absolute path could be either a pathname starting
122 // with a slash (Unix) or a pathname starting with a drive letter
123 // followed by a colon. Because a colon is not valid in pathes in Unix
124 // and at another location in Win32 testing just for the existance
125 // of the colon in the 2nd position seems to be enough!
126 bool is_absolute_path(string const & p)
127 {
128         if (p.empty())
129                 return false;
130
131         bool isDosPath = (p.length() > 1 && p[1] == ':');
132         bool isUnixPath = (p[0] == '/');
133
134         return isDosPath || isUnixPath;
135 }
136
137
138 // returns a string suitable to be passed to popen when
139 // reading a pipe
140 char const * popen_read_mode()
141 {
142         return "r";
143 }
144
145
146 string const & nulldev()
147 {
148 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
149         static string const nulldev_ = "/dev/null";
150 #else
151         static string const nulldev_ = "nul";
152 #endif
153         return nulldev_;
154 }
155
156
157 shell_type shell()
158 {
159 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
160         return UNIX;
161 #else
162         return CMD_EXE;
163 #endif
164 }
165
166 } // namespace os
167 } // namespace support
168 } // namespace lyx