]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
reorder.
[lyx.git] / src / support / os_unix.cpp
1 /**
2  * \file os_unix.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Various OS specific functions
11  */
12
13 #include <config.h>
14
15 #include "support/os.h"
16 #include "support/docstring.h"
17 #include "support/FileName.h"
18 #include "support/lstrings.h"
19
20 #include <limits.h>
21 #include <stdlib.h>
22
23 #ifdef __APPLE__
24 #include <Carbon/Carbon.h>
25 #endif
26
27 using namespace std;
28
29 namespace lyx {
30 namespace support {
31 namespace os {
32
33 void init(int, char *[])
34 {}
35
36
37 string current_root()
38 {
39         return "/";
40 }
41
42
43 bool isFilesystemCaseSensitive()
44 {
45 #ifdef __APPLE__
46         return false;
47 #else
48         return true;
49 #endif
50 }
51
52
53 docstring::size_type common_path(docstring const & p1, docstring const & p2)
54 {
55         docstring::size_type i = 0;
56         docstring::size_type const p1_len = p1.length();
57         docstring::size_type const p2_len = p2.length();
58         while (i < p1_len && i < p2_len && p1[i] == p2[i])
59                 ++i;
60         if ((i < p1_len && i < p2_len)
61             || (i < p1_len && p1[i] != '/' && i == p2_len)
62             || (i < p2_len && p2[i] != '/' && i == p1_len))
63         {
64                 if (i)
65                         --i;     // here was the last match
66                 while (i && p1[i] != '/')
67                         --i;
68         }
69         return i;
70 }
71
72
73 string external_path(string const & p)
74 {
75         return p;
76 }
77
78
79 string internal_path(string const & p)
80 {
81         return p;
82 }
83
84
85 string external_path_list(string const & p)
86 {
87         return p;
88 }
89
90
91 string internal_path_list(string const & p)
92 {
93         return p;
94 }
95
96
97 string latex_path(string const & p)
98 {
99         return p;
100 }
101
102
103 bool is_valid_strftime(string const & p)
104 {
105         string::size_type pos = p.find_first_of('%');
106         while (pos != string::npos) {
107                 if (pos + 1 == string::npos)
108                         break;
109                 if (!containsOnly(p.substr(pos + 1, 1),
110                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
111                         return false;
112                 if (pos + 2 == string::npos)
113                       break;
114                 pos = p.find_first_of('%', pos + 2);
115         }
116         return true;
117 }
118
119
120 char const * popen_read_mode()
121 {
122         return "r";
123 }
124
125
126 string const & nulldev()
127 {
128         static string const nulldev_ = "/dev/null";
129         return nulldev_;
130 }
131
132
133 bool is_terminal(io_channel channel)
134 {
135         return isatty(channel);
136 }
137
138
139 shell_type shell()
140 {
141         return UNIX;
142 }
143
144
145 char path_separator()
146 {
147         return ':';
148 }
149
150
151 void windows_style_tex_paths(bool)
152 {}
153
154 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
155 {
156 #ifdef __APPLE__
157 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
158         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
159                                         (UInt8 *) ext.c_str(), ext.length(),
160                                         kCFStringEncodingISOLatin1, false);
161         // this is what we would like to do but it seems that the
162         // viewer for PDF is often quicktime...
163         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
164         (void)mode;
165         LSRolesMask role = kLSRolesAll;
166         FSRef outAppRef;
167         OSStatus status =
168                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
169                                         cfs_ext, role, &outAppRef, NULL);
170         CFRelease(cfs_ext);
171
172         return status != kLSApplicationNotFoundErr;
173 #else
174         // silence compiler warnings
175         (void)ext;
176         (void)mode;
177
178         // currently, no default viewer is tried for non-windows system
179         // support for KDE/Gnome/Macintosh may be added later
180         return false;
181 #endif
182 }
183
184
185 bool autoOpenFile(string const & filename, auto_open_mode const mode)
186 {
187 #ifdef __APPLE__
188 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
189         FSRef fileref;
190         OSStatus status =
191                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
192         if (status != 0)
193                 return false;
194
195         // this is what we would like to do but it seems that the
196         // viewer for PDF is often quicktime...
197         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
198         (void)mode;
199         LSRolesMask role = kLSRolesAll;
200         FSRef outAppRef;
201
202         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
203         if (status == kLSApplicationNotFoundErr)
204                 return false;
205
206         LSLaunchFSRefSpec inLaunchSpec;
207         inLaunchSpec.appRef = &outAppRef;
208         inLaunchSpec.numDocs = 1;
209         inLaunchSpec.itemRefs = &fileref;
210         inLaunchSpec.passThruParams = NULL;
211         inLaunchSpec.launchFlags = kLSLaunchDefaults;
212         inLaunchSpec.asyncRefCon = NULL;
213         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
214
215         return status != kLSApplicationNotFoundErr;
216 #else
217         // silence compiler warnings
218         (void)filename;
219         (void)mode;
220
221         // currently, no default viewer is tried for non-windows system
222         // support for KDE/Gnome/Macintosh may be added later
223         return false;
224 #endif
225 }
226
227
228 string real_path(string const & path)
229 {
230         char rpath[PATH_MAX + 1];
231         char * result = realpath(path.c_str(), rpath);
232         return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
233 }
234
235 } // namespace os
236 } // namespace support
237 } // namespace lyx