3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Ruurd A. Reitsma
7 * \author Claus Hentschel
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
12 * Various OS specific functions
17 #include "support/os.h"
18 #include "support/lstrings.h"
25 #include <sys/cygwin.h>
30 using lyx::support::contains;
37 void os::init(int, char *[])
47 string::size_type common_path(string const & p1, string const & p2)
49 string::size_type i = 0;
50 string::size_type p1_len = p1.length();
51 string::size_type p2_len = p2.length();
52 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
54 if ((i < p1_len && i < p2_len)
55 || (i < p1_len && p1[i] != '/' && i == p2_len)
56 || (i < p2_len && p2[i] != '/' && i == p1_len))
59 --i; // here was the last match
60 while (i && p1[i] != '/')
69 bool cygwin_path_fix_ = false;
71 // In both is_posix_path() and is_windows_path() it is assumed that
72 // a valid posix or pseudo-windows path is passed. They simply tell
73 // whether the path looks posix/pseudo-windows or not.
75 bool is_posix_path(string const & p)
78 (!contains(p, '\\') && (p.length() <= 1 || p[1] != ':'));
81 // This is a test for a win32 style path with forward slashes (pseudo-windows).
83 bool is_windows_path(string const & p)
86 (!contains(p, '\\') && (p.length() <= 1 || p[1] == ':'));
96 string convert_path(string const & p, PathStyle const & target)
98 char path_buf[PATH_MAX];
100 if ((target == posix && is_posix_path(p)) ||
101 (target == windows && is_windows_path(p)))
107 cygwin_conv_to_posix_path(p.c_str(), path_buf);
109 cygwin_conv_to_win32_path(p.c_str(), path_buf);
111 return subst(path_buf[0] ? path_buf : p, '\\', '/');
115 string convert_path_list(string const & p, PathStyle const & target)
117 char const * const pc = p.c_str();
118 PathStyle const actual = cygwin_posix_path_list_p(pc) ? posix : windows;
120 if (target != actual) {
121 int const target_size = (target == posix) ?
122 cygwin_win32_to_posix_path_list_buf_size(pc) :
123 cygwin_posix_to_win32_path_list_buf_size(pc);
125 char * ptr = new char[target_size];
129 cygwin_win32_to_posix_path_list(pc, ptr);
131 cygwin_posix_to_win32_path_list(pc, ptr);
133 string path_list = subst(ptr, '\\', '/');
139 return subst(p, '\\', '/');
145 string external_path(string const & p)
147 return convert_path(p, cygwin_path_fix_ ? PathStyle(windows)
152 string internal_path(string const & p)
154 return convert_path(p, PathStyle(posix));
158 string external_path_list(string const & p)
160 return convert_path_list(p, cygwin_path_fix_ ? PathStyle(windows)
165 string internal_path_list(string const & p)
167 return convert_path_list(p, PathStyle(posix));
171 string latex_path(string const & p)
173 // We may need a posix style path or a windows style path (depending
174 // on cygwin_path_fix_), but we use always forward slashes, since it
175 // gets written into a .tex file.
177 if (cygwin_path_fix_ && is_absolute_path(p)) {
178 string dos_path = convert_path(p, PathStyle(windows));
180 << "<Cygwin path correction> ["
182 << dos_path << ']' << endl;
186 return convert_path(p, PathStyle(posix));
190 bool is_absolute_path(string const & p)
195 bool isDosPath = (p.length() > 1 && p[1] == ':');
196 bool isUnixPath = (p[0] == '/');
198 return isDosPath || isUnixPath;
202 // returns a string suitable to be passed to popen when
204 char const * popen_read_mode()
210 string const & nulldev()
212 static string const nulldev_ = "/dev/null";
223 char path_separator()
229 void cygwin_path_fix(bool use_cygwin_paths)
231 cygwin_path_fix_ = use_cygwin_paths;
235 } // namespace support