]> git.lyx.org Git - lyx.git/blob - src/support/os_win32.C
Actually move the OS2-specific block to make the code more readable too.
[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 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
76 namespace {
77
78 bool cygwin_path_fix_ = false;
79
80 } // namespace anon
81 #endif
82
83
84 string external_path(string const & p)
85 {
86         string dos_path;
87
88 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
89         // Translate from cygwin path syntax to dos path syntax
90         if (cygwin_path_fix_ && is_absolute_path(p)) {
91                 char dp[PATH_MAX];
92                 cygwin_conv_to_full_win32_path(p.c_str(), dp);
93                 dos_path = !dp ? "" : dp;
94         }
95
96         else return p;
97 #else // regular Win32
98         dos_path = p;
99 #endif
100         
101         //No backslashes in LaTeX files
102         dos_path = subst(dos_path,'\\','/');
103
104         lyxerr[Debug::LATEX]
105                 << "<Win32 path correction> ["
106                 << p << "]->>["
107                 << dos_path << ']' << endl;
108         return dos_path;
109 }
110
111
112 // (Claus H.) Parsing the latex log file in an Win32 environment all
113 // files are mentioned in Win32/DOS syntax. Because LyX uses the dep file
114 // entries to check if any file has been changed we must retranslate
115 // the Win32/DOS pathnames into Cygwin pathnames.
116 string internal_path(string const & p)
117 {
118 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
119         char posix_path[PATH_MAX];
120         posix_path[0] = '\0';
121         cygwin_conv_to_posix_path(p.c_str(), posix_path);
122         return posix_path;
123 #else
124         return subst(p,"\\","/");
125 #endif
126 }
127
128
129 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
130 // Therefore an absolute path could be either a pathname starting
131 // with a slash (Unix) or a pathname starting with a drive letter
132 // followed by a colon. Because a colon is not valid in pathes in Unix
133 // and at another location in Win32 testing just for the existance
134 // of the colon in the 2nd position seems to be enough!
135 bool is_absolute_path(string const & p)
136 {
137         if (p.empty())
138                 return false;
139
140         bool isDosPath = (p.length() > 1 && p[1] == ':');
141         bool isUnixPath = (p[0] == '/');
142
143         return isDosPath || isUnixPath;
144 }
145
146
147 // returns a string suitable to be passed to popen when
148 // reading a pipe
149 char const * popen_read_mode()
150 {
151         return "r";
152 }
153
154
155 string const & nulldev()
156 {
157 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
158         static string const nulldev_ = "/dev/null";
159 #else
160         static string const nulldev_ = "nul";
161 #endif
162         return nulldev_;
163 }
164
165
166 shell_type shell()
167 {
168 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
169         return UNIX;
170 #else
171         return CMD_EXE;
172 #endif
173 }
174
175
176 char path_separator()
177 {
178 #if defined (_WIN32)
179         return ';';
180 #else // Cygwin
181         return ':';
182 #endif
183 }
184
185
186 void cygwin_path_fix(bool use_cygwin_paths)
187 {
188 #if defined (_WIN32)
189         // Silence warning.
190         (void)use_cygwin_paths;
191 #else // Cygwin
192         use_cygwin_paths_ = use_cygwin_paths;
193 #endif
194 }
195
196 } // namespace os
197 } // namespace support
198 } // namespace lyx