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