]> git.lyx.org Git - features.git/blob - src/support/os_cygwin.cpp
Speed up FileName operator==, such that working with child documents on
[features.git] / src / support / os_cygwin.cpp
1 /**
2  * \file os_cygwin.cpp
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  * \author Claus Hentschel
8  * \author Angus Leeming
9  * \author Enrico Forestieri
10  *
11  * Full author contact details are available in file CREDITS.
12  *
13  * Various OS specific functions
14  */
15
16 #include <config.h>
17
18 #include "support/os.h"
19
20 #include "support/FileName.h"
21 #include "support/lstrings.h"
22 #include "support/debug.h"
23
24 #include <windows.h>
25 #include <io.h>
26 #include <windef.h>
27 #include <shellapi.h>
28 #include <shlwapi.h>
29 #include <limits.h>
30 #include <sys/cygwin.h>
31 #include <sys/stat.h>
32
33 using namespace std;
34
35 namespace lyx {
36 namespace support {
37 namespace os {
38
39 namespace {
40
41 bool windows_style_tex_paths_ = false;
42
43 // In both is_posix_path() and is_windows_path() it is assumed that
44 // a valid posix or pseudo-windows path is passed. They simply tell
45 // whether the path looks posix/pseudo-windows or not.
46
47 bool is_posix_path(string const & p)
48 {
49         return  p.empty() ||
50                 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
51 }
52
53 // This is a test for a win32 style path with forward slashes (pseudo-windows).
54
55 bool is_windows_path(string const & p)
56 {
57         return p.empty() || (!contains(p, '\\') && p[0] != '/');
58 }
59
60
61 enum PathStyle {
62         posix,
63         windows
64 };
65
66
67 /// Convert a path to or from posix style.
68 /// \p p is encoded in local 8bit encoding or utf8.
69 /// The result is returned in the same encoding as \p p.
70 string convert_path(string const & p, PathStyle const & target)
71 {
72         char path_buf[PATH_MAX];
73
74         if ((target == posix && is_posix_path(p)) ||
75             (target == windows && is_windows_path(p)))
76                 return p;
77
78         path_buf[0] = '\0';
79
80         // cygwin_conv_to_posix_path and cygwin_conv_to_win32_path do not
81         // care about the encoding.
82         if (target == posix)
83                 cygwin_conv_to_posix_path(p.c_str(), path_buf);
84         else
85                 cygwin_conv_to_win32_path(p.c_str(), path_buf);
86
87         return subst(path_buf[0] ? path_buf : p, '\\', '/');
88 }
89
90
91 /// Convert a path list to or from posix style.
92 /// \p p is encoded in local 8bit encoding or utf8.
93 /// The result is returned in the same encoding as \p p.
94 string convert_path_list(string const & p, PathStyle const & target)
95 {
96         if (p.empty())
97                 return p;
98
99         char const * const pc = p.c_str();
100         PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
101
102         if (target != actual) {
103                 int const target_size = (target == posix) ?
104                                 cygwin_win32_to_posix_path_list_buf_size(pc) :
105                                 cygwin_posix_to_win32_path_list_buf_size(pc);
106
107                 char * ptr = new char[target_size];
108
109                 if (ptr) {
110                         // FIXME: See comment in convert_path() above
111                         if (target == posix)
112                                 cygwin_win32_to_posix_path_list(pc, ptr);
113                         else
114                                 cygwin_posix_to_win32_path_list(pc, ptr);
115
116                         string path_list = subst(ptr, '\\', '/');
117                         delete ptr;
118                         return path_list;
119                 }
120         }
121
122         return subst(p, '\\', '/');
123 }
124
125 } // namespace anon
126
127 void init(int, char *[])
128 {
129         // Make sure that the TEMP variable is set
130         // and sync the Windows environment.
131
132         setenv("TEMP", "/tmp", false);
133         cygwin_internal(CW_SYNC_WINENV);
134 }
135
136
137 string current_root()
138 {
139         return string("/");
140 }
141
142
143 bool isFilesystemCaseSensitive()
144 {
145         return false;
146 }
147
148
149 docstring::size_type common_path(docstring const & p1, docstring const & p2)
150 {
151         docstring::size_type i = 0;
152         docstring::size_type const p1_len = p1.length();
153         docstring::size_type const p2_len = p2.length();
154         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
155                 ++i;
156         if ((i < p1_len && i < p2_len)
157             || (i < p1_len && p1[i] != '/' && i == p2_len)
158             || (i < p2_len && p2[i] != '/' && i == p1_len))
159         {
160                 if (i)
161                         --i;     // here was the last match
162                 while (i && p1[i] != '/')
163                         --i;
164         }
165         return i;
166 }
167
168
169 string external_path(string const & p)
170 {
171         return convert_path(p, PathStyle(posix));
172 }
173
174
175 string internal_path(string const & p)
176 {
177         return convert_path(p, PathStyle(posix));
178 }
179
180
181 string external_path_list(string const & p)
182 {
183         return convert_path_list(p, PathStyle(posix));
184 }
185
186
187 string internal_path_list(string const & p)
188 {
189         return convert_path_list(p, PathStyle(posix));
190 }
191
192
193 string latex_path(string const & p)
194 {
195         // We may need a posix style path or a windows style path (depending
196         // on windows_style_tex_paths_), but we use always forward slashes,
197         // since it gets written into a .tex file.
198
199         if (windows_style_tex_paths_ && FileName(p).isAbsolute()) {
200                 string dos_path = convert_path(p, PathStyle(windows));
201                 LYXERR(Debug::LATEX, "<Path correction for LaTeX> ["
202                         << p << "]->>[" << dos_path << ']');
203                 return dos_path;
204         }
205
206         return convert_path(p, PathStyle(posix));
207 }
208
209
210 bool is_valid_strftime(string const & p)
211 {
212         string::size_type pos = p.find_first_of('%');
213         while (pos != string::npos) {
214                 if (pos + 1 == string::npos)
215                         break;
216                 if (!containsOnly(p.substr(pos + 1, 1),
217                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
218                         return false;
219                 if (pos + 2 == string::npos)
220                       break;
221                 pos = p.find_first_of('%', pos + 2);
222         }
223         return true;
224 }
225
226
227 // returns a string suitable to be passed to popen when
228 // reading a pipe
229 char const * popen_read_mode()
230 {
231         return "r";
232 }
233
234
235 string const & nulldev()
236 {
237         static string const nulldev_ = "/dev/null";
238         return nulldev_;
239 }
240
241
242 shell_type shell()
243 {
244         return UNIX;
245 }
246
247
248 char path_separator()
249 {
250         return ':';
251 }
252
253
254 void windows_style_tex_paths(bool use_windows_paths)
255 {
256         windows_style_tex_paths_ = use_windows_paths;
257 }
258
259
260 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
261 {
262         if (ext.empty())
263                 return false;
264
265         string const full_ext = "." + ext;
266
267         DWORD bufSize = MAX_PATH + 100;
268         TCHAR buf[MAX_PATH + 100];
269         // reference: http://msdn.microsoft.com/en-us/library/bb773471.aspx
270         char const * action = (mode == VIEW) ? "open" : "edit";
271         return S_OK == AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN,
272                 ASSOCSTR_EXECUTABLE, full_ext.c_str(), action, buf, &bufSize);
273 }
274
275
276 bool autoOpenFile(string const & filename, auto_open_mode const mode)
277 {
278         // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
279         string const win_path = to_local8bit(from_utf8(convert_path(filename, PathStyle(windows))));
280         char const * action = (mode == VIEW) ? "open" : "edit";
281         return reinterpret_cast<int>(ShellExecute(NULL, action,
282                 win_path.c_str(), NULL, NULL, 1)) > 32;
283 }
284
285
286 bool isSameFile(string const & fileone, string const & filetwo)
287 {
288         struct stat st1;
289         struct stat st2;
290
291         if (::stat(fileone.c_str(), &st1) == 0
292             && ::stat(filetwo.c_str(), &st2) == 0) {
293                 return st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev;
294         }
295
296         // One or both files cannot be accessed.
297         return false;
298 }
299
300 } // namespace os
301 } // namespace support
302 } // namespace lyx