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] != '/')
80 bool path_prefix_is(string const & path, string const & pre)
84 return path_prefix_is(tmp, pre, CASE_UNCHANGED);
86 return prefixIs(path, pre);
91 bool path_prefix_is(string & path, string const & pre, path_case how)
94 docstring const p1 = from_utf8(path);
95 docstring const p2 = from_utf8(pre);
96 docstring::size_type i = common_path(p1, p2);
98 if (i + 1 != p2.length())
101 if (!prefixIs(path, pre) && how == CASE_ADJUSTED)
102 path = to_utf8(p2 + p1.substr(i + 1, p1.length() - i + 1));
106 // silence compiler warnings
109 return prefixIs(path, pre);
114 string external_path(string const & p)
120 string internal_path(string const & p)
126 string external_path_list(string const & p)
132 string internal_path_list(string const & p)
138 string latex_path(string const & p)
144 bool is_valid_strftime(string const & p)
146 string::size_type pos = p.find_first_of('%');
147 while (pos != string::npos) {
148 if (pos + 1 == string::npos)
150 if (!containsOnly(p.substr(pos + 1, 1),
151 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
153 if (pos + 2 == string::npos)
155 pos = p.find_first_of('%', pos + 2);
161 char const * popen_read_mode()
167 string const & nulldev()
169 static string const nulldev_ = "/dev/null";
174 bool is_terminal(io_channel channel)
176 return isatty(channel);
186 char path_separator()
192 void windows_style_tex_paths(bool)
195 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
198 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
199 CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
200 (UInt8 *) ext.c_str(), ext.length(),
201 kCFStringEncodingISOLatin1, false);
202 // this is what we would like to do but it seems that the
203 // viewer for PDF is often quicktime...
204 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
206 LSRolesMask role = kLSRolesAll;
209 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
210 cfs_ext, role, &outAppRef, NULL);
213 return status != kLSApplicationNotFoundErr;
215 // silence compiler warnings
219 // currently, no default viewer is tried for non-windows system
220 // support for KDE/Gnome/Macintosh may be added later
226 bool autoOpenFile(string const & filename, auto_open_mode const mode)
229 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
232 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
236 // this is what we would like to do but it seems that the
237 // viewer for PDF is often quicktime...
238 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
240 LSRolesMask role = kLSRolesAll;
243 status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
244 if (status == kLSApplicationNotFoundErr)
247 LSLaunchFSRefSpec inLaunchSpec;
248 inLaunchSpec.appRef = &outAppRef;
249 inLaunchSpec.numDocs = 1;
250 inLaunchSpec.itemRefs = &fileref;
251 inLaunchSpec.passThruParams = NULL;
252 inLaunchSpec.launchFlags = kLSLaunchDefaults;
253 inLaunchSpec.asyncRefCon = NULL;
254 status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
256 return status != kLSApplicationNotFoundErr;
258 // silence compiler warnings
262 // currently, no default viewer is tried for non-windows system
263 // support for KDE/Gnome/Macintosh may be added later
269 string real_path(string const & path)
271 char rpath[PATH_MAX + 1];
272 char * result = realpath(path.c_str(), rpath);
273 return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
277 } // namespace support