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"
19 #include "support/lassert.h"
25 #include <Carbon/Carbon.h>
41 void init(int argc, char * argv[])
48 string utf8_argv(int i)
50 LASSERT(i < argc_, /**/);
51 return to_utf8(from_local8bit(argv_[i]));
61 bool isFilesystemCaseSensitive()
71 docstring::size_type common_path(docstring const & p1, docstring const & p2)
73 docstring::size_type i = 0;
74 docstring::size_type const p1_len = p1.length();
75 docstring::size_type const p2_len = p2.length();
77 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
80 while (i < p1_len && i < p2_len && p1[i] == p2[i])
83 if ((i < p1_len && i < p2_len)
84 || (i < p1_len && p1[i] != '/' && i == p2_len)
85 || (i < p2_len && p2[i] != '/' && i == p1_len))
88 --i; // here was the last match
89 while (i && p1[i] != '/')
96 bool path_prefix_is(string const & path, string const & pre)
99 return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
101 return prefixIs(path, pre);
106 bool path_prefix_is(string & path, string const & pre, path_case how)
109 docstring const p1 = from_utf8(path);
110 docstring const p2 = from_utf8(pre);
111 docstring::size_type const p1_len = p1.length();
112 docstring::size_type const p2_len = p2.length();
113 docstring::size_type common_len = common_path(p1, p2);
115 if (p2[p2_len - 1] == '/' && p1_len != p2_len)
118 if (common_len != p2_len)
121 if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
122 if (p1_len < common_len)
123 path = to_utf8(p2.substr(0, p1_len));
125 path = to_utf8(p2 + p1.substr(common_len,
126 p1_len - common_len));
131 // silence compiler warnings
134 return prefixIs(path, pre);
139 string external_path(string const & p)
145 string internal_path(string const & p)
151 string safe_internal_path(string const & p, file_access)
157 string external_path_list(string const & p)
163 string internal_path_list(string const & p)
169 string latex_path(string const & p)
175 bool is_valid_strftime(string const & p)
177 string::size_type pos = p.find_first_of('%');
178 while (pos != string::npos) {
179 if (pos + 1 == string::npos)
181 if (!containsOnly(p.substr(pos + 1, 1),
182 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
184 if (pos + 2 == string::npos)
186 pos = p.find_first_of('%', pos + 2);
192 char const * popen_read_mode()
198 string const & nulldev()
200 static string const nulldev_ = "/dev/null";
205 bool is_terminal(io_channel channel)
207 return isatty(channel);
217 char path_separator()
223 void windows_style_tex_paths(bool)
226 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
229 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
230 CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
231 (UInt8 *) ext.c_str(), ext.length(),
232 kCFStringEncodingISOLatin1, false);
233 // this is what we would like to do but it seems that the
234 // viewer for PDF is often quicktime...
235 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
237 LSRolesMask role = kLSRolesAll;
240 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
241 cfs_ext, role, &outAppRef, NULL);
244 return status != kLSApplicationNotFoundErr;
246 // silence compiler warnings
250 // currently, no default viewer is tried for non-windows system
251 // support for KDE/Gnome/Macintosh may be added later
257 bool autoOpenFile(string const & filename, auto_open_mode const mode)
260 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
263 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
267 // this is what we would like to do but it seems that the
268 // viewer for PDF is often quicktime...
269 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
271 LSRolesMask role = kLSRolesAll;
274 status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
275 if (status == kLSApplicationNotFoundErr)
278 LSLaunchFSRefSpec inLaunchSpec;
279 inLaunchSpec.appRef = &outAppRef;
280 inLaunchSpec.numDocs = 1;
281 inLaunchSpec.itemRefs = &fileref;
282 inLaunchSpec.passThruParams = NULL;
283 inLaunchSpec.launchFlags = kLSLaunchDefaults;
284 inLaunchSpec.asyncRefCon = NULL;
285 status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
287 return status != kLSApplicationNotFoundErr;
289 // silence compiler warnings
293 // currently, no default viewer is tried for non-windows system
294 // support for KDE/Gnome/Macintosh may be added later
300 string real_path(string const & path)
302 char rpath[PATH_MAX + 1];
303 char * result = realpath(path.c_str(), rpath);
304 return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
308 } // namespace support