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
8 * Full author contact details are available in file CREDITS.
10 * Various OS specific functions
15 #include "support/os.h"
16 #include "support/docstring.h"
17 #include "support/FileName.h"
18 #include "support/lstrings.h"
24 #include <Carbon/Carbon.h>
33 void init(int, char *[])
43 bool isFilesystemCaseSensitive()
53 docstring::size_type common_path(docstring const & p1, docstring const & p2)
55 docstring::size_type i = 0;
56 docstring::size_type const p1_len = p1.length();
57 docstring::size_type const p2_len = p2.length();
59 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
62 while (i < p1_len && i < p2_len && p1[i] == p2[i])
65 if ((i < p1_len && i < p2_len)
66 || (i < p1_len && p1[i] != '/' && i == p2_len)
67 || (i < p2_len && p2[i] != '/' && i == p1_len))
70 --i; // here was the last match
71 while (i && p1[i] != '/')
78 bool path_prefix_is(string const & path, string const & pre)
81 return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
83 return prefixIs(path, pre);
88 bool path_prefix_is(string & path, string const & pre, path_case how)
91 docstring const p1 = from_utf8(path);
92 docstring const p2 = from_utf8(pre);
93 docstring::size_type const p1_len = p1.length();
94 docstring::size_type const p2_len = p2.length();
95 docstring::size_type common_len = common_path(p1, p2);
97 if (p2[p2_len - 1] == '/' && p1_len != p2_len)
100 if (common_len != p2_len)
103 if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
104 if (p1_len < common_len)
105 path = to_utf8(p2.substr(0, p1_len));
107 path = to_utf8(p2 + p1.substr(common_len,
108 p1_len - common_len));
113 // silence compiler warnings
116 return prefixIs(path, pre);
121 string external_path(string const & p)
127 string internal_path(string const & p)
133 string safe_internal_path(string const & p, file_access)
139 string external_path_list(string const & p)
145 string internal_path_list(string const & p)
151 string latex_path(string const & p)
157 bool is_valid_strftime(string const & p)
159 string::size_type pos = p.find_first_of('%');
160 while (pos != string::npos) {
161 if (pos + 1 == string::npos)
163 if (!containsOnly(p.substr(pos + 1, 1),
164 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
166 if (pos + 2 == string::npos)
168 pos = p.find_first_of('%', pos + 2);
174 char const * popen_read_mode()
180 string const & nulldev()
182 static string const nulldev_ = "/dev/null";
187 bool is_terminal(io_channel channel)
189 return isatty(channel);
199 char path_separator()
205 void windows_style_tex_paths(bool)
208 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
211 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
212 CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
213 (UInt8 *) ext.c_str(), ext.length(),
214 kCFStringEncodingISOLatin1, false);
215 // this is what we would like to do but it seems that the
216 // viewer for PDF is often quicktime...
217 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
219 LSRolesMask role = kLSRolesAll;
222 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
223 cfs_ext, role, &outAppRef, NULL);
226 return status != kLSApplicationNotFoundErr;
228 // silence compiler warnings
232 // currently, no default viewer is tried for non-windows system
233 // support for KDE/Gnome/Macintosh may be added later
239 bool autoOpenFile(string const & filename, auto_open_mode const mode)
242 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
245 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
249 // this is what we would like to do but it seems that the
250 // viewer for PDF is often quicktime...
251 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
253 LSRolesMask role = kLSRolesAll;
256 status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
257 if (status == kLSApplicationNotFoundErr)
260 LSLaunchFSRefSpec inLaunchSpec;
261 inLaunchSpec.appRef = &outAppRef;
262 inLaunchSpec.numDocs = 1;
263 inLaunchSpec.itemRefs = &fileref;
264 inLaunchSpec.passThruParams = NULL;
265 inLaunchSpec.launchFlags = kLSLaunchDefaults;
266 inLaunchSpec.asyncRefCon = NULL;
267 status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
269 return status != kLSApplicationNotFoundErr;
271 // silence compiler warnings
275 // currently, no default viewer is tried for non-windows system
276 // support for KDE/Gnome/Macintosh may be added later
282 string real_path(string const & path)
284 char rpath[PATH_MAX + 1];
285 char * result = realpath(path.c_str(), rpath);
286 return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
290 } // namespace support