]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
4bbd76fe1c784773a09da5a4e591d0cbf34e8dba
[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 shell_type shell()
134 {
135         return UNIX;
136 }
137
138
139 char path_separator()
140 {
141         return ':';
142 }
143
144
145 void windows_style_tex_paths(bool)
146 {}
147
148 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
149 {
150 #ifdef __APPLE__
151 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
152         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
153                                         (UInt8 *) ext.c_str(), ext.length(),
154                                         kCFStringEncodingISOLatin1, false);
155         // this is what we would like to do but it seems that the
156         // viewer for PDF is often quicktime...
157         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
158         (void)mode;
159         LSRolesMask role = kLSRolesAll;
160         FSRef outAppRef;
161         OSStatus status =
162                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
163                                         cfs_ext, role, &outAppRef, NULL);
164         CFRelease(cfs_ext);
165
166         return status != kLSApplicationNotFoundErr;
167 #else
168         // silence compiler warnings
169         (void)ext;
170         (void)mode;
171
172         // currently, no default viewer is tried for non-windows system
173         // support for KDE/Gnome/Macintosh may be added later
174         return false;
175 #endif
176 }
177
178
179 bool autoOpenFile(string const & filename, auto_open_mode const mode)
180 {
181 #ifdef __APPLE__
182 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
183         FSRef fileref;
184         OSStatus status =
185                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
186         if (status != 0)
187                 return false;
188
189         // this is what we would like to do but it seems that the
190         // viewer for PDF is often quicktime...
191         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
192         (void)mode;
193         LSRolesMask role = kLSRolesAll;
194         FSRef outAppRef;
195
196         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
197         if (status == kLSApplicationNotFoundErr)
198                 return false;
199
200         LSLaunchFSRefSpec inLaunchSpec;
201         inLaunchSpec.appRef = &outAppRef;
202         inLaunchSpec.numDocs = 1;
203         inLaunchSpec.itemRefs = &fileref;
204         inLaunchSpec.passThruParams = NULL;
205         inLaunchSpec.launchFlags = kLSLaunchDefaults;
206         inLaunchSpec.asyncRefCon = NULL;
207         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
208
209         return status != kLSApplicationNotFoundErr;
210 #else
211         // silence compiler warnings
212         (void)filename;
213         (void)mode;
214
215         // currently, no default viewer is tried for non-windows system
216         // support for KDE/Gnome/Macintosh may be added later
217         return false;
218 #endif
219 }
220
221
222 string real_path(string const & path)
223 {
224         char rpath[PATH_MAX + 1];
225         char * result = realpath(path.c_str(), rpath);
226         return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
227 }
228
229 } // namespace os
230 } // namespace support
231 } // namespace lyx