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]));
55 void remove_internal_args(int, int)
65 bool isFilesystemCaseSensitive()
75 docstring::size_type common_path(docstring const & p1, docstring const & p2)
77 docstring::size_type i = 0;
78 docstring::size_type const p1_len = p1.length();
79 docstring::size_type const p2_len = p2.length();
81 while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
84 while (i < p1_len && i < p2_len && p1[i] == p2[i])
87 if ((i < p1_len && i < p2_len)
88 || (i < p1_len && p1[i] != '/' && i == p2_len)
89 || (i < p2_len && p2[i] != '/' && i == p1_len))
92 --i; // here was the last match
93 while (i && p1[i] != '/')
100 bool path_prefix_is(string const & path, string const & pre)
103 return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
105 return prefixIs(path, pre);
110 bool path_prefix_is(string & path, string const & pre, path_case how)
113 docstring const p1 = from_utf8(path);
114 docstring const p2 = from_utf8(pre);
115 docstring::size_type const p1_len = p1.length();
116 docstring::size_type const p2_len = p2.length();
117 docstring::size_type common_len = common_path(p1, p2);
119 if (p2[p2_len - 1] == '/' && p1_len != p2_len)
122 if (common_len != p2_len)
125 if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
126 if (p1_len < common_len)
127 path = to_utf8(p2.substr(0, p1_len));
129 path = to_utf8(p2 + p1.substr(common_len,
130 p1_len - common_len));
135 // silence compiler warnings
138 return prefixIs(path, pre);
143 string external_path(string const & p)
149 string internal_path(string const & p)
155 string safe_internal_path(string const & p, file_access)
161 string external_path_list(string const & p)
167 string internal_path_list(string const & p)
173 string latex_path(string const & p)
179 bool is_valid_strftime(string const & p)
181 string::size_type pos = p.find_first_of('%');
182 while (pos != string::npos) {
183 if (pos + 1 == string::npos)
185 if (!containsOnly(p.substr(pos + 1, 1),
186 "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
188 if (pos + 2 == string::npos)
190 pos = p.find_first_of('%', pos + 2);
196 char const * popen_read_mode()
202 string const & nulldev()
204 static string const nulldev_ = "/dev/null";
215 char path_separator()
221 void windows_style_tex_paths(bool)
224 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
227 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
228 CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
229 (UInt8 *) ext.c_str(), ext.length(),
230 kCFStringEncodingISOLatin1, false);
231 // this is what we would like to do but it seems that the
232 // viewer for PDF is often quicktime...
233 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
235 LSRolesMask role = kLSRolesAll;
238 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
239 cfs_ext, role, &outAppRef, NULL);
242 return status != kLSApplicationNotFoundErr;
244 // silence compiler warnings
248 // currently, no default viewer is tried for non-windows system
249 // support for KDE/Gnome/Macintosh may be added later
255 bool autoOpenFile(string const & filename, auto_open_mode const mode)
258 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
261 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
265 // this is what we would like to do but it seems that the
266 // viewer for PDF is often quicktime...
267 //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer : kLSRolesEditor;
269 LSRolesMask role = kLSRolesAll;
272 status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
273 if (status == kLSApplicationNotFoundErr)
276 LSLaunchFSRefSpec inLaunchSpec;
277 inLaunchSpec.appRef = &outAppRef;
278 inLaunchSpec.numDocs = 1;
279 inLaunchSpec.itemRefs = &fileref;
280 inLaunchSpec.passThruParams = NULL;
281 inLaunchSpec.launchFlags = kLSLaunchDefaults;
282 inLaunchSpec.asyncRefCon = NULL;
283 status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
285 return status != kLSApplicationNotFoundErr;
287 // silence compiler warnings
291 // currently, no default viewer is tried for non-windows system
292 // support for KDE/Gnome/Macintosh may be added later
298 string real_path(string const & path)
300 #ifdef HAVE_DEF_PATH_MAX
301 char rpath[PATH_MAX + 1];
302 char * result = realpath(path.c_str(), rpath);
303 return FileName::fromFilesystemEncoding(result ? rpath : path).absFileName();
305 char * result = realpath(path.c_str(), NULL);
306 string ret = FileName::fromFilesystemEncoding(result ? result : path).absFileName();
313 } // namespace support